Skip to content

[test-improver] Improve tests for config/validation_schema package#3380

Merged
lpcox merged 1 commit intomainfrom
test-improver/validation-schema-testify-2026-04-08-081b18dd35094ded
Apr 11, 2026
Merged

[test-improver] Improve tests for config/validation_schema package#3380
lpcox merged 1 commit intomainfrom
test-improver/validation-schema-testify-2026-04-08-081b18dd35094ded

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Apr 8, 2026

Test Improvements: validation_schema_test.go

File Analyzed

  • Test File: internal/config/validation_schema_test.go
  • Package: internal/config
  • Lines Changed: 755 → 746 (net -9 lines, removed duplication)

Improvements Made

1. Better Testify Usage

  • ✅ Replaced assert.Error + manual t.Errorf with require.Error — stops the subtest immediately if err is nil, preventing a nil-pointer dereference on the subsequent err.Error() call
  • ✅ Replaced if !strings.Contains(err.Error(), tt.errorMsg) { t.Errorf(...) } with assert.Contains(t, err.Error(), tt.errorMsg) — idiomatic testify with automatic descriptive failure messages
  • ✅ Replaced manual if schema1 != schema2 { t.Error(...) } with assert.Same(t, schema1, schema2, ...) — testify's pointer-equality helper

2. Cleaner Code

  • ✅ Removed now-unused "strings" import
  • ✅ Added "github.com/stretchr/testify/require" import
  • ✅ Simplified error-check guard: the old pattern if tt.errorMsg != "" && err != nil && !strings.Contains(...) simplified to if tt.errorMsg != "" { assert.Contains(...) } — the err != nil guard is now handled by the preceding require.Error

3. More Consistent Pattern

Affected test functions:

  • TestValidateJSONSchema (error-message loop)
  • TestValidateStringPatterns (error-message loop)
  • TestEnhancedErrorMessages (multiple expected-strings loop)
  • TestSchemaCaching (pointer equality assertion)

Why These Changes?

The file used testify/assert for most assertions but fell back to manual strings.Contains + t.Errorf for checking error message content — a mixed pattern that's harder to read and produces less informative failure output. Testify's assert.Contains automatically prints both the haystack and needle on failure, making it much easier to diagnose test failures without adding extra format strings.

The switch from assert.Error to require.Error is a correctness improvement: if err is unexpectedly nil, the old code would continue and panic on err.Error(), whereas require.Error stops the subtest immediately with a clear message.


Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests

Generated by Test Improver · ● 8.3M ·

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>
@github-actions github-actions bot mentioned this pull request Apr 9, 2026
@lpcox lpcox marked this pull request as ready for review April 11, 2026 20:05
Copilot AI review requested due to automatic review settings April 11, 2026 20:05
@lpcox lpcox merged commit 9e24485 into main Apr 11, 2026
5 checks passed
@lpcox lpcox deleted the test-improver/validation-schema-testify-2026-04-08-081b18dd35094ded branch April 11, 2026 20:06
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

This PR refactors internal/config/validation_schema_test.go to use more idiomatic testify/require + testify/assert patterns, reducing duplicated manual error-message checks and avoiding potential nil dereferences in failing subtests.

Changes:

  • Replace assert.Error + manual t.Errorf/strings.Contains with require.Error + assert.Contains in error-checking loops.
  • Remove the now-unused strings import and add github.com/stretchr/testify/require.
  • Replace manual pointer equality check in schema caching test with assert.Same.
Show a summary per file
File Description
internal/config/validation_schema_test.go Cleans up and hardens schema/validation tests by using require for error gating and assert helpers for error content and pointer equality.

Copilot's findings

Tip

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

  • Files reviewed: 1/1 changed files
  • Comments generated: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants