From e837a53fad1c3cc895e1bc9eb9d76a462bbdee15 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 8 Apr 2026 11:59:33 +0000 Subject: [PATCH] Improve tests for config/validation_schema package Replace manual t.Errorf patterns with idiomatic testify assertions: - Use require.Error instead of assert.Error for critical nil-check before calling err.Error() (prevents nil dereference if assertion is ignored) - Use assert.Contains instead of manual strings.Contains + t.Errorf - Use assert.Same for pointer equality check instead of manual if comparison - Remove now-unused 'strings' import; add 'require' import Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- internal/config/validation_schema_test.go | 29 ++++++++--------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/internal/config/validation_schema_test.go b/internal/config/validation_schema_test.go index b4c3f573..6b7906c5 100644 --- a/internal/config/validation_schema_test.go +++ b/internal/config/validation_schema_test.go @@ -1,11 +1,11 @@ package config import ( - "strings" "testing" "github.com/github/gh-aw-mcpg/internal/version" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestValidateJSONSchema(t *testing.T) { @@ -311,9 +311,9 @@ func TestValidateJSONSchema(t *testing.T) { err := validateJSONSchema([]byte(tt.config)) if tt.shouldErr { - assert.Error(t, err) - if tt.errorMsg != "" && err != nil && !strings.Contains(err.Error(), tt.errorMsg) { - t.Errorf("Expected error containing %q, got: %v", tt.errorMsg, err) + require.Error(t, err) + if tt.errorMsg != "" { + assert.Contains(t, err.Error(), tt.errorMsg) } } else { assert.NoError(t, err, "Unexpected error") @@ -539,9 +539,9 @@ func TestValidateStringPatterns(t *testing.T) { err := validateStringPatterns(tt.config) if tt.shouldErr { - assert.Error(t, err) - if tt.errorMsg != "" && err != nil && !strings.Contains(err.Error(), tt.errorMsg) { - t.Errorf("Expected error containing %q, got: %v", tt.errorMsg, err) + require.Error(t, err) + if tt.errorMsg != "" { + assert.Contains(t, err.Error(), tt.errorMsg) } } else { assert.NoError(t, err, "Unexpected error") @@ -629,16 +629,9 @@ func TestEnhancedErrorMessages(t *testing.T) { t.Run(tt.name, func(t *testing.T) { err := validateJSONSchema([]byte(tt.config)) - if err == nil { - t.Errorf("Expected error but got none") - return - } - - errStr := err.Error() + require.Error(t, err, "Expected validation to fail for: %s", tt.name) for _, expected := range tt.expectInError { - if !strings.Contains(errStr, expected) { - t.Errorf("Expected error to contain %q, but it didn't.\nFull error:\n%s", expected, errStr) - } + assert.Contains(t, err.Error(), expected) } }) } @@ -659,9 +652,7 @@ func TestSchemaCaching(t *testing.T) { // Verify that both calls return the exact same schema instance (pointer equality) // This confirms caching is working correctly - if schema1 != schema2 { - t.Error("Expected both calls to return the same cached schema instance") - } + assert.Same(t, schema1, schema2, "Both calls should return the same cached schema instance") // Verify the cached schema can actually validate configurations validConfig := `{