Skip to content

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

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
improve-tests-validation-env-006d8eaf1be5afc8
Draft

[test-improver] Improve tests for config/validation_env package#1524
github-actions[bot] wants to merge 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>
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.

0 participants