Skip to content

Conversation

kevwan
Copy link
Contributor

@kevwan kevwan commented Oct 13, 2025

Unit Tests for PR #4343

This PR adds comprehensive unit tests for the goctl docker command to verify the fixes introduced in PR #4343.

Related PR

Tests Added

Test File: tools/goctl/docker/docker_test.go (376 lines, 25 test cases)

Test Functions:

  1. TestDockerCommand_EtcDirResolution - Validates etc directory resolution logic

    • Tests etc directory resolved relative to Go file (not CWD)
    • Tests edge cases: empty goFile, absolute paths
  2. TestGenerateDockerfile_GoMainFromPath - Validates GoMainFrom path generation

    • Tests various project structures (simple, nested, deeply nested)
    • Ensures no path duplication occurs
  3. TestGenerateDockerfile_PathJoinBehavior - Demonstrates bug and fix

    • Shows old buggy behavior (path duplication)
    • Shows new fixed behavior (using filepath.Base)
  4. TestFindConfig - Tests config file discovery

    • Finds config matching Go file name
    • Returns first config when no match
    • Handles paths in Go file names
    • Returns error when no yaml files exist
  5. TestGetFilePath - Tests file path resolution

    • Returns relative path from go.mod
    • Handles current directory
  6. TestDockerCommandIntegration - Integration tests

    • Tests complete workflow with realistic project structure
    • Verifies etc directory detection from different working directory
    • Verifies GoMainFrom path correctness
  7. TestPR4343_BugFixes - Specific validation for PR fix: ensure Dockerfile includes etc directory and correct CMD based on config  #4343

    • Bug 1: Tests etc directory check uses correct base path
    • Bug 2: Tests GoMainFrom path is not duplicated
    • Tests multiple scenarios for robustness

Test Results

  • ✅ All 25 test cases pass
  • ✅ 27.1% code coverage added (package had 0% before)
  • ✅ No linting errors
  • ✅ Tests run successfully in tools/goctl/docker directory

Bugs Validated

The tests clearly demonstrate and validate the fixes for:

Bug 1: Incorrect etc directory detection

Problem: Checks for "etc" in CWD instead of relative to Go file
Fix: Line 76 in PR #4343 adds: etcDir := filepath.Join(filepath.Dir(goFile), etcDir)

Bug 2: Path duplication in GoMainFrom

Problem: service/api/service/api/api.go (duplicated path)
Fix: Line 174 in PR #4343 uses: filepath.Base(goFile)
Result: service/api/api.go (correct)

How to Test

cd tools/goctl/docker
go test -v
go test -cover

Recommendation

Should be merged together with or after PR #4343 to ensure the fixes are properly validated and prevent future regressions.

Add comprehensive unit tests for goctl docker command to verify fixes for:
- Bug 1: etc directory check using wrong base path
- Bug 2: GoMainFrom path duplication

Test coverage:
- TestDockerCommand_EtcDirResolution: Tests etc dir resolution logic
- TestGenerateDockerfile_GoMainFromPath: Tests path generation
- TestGenerateDockerfile_PathJoinBehavior: Demonstrates bug and fix
- TestFindConfig: Tests config file finding
- TestGetFilePath: Tests file path resolution
- TestDockerCommandIntegration: Integration tests
- TestPR4343_BugFixes: Specific tests for PR #4343 bugs

All 25 test cases pass with 27.1% coverage added.

Related to PR #4343
@kevwan kevwan force-pushed the test/docker-pr4343 branch from 8d31359 to f0d5219 Compare October 13, 2025 13:43
@kevwan kevwan merged commit bf8e17a into master Oct 13, 2025
5 checks passed
@kevwan kevwan deleted the test/docker-pr4343 branch October 13, 2025 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant