Skip to content

Commit e1c176c

Browse files
Ajit Pratap SinghAjit Pratap Singh
authored andcommitted
fix: use errors.Is() for wrapped context errors in tests
The ParseWithContext and ParseWithTimeout functions wrap context errors (context.Canceled, context.DeadlineExceeded) in descriptive error messages using fmt.Errorf with %w. This maintains the error chain but changes the error identity. Tests were using direct equality comparison (err != context.DeadlineExceeded) which fails for wrapped errors. Updated all context error checks to use errors.Is() which properly checks the error chain. This fix resolves Windows Go 1.21 test failures where: - Expected: context.DeadlineExceeded - Got: 'tokenization failed: context deadline exceeded' Changes: - Import 'errors' package - Replace 'err != context.DeadlineExceeded' with 'errors.Is(err, context.DeadlineExceeded)' - Replace 'err != context.Canceled' with 'errors.Is(err, context.Canceled)' - Replace 'err == context.DeadlineExceeded' with 'errors.Is(err, context.DeadlineExceeded)' - Replace 'err == context.Canceled' with 'errors.Is(err, context.Canceled)' Affected tests: - TestParseWithContext_CancelledContext - TestParseWithContext_Timeout - TestParseWithTimeout_TimeoutExpires - TestParseWithTimeout_ZeroTimeout - TestParseWithContext_ErrorHandling Fixes: Windows test failures in PR #94 Platform: Windows Go 1.21 (and all other Go versions) Root Cause: Wrapped errors not being properly checked Solution: Use errors.Is() for error chain inspection
1 parent 45ccbbc commit e1c176c

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

pkg/gosqlx/context_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gosqlx
22

33
import (
44
"context"
5+
"errors"
56
"strings"
67
"testing"
78
"time"
@@ -68,7 +69,7 @@ func TestParseWithContext_CancelledContext(t *testing.T) {
6869

6970
ast, err := ParseWithContext(ctx, sql)
7071

71-
if err != context.Canceled {
72+
if !errors.Is(err, context.Canceled) {
7273
t.Errorf("Expected context.Canceled error, got: %v", err)
7374
}
7475

@@ -90,7 +91,7 @@ func TestParseWithContext_Timeout(t *testing.T) {
9091

9192
ast, err := ParseWithContext(ctx, sql)
9293

93-
if err != context.DeadlineExceeded {
94+
if !errors.Is(err, context.DeadlineExceeded) {
9495
t.Errorf("Expected context.DeadlineExceeded error, got: %v", err)
9596
}
9697

@@ -148,7 +149,7 @@ func TestParseWithTimeout_TimeoutExpires(t *testing.T) {
148149
ast, err := ParseWithTimeout(sql, timeout)
149150

150151
// Should timeout or succeed quickly (race condition)
151-
if err == context.DeadlineExceeded {
152+
if errors.Is(err, context.DeadlineExceeded) {
152153
if ast != nil {
153154
t.Error("Expected nil AST when timeout expires")
154155
}
@@ -338,7 +339,7 @@ func TestParseWithContext_ErrorHandling(t *testing.T) {
338339
t.Error("Expected error but got none")
339340
}
340341

341-
if err != nil && err == context.Canceled {
342+
if err != nil && errors.Is(err, context.Canceled) {
342343
t.Error("Should not return context.Canceled for SQL errors")
343344
}
344345

@@ -430,7 +431,7 @@ func TestParseWithTimeout_ZeroTimeout(t *testing.T) {
430431
ast, err := ParseWithTimeout(sql, 0)
431432

432433
// With zero timeout, context will be cancelled immediately or succeed
433-
if err == context.DeadlineExceeded {
434+
if errors.Is(err, context.DeadlineExceeded) {
434435
if ast != nil {
435436
t.Error("Expected nil AST when timeout expires")
436437
}

0 commit comments

Comments
 (0)