Skip to content

[test-improver] Improve tests for config/validation_env package#1524

Merged
lpcox merged 1 commit intomainfrom
improve-tests-validation-env-006d8eaf1be5afc8
Mar 3, 2026
Merged

[test-improver] Improve tests for config/validation_env package#1524
lpcox merged 1 commit intomainfrom
improve-tests-validation-env-006d8eaf1be5afc8

Conversation

@github-actions
Copy link
Contributor

Test Improvements: validation_env_test.go

File Analyzed

  • Test File: internal/config/validation_env_test.go
  • Package: internal/config
  • Lines of Code: 880 → 848 (32 lines removed via cleanup)

Improvements Made

1. Added require Package Import

Added github.com/stretchr/testify/require to enable fail-fast assertions for critical checks (errors and no-errors that would cause panics if execution continued).

2. Better Testing Patterns

  • TestValidateContainerID: Replaced if err == nil { t.Error(...) } with require.Error(t, err, ...) and require.NoError(t, err, ...) — fail-fast is appropriate here since continuing after an unexpected error state is meaningless
  • TestGetGatewayPortFromEnv: Same pattern — replaced manual nil-check with require.Error / require.NoError
  • TestDetectContainerized: Replaced if len < 12 { t.Errorf(...) } with assert.GreaterOrEqual(t, len(...), 12, ...)
  • TestEnvValidationResultIsValid: Replaced if got != want { t.Errorf(...) } with assert.Equal(t, tt.valid, tt.result.IsValid())
  • TestEnvValidationResultError: Same pattern — replaced manual comparison with assert.Equal(t, tt.expected, tt.result.Error())

3. Cleaner Assertions

  • TestGetGatewayDomainFromEnv: Replaced dual if checks with assert.Equal / assert.Empty
  • TestGetGatewayAPIKeyFromEnv: Same — replaced dual if checks with assert.Equal / assert.Empty
  • TestValidateExecutionEnvironment: Replaced assert.False(t, len(...) > 0, ...) with assert.Empty; replaced manual if len != 3 and if len == 0 with assert.Len and assert.NotEmpty
  • TestValidateContainerizedEnvironment (log warnings check): Replaced assert.Greater(t, len(...), 0, ...) with assert.NotEmpty

4. Bug Fix: Incorrect assert.Contains Inside Loop

  • Fixed a subtle bug in the "docker not accessible" subtest: assert.Contains(t, err, "Docker daemon") was being called inside a loop and its return value used to set hasDockerError. This is incorrect — assert.Contains always fails the test immediately when the string doesn't match, rather than just returning false. This meant the test would spuriously fail as soon as any non-matching error message was checked. Fixed by replacing with strings.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 single assert.ElementsMatch(t, tt.expected, missing, ...) call

Why These Changes?

This file was selected because it mixed testify assertions with old-style manual t.Error/t.Errorf checks, which is inconsistent and harder to read. The codebase already uses testify throughout, so these inconsistencies stood out. The most important fix is the assert.Contains bug 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

Generated by Test Improver

Replace manual error checking patterns with idiomatic testify assertions

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review March 3, 2026 16:35
Copilot AI review requested due to automatic review settings March 3, 2026 16:35
@lpcox lpcox merged commit 96b2d96 into main Mar 3, 2026
2 checks passed
@lpcox lpcox deleted the improve-tests-validation-env-006d8eaf1be5afc8 branch March 3, 2026 16:35
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 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 require for 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.Contains inside a loop (switching to strings.Contains for the boolean check).

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

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