[test-improver] Improve tests for config/validation_env package#1524
Merged
[test-improver] Improve tests for config/validation_env package#1524
Conversation
Replace manual error checking patterns with idiomatic testify assertions Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced Mar 1, 2026
Closed
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves the readability and correctness of tests in internal/config/validation_env_test.go by standardizing on testify/assert + testify/require patterns and removing brittle/manual assertion logic.
Changes:
- Introduces
requirefor fail-fast assertions where continuing a test would be meaningless after an unexpected error state. - Replaces manual comparison and length-check loops with idiomatic testify assertions (
ElementsMatch,Len,Empty,NotEmpty, etc.). - Fixes a real test bug in the “docker not accessible” subtest by avoiding
assert.Containsinside a loop (switching tostrings.Containsfor the boolean check).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Test Improvements:
validation_env_test.goFile Analyzed
internal/config/validation_env_test.gointernal/configImprovements Made
1. Added
requirePackage ImportAdded
github.com/stretchr/testify/requireto enable fail-fast assertions for critical checks (errors and no-errors that would cause panics if execution continued).2. Better Testing Patterns
TestValidateContainerID: Replacedif err == nil { t.Error(...) }withrequire.Error(t, err, ...)andrequire.NoError(t, err, ...)— fail-fast is appropriate here since continuing after an unexpected error state is meaninglessTestGetGatewayPortFromEnv: Same pattern — replaced manual nil-check withrequire.Error/require.NoErrorTestDetectContainerized: Replacedif len < 12 { t.Errorf(...) }withassert.GreaterOrEqual(t, len(...), 12, ...)TestEnvValidationResultIsValid: Replacedif got != want { t.Errorf(...) }withassert.Equal(t, tt.valid, tt.result.IsValid())TestEnvValidationResultError: Same pattern — replaced manual comparison withassert.Equal(t, tt.expected, tt.result.Error())3. Cleaner Assertions
TestGetGatewayDomainFromEnv: Replaced dualifchecks withassert.Equal/assert.EmptyTestGetGatewayAPIKeyFromEnv: Same — replaced dualifchecks withassert.Equal/assert.EmptyTestValidateExecutionEnvironment: Replacedassert.False(t, len(...) > 0, ...)withassert.Empty; replaced manualif len != 3andif len == 0withassert.Lenandassert.NotEmptyTestValidateContainerizedEnvironment(log warnings check): Replacedassert.Greater(t, len(...), 0, ...)withassert.NotEmpty4. Bug Fix: Incorrect
assert.ContainsInside Loop"docker not accessible"subtest:assert.Contains(t, err, "Docker daemon")was being called inside a loop and its return value used to sethasDockerError. This is incorrect —assert.Containsalways fails the test immediately when the string doesn't match, rather than just returningfalse. This meant the test would spuriously fail as soon as any non-matching error message was checked. Fixed by replacing withstrings.Contains(errMsg, "Docker daemon").5. Replaced 20-Line Manual Slice Comparison
TestCheckRequiredEnvVars: Replaced a 20-line manual unordered slice comparison loop (with length check + nested loops) with a singleassert.ElementsMatch(t, tt.expected, missing, ...)callWhy These Changes?
This file was selected because it mixed testify assertions with old-style manual
t.Error/t.Errorfchecks, which is inconsistent and harder to read. The codebase already uses testify throughout, so these inconsistencies stood out. The most important fix is theassert.Containsbug in the loop which would cause tests to fail incorrectly when iterating over error messages.Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests