Skip to content

Refine testify assertions in tests for clearer failures and lint-aligned patterns#4051

Merged
lpcox merged 4 commits intomainfrom
copilot/go-fan-review-stretchr-testify
Apr 18, 2026
Merged

Refine testify assertions in tests for clearer failures and lint-aligned patterns#4051
lpcox merged 4 commits intomainfrom
copilot/go-fan-review-stretchr-testify

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 18, 2026

This PR addresses the Go Fan/testify review by replacing low-signal assertion patterns with more specific testify assertions across the reported test areas. The goal is to improve failure diagnostics and align tests with current assertion best practices without changing production behavior.

  • Assertion specificity cleanup

    • Replaced boolean equality assertions with direct boolean assertions:
      • assert.Equal(t, true/false, ...)assert.True(...) / assert.False(...)
    • Replaced generic substring checks:
      • assert.True(t, strings.Contains(...))assert.Contains(...)
      • assert.False(t, strings.Contains(...))assert.NotContains(...)
    • Replaced length-comparison boolean assertions:
      • assert.True(t, len(x) <= N)assert.LessOrEqual(t, len(x), N)
    • Replaced one compound substring OR check with assert.Regexp(...) for direct intent.
  • Typed-nil error assertion handling in rules_test.go

    • The five require.Nil(t, err) sites were updated to require.NoError(...) semantics while preserving behavior for typed-nil *ValidationError returns.
    • Added a tiny adapter helper used only in tests:
      • validationErrAsError(err *ValidationError) error
  • Test import cleanup

    • Removed now-unused strings imports in files where assert.Contains/assert.NotContains replaced manual strings.Contains calls.
// Before
assert.True(t, strings.Contains(text, "Bug report"), "response text should contain the issue title")

// After
assert.Contains(t, text, "Bug report", "response text should contain the issue title")

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • example.com
    • Triggering command: /tmp/go-build850057972/b514/launcher.test /tmp/go-build850057972/b514/launcher.test -test.testlogfile=/tmp/go-build850057972/b514/testlog.txt -test.paniconexit0 -test.timeout=10m0s E=3 .cfg /tmp/go-build403-w x_amd64/vet . --gdwarf2 --64 x_amd64/vet -I .cfg TiyY/dyEj6sNqXXBEEx-BTiyY x_amd64/vet --gdwarf-5 g/grpc/internal/-qE -o x_amd64/vet (dns block)
  • invalid-host-that-does-not-exist-12345.com
    • Triggering command: /tmp/go-build850057972/b496/config.test /tmp/go-build850057972/b496/config.test -test.testlogfile=/tmp/go-build850057972/b496/testlog.txt -test.paniconexit0 -test.timeout=10m0s /tmp/go-build850057972/b394/vet.cfg @v1.1.3/cpu/x86/-errorsas -I x_amd64/vet --gdwarf-5 .io/otel/baggage-atomic -o x_amd64/vet 2483�� g_.a L_XeGB1fo x_amd64/vet -p vendor/golang.or-atomic -lang=go1.25 x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build772277680/b224/config.test /tmp/go-build772277680/b224/config.test -test.testlogfile=/tmp/go-build772277680/b224/testlog.txt -test.paniconexit0 -test.timeout=10m0s rtcf�� by/280457505e8d1REDACTED y ache/go/1.25.8/x64/pkg/tool/linujson ntime.v2.task/mo/usr/libexec/docker/cli-plugins/docker-buildx -ifaceassert -nilfunc ache/go/1.25.8/x64/pkg/tool/linu--no-legend &#34;SSL�� ef5286be851aaba1bad991c168a5fac9-- -tests ker/cli-plugins/docker-compose by/c68777e185eb8node ache/go/1.25.8/xmock-mcp-server.js x_amd64/vet fce/log.json (dns block)
  • nonexistent.local
    • Triggering command: /tmp/go-build850057972/b514/launcher.test /tmp/go-build850057972/b514/launcher.test -test.testlogfile=/tmp/go-build850057972/b514/testlog.txt -test.paniconexit0 -test.timeout=10m0s E=3 .cfg /tmp/go-build403-w x_amd64/vet . --gdwarf2 --64 x_amd64/vet -I .cfg TiyY/dyEj6sNqXXBEEx-BTiyY x_amd64/vet --gdwarf-5 g/grpc/internal/-qE -o x_amd64/vet (dns block)
  • slow.example.com
    • Triggering command: /tmp/go-build850057972/b514/launcher.test /tmp/go-build850057972/b514/launcher.test -test.testlogfile=/tmp/go-build850057972/b514/testlog.txt -test.paniconexit0 -test.timeout=10m0s E=3 .cfg /tmp/go-build403-w x_amd64/vet . --gdwarf2 --64 x_amd64/vet -I .cfg TiyY/dyEj6sNqXXBEEx-BTiyY x_amd64/vet --gdwarf-5 g/grpc/internal/-qE -o x_amd64/vet (dns block)
  • this-host-does-not-exist-12345.com
    • Triggering command: /tmp/go-build850057972/b523/mcp.test /tmp/go-build850057972/b523/mcp.test -test.testlogfile=/tmp/go-build850057972/b523/testlog.txt -test.paniconexit0 -test.timeout=10m0s .cfg�� 2483721/b343/_pkg_.a -fPIC x_amd64/vet us.pb.go g/grpc/balancer -fmessage-length=0 x_amd64/vet .cfg�� /oidc/errors.go /oidc/provider.go x_amd64/vet -dynout g/grpc/health/gr/usr/bin/runc -ffile-prefix-ma--version x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build772277680/b484/mcp.test /tmp/go-build772277680/b484/mcp.test -test.testlogfile=/tmp/go-build772277680/b484/testlog.txt -test.paniconexit0 -test.timeout=10m0s -ato�� -bool y etc/ssl/certs/ca-certificates.crt&#34;, &#34;REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certifi 3cfebab8779a2365/check -ifaceassert -nilfunc noffline 287f�� ef5286be851aaba1bad991c168a5fac9/run/containerd/io.containerd.runtime.v2.task/moby/5878ee59bbb48go 3cfebab8779a2365c0a0927369ddf64be3c02162ae57d53bf27 /usr/bin/grep 9381bd4d1eab940e/usr/bin/runc.original (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI linked an issue Apr 18, 2026 that may be closed by this pull request
6 tasks
Copilot AI changed the title [WIP] Review usage of stretchr/testify in the project Refine testify assertions in tests for clearer failures and lint-aligned patterns Apr 18, 2026
Copilot AI requested a review from lpcox April 18, 2026 01:16
@lpcox lpcox marked this pull request as ready for review April 18, 2026 01:21
Copilot AI review requested due to automatic review settings April 18, 2026 01:21
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates Go test code to use more specific testify/assert and require assertions so failures are clearer and more lint-aligned, without changing production behavior.

Changes:

  • Replaces low-signal patterns like assert.Equal(t, true/false, ...) and strings.Contains(...) boolean checks with assert.True/False, assert.Contains/NotContains, and assert.LessOrEqual.
  • Replaces a compound strings.Contains(...) || ... check with assert.Regexp(...) for more direct intent.
  • Adds a small test-only adapter (validationErrAsError) to allow require.NoError style checks for *ValidationError.
Show a summary per file
File Description
test/integration/large_payload_test.go Uses assert.Contains instead of strings.Contains for clearer failures; drops unused import.
internal/server/call_backend_tool_test.go Uses assert.Contains for error-message substring checks; drops unused import.
internal/proxy/rest_backend_caller_tool_test.go Uses assert.Contains for response text checks; drops unused import.
internal/proxy/proxy_test.go Replaces assert.Equal(false, ...) with assert.False(...) for incomplete_results.
internal/middleware/jqschema_integration_test.go Replaces length-boolean assertions with assert.LessOrEqual for truncation checks.
internal/mcp/unmarshal_params_test.go Uses assert.True for bool field assertion after unmarshal.
internal/mcp/tool_result_test.go Uses assert.True for parsed boolean argument assertion.
internal/difc/labels_test.go Replaces manual strings.Contains checks with assert.Contains/NotContains; drops unused import.
internal/config/rules/rules_test.go Adds validationErrAsError helper and switches some require.Nil sites to require.NoError semantics.
internal/config/fetch_and_fix_schema_test.go Uses assert.True for additionalProperties boolean assertion.
internal/config/expand_raw_json_test.go Uses assert.Contains for error substring checks; drops unused import.
internal/config/config_test.go Replaces a compound substring OR check with assert.Regexp for parse error messaging.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 12/12 changed files
  • Comments generated: 5

Comment thread internal/proxy/proxy_test.go Outdated
m := result.(map[string]interface{})
assert.Equal(t, float64(2), m["total_count"])
assert.Equal(t, false, m["incomplete_results"])
assert.False(t, m["incomplete_results"].(bool))
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These assertions use direct type assertions (e.g. m["incomplete_results"].(bool)), which will panic if the value is missing or not a bool and will hide useful assertion output. Prefer checking the type first (e.g. v, ok := ...; require.True(t, ok) / require.IsType) and then asserting on v, so test failures stay diagnostic rather than crashing.

Suggested change
assert.False(t, m["incomplete_results"].(bool))
incompleteResults, ok := m["incomplete_results"]
require.True(t, ok)
incompleteResultsBool, ok := incompleteResults.(bool)
require.True(t, ok)
assert.False(t, incompleteResultsBool)

Copilot uses AI. Check for mistakes.
Comment thread internal/mcp/unmarshal_params_test.go Outdated
assert.Equal(t, float64(42), target["int_val"])
assert.Equal(t, 3.14, target["float_val"])
assert.Equal(t, true, target["bool_val"])
assert.True(t, target["bool_val"].(bool))
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

target["bool_val"].(bool) can panic if unmarshalParams ever returns an unexpected type (which is exactly what this test is validating). Consider asserting the type first (v, ok := ...; require.True(t, ok)) and then assert.True(t, v) so the test fails with a clear message instead of a panic.

Suggested change
assert.True(t, target["bool_val"].(bool))
boolVal, ok := target["bool_val"].(bool)
require.True(t, ok, "bool_val should be a bool")
assert.True(t, boolVal)

Copilot uses AI. Check for mistakes.
Comment thread internal/mcp/tool_result_test.go Outdated
assert.Equal(t, "search term", args["query"])
assert.Equal(t, float64(10), args["limit"])
assert.Equal(t, true, args["active"])
assert.True(t, args["active"].(bool))
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This uses args["active"].(bool), which will panic (and skip useful assertion output) if the parsed JSON yields an unexpected type. Prefer v, ok := args["active"].(bool); require.True(t, ok) followed by assert.True(t, v) to keep failures diagnostic.

Suggested change
assert.True(t, args["active"].(bool))
active, ok := args["active"].(bool)
require.True(t, ok, "expected active to be a bool")
assert.True(t, active)

Copilot uses AI. Check for mistakes.
gpMap := guardPolicies.(map[string]interface{})
assert.Equal(t, "object", gpMap["type"], "guard-policies.type should be 'object'")
assert.Equal(t, true, gpMap["additionalProperties"], "guard-policies.additionalProperties should be true")
assert.True(t, gpMap["additionalProperties"].(bool), "guard-policies.additionalProperties should be true")
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gpMap["additionalProperties"].(bool) will panic if the field is absent or not a bool (e.g. if schema generation regresses), which makes the test output less actionable. Consider asserting presence/type first (lookup + require.True(t, ok) / require.IsType) and then assert.True on the typed value.

Suggested change
assert.True(t, gpMap["additionalProperties"].(bool), "guard-policies.additionalProperties should be true")
additionalProperties, hasAdditionalProperties := gpMap["additionalProperties"]
require.True(t, hasAdditionalProperties, "guard-policies.additionalProperties should be present")
require.IsType(t, true, additionalProperties, "guard-policies.additionalProperties should be a bool")
assert.True(t, additionalProperties.(bool), "guard-policies.additionalProperties should be true")

Copilot uses AI. Check for mistakes.
Comment thread internal/config/config_test.go Outdated
assert.Contains(t, errMsg, "line", "Error should mention line number")
// Our improved format includes "column" explicitly when ParseError is detected
assert.True(t, strings.Contains(errMsg, "column") || strings.Contains(errMsg, "line 2"),
assert.Regexp(t, `column|line 2`, errMsg,
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regexp column|line 2 is a bit permissive (e.g. it would also match "line 20" because it contains the substring "line 2"). Consider tightening it with word boundaries or a more specific pattern (e.g. \bcolumn\b|\bline\s+2\b) to avoid false positives.

Suggested change
assert.Regexp(t, `column|line 2`, errMsg,
assert.Regexp(t, `\bcolumn\b|\bline\s+2\b`, errMsg,

Copilot uses AI. Check for mistakes.
@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented Apr 18, 2026

@copilot update the PR based on this review feedback #4051 (review)

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 18, 2026

@copilot update the PR based on this review feedback #4051 (review)

Updated the PR based on that review feedback in commit bcfd993.

I addressed the flagged items by adding safe type/presence checks before bool assertions in the affected tests and tightened the regex in internal/config/config_test.go to avoid permissive matching. Targeted tests, make agent-finished, and parallel validation all pass.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • example.com
    • Triggering command: /tmp/go-build2126308760/b514/launcher.test /tmp/go-build2126308760/b514/launcher.test -test.testlogfile=/tmp/go-build2126308760/b514/testlog.txt -test.paniconexit0 -test.timeout=10m0s -W telabs/wazero/internal/engine/wazevo/backend/isa-errorsas /tmp/go-build556362074/b309/ 64/pkg/tool/linux_amd64/vet . 362074/b229/ --64 64/pkg/tool/linux_amd64/vet (dns block)
  • invalid-host-that-does-not-exist-12345.com
    • Triggering command: /tmp/go-build556362074/b480/config.test /tmp/go-build556362074/b480/config.test -test.testlogfile=/tmp/go-build556362074/b480/testlog.txt -test.paniconexit0 -test.timeout=10m0s /tmp/go-build556362074/b377/vet.cfg rnal/version/version.go om/tetratelabs/wazero@v1.11.0/ex--64 x_amd64/compile (dns block)
  • nonexistent.local
    • Triggering command: /tmp/go-build2126308760/b514/launcher.test /tmp/go-build2126308760/b514/launcher.test -test.testlogfile=/tmp/go-build2126308760/b514/testlog.txt -test.paniconexit0 -test.timeout=10m0s -W telabs/wazero/internal/engine/wazevo/backend/isa-errorsas /tmp/go-build556362074/b309/ 64/pkg/tool/linux_amd64/vet . 362074/b229/ --64 64/pkg/tool/linux_amd64/vet (dns block)
  • slow.example.com
    • Triggering command: /tmp/go-build2126308760/b514/launcher.test /tmp/go-build2126308760/b514/launcher.test -test.testlogfile=/tmp/go-build2126308760/b514/testlog.txt -test.paniconexit0 -test.timeout=10m0s -W telabs/wazero/internal/engine/wazevo/backend/isa-errorsas /tmp/go-build556362074/b309/ 64/pkg/tool/linux_amd64/vet . 362074/b229/ --64 64/pkg/tool/linux_amd64/vet (dns block)
  • this-host-does-not-exist-12345.com
    • Triggering command: /tmp/go-build556362074/b450/mcp.test /tmp/go-build556362074/b450/mcp.test -test.testlogfile=/tmp/go-build556362074/b450/testlog.txt -test.paniconexit0 -test.timeout=10m0s /tmp/go-build556362074/b402/vet.cfg lcache/go/1.25.8/x64=/_/GOROOT rg x_amd64/compile 64/src/runtime/c/opt/hostedtoolcache/go/1.25.8/x64/pkg/tool/linux_amd64/vet (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@lpcox lpcox merged commit 52f30ba into main Apr 18, 2026
14 checks passed
@lpcox lpcox deleted the copilot/go-fan-review-stretchr-testify branch April 18, 2026 01:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[go-fan] Go Module Review: stretchr/testify

3 participants