Skip to content

Commit 9f8a5ca

Browse files
authored
fix(clix): eliminate data race in test-race CI job (#238)
* fix(clix): eliminate data race on global defaultManager in tests * chore(justfile): use test-race on to install recipe
1 parent 55e9570 commit 9f8a5ca

3 files changed

Lines changed: 13 additions & 8 deletions

File tree

internal/clix/clix.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,20 @@ func FromCommand(cmd *cli.Command) (bool, error) {
2727
// It returns true if the file was created, false if it already existed.
2828
// Returns a typed error (*apperrors.VersionFileNotFoundError) instead of cli.Exit.
2929
func GetOrInitVersionFile(path string, strict bool) (bool, error) {
30+
return getOrInitVersionFileWith(path, strict, semver.DefaultVersionManager())
31+
}
32+
33+
// getOrInitVersionFileWith is the internal implementation that accepts a
34+
// VersionManager, avoiding reliance on package-level mutable state.
35+
func getOrInitVersionFileWith(path string, strict bool, mgr *semver.VersionManager) (bool, error) {
3036
if strict {
3137
if _, err := os.Stat(path); err != nil {
3238
return false, &apperrors.VersionFileNotFoundError{Path: path}
3339
}
3440
return false, nil
3541
}
3642

37-
created, err := semver.InitializeVersionFileWithFeedback(path)
43+
created, err := mgr.InitializeWithFeedback(context.Background(), path)
3844
if err != nil {
3945
return false, err
4046
}

internal/clix/clix_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,19 @@ func TestGetOrInitVersionFile(t *testing.T) {
6464
}
6565

6666
func TestGetOrInitVersionFile_InitError(t *testing.T) {
67-
t.Parallel(
68-
// Use MockFileSystem with write error to simulate initialization failure
69-
)
67+
t.Parallel()
7068

69+
// Use MockFileSystem with write error to simulate initialization failure.
70+
// Call getOrInitVersionFileWith directly with the mock manager to avoid
71+
// mutating the package-level global defaultManager.
7172
mockFS := core.NewMockFileSystem()
7273
mockFS.WriteErr = errors.New("mock init failure")
7374

7475
mgr := semver.NewVersionManager(mockFS, nil)
75-
restore := semver.SetDefaultManager(mgr)
76-
defer restore()
7776

7877
targetPath := "/test/.version"
7978

80-
created, err := GetOrInitVersionFile(targetPath, false)
79+
created, err := getOrInitVersionFileWith(targetPath, false, mgr)
8180
if err == nil {
8281
t.Fatal("expected error, got nil")
8382
}

justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ build:
4343
@ls -lh {{ build_dir }}/{{ app_name }} | awk '{print " " $5}'
4444

4545
# Install the binary using Go install
46-
install: check test-force
46+
install: check test-race
4747
@. {{ logger }} && log_info "Install the binary using Go install"
4848
cd {{ cmd_dir }} && {{ go }} install {{ buildflags }} -ldflags="{{ ldflags }}" .
4949

0 commit comments

Comments
 (0)