Skip to content

[test-improver] Improve tests for config/rules package#1890

Merged
lpcox merged 1 commit intomainfrom
test-improver/rules-test-improvements-3e02acd05b33e8c7
Mar 14, 2026
Merged

[test-improver] Improve tests for config/rules package#1890
lpcox merged 1 commit intomainfrom
test-improver/rules-test-improvements-3e02acd05b33e8c7

Conversation

@github-actions
Copy link
Contributor

File Analyzed

  • Test File: internal/config/rules/rules_test.go
  • Package: internal/config/rules
  • Lines of Code: ~794 → 816 (+22 net, 35 insertions / 13 deletions)

Improvements Made

1. Better Testify Patterns: require.NotNil for *ValidationError return types

The validation functions (PortRange, TimeoutPositive, MountFormat, NonEmptyString, AbsolutePath) all return *ValidationError, not the standard error interface. Using require.Error(t, err) on a *ValidationError value hits Go's interface-nil gotcha: a nil *ValidationError passed to a function accepting error produces a non-nil interface (type info present, value nil), so require.Error would incorrectly pass even when the function returned nil.

Replaced all 5 occurrences with require.NotNil(t, err, ...) which correctly uses reflection to detect nil pointers.

2. Fail-Fast on Unexpected Success: require.Nil over assert.Nil

Changed assert.Nil(t, err, "Unexpected error")require.Nil(t, err, "Unexpected validation error") at all 5 success-path locations. When a validation function unexpectedly returns an error, require stops the test immediately rather than continuing (which could cause confusing nil-dereference panics or misleading follow-up failures).

3. Increased Coverage: TestValidationError_Error negative assertion

Added notWantSubstr field and assert.NotContains checks to TestValidationError_Error. The "error without suggestion" test case now explicitly verifies that the string "Suggestion:" is absent from the error message, confirming the conditional rendering in ValidationError.Error() works correctly in both directions.

4. Increased Coverage: Edge cases for TestAbsolutePath

Added two missing edge cases for the AbsolutePath validator:

Input Description Expected
"C:" Windows-like path, too short (len=2, needs ≥3) Error: must be absolute
"C" Single drive letter, too short (len=1) Error: must be absolute

These cover the len(value) >= 3 boundary condition in the Windows path detection logic, which was previously untested.

Test Execution

Tests cannot be run in this environment due to sandbox restrictions on binary execution. All changes are backward-compatible: no existing tests were removed or modified in logic, only improved in assertion style, with new test cases added.

Why These Changes?

rules_test.go was selected because it contained the most non-idiomatic testify usage in the codebase: five assert.Nil calls that should be require.Nil (fail-fast), and five require.Error calls that silently pass Go's interface-nil pitfall when used with concrete pointer types. The additional edge cases cover boundary conditions in AbsolutePath that were previously untested.


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

Generated by Test Improver ·

- Replace assert.Nil with require.Nil for fail-fast behavior on
  unexpected validation errors (5 locations)
- Replace require.Error with require.NotNil for *ValidationError
  return types to avoid Go's interface-nil gotcha (5 locations)
- Add notWantSubstr check in TestValidationError_Error to verify
  'Suggestion:' is absent when no suggestion is set
- Add edge case tests for AbsolutePath: 'C:' (2-char path too short
  for Windows check) and single-char path 'C'

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review March 14, 2026 22:24
@lpcox lpcox merged commit f520214 into main Mar 14, 2026
3 checks passed
Copilot AI review requested due to automatic review settings March 14, 2026 22:24
@lpcox lpcox deleted the test-improver/rules-test-improvements-3e02acd05b33e8c7 branch March 14, 2026 22:24
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 improves the robustness and coverage of the internal/config/rules test suite by updating assertions to correctly handle validators that return *ValidationError (a concrete pointer type) and by adding a few missing edge cases.

Changes:

  • Replace require.Error/assert.Nil patterns with require.NotNil/require.Nil to avoid the Go “typed-nil to interface” pitfall and to fail fast on unexpected successes.
  • Extend TestValidationError_Error to assert the absence of the “Suggestion:” section when Suggestion is empty.
  • Add additional boundary test cases for AbsolutePath Windows-drive inputs ("C:" and "C").

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

You can also share your feedback on Copilot code review. Take the survey.

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