Skip to content

Comments

[test-improver] Improve tests for sanitize package#1230

Merged
lpcox merged 1 commit intomainfrom
test-improver/sanitize-package-c0579d2ca8e50fba
Feb 21, 2026
Merged

[test-improver] Improve tests for sanitize package#1230
lpcox merged 1 commit intomainfrom
test-improver/sanitize-package-c0579d2ca8e50fba

Conversation

@github-actions
Copy link
Contributor

File Analyzed

  • Test File: internal/logger/sanitize/sanitize_test.go
  • Package: internal/logger/sanitize
  • Lines changed: 41 insertions, 71 deletions (net -30 lines — less code, same coverage)

Improvements Made

1. Better Testify Usage

  • ✅ Added requireContainsRedacted test helper using t.Helper() to deduplicate repeated assertion logic across six test functions
  • ✅ Replaced if !strings.Contains(result, "[REDACTED]") { t.Errorf(...) } patterns with assert.Contains(t, result, "[REDACTED]", ...)
  • ✅ Replaced if strings.Contains(result, secret) { t.Errorf(...) } patterns with assert.NotContains(t, result, secret, ...)
  • ✅ Replaced assert.True(t, strings.Contains(...)) with direct assert.Contains/assert.NotContains
  • ✅ Replaced t.Fatalf("Failed to parse result: %v", err) with require.NoError(t, err, ...) in TestSanitizeJSONWithInvalidJSON
  • ✅ Replaced fmt.Sprintf("%v", payloadObj["_raw"]) + string comparison with a proper string type assertion and assert.Contains

2. Bug Fixes in Test Assertions

  • Fixed assert.False misuse in TestSanitizeStringMultipleSecretsInSameString: assert.False(t, secretCount < 3, "got %d in: %s") had format verbs but no format arguments → changed to assert.GreaterOrEqual(t, secretCount, 3, "...", secretCount, result) which is both semantically correct and provides useful output on failure
  • Fixed broken format string in TestSecretPatternsCount: assert.Equal(t, expectedPatternCount, actualCount, "%d secret patterns, got %d") had format verbs but no format arguments → now passes expectedPatternCount and actualCount as format args

3. Cleaner & More Stable Tests

  • ✅ Consistent testify assertion style throughout the file instead of mixed manual-checking style
  • ✅ Each assertion now has a descriptive message for easier test failure diagnosis
  • ✅ Test failures report actual values rather than just the format string literal

Why These Changes?

The file had a clearly inconsistent style — some test functions used proper testify assertions while others used verbose if !strings.Contains(...) { t.Errorf(...) } patterns. Testify's assert.Contains/assert.NotContains provide equivalent semantics with less boilerplate and better failure output.

Two tests contained real bugs where format strings with %d/%s verbs had no format arguments, meaning test failure messages would print the literal format string "got %d in: %s" instead of showing actual values. The assert.False(t, count < 3, ...) pattern was also semantically misleading — assert.GreaterOrEqual directly expresses the intent and is idiomatic testify.


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

AI generated by Test Improver

- Replace manual strings.Contains + t.Errorf patterns with testify assertions
- Add requireContainsRedacted test helper with t.Helper() to deduplicate logic
- Fix assert.False(secretCount < 3) bug -> assert.GreaterOrEqual
- Fix broken format strings in assert.Equal and assert.False calls
- Use assert.NotContains instead of manual negative strings.Contains checks
- Use require.NoError/require.True instead of t.Fatalf for error checks

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review February 21, 2026 18:35
Copilot AI review requested due to automatic review settings February 21, 2026 18:36
@lpcox lpcox merged commit 7d55617 into main Feb 21, 2026
2 checks passed
@lpcox lpcox deleted the test-improver/sanitize-package-c0579d2ca8e50fba branch February 21, 2026 18:36
Copy link
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 and stabilizes the internal/logger/sanitize test suite by deduplicating repeated redaction assertions and standardizing on Testify assertions for clearer failure output.

Changes:

  • Introduces a shared redaction assertion helper to reduce duplication across tests.
  • Replaces manual strings.Contains checks and mixed assertion patterns with assert.Contains/assert.NotContains and require.NoError.
  • Fixes previously broken failure messages where format verbs were provided without format arguments.

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

Comment on lines +17 to +19
assert.Contains(t, result, "[REDACTED]", "Expected sanitized output to contain [REDACTED]")
if mustNotContain != "" {
assert.NotContains(t, result, mustNotContain, "Sanitized output still contains secret")
Copy link

Copilot AI Feb 21, 2026

Choose a reason for hiding this comment

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

requireContainsRedacted is named like a require.* helper (implying it stops the test on failure), but it uses assert.Contains/assert.NotContains. This is misleading and can allow the test to continue after the helper fails. Either rename it (e.g., assertContainsRedacted) or switch the assertions inside to require.Contains/require.NotContains to match the name/intent.

Suggested change
assert.Contains(t, result, "[REDACTED]", "Expected sanitized output to contain [REDACTED]")
if mustNotContain != "" {
assert.NotContains(t, result, mustNotContain, "Sanitized output still contains secret")
require.Contains(t, result, "[REDACTED]", "Expected sanitized output to contain [REDACTED]")
if mustNotContain != "" {
require.NotContains(t, result, mustNotContain, "Sanitized output still contains secret")

Copilot uses AI. Check for mistakes.
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.

1 participant