diff --git a/.golangci.yml b/.golangci.yml index 8934db7..ea8d214 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -12,24 +12,27 @@ linters: - noctx # Finds sending http request without context.Context - prealloc # Temporarily disable until slice allocation issues are fixed enable: - - bodyclose # Checks HTTP response body is closed - - contextcheck # Check whether the function uses a non-inherited context - - dupl # Find duplicate code - - dupword # Find duplicate words in comments and strings - - errcheck - - errorlint # Check error handling - - misspell # Find commonly misspelled English words - - unconvert # Remove unnecessary type conversions - - reassign # Checks that package variables are not reassigned - - tagalign # Check that struct tags are well aligned - - nilerr # Finds code that returns nil even if it checks that the error is not nil - - nolintlint # Checks for invalid or missing nolint directives - - whitespace # Check for unnecessary whitespace - - thelper # Detects test helpers which should start with t.Helper() - - govet # Examines Go source code and reports suspicious constructs - - ineffassign # Detects when assignments to existing variables are not used - - staticcheck # Staticcheck is a go linter - - unused # Checks for unused code + - bodyclose # Ensure HTTP response bodies are closed + - contextcheck # Ensure functions use a non-inherited context + - dupl # Detect duplicate code + - dupword # Detect duplicate words in comments/strings + - errcheck # Check for unchecked errors + - errorlint # Enforce idiomatic error handling + - govet # Report suspicious constructs + - ineffassign # Detect unused variable assignments + - misspell # Detect misspelled English words + - nilerr # Detect returning nil after error checks + - nolintlint # Check for invalid/missing nolint directives + - reassign # Prevent package variable reassignment + - staticcheck # Advanced static analysis + - tagalign # Check struct tag alignment + - tagliatelle # Enforce struct tag formatting + - testifylint # Avoid common testify mistakes + - thelper # Ensure test helpers use t.Helper() + - unconvert # Remove unnecessary type conversions + - usetesting # Detects when some calls can be replaced by methods from the testing package + - unused # Detect unused code + - whitespace # Detect unnecessary whitespace settings: errcheck: check-blank: true @@ -41,6 +44,18 @@ linters: errorf: true asserts: true comparison: true + tagalign: + strict: true + order: + - json + - toml + - yaml + - xml + - env_interpolation + usetesting: + os-temp-dir: true + context-background: true + context-todo: true formatters: enable: @@ -48,7 +63,6 @@ formatters: - gofmt - goimports - gofumpt - - golines issues: max-issues-per-linter: 20 diff --git a/engines/extism/adapters/sdkAdapters_test.go b/engines/extism/adapters/sdkAdapters_test.go index d0cc6e4..0452418 100644 --- a/engines/extism/adapters/sdkAdapters_test.go +++ b/engines/extism/adapters/sdkAdapters_test.go @@ -24,17 +24,6 @@ func TestInterfaceImplementation(t *testing.T) { // For the sdkCompiledPluginAdapter and sdkPluginAdapter implementations, we rely on // the Extism SDK's types, so we don't need exhaustive tests for this wrapper code. -func TestAdapterCorrectlyImplementsInterfaces(t *testing.T) { - // These tests verify that the methods of the interfaces correctly match - // the methods of the SDK types. If there's a mismatch, Go won't compile. - // - // We don't need to test the implementation since it's just directly - // calling the SDK's methods. We're just verifying the types at compile time. - - // Implicitly test this with the interface check above - assert.True(t, true, "Adapters correctly implement their interfaces") -} - func TestNilPlugin(t *testing.T) { // Test that NewCompiledPluginAdapter returns nil when given a nil plugin adapter := NewCompiledPluginAdapter(nil) diff --git a/engines/extism/compiler/internal/compile/compile_test.go b/engines/extism/compiler/internal/compile/compile_test.go index 370e06c..cb7032b 100644 --- a/engines/extism/compiler/internal/compile/compile_test.go +++ b/engines/extism/compiler/internal/compile/compile_test.go @@ -181,12 +181,12 @@ func testFunctions(t *testing.T, instance adapters.PluginInstance) { assertFunc: func(t *testing.T, output []byte) { t.Helper() var result struct { - RequestID string `json:"request_id"` - ProcessedAt string `json:"processed_at"` + RequestID string `json:"requestId"` + ProcessedAt string `json:"processedAt"` Results map[string]any `json:"results"` - TagCount int `json:"tag_count"` - MetaCount int `json:"meta_count"` - IsActive bool `json:"is_active"` + TagCount int `json:"tagCount"` + MetaCount int `json:"metaCount"` + IsActive bool `json:"isActive"` Summary string `json:"summary"` } require.NoError(t, json.Unmarshal(output, &result)) diff --git a/engines/extism/compiler/options_test.go b/engines/extism/compiler/options_test.go index 8705983..a397820 100644 --- a/engines/extism/compiler/options_test.go +++ b/engines/extism/compiler/options_test.go @@ -53,7 +53,7 @@ func TestCompilerOptions_Options(t *testing.T) { t.Run("empty value", func(t *testing.T) { c := &Compiler{entryPointName: ""} - require.Equal(t, "", c.GetEntryPointName()) + require.Empty(t, c.GetEntryPointName()) }) t.Run("with defaults", func(t *testing.T) { @@ -617,7 +617,7 @@ func TestCompilerOptions(t *testing.T) { c2 := &Compiler{ entryPointName: "", } - require.Equal(t, "", c2.GetEntryPointName()) + require.Empty(t, c2.GetEntryPointName()) }) }) diff --git a/engines/extism/evaluator/evaluator_test.go b/engines/extism/evaluator/evaluator_test.go index 62adaa5..8ad3e67 100644 --- a/engines/extism/evaluator/evaluator_test.go +++ b/engines/extism/evaluator/evaluator_test.go @@ -178,7 +178,7 @@ func TestEvaluator_Evaluate(t *testing.T) { require.Contains(t, resultMap, "result") require.Equal(t, "success", resultMap["result"]) require.Contains(t, resultMap, "value") - require.Equal(t, float64(42), resultMap["value"]) + require.InDelta(t, float64(42), resultMap["value"], 0.0001) }) // Test successful string response @@ -398,8 +398,7 @@ func TestEvaluator_Evaluate(t *testing.T) { // Call Eval, which should be cancelled during execution result, err := evaluator.Eval(ctx) - // Should get a cancellation error - assert.Error(t, err) + require.Error(t, err, "Expected cancellation error") assert.Nil(t, result) assert.Contains(t, err.Error(), "execution") @@ -588,19 +587,18 @@ func TestEvaluator_Evaluate(t *testing.T) { "Expected the mock instance to be called", ) - // Check for expected errors if tt.wantErr { - assert.Error(t, err) + require.Error(t, err) if tt.errContains != "" { assert.Contains(t, err.Error(), tt.errContains) } } else { - assert.NoError(t, err) + require.NoError(t, err) assert.NotNil(t, result) } // Execution time should always be measured - assert.Greater(t, execTime.Nanoseconds(), int64(0)) + assert.Positive(t, execTime.Nanoseconds()) }) } }) diff --git a/engines/extism/internal/converters_test.go b/engines/extism/internal/converters_test.go index 9d2a850..2ef1449 100644 --- a/engines/extism/internal/converters_test.go +++ b/engines/extism/internal/converters_test.go @@ -84,7 +84,7 @@ func TestConvertToExtismFormat(t *testing.T) { // Handle type conversions that happen during JSON marshaling if intVal, isInt := expectedVal.(int); isInt { // JSON unmarshaling converts numbers to float64 - assert.Equal(t, float64(intVal), checkData[k]) + assert.InDelta(t, float64(intVal), checkData[k], 0.0001) } else if _, isIntSlice := expectedVal.([]int); isIntSlice { // Skip int slice checks (arrays become []any) } else if _, isSlice := expectedVal.([]any); !isSlice { diff --git a/engines/extism/internal/jsonHelpers_test.go b/engines/extism/internal/jsonHelpers_test.go index 31b550e..db4c096 100644 --- a/engines/extism/internal/jsonHelpers_test.go +++ b/engines/extism/internal/jsonHelpers_test.go @@ -77,9 +77,9 @@ func TestFixJSONNumberTypes(t *testing.T) { assert.True(t, ok, "Result should be a map") // Check that the values were converted to float64 - assert.Equal(t, float64(19.99), mapResult["price"]) - assert.Equal(t, float64(4.5), mapResult["rating"]) - assert.Equal(t, float64(75.5), mapResult["percentage"]) + assert.InDelta(t, float64(19.99), mapResult["price"], 0.0001) + assert.InDelta(t, float64(4.5), mapResult["rating"], 0.0001) + assert.InDelta(t, float64(75.5), mapResult["percentage"], 0.0001) assert.IsType(t, float64(0), mapResult["price"]) assert.IsType(t, float64(0), mapResult["rating"]) assert.IsType(t, float64(0), mapResult["percentage"]) @@ -113,7 +113,7 @@ func TestFixJSONNumberTypes(t *testing.T) { stats, ok := user["stats"].(map[string]any) assert.True(t, ok, "stats should be a map") assert.Equal(t, int(42), stats["login_count"]) - assert.Equal(t, float64(95.5), stats["score"]) + assert.InDelta(t, float64(95.5), stats["score"], 0.0001) }) t.Run("handles slices", func(t *testing.T) { @@ -142,7 +142,7 @@ func TestFixJSONNumberTypes(t *testing.T) { itemMap, ok := sliceResult[3].(map[string]any) assert.True(t, ok, "Item at index 3 should be a map") assert.Equal(t, int(123), itemMap["item_id"]) - assert.Equal(t, float64(9.99), itemMap["price"]) + assert.InDelta(t, float64(9.99), itemMap["price"], 0.0001) }) t.Run("handles nested slices", func(t *testing.T) { @@ -177,13 +177,13 @@ func TestFixJSONNumberTypes(t *testing.T) { product1, ok := products[0].(map[string]any) assert.True(t, ok, "First product should be a map") assert.Equal(t, int(1), product1["product_id"]) - assert.Equal(t, float64(19.99), product1["price"]) + assert.InDelta(t, float64(19.99), product1["price"], 0.0001) // Check second product product2, ok := products[1].(map[string]any) assert.True(t, ok, "Second product should be a map") assert.Equal(t, int(2), product2["product_id"]) - assert.Equal(t, float64(29.99), product2["price"]) + assert.InDelta(t, float64(29.99), product2["price"], 0.0001) }) t.Run("handles invalid numbers gracefully", func(t *testing.T) { diff --git a/engines/extism/wasmdata/examples/main.go b/engines/extism/wasmdata/examples/main.go index de80373..3cf62c3 100644 --- a/engines/extism/wasmdata/examples/main.go +++ b/engines/extism/wasmdata/examples/main.go @@ -22,12 +22,12 @@ type Request struct { // Response represents a complex output object type Response struct { - RequestID string `json:"request_id"` - ProcessedAt string `json:"processed_at"` + RequestID string `json:"requestId"` + ProcessedAt string `json:"processedAt"` Results map[string]any `json:"results"` - TagCount int `json:"tag_count"` - MetaCount int `json:"meta_count"` - IsActive bool `json:"is_active"` + TagCount int `json:"tagCount"` + MetaCount int `json:"metaCount"` + IsActive bool `json:"isActive"` Summary string `json:"summary"` } diff --git a/engines/extism/wasmdata/integration_test.go b/engines/extism/wasmdata/integration_test.go index 63357b0..28b43ee 100644 --- a/engines/extism/wasmdata/integration_test.go +++ b/engines/extism/wasmdata/integration_test.go @@ -209,10 +209,10 @@ func TestExtismWasmIntegration(t *testing.T) { err = json.Unmarshal(output, &response) require.NoError(t, err, "Failed to parse complex response") - assert.Equal(t, "test-123", response["request_id"]) - assert.NotEmpty(t, response["processed_at"]) - assert.Equal(t, float64(3), response["tag_count"]) // JSON numbers are float64 - assert.Equal(t, float64(3), response["meta_count"]) + assert.Equal(t, "test-123", response["requestId"]) + assert.NotEmpty(t, response["processedAt"]) + assert.InDelta(t, float64(3), response["tagCount"], 0.0001) // JSON numbers are float64 + assert.InDelta(t, float64(3), response["metaCount"], 0.0001) assert.Contains(t, response["summary"], "test-123") }) @@ -245,7 +245,7 @@ func TestExtismWasmIntegration(t *testing.T) { require.NoError(t, err, "Failed to parse vowel result") assert.Equal(t, "Hello World", vowelResult["input"]) - assert.Equal(t, float64(3), vowelResult["count"]) // 3 vowels in "Hello World" + assert.InDelta(t, float64(3), vowelResult["count"], 0.0001) // 3 vowels in "Hello World" }) t.Run("reverse_string function", func(t *testing.T) { @@ -312,7 +312,7 @@ func TestExtismWasmIntegration(t *testing.T) { require.NoError(t, err, "Failed to parse vowel result") assert.Equal(t, "Hello World", vowelResult["input"]) - assert.Equal(t, float64(3), vowelResult["count"]) // 3 vowels in "Hello World" + assert.InDelta(t, float64(3), vowelResult["count"], 0.0001) // 3 vowels in "Hello World" }) t.Run("reverse_string_namespaced function", func(t *testing.T) { diff --git a/engines/extism/wasmdata/main.wasm b/engines/extism/wasmdata/main.wasm index 40b3e71..4fc392c 100644 Binary files a/engines/extism/wasmdata/main.wasm and b/engines/extism/wasmdata/main.wasm differ diff --git a/engines/integration_test.go b/engines/integration_test.go index 581e7c2..ca437c2 100644 --- a/engines/integration_test.go +++ b/engines/integration_test.go @@ -422,7 +422,7 @@ _ = result // The greet function returns {"greeting": "Hello, !"} greeting, exists := resultMap["greeting"] require.True(t, exists) - assert.True(t, strings.Contains(greeting.(string), "World")) + assert.Contains(t, greeting.(string), "World") }) } diff --git a/engines/risor/compiler/compiler_test.go b/engines/risor/compiler/compiler_test.go index ea3d0dd..5aa6fc0 100644 --- a/engines/risor/compiler/compiler_test.go +++ b/engines/risor/compiler/compiler_test.go @@ -221,7 +221,7 @@ main() execContent, err := comp.Compile(reader) require.Error(t, err, "Expected an error but got none") require.Nil(t, execContent, "Expected execContent to be nil") - require.True(t, errors.Is(err, tt.err), "Expected error %v, got %v", tt.err, err) + require.ErrorIs(t, err, tt.err, "Expected error %v, got %v", tt.err, err) // Verify mock expectations if mockReader, ok := reader.(*mockScriptReaderCloser); ok { @@ -238,7 +238,7 @@ main() execContent, err := comp.Compile(nil) require.Error(t, err, "Expected an error but got none") require.Nil(t, execContent, "Expected execContent to be nil") - require.True(t, errors.Is(err, ErrContentNil), "Expected error to be ErrContentNil") + require.ErrorIs(t, err, ErrContentNil, "Expected error to be ErrContentNil") }) t.Run("io error", func(t *testing.T) { @@ -395,7 +395,7 @@ func TestCompileError(t *testing.T) { execContent, err := comp.Compile(nil) require.Error(t, err, "Expected an error but got none") require.Nil(t, execContent, "Expected execContent to be nil") - require.True(t, errors.Is(err, ErrContentNil), "Expected error to be ErrContentNil") + require.ErrorIs(t, err, ErrContentNil, "Expected error to be ErrContentNil") } func TestCompileWithBytecode(t *testing.T) { diff --git a/engines/starlark/compiler/compiler_test.go b/engines/starlark/compiler/compiler_test.go index eb9f1d5..a8721c6 100644 --- a/engines/starlark/compiler/compiler_test.go +++ b/engines/starlark/compiler/compiler_test.go @@ -228,7 +228,7 @@ main() execContent, err := comp.Compile(reader) require.Error(t, err, "Expected an error but got none") require.Nil(t, execContent, "Expected execContent to be nil") - require.True(t, errors.Is(err, tt.err), "Expected error %v, got %v", tt.err, err) + require.ErrorIs(t, err, tt.err, "Expected error %v, got %v", tt.err, err) // Verify mock expectations if mockReader, ok := reader.(*mockScriptReaderCloser); ok { @@ -245,7 +245,7 @@ main() execContent, err := comp.Compile(nil) require.Error(t, err, "Expected an error but got none") require.Nil(t, execContent, "Expected execContent to be nil") - require.True(t, errors.Is(err, ErrContentNil), "Expected error to be ErrContentNil") + require.ErrorIs(t, err, ErrContentNil, "Expected error to be ErrContentNil") }) t.Run("io error", func(t *testing.T) { @@ -398,7 +398,7 @@ func TestCompileError(t *testing.T) { execContent, err := comp.Compile(nil) require.Error(t, err, "Expected an error but got none") require.Nil(t, execContent, "Expected execContent to be nil") - require.True(t, errors.Is(err, ErrContentNil), "Expected error to be ErrContentNil") + require.ErrorIs(t, err, ErrContentNil, "Expected error to be ErrContentNil") } func TestCompileIOError(t *testing.T) { diff --git a/engines/starlark/internal/converters_test.go b/engines/starlark/internal/converters_test.go index 95f6d81..30abfb5 100644 --- a/engines/starlark/internal/converters_test.go +++ b/engines/starlark/internal/converters_test.go @@ -327,7 +327,7 @@ func TestConvertToStarlarkFormat(t *testing.T) { } require.NoError(t, err) - require.Equal(t, len(tt.expected), len(result)) + require.Len(t, result, len(tt.expected)) // Get ctx value and verify it's a dict ctxVal, ok := result[constants.Ctx].(*starlarkLib.Dict) diff --git a/examples/data-prep/extism/main_test.go b/examples/data-prep/extism/main_test.go index 1fb2416..312f233 100644 --- a/examples/data-prep/extism/main_test.go +++ b/examples/data-prep/extism/main_test.go @@ -69,7 +69,7 @@ func TestPrepareRuntimeData(t *testing.T) { // Test prepareRuntimeData function ctx := t.Context() enrichedCtx, err := prepareRuntimeData(ctx, logger, evaluator) - assert.NoError(t, err, "prepareRuntimeData should not return an error") + require.NoError(t, err, "prepareRuntimeData should not return an error") assert.NotNil(t, enrichedCtx, "Enriched context should not be nil") } @@ -95,7 +95,7 @@ func TestEvalAndExtractResult(t *testing.T) { // Test evaluation result, err := evalAndExtractResult(ctx, logger, evaluator) - assert.NoError(t, err, "evalAndExtractResult should not return an error") + require.NoError(t, err, "evalAndExtractResult should not return an error") assert.NotNil(t, result, "Result should not be nil") } @@ -110,7 +110,7 @@ func TestFromExtismFileWithData(t *testing.T) { slog.Default().Handler(), wasmdata.EntrypointGreet, ) - assert.NoError(t, err, "Should create evaluator without error") + require.NoError(t, err, "Should create evaluator without error") assert.NotNil(t, evaluator, "Evaluator should not be nil") } diff --git a/examples/data-prep/risor/main_test.go b/examples/data-prep/risor/main_test.go index 16d587d..74f609b 100644 --- a/examples/data-prep/risor/main_test.go +++ b/examples/data-prep/risor/main_test.go @@ -58,7 +58,7 @@ func TestPrepareRuntimeData(t *testing.T) { // Test prepareRuntimeData function ctx := t.Context() enrichedCtx, err := prepareRuntimeData(ctx, logger, evaluator) - assert.NoError(t, err, "prepareRuntimeData should not return an error") + require.NoError(t, err, "prepareRuntimeData should not return an error") assert.NotNil(t, enrichedCtx, "Enriched context should not be nil") } @@ -76,7 +76,7 @@ func TestEvalAndExtractResult(t *testing.T) { // Test evaluation result, err := evalAndExtractResult(preparedCtx, logger, evaluator) - assert.NoError(t, err, "evalAndExtractResult should not return an error") + require.NoError(t, err, "evalAndExtractResult should not return an error") assert.NotNil(t, result, "Result should not be nil") // Check basic result fields diff --git a/examples/data-prep/starlark/main_test.go b/examples/data-prep/starlark/main_test.go index 4334af6..56e3ad8 100644 --- a/examples/data-prep/starlark/main_test.go +++ b/examples/data-prep/starlark/main_test.go @@ -56,7 +56,7 @@ func TestPrepareRuntimeData(t *testing.T) { // Test prepareRuntimeData function ctx := t.Context() enrichedCtx, err := prepareRuntimeData(ctx, logger, evaluator) - assert.NoError(t, err, "prepareRuntimeData should not return an error") + require.NoError(t, err, "prepareRuntimeData should not return an error") assert.NotNil(t, enrichedCtx, "Enriched context should not be nil") } @@ -75,7 +75,7 @@ func TestEvalAndExtractResult(t *testing.T) { // Test evaluation result, err := evalAndExtractResult(preparedCtx, logger, evaluator) - assert.NoError(t, err, "evalAndExtractResult should not return an error") + require.NoError(t, err, "evalAndExtractResult should not return an error") assert.NotNil(t, result, "Result should not be nil") // Check basic result fields diff --git a/internal/helpers/requestToMap_test.go b/internal/helpers/requestToMap_test.go index d2a5404..7aa6ef0 100644 --- a/internal/helpers/requestToMap_test.go +++ b/internal/helpers/requestToMap_test.go @@ -55,10 +55,10 @@ func TestNewHTTPRequestWrapper(t *testing.T) { require.Equal(t, "HTTP/1.1", reqStruct.Proto) require.Equal(t, int64(0), reqStruct.ContentLength) require.Equal(t, "localhost:8080", reqStruct.Host) - require.Equal(t, "", reqStruct.RemoteAddr) + require.Empty(t, reqStruct.RemoteAddr) require.Equal(t, map[string][]string{}, reqStruct.QueryParams) require.Equal(t, map[string][]string{}, reqStruct.Headers) - require.Equal(t, "", reqStruct.Body) + require.Empty(t, reqStruct.Body) }) t.Run("with query parameters", func(t *testing.T) { @@ -82,13 +82,13 @@ func TestNewHTTPRequestWrapper(t *testing.T) { require.Equal(t, "HTTP/1.1", reqStruct.Proto) require.Equal(t, int64(0), reqStruct.ContentLength) require.Equal(t, "localhost:8080", reqStruct.Host) - require.Equal(t, "", reqStruct.RemoteAddr) + require.Empty(t, reqStruct.RemoteAddr) require.Equal(t, map[string][]string{ "param1": {"value1"}, "param2": {"value2"}, }, reqStruct.QueryParams) require.Equal(t, map[string][]string{}, reqStruct.Headers) - require.Equal(t, "", reqStruct.Body) + require.Empty(t, reqStruct.Body) }) t.Run("nil request", func(t *testing.T) { @@ -146,10 +146,10 @@ func TestRequestToMap(t *testing.T) { require.Equal(t, "HTTP/1.1", result["Proto"]) require.Equal(t, int64(0), result["ContentLength"]) require.Equal(t, "localhost:8080", result["Host"]) - require.Equal(t, "", result["RemoteAddr"]) + require.Empty(t, result["RemoteAddr"]) require.Equal(t, map[string][]string{}, result["QueryParams"]) require.Equal(t, map[string][]string{}, result["Headers"]) - require.Equal(t, "", result["Body"]) + require.Empty(t, result["Body"]) }) t.Run("with query parameters", func(t *testing.T) { @@ -173,12 +173,12 @@ func TestRequestToMap(t *testing.T) { require.Equal(t, "HTTP/1.1", result["Proto"]) require.Equal(t, int64(0), result["ContentLength"]) require.Equal(t, "localhost:8080", result["Host"]) - require.Equal(t, "", result["RemoteAddr"]) + require.Empty(t, result["RemoteAddr"]) require.Equal(t, map[string][]string{ "param1": {"value1"}, "param2": {"value2"}, }, result["QueryParams"]) require.Equal(t, map[string][]string{}, result["Headers"]) - require.Equal(t, "", result["Body"]) + require.Empty(t, result["Body"]) }) } diff --git a/platform/data/addDataToContext_test.go b/platform/data/addDataToContext_test.go index b2e9ccc..17c7fac 100644 --- a/platform/data/addDataToContext_test.go +++ b/platform/data/addDataToContext_test.go @@ -25,7 +25,7 @@ func TestAddDataToContextHelper(t *testing.T) { map[string]any{"key": "value"}, ) - assert.Error(t, err) + require.Error(t, err) assert.Equal(t, baseCtx, enrichedCtx, "Context should remain unchanged") }) @@ -40,7 +40,7 @@ func TestAddDataToContextHelper(t *testing.T) { map[string]any{"key": "value"}, ) - assert.Error(t, err) + require.Error(t, err) assert.Equal(t, baseCtx, enrichedCtx, "Context should remain unchanged") assert.Nil(t, enrichedCtx.Value(constants.EvalData), "Context should not have data added") }) @@ -56,7 +56,7 @@ func TestAddDataToContextHelper(t *testing.T) { map[string]any{"key": "value"}, ) - assert.NoError(t, err) + require.NoError(t, err) assert.NotEqual(t, baseCtx, enrichedCtx, "Context should be modified") // Verify data was added to context @@ -83,7 +83,7 @@ func TestAddDataToContextHelper(t *testing.T) { map[string]any{"request": req}, ) - assert.NoError(t, err) + require.NoError(t, err) assert.NotEqual(t, baseCtx, enrichedCtx, "Context should be modified") // Verify request data was added to context @@ -109,7 +109,7 @@ func TestAddDataToContextHelper(t *testing.T) { enrichedCtx, err := AddDataToContextHelper(baseCtx, logger, provider, map[string]any{"key": "value"}, map[string]any{"request": req}) - assert.NoError(t, err) + require.NoError(t, err) assert.NotEqual(t, baseCtx, enrichedCtx, "Context should be modified") // Verify data was added to context @@ -141,7 +141,7 @@ func TestAddDataToContextHelper(t *testing.T) { map[string]any{"": 42}, // Empty key should cause an error ) - assert.Error(t, err) + require.Error(t, err) assert.Equal(t, baseCtx, enrichedCtx, "Context should be unchanged when there's an error") // No data should be added to context when there's an error @@ -159,7 +159,7 @@ func TestAddDataToContextHelper(t *testing.T) { enrichedCtx, err := AddDataToContextHelper(baseCtx, logger, provider, map[string]any{"key": "value"}) - assert.NoError(t, err) + require.NoError(t, err) assert.NotEqual(t, baseCtx, enrichedCtx, "Context should be modified") // Verify data was added to context @@ -193,7 +193,7 @@ func TestAddDataToContextWithErrorHandling(t *testing.T) { ) // Should return an error - assert.Error(t, err) + require.Error(t, err) // Context should remain unchanged when there's an error assert.Equal(t, baseCtx, enrichedCtx, "Context should be unchanged when there's an error") diff --git a/platform/data/compositeProvider_test.go b/platform/data/compositeProvider_test.go index 60bb41c..24ba3dd 100644 --- a/platform/data/compositeProvider_test.go +++ b/platform/data/compositeProvider_test.go @@ -2,7 +2,6 @@ package data import ( "context" - "errors" "testing" "github.com/robbyt/go-polyscript/platform/constants" @@ -236,11 +235,11 @@ func TestCompositeProvider_GetData(t *testing.T) { result, err := provider.GetData(ctx) if tt.expectError { - assert.Error(t, err, "Should return error when a provider fails") + require.Error(t, err, "Should return error when a provider fails") return } - assert.NoError(t, err, "Should not return error for valid providers") + require.NoError(t, err, "Should not return error for valid providers") assertMapContainsExpectedHelper(t, tt.expectedData, result) // Verify data consistency across calls @@ -410,7 +409,7 @@ func TestCompositeProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, inputData) - assert.NoError(t, err, "Should not return error with empty provider list") + require.NoError(t, err, "Should not return error with empty provider list") assert.Equal(t, ctx, newCtx, "Context should remain unchanged") }) @@ -423,12 +422,12 @@ func TestCompositeProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, inputData) - assert.NoError(t, err, "Should not return error for context provider") + require.NoError(t, err, "Should not return error for context provider") assert.NotEqual(t, ctx, newCtx, "Context should be modified") // Verify data was added correctly data, err := provider.GetData(newCtx) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "value", data["key"]) }) @@ -441,13 +440,13 @@ func TestCompositeProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, inputData) - assert.Error(t, err, "Should return error for static provider") + require.Error(t, err, "Should return error for static provider") assert.Equal(t, ctx, newCtx, "Context should remain unchanged") - assert.True(t, errors.Is(err, ErrStaticProviderNoRuntimeUpdates)) + require.ErrorIs(t, err, ErrStaticProviderNoRuntimeUpdates) // Verify static data is still available data, getErr := provider.GetData(ctx) - assert.NoError(t, getErr) + require.NoError(t, getErr) assert.Equal(t, simpleData, data, "Static data should still be available") }) @@ -463,12 +462,12 @@ func TestCompositeProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, inputData) - assert.NoError(t, err, "Should not return error when at least one provider succeeds") + require.NoError(t, err, "Should not return error when at least one provider succeeds") assert.NotEqual(t, ctx, newCtx, "Context should be modified") // Verify both static and context data are available data, err := provider.GetData(newCtx) - assert.NoError(t, err) + require.NoError(t, err) // Static data should be present assert.Equal(t, simpleData["string"], data["string"], "Static data should be present") @@ -489,7 +488,7 @@ func TestCompositeProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, inputData) - assert.Error(t, err, "Should return error when all non-static providers fail") + require.Error(t, err, "Should return error when all non-static providers fail") assert.Equal(t, ctx, newCtx, "Context should remain unchanged") }) @@ -506,12 +505,12 @@ func TestCompositeProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, inputData) - assert.NoError(t, err, "Should not return error when skipping nil providers") + require.NoError(t, err, "Should not return error when skipping nil providers") assert.NotEqual(t, ctx, newCtx, "Context should be modified") // Verify context data was added data, err := provider.GetData(newCtx) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "value", data["key"]) }) @@ -527,9 +526,9 @@ func TestCompositeProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, inputData) - assert.Error(t, err, "Should return error when all providers are static") + require.Error(t, err, "Should return error when all providers are static") assert.Equal(t, ctx, newCtx, "Context should remain unchanged") - assert.True(t, errors.Is(err, ErrStaticProviderNoRuntimeUpdates), + require.ErrorIs(t, err, ErrStaticProviderNoRuntimeUpdates, "Error should be StaticProviderNoRuntimeUpdates") }) } diff --git a/platform/data/contextProvider_test.go b/platform/data/contextProvider_test.go index 5ebe767..0a26721 100644 --- a/platform/data/contextProvider_test.go +++ b/platform/data/contextProvider_test.go @@ -46,7 +46,7 @@ func TestContextProvider_GetData(t *testing.T) { result, err := provider.GetData(ctx) - assert.Error(t, err, "Should return error for empty context key") + require.Error(t, err, "Should return error for empty context key") assert.Nil(t, result, "Result should be nil when error occurs") }) @@ -56,7 +56,7 @@ func TestContextProvider_GetData(t *testing.T) { result, err := provider.GetData(ctx) - assert.NoError(t, err, "Should not return error for nil context value") + require.NoError(t, err, "Should not return error for nil context value") assert.NotNil(t, result, "Result should be an empty map, not nil") assert.Empty(t, result, "Result map should be empty") @@ -70,7 +70,7 @@ func TestContextProvider_GetData(t *testing.T) { result, err := provider.GetData(ctx) - assert.NoError(t, err, "Should not return error for valid context") + require.NoError(t, err, "Should not return error for valid context") assert.Equal(t, simpleData, result, "Result should match expected data") // Verify data consistency @@ -83,7 +83,7 @@ func TestContextProvider_GetData(t *testing.T) { result, err := provider.GetData(ctx) - assert.NoError(t, err, "Should not return error for valid context") + require.NoError(t, err, "Should not return error for valid context") assert.Equal(t, complexData, result, "Result should match expected data") // Verify data consistency @@ -108,7 +108,7 @@ func TestContextProvider_GetData(t *testing.T) { ctx := context.WithValue(t.Context(), constants.EvalData, data) result, err := provider.GetData(ctx) - assert.NoError(t, err, "Should not return error for valid context") + require.NoError(t, err, "Should not return error for valid context") // Verify top-level keys exist assert.Contains(t, result, "user", "Should contain user key") @@ -142,7 +142,7 @@ func TestContextProvider_GetData(t *testing.T) { ctx := context.WithValue(t.Context(), constants.EvalData, data) result, err := provider.GetData(ctx) - assert.NoError(t, err, "Should not return error for valid context") + require.NoError(t, err, "Should not return error for valid context") // Verify all types are preserved assert.Equal(t, "value", result["string"], "String should match") @@ -152,7 +152,7 @@ func TestContextProvider_GetData(t *testing.T) { // Note that we can't assert directly on slices, but we can check length and contents arr, ok := result["array"].([]string) assert.True(t, ok, "Array should be preserved") - assert.Equal(t, 2, len(arr), "Array should have correct length") + assert.Len(t, arr, 2, "Array should have correct length") nestedMap, ok := result["map"].(map[string]any) assert.True(t, ok, "Nested map should be preserved") @@ -165,7 +165,7 @@ func TestContextProvider_GetData(t *testing.T) { result, err := provider.GetData(ctx) - assert.Error(t, err, "Should return error for invalid data type") + require.Error(t, err, "Should return error for invalid data type") assert.Nil(t, result, "Result should be nil when error occurs") }) @@ -175,7 +175,7 @@ func TestContextProvider_GetData(t *testing.T) { result, err := provider.GetData(ctx) - assert.Error(t, err, "Should return error for invalid data type") + require.Error(t, err, "Should return error for invalid data type") assert.Nil(t, result, "Result should be nil when error occurs") }) } @@ -190,7 +190,7 @@ func TestContextProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, map[string]any{"key": "value"}) - assert.Error(t, err, "Should return error for empty context key") + require.Error(t, err, "Should return error for empty context key") assert.Equal(t, ctx, newCtx, "Context should remain unchanged") }) @@ -200,11 +200,11 @@ func TestContextProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, nil) - assert.NoError(t, err, "Should not return error with nil data") + require.NoError(t, err, "Should not return error with nil data") assert.NotEqual(t, ctx, newCtx, "Context should be modified even with nil data") data, err := provider.GetData(newCtx) - assert.NoError(t, err) + require.NoError(t, err) assert.Empty(t, data, "Data should be empty with nil input") }) @@ -214,11 +214,11 @@ func TestContextProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, map[string]any{"key1": "value1", "key2": 123}) - assert.NoError(t, err, "Should not return error with valid map data") + require.NoError(t, err, "Should not return error with valid map data") assert.NotEqual(t, ctx, newCtx, "Context should be modified") data, err := provider.GetData(newCtx) - assert.NoError(t, err) + require.NoError(t, err) // Data should be at root level assert.Equal(t, "value1", data["key1"], "Should contain key1 at root level") @@ -233,11 +233,11 @@ func TestContextProvider_AddDataToContext(t *testing.T) { map[string]any{"key1": "value1"}, map[string]any{"key2": "value2"}) - assert.NoError(t, err, "Should not return error with multiple map items") + require.NoError(t, err, "Should not return error with multiple map items") assert.NotEqual(t, ctx, newCtx, "Context should be modified") data, err := provider.GetData(newCtx) - assert.NoError(t, err) + require.NoError(t, err) // Data should be at root level assert.Equal(t, "value1", data["key1"], "Should contain key1 at root level") @@ -252,11 +252,11 @@ func TestContextProvider_AddDataToContext(t *testing.T) { req := createTestRequestHelper() newCtx, err := provider.AddDataToContext(ctx, map[string]any{"request": req}) - assert.NoError(t, err, "Should not return error with HTTP request in map") + require.NoError(t, err, "Should not return error with HTTP request in map") assert.NotEqual(t, ctx, newCtx, "Context should be modified") data, err := provider.GetData(newCtx) - assert.NoError(t, err) + require.NoError(t, err) assert.Contains(t, data, "request", "Should contain request key") requestData, ok := data["request"].(map[string]any) @@ -272,12 +272,12 @@ func TestContextProvider_AddDataToContext(t *testing.T) { // Try to add data with an empty key newCtx, err := provider.AddDataToContext(ctx, map[string]any{"": "value"}) - assert.Error(t, err, "Should error with empty key") + require.Error(t, err, "Should error with empty key") assert.Contains(t, err.Error(), "empty keys are not allowed") // Context should be modified but the empty key should not be added data, getErr := provider.GetData(newCtx) - assert.NoError(t, getErr) + require.NoError(t, getErr) assert.NotContains(t, data, "", "Empty key should not be added") }) @@ -297,10 +297,10 @@ func TestContextProvider_AddDataToContext(t *testing.T) { }, }) - assert.NoError(t, err) + require.NoError(t, err) data, getErr := provider.GetData(newCtx) - assert.NoError(t, getErr) + require.NoError(t, getErr) // Navigate the nested structure userMap, ok := data["user"].(map[string]any) @@ -325,7 +325,7 @@ func TestContextProvider_ProcessValue(t *testing.T) { provider := NewContextProvider(constants.EvalData) result, err := provider.processValue(nil) - assert.NoError(t, err, "Should not error for nil value") + require.NoError(t, err, "Should not error for nil value") assert.Nil(t, result, "Result should be nil") }) @@ -334,23 +334,23 @@ func TestContextProvider_ProcessValue(t *testing.T) { // Test string result, err := provider.processValue("test string") - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "test string", result) // Test number result, err = provider.processValue(42) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, 42, result) // Test boolean result, err = provider.processValue(true) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, true, result) // Test slice slice := []string{"one", "two"} result, err = provider.processValue(slice) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, slice, result) }) @@ -359,7 +359,7 @@ func TestContextProvider_ProcessValue(t *testing.T) { var nilReq *http.Request = nil result, err := provider.processValue(nilReq) - assert.NoError(t, err, "Should not error for nil HTTP request") + require.NoError(t, err, "Should not error for nil HTTP request") assert.Nil(t, result, "Result should be nil") }) @@ -369,14 +369,14 @@ func TestContextProvider_ProcessValue(t *testing.T) { // Test *http.Request result, err := provider.processValue(req) - assert.NoError(t, err) + require.NoError(t, err) resultMap, ok := result.(map[string]any) assert.True(t, ok, "Result should be a map") assert.Equal(t, "GET", resultMap["Method"]) // Test http.Request (value) result, err = provider.processValue(*req) - assert.NoError(t, err) + require.NoError(t, err) resultMap, ok = result.(map[string]any) assert.True(t, ok, "Result should be a map") assert.Equal(t, "GET", resultMap["Method"]) @@ -390,7 +390,7 @@ func TestContextProvider_ProcessValue(t *testing.T) { } _, err := provider.processValue(mapWithEmptyKey) - assert.Error(t, err, "Should reject maps with empty keys") + require.Error(t, err, "Should reject maps with empty keys") assert.Contains(t, err.Error(), "empty keys are not allowed") }) @@ -407,7 +407,7 @@ func TestContextProvider_ProcessValue(t *testing.T) { } result, err := provider.processValue(nestedMap) - assert.NoError(t, err) + require.NoError(t, err) // Navigate through the levels to verify all maps were processed resultMap, ok := result.(map[string]any) @@ -436,7 +436,7 @@ func TestContextProvider_ProcessValue(t *testing.T) { } _, err := provider.processValue(problematicMap) - assert.Error(t, err) + require.Error(t, err) assert.Contains(t, err.Error(), "empty keys are not allowed") }) } @@ -455,7 +455,7 @@ func TestContextProvider_DataIntegration(t *testing.T) { // Verify data data, err := provider.GetData(newCtx) - assert.NoError(t, err) + require.NoError(t, err) // Data should be available directly at root level assert.Equal(t, "value", data["key"], "Should contain the correct value at root") @@ -476,7 +476,7 @@ func TestContextProvider_DataIntegration(t *testing.T) { // Verify both pieces of data exist data, err := provider.GetData(newCtx) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "value", data["existing"], "Should preserve existing value") assert.Equal(t, "value", data["new"], "Should add new value") @@ -495,7 +495,7 @@ func TestContextProvider_DataIntegration(t *testing.T) { // Get raw data from context data, err := provider.GetData(newCtx) - assert.NoError(t, err) + require.NoError(t, err) // Request should be converted to a map requestData, ok := data["request"].(map[string]any) @@ -531,7 +531,7 @@ func TestContextProvider_DataIntegration(t *testing.T) { // Verify all data correctly merged data, err := provider.GetData(newCtx) - assert.NoError(t, err) + require.NoError(t, err) userMap, ok := data["user"].(map[string]any) assert.True(t, ok, "User should be a map") @@ -579,7 +579,7 @@ func TestContextProvider_DataIntegration(t *testing.T) { badData := map[string]any{"": "value"} _, err := provider.AddDataToContext(ctx, badData) - assert.Error(t, err, "Should reject empty keys") + require.Error(t, err, "Should reject empty keys") assert.Contains(t, err.Error(), "empty keys are not allowed") }) } diff --git a/platform/data/provider_test.go b/platform/data/provider_test.go index 2d4bd25..107e069 100644 --- a/platform/data/provider_test.go +++ b/platform/data/provider_test.go @@ -2,11 +2,11 @@ package data import ( "context" - "errors" "testing" "github.com/robbyt/go-polyscript/platform/constants" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // TestProvider_Interface ensures that all provider implementations comply with the Provider interface @@ -40,12 +40,12 @@ func TestProvider_GetData(t *testing.T) { ctx := t.Context() result, err := provider.GetData(ctx) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, simpleData, result) // Get a fresh copy to verify data consistency newResult, err := provider.GetData(ctx) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, simpleData, newResult) }) @@ -54,7 +54,7 @@ func TestProvider_GetData(t *testing.T) { ctx := t.Context() result, err := provider.GetData(ctx) - assert.NoError(t, err) + require.NoError(t, err) assert.Empty(t, result) }) @@ -64,12 +64,12 @@ func TestProvider_GetData(t *testing.T) { ctx := context.WithValue(t.Context(), constants.EvalData, simpleData) result, err := provider.GetData(ctx) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, simpleData, result) // Get a fresh copy to verify data consistency newResult, err := provider.GetData(ctx) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, simpleData, newResult) }) @@ -78,7 +78,7 @@ func TestProvider_GetData(t *testing.T) { ctx := t.Context() result, err := provider.GetData(ctx) - assert.Error(t, err) + require.Error(t, err) assert.Nil(t, result) }) @@ -87,7 +87,7 @@ func TestProvider_GetData(t *testing.T) { ctx := context.WithValue(t.Context(), constants.EvalData, "not a map") result, err := provider.GetData(ctx) - assert.Error(t, err) + require.Error(t, err) assert.Nil(t, result) }) @@ -105,7 +105,7 @@ func TestProvider_GetData(t *testing.T) { ) result, err := provider.GetData(ctx) - assert.NoError(t, err) + require.NoError(t, err) // Verify expected values (context overrides static for shared keys) assert.Equal(t, "value", result["static"]) @@ -119,7 +119,7 @@ func TestProvider_GetData(t *testing.T) { // Get a fresh copy to verify data consistency newResult, err := provider.GetData(ctx) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, result, newResult) }) @@ -128,7 +128,7 @@ func TestProvider_GetData(t *testing.T) { ctx := t.Context() result, err := provider.GetData(ctx) - assert.NoError(t, err) + require.NoError(t, err) assert.Empty(t, result) }) @@ -140,7 +140,7 @@ func TestProvider_GetData(t *testing.T) { ctx := t.Context() result, err := provider.GetData(ctx) - assert.Error(t, err) + require.Error(t, err) assert.Nil(t, result) }) } @@ -156,13 +156,13 @@ func TestProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, map[string]any{"key": "value"}) - assert.Error(t, err, "StaticProvider should reject data additions") - assert.True(t, errors.Is(err, ErrStaticProviderNoRuntimeUpdates)) + require.Error(t, err, "StaticProvider should reject data additions") + require.ErrorIs(t, err, ErrStaticProviderNoRuntimeUpdates) assert.Equal(t, ctx, newCtx, "Context should remain unchanged") // Verify static data is still available data, err := provider.GetData(ctx) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, simpleData, data) }) @@ -173,12 +173,12 @@ func TestProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, map[string]any{"key": "value"}) - assert.NoError(t, err) + require.NoError(t, err) assert.NotEqual(t, ctx, newCtx, "Context should be modified") // Verify data was stored correctly data, err := provider.GetData(newCtx) - assert.NoError(t, err) + require.NoError(t, err) // Data should be at root level with no namespace assert.Equal(t, "value", data["key"], "Data should be at root level") @@ -191,12 +191,12 @@ func TestProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, map[string]any{"request": req}) - assert.NoError(t, err) + require.NoError(t, err) assert.NotEqual(t, ctx, newCtx, "Context should be modified") // Verify request data was stored correctly data, err := provider.GetData(newCtx) - assert.NoError(t, err) + require.NoError(t, err) // Request should be at root level with direct access requestMap, ok := data["request"].(map[string]any) @@ -211,7 +211,7 @@ func TestProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, map[string]any{"key": "value"}) - assert.Error(t, err, "Empty context key should cause error") + require.Error(t, err, "Empty context key should cause error") assert.Equal(t, ctx, newCtx, "Context should remain unchanged on error") }) @@ -225,7 +225,7 @@ func TestProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, map[string]any{"key": "value"}) - assert.NoError( + require.NoError( t, err, "StaticProvider error should be ignored when ContextProvider succeeds", @@ -234,7 +234,7 @@ func TestProvider_AddDataToContext(t *testing.T) { // Verify data data, err := provider.GetData(newCtx) - assert.NoError(t, err) + require.NoError(t, err) // Should have both static and context data assert.Equal(t, simpleData["string"], data["string"], "Should contain static data") @@ -250,7 +250,7 @@ func TestProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, map[string]any{"key": "value"}) - assert.Error(t, err, "Should error when all providers fail") + require.Error(t, err, "Should error when all providers fail") assert.Equal(t, ctx, newCtx, "Context should remain unchanged") }) @@ -263,12 +263,12 @@ func TestProvider_AddDataToContext(t *testing.T) { map[string]any{"key1": "value1"}, map[string]any{"key2": "value2"}) - assert.NoError(t, err) + require.NoError(t, err) assert.NotEqual(t, ctx, newCtx, "Context should be modified") // Verify data was merged correctly data, err := provider.GetData(newCtx) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "value1", data["key1"], "Should contain first item") assert.Equal(t, "value2", data["key2"], "Should contain second item") diff --git a/platform/data/staticProvider_test.go b/platform/data/staticProvider_test.go index 5e0ce48..a2b5908 100644 --- a/platform/data/staticProvider_test.go +++ b/platform/data/staticProvider_test.go @@ -1,7 +1,6 @@ package data import ( - "errors" "testing" "github.com/stretchr/testify/assert" @@ -47,7 +46,7 @@ func TestStaticProvider_Creation(t *testing.T) { ctx := t.Context() result, err := provider.GetData(ctx) - assert.NoError(t, err, "GetData should never return an error") + require.NoError(t, err, "GetData should never return an error") if tt.expectEmpty { assert.Empty(t, result, "Result map should be empty") @@ -96,7 +95,7 @@ func TestStaticProvider_GetData(t *testing.T) { result, err := provider.GetData(ctx) - assert.NoError(t, err, "GetData should never return an error") + require.NoError(t, err, "GetData should never return an error") if tt.inputData == nil { assert.Empty(t, result, "Result map should be empty for nil input") @@ -110,7 +109,7 @@ func TestStaticProvider_GetData(t *testing.T) { result["newTestKey"] = "newTestValue" newResult, err := provider.GetData(ctx) - assert.NoError(t, err, "GetData should never return an error") + require.NoError(t, err, "GetData should never return an error") assert.NotContains( t, newResult, @@ -135,14 +134,14 @@ func TestStaticProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, nil) - assert.Error(t, err, "StaticProvider should reject all attempts to add data") + require.Error(t, err, "StaticProvider should reject all attempts to add data") assert.Equal(t, ctx, newCtx, "Context should remain unchanged") - assert.True(t, errors.Is(err, ErrStaticProviderNoRuntimeUpdates), + require.ErrorIs(t, err, ErrStaticProviderNoRuntimeUpdates, "Error should be ErrStaticProviderNoRuntimeUpdates") // Verify data is still available data, getErr := provider.GetData(ctx) - assert.NoError(t, getErr) + require.NoError(t, getErr) assert.Equal(t, simpleData, data) }) @@ -152,9 +151,9 @@ func TestStaticProvider_AddDataToContext(t *testing.T) { newCtx, err := provider.AddDataToContext(ctx, map[string]any{"new": "data"}) - assert.Error(t, err, "StaticProvider should reject all attempts to add data") + require.Error(t, err, "StaticProvider should reject all attempts to add data") assert.Equal(t, ctx, newCtx, "Context should remain unchanged") - assert.True(t, errors.Is(err, ErrStaticProviderNoRuntimeUpdates), + require.ErrorIs(t, err, ErrStaticProviderNoRuntimeUpdates, "Error should be ErrStaticProviderNoRuntimeUpdates") }) @@ -167,9 +166,9 @@ func TestStaticProvider_AddDataToContext(t *testing.T) { map[string]any{"request": createTestRequestHelper()}, ) - assert.Error(t, err, "StaticProvider should reject all attempts to add data") + require.Error(t, err, "StaticProvider should reject all attempts to add data") assert.Equal(t, ctx, newCtx, "Context should remain unchanged") - assert.True(t, errors.Is(err, ErrStaticProviderNoRuntimeUpdates), + require.ErrorIs(t, err, ErrStaticProviderNoRuntimeUpdates, "Error should be ErrStaticProviderNoRuntimeUpdates") }) @@ -184,9 +183,9 @@ func TestStaticProvider_AddDataToContext(t *testing.T) { map[string]any{"num": 42}, ) - assert.Error(t, err, "StaticProvider should reject all attempts to add data") + require.Error(t, err, "StaticProvider should reject all attempts to add data") assert.Equal(t, ctx, newCtx, "Context should remain unchanged") - assert.True(t, errors.Is(err, ErrStaticProviderNoRuntimeUpdates), + require.ErrorIs(t, err, ErrStaticProviderNoRuntimeUpdates, "Error should be ErrStaticProviderNoRuntimeUpdates") }) } @@ -201,7 +200,7 @@ func TestStaticProvider_ErrorIdentification(t *testing.T) { _, err := provider.AddDataToContext(ctx, map[string]any{"data": "some data"}) // Test that errors.Is works correctly with the sentinel error - assert.True(t, errors.Is(err, ErrStaticProviderNoRuntimeUpdates), + require.ErrorIs(t, err, ErrStaticProviderNoRuntimeUpdates, "Error should be identifiable with errors.Is") // Test direct equality for legacy code @@ -214,6 +213,6 @@ func TestStaticProvider_ErrorIdentification(t *testing.T) { // Verify data is still available data, getErr := provider.GetData(ctx) - assert.NoError(t, getErr, "GetData should never return an error") + require.NoError(t, getErr, "GetData should never return an error") assert.Equal(t, simpleData, data, "Static data should be available after error") } diff --git a/platform/evaluatorResponse_test.go b/platform/evaluatorResponse_test.go index 2b90183..ca7c727 100644 --- a/platform/evaluatorResponse_test.go +++ b/platform/evaluatorResponse_test.go @@ -118,7 +118,7 @@ func TestEvaluatorResponseUsage(t *testing.T) { // Type checking pattern result := mockResponse.Interface() - require.Equal(t, mockResponse.Type(), data.STRING) + require.Equal(t, data.STRING, mockResponse.Type()) strResult, ok := result.(string) assert.True(t, ok, "Should convert to string") @@ -134,7 +134,7 @@ func TestEvaluatorResponseUsage(t *testing.T) { // Type checking for map result = mockResponse.Interface() - require.Equal(t, mockResponse.Type(), data.MAP) + require.Equal(t, data.MAP, mockResponse.Type()) mapResult, ok := result.(map[string]any) assert.True(t, ok, "Should convert to map") diff --git a/platform/evaluator_test.go b/platform/evaluator_test.go index b47d91e..6ba2cb9 100644 --- a/platform/evaluator_test.go +++ b/platform/evaluator_test.go @@ -103,7 +103,7 @@ func TestEvaluatorInterface(t *testing.T) { Return((*mocks.EvaluatorResponse)(nil), errors.New("evaluation error")) response, err = errorEvaluator.Eval(t.Context()) - assert.Error(t, err, "Eval should return an error") + require.Error(t, err, "Eval should return an error") assert.Nil(t, response, "Response should be nil when there's an error") assert.Contains(t, err.Error(), "evaluation error", "Error message should be preserved") } @@ -197,8 +197,8 @@ func TestEvalDataPreparerInterfaceDirectImplementation(t *testing.T) { Return(ctx, errors.New("preparation error")) ogCtx, err := errorPreparer.AddDataToContext(ctx, map[string]any{"test": "value"}) - assert.Error(t, err, "Should return an error") - assert.ErrorContains(t, err, "preparation error", "Error message should be preserved") + require.Error(t, err, "Should return an error") + require.ErrorContains(t, err, "preparation error", "Error message should be preserved") assert.Equal(t, ctx, ogCtx, "Original context should be returned on error") } @@ -254,7 +254,7 @@ func TestEvaluatorWithPrepInterface(t *testing.T) { Return(ctx, errors.New("preparation error")) _, err = prepErrorEvaluator.AddDataToContext(ctx, map[string]any{"test": "data"}) - assert.Error(t, err, "Should return an error when preparation fails") + require.Error(t, err, "Should return an error when preparation fails") // Test error in evaluation evalErrorEvaluator := &mockEvaluatorWithPreparer{} @@ -289,6 +289,6 @@ func TestEvaluatorWithPrepErrors(t *testing.T) { ) // Properly wrapped in map // This should now succeed as integers are properly wrapped in a map - assert.NoError(t, err, "AddDataToContext should succeed with properly wrapped integers") + require.NoError(t, err, "AddDataToContext should succeed with properly wrapped integers") assert.NotNil(t, enrichedCtx, "Should return a context regardless") } diff --git a/platform/script/executableUnit_test.go b/platform/script/executableUnit_test.go index 40ba44a..a0db961 100644 --- a/platform/script/executableUnit_test.go +++ b/platform/script/executableUnit_test.go @@ -238,10 +238,9 @@ func TestNewVersion(t *testing.T) { // Verify version ID is set from content checksum versionID := exe.GetID() require.NotEmpty(t, versionID, "Expected version ID to be non-empty") - require.Equal( + require.Len( t, - len(versionID), - checksumLength, + versionID, checksumLength, "Expected version ID length to match checksum length", ) diff --git a/platform/script/loader/fromHTTP_test.go b/platform/script/loader/fromHTTP_test.go index 33c00e5..0dac81a 100644 --- a/platform/script/loader/fromHTTP_test.go +++ b/platform/script/loader/fromHTTP_test.go @@ -10,6 +10,7 @@ import ( "time" "github.com/robbyt/go-polyscript/platform/script/loader/httpauth" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -22,7 +23,7 @@ func TestNewFromHTTP(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(FunctionContent)) - require.NoError(t, err) + assert.NoError(t, err) }), ) defer tlsServer.Close() @@ -55,7 +56,7 @@ func TestNewFromHTTP(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(FunctionContent)) - require.NoError(t, err) + assert.NoError(t, err) })) defer server.Close() @@ -163,7 +164,7 @@ func TestNewFromHTTPWithOptions(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(FunctionContent)) - require.NoError(t, err) + assert.NoError(t, err) }), ) defer server.Close() @@ -212,7 +213,7 @@ func TestFromHTTP_TLSConfig(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(FunctionContent)) - require.NoError(t, err) + assert.NoError(t, err) }), ) defer server.Close() @@ -240,7 +241,7 @@ func TestFromHTTP_TLSConfig(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(FunctionContent)) - require.NoError(t, err) + assert.NoError(t, err) }), ) defer server.Close() @@ -276,7 +277,7 @@ func TestFromHTTP_TLSConfig(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(FunctionContent)) - require.NoError(t, err) + assert.NoError(t, err) }), ) defer server.Close() @@ -315,7 +316,7 @@ func TestFromHTTP_TLSConfig(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(FunctionContent)) - require.NoError(t, err) + assert.NoError(t, err) }), ) defer server.Close() @@ -352,7 +353,7 @@ func TestFromHTTP_GetReader(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(testScript)) - require.NoError(t, err) + assert.NoError(t, err) })) defer server.Close() @@ -371,7 +372,7 @@ func TestFromHTTP_GetReader(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusUnauthorized) _, err := w.Write([]byte("Unauthorized")) - require.NoError(t, err) + assert.NoError(t, err) })) defer server.Close() @@ -390,7 +391,7 @@ func TestFromHTTP_GetReader(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) _, err := w.Write([]byte("Not Found")) - require.NoError(t, err) + assert.NoError(t, err) })) defer server.Close() @@ -436,7 +437,7 @@ func TestFromHTTP_GetReaderWithContext(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(testScript)) - require.NoError(t, err) + assert.NoError(t, err) })) defer server.Close() @@ -457,7 +458,7 @@ func TestFromHTTP_GetReaderWithContext(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(testScript)) - require.NoError(t, err) + assert.NoError(t, err) })) defer server.Close() @@ -492,7 +493,7 @@ func TestFromHTTP_GetReaderWithContext(t *testing.T) { time.Sleep(100 * time.Millisecond) w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(testScript)) - require.NoError(t, err) + assert.NoError(t, err) })) defer server.Close() @@ -530,7 +531,7 @@ func TestFromHTTP_String(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, err := w.Write([]byte("test script content")) - require.NoError(t, err) + assert.NoError(t, err) })) defer server.Close() @@ -563,7 +564,7 @@ func TestFromHTTP_String(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) _, err := w.Write([]byte("Not Found")) - require.NoError(t, err) + assert.NoError(t, err) })) defer server.Close() @@ -645,7 +646,7 @@ func TestFromHTTP_GetSourceURL(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(FunctionContent)) - require.NoError(t, err) + assert.NoError(t, err) })) defer server.Close() diff --git a/platform/script/loader/inference_test.go b/platform/script/loader/inference_test.go index 63582f9..c4c04d3 100644 --- a/platform/script/loader/inference_test.go +++ b/platform/script/loader/inference_test.go @@ -189,7 +189,7 @@ func TestInferLoader(t *testing.T) { t.Run(tc.name, func(t *testing.T) { result, err := InferLoader(tc.input) - assert.Error(t, err, "Expected error for unsupported type") + require.Error(t, err, "Expected error for unsupported type") assert.Nil(t, result, "Result should be nil for unsupported type") assert.Contains(t, err.Error(), "unsupported input type") }) diff --git a/polyscript_test.go b/polyscript_test.go index 229e291..c382b54 100644 --- a/polyscript_test.go +++ b/polyscript_test.go @@ -322,7 +322,7 @@ func TestEvalHelpers(t *testing.T) { case int64: assert.Equal(t, int64(5), v, "length should be 5") case float64: - assert.Equal(t, float64(5), v, "length should be 5") + assert.InDelta(t, float64(5), v, 0.0001, "length should be 5") default: t.Errorf("length is unexpected type %T", v) } @@ -426,7 +426,7 @@ func TestEvalHelpers(t *testing.T) { case int64: assert.Equal(t, int64(12), v, "length should be 12") case float64: - assert.Equal(t, float64(12), v, "length should be 12") + assert.InDelta(t, float64(12), v, 0.0001, "length should be 12") default: t.Errorf("length is unexpected type %T", v) } @@ -552,7 +552,7 @@ _ = result` case int64: assert.Equal(t, int64(30), v, "timeout should be 30") case float64: - assert.Equal(t, float64(30), v, "timeout should be 30") + assert.InDelta(t, float64(30), v, 0.0001, "timeout should be 30") default: t.Errorf("timeout is unexpected type %T", v) } diff --git a/readme_test.go b/readme_test.go index 2226af6..5c283c0 100644 --- a/readme_test.go +++ b/readme_test.go @@ -1,7 +1,6 @@ package polyscript_test import ( - "context" "log/slog" "os" "testing" @@ -44,7 +43,7 @@ func TestReadmeQuickStart(t *testing.T) { ) require.NoError(t, err, "Should create evaluator successfully") - ctx := context.Background() + ctx := t.Context() result, err := evaluator.Eval(ctx) require.NoError(t, err, "Should evaluate successfully") require.NotNil(t, result, "Result should not be nil") @@ -80,7 +79,7 @@ func TestReadmeStaticProvider(t *testing.T) { evaluator, err := polyscript.FromRisorStringWithData(script, inputData, logger.Handler()) require.NoError(t, err, "Should create evaluator successfully") - ctx := context.Background() + ctx := t.Context() result, err := evaluator.Eval(ctx) require.NoError(t, err, "Should evaluate successfully") @@ -107,7 +106,7 @@ func TestReadmeContextProvider(t *testing.T) { evaluator, err := polyscript.FromRisorString(script, logger.Handler()) require.NoError(t, err, "Should create evaluator successfully") - ctx := context.Background() + ctx := t.Context() runtimeData := map[string]any{"name": "Billie Jean", "relationship": false} enrichedCtx, err := evaluator.AddDataToContext(ctx, runtimeData) require.NoError(t, err, "Should add data to context successfully") @@ -152,7 +151,7 @@ func TestReadmeCombiningStaticAndDynamic(t *testing.T) { require.NoError(t, err, "Should create evaluator with static data") requestData := map[string]any{"name": "Robert"} - enrichedCtx, err := evaluator.AddDataToContext(context.Background(), requestData) + enrichedCtx, err := evaluator.AddDataToContext(t.Context(), requestData) require.NoError(t, err, "Should add runtime data to context") result, err := evaluator.Eval(enrichedCtx) @@ -188,7 +187,7 @@ _ = result ) require.NoError(t, err, "Should create Starlark evaluator") - result, err := evaluator.Eval(context.Background()) + result, err := evaluator.Eval(t.Context()) require.NoError(t, err, "Should evaluate Starlark script") resultMap, ok := result.Interface().(map[string]any) @@ -211,7 +210,7 @@ func TestReadmeExtism(t *testing.T) { ) require.NoError(t, err, "Should create Extism evaluator") - result, err := evaluator.Eval(context.Background()) + result, err := evaluator.Eval(t.Context()) require.NoError(t, err, "Should evaluate WASM module") require.NotNil(t, result, "Result should not be nil")