Skip to content

Commit b8c29b0

Browse files
committed
Windows test fixes
1 parent 6630362 commit b8c29b0

File tree

8 files changed

+81
-74
lines changed

8 files changed

+81
-74
lines changed

cmd/meta/getstarted_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ func TestGetStartedCommand_FishShell(t *testing.T) {
161161
}
162162

163163
func TestGetStartedCommand_UnknownShell(t *testing.T) {
164+
// On Windows, shell detection always defaults to PowerShell
165+
// This test is for Unix where unknown shells fall back to bash
166+
if utils.IsWindows() {
167+
t.Skip("Skipping unknown shell test on Windows - defaults to PowerShell")
168+
}
169+
164170
tmpDir := t.TempDir()
165171
t.Setenv(utils.GoenvEnvVarRoot.String(), tmpDir)
166172
t.Setenv(utils.GoenvEnvVarShell.String(), "")

cmd/tools/list_tools_test.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package tools
33
import (
44
"bytes"
55
"encoding/json"
6-
"github.com/go-nv/goenv/internal/utils"
76
"path/filepath"
87
"testing"
98

9+
"github.com/go-nv/goenv/internal/utils"
10+
1011
"github.com/go-nv/goenv/internal/cmdtest"
1112
"github.com/go-nv/goenv/internal/config"
1213
"github.com/go-nv/goenv/internal/manager"
@@ -32,13 +33,12 @@ func TestListCommand_AllFlag(t *testing.T) {
3233
// Create version with tool directory
3334
cmdtest.CreateMockGoVersionWithTools(t, tmpDir, version)
3435

35-
// Create individual tool binaries
36+
// Create individual tool binaries using helper (handles .bat on Windows)
3637
cfg := &config.Config{Root: tmpDir}
3738
binPath := cfg.VersionGopathBin(version)
3839

3940
for _, tool := range tools {
40-
toolPath := filepath.Join(binPath, tool)
41-
testutil.WriteTestFile(t, toolPath, []byte("fake"), utils.PermFileExecutable)
41+
cmdtest.CreateToolExecutable(t, binPath, tool)
4242
}
4343
}
4444

@@ -104,8 +104,7 @@ func TestListCommand_JSONOutput(t *testing.T) {
104104

105105
tools := []string{"gopls", "staticcheck"}
106106
for _, tool := range tools {
107-
toolPath := filepath.Join(binPath, tool)
108-
testutil.WriteTestFile(t, toolPath, []byte("fake"), utils.PermFileExecutable)
107+
cmdtest.CreateToolExecutable(t, binPath, tool)
109108
}
110109

111110
// Enable JSON output
@@ -205,8 +204,8 @@ func TestListCommand_HiddenFiles(t *testing.T) {
205204
t.Fatal(err)
206205
}
207206

208-
// Create regular and hidden files
209-
testutil.WriteTestFile(t, filepath.Join(binPath, "gopls"), []byte("fake"), utils.PermFileExecutable)
207+
// Create regular and hidden files (use helper for gopls to handle .bat on Windows)
208+
cmdtest.CreateToolExecutable(t, binPath, "gopls")
210209
testutil.WriteTestFile(t, filepath.Join(binPath, ".hidden"), []byte("fake"), utils.PermFileExecutable)
211210

212211
toolList, err := toolspkg.ListForVersion(cfg, version)
@@ -282,8 +281,7 @@ func TestListCommand_RunE(t *testing.T) {
282281

283282
tools := []string{"gopls", "staticcheck"}
284283
for _, tool := range tools {
285-
toolPath := filepath.Join(binPath, tool)
286-
testutil.WriteTestFile(t, toolPath, []byte("fake"), utils.PermFileExecutable)
284+
cmdtest.CreateToolExecutable(t, binPath, tool)
287285
}
288286

289287
// Create .go-version to set current version
@@ -322,22 +320,16 @@ func TestListCommand_MultipleVersions(t *testing.T) {
322320
// Create multiple versions
323321
versions := []string{"1.21.0", "1.22.0", "1.23.0"}
324322
for _, v := range versions {
325-
// Create Go binary for version (required by ListInstalledVersions)
323+
// Create Go binary for version using helper (handles .bat on Windows)
326324
goBinDir := filepath.Join(tmpDir, "versions", v, "bin")
327-
if err := utils.EnsureDirWithContext(goBinDir, "create test directory"); err != nil {
328-
t.Fatal(err)
329-
}
330-
goBin := filepath.Join(goBinDir, "go")
331-
testutil.WriteTestFile(t, goBin, []byte("#!/bin/sh\necho go version"), utils.PermFileExecutable)
325+
cmdtest.CreateTestBinary(t, tmpDir, v, "go")
332326

333-
// Create tools
327+
// Create tools using helper (handles .bat on Windows)
334328
binPath := filepath.Join(tmpDir, "versions", v, "gopath", "bin")
335329
if err := utils.EnsureDirWithContext(binPath, "create test directory"); err != nil {
336330
t.Fatal(err)
337331
}
338-
339-
// Each version has gopls
340-
testutil.WriteTestFile(t, filepath.Join(binPath, "gopls"), []byte("fake"), utils.PermFileExecutable)
332+
cmdtest.CreateToolExecutable(t, binPath, "gopls")
341333
}
342334

343335
// Get all versions

cmd/tools/status_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,10 @@ func TestStatusCommand_Categorization(t *testing.T) {
166166
// Create version with tool directory
167167
cmdtest.CreateMockGoVersionWithTools(t, tmpDir, version)
168168

169-
// Create individual tool binaries
169+
// Create individual tool binaries using helper (handles .bat on Windows)
170170
binPath := cfg.VersionGopathBin(version)
171171
for _, tool := range tools {
172-
toolPath := filepath.Join(binPath, tool)
173-
testutil.WriteTestFile(t, toolPath, []byte("fake"), utils.PermFileExecutable)
172+
cmdtest.CreateToolExecutable(t, binPath, tool)
174173
}
175174
}
176175

cmd/tools/sync_tools_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,10 @@ func setupSyncTestEnv(t *testing.T, versions []string, tools map[string][]string
4747
t.Fatalf("Failed to create GOPATH/bin: %v", err)
4848
}
4949

50-
// Create tools for this version
50+
// Create tools for this version using helper (handles .bat on Windows)
5151
if versionTools, ok := tools[version]; ok {
5252
for _, tool := range versionTools {
53-
toolPath := filepath.Join(gopathBin, tool)
54-
content := "mock tool"
55-
if utils.IsWindows() {
56-
toolPath += ".bat"
57-
content = "@echo off\necho mock tool\n"
58-
}
59-
testutil.WriteTestFile(t, toolPath, []byte(content), utils.PermFileExecutable)
53+
cmdtest.CreateToolExecutable(t, gopathBin, tool)
6054
}
6155
}
6256
}

cmd/tools/uninstall_tools_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,10 @@ func TestFindCurrentVersionToolTargets(t *testing.T) {
115115
cmdtest.CreateMockGoVersionWithTools(t, tmpDir, version)
116116
binPath := cfg.VersionGopathBin(version)
117117

118-
// Create test tools
118+
// Create test tools using helper (handles .bat on Windows)
119119
tools := []string{"gopls", "staticcheck"}
120120
for _, tool := range tools {
121-
toolPath := filepath.Join(binPath, tool)
122-
testutil.WriteTestFile(t, toolPath, []byte("fake"), utils.PermFileExecutable)
121+
cmdtest.CreateToolExecutable(t, binPath, tool)
123122
}
124123

125124
// Set current version

cmd/tools/update_tools_test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,7 @@ func setupUpdateTestEnv(t *testing.T, version string, tools []testToolInfo, shou
5656
}
5757

5858
for _, tool := range tools {
59-
toolPath := filepath.Join(gopathBin, tool.name)
60-
mockContent := "mock tool binary"
61-
if utils.IsWindows() {
62-
toolPath += ".bat"
63-
mockContent = "@echo off\necho mock tool binary\n"
64-
}
65-
66-
testutil.WriteTestFile(t, toolPath, []byte(mockContent), utils.PermFileExecutable)
59+
cmdtest.CreateToolExecutable(t, gopathBin, tool.name)
6760
}
6861
}
6962

internal/cmdtest/fixtures_test.go

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,30 @@ func TestFixtureWithVersions(t *testing.T) {
3939
defer f.Cleanup()
4040

4141
// Add versions using method chaining
42-
f.WithVersions("1.21.0", "1.22.0")
42+
f.WithVersions("1.21.0", "1.22.0", "1.23.0")
4343

44-
// Verify versions were created
45-
v1Path := filepath.Join(f.Root, "versions", "1.21.0", "bin", "go")
46-
v2Path := filepath.Join(f.Root, "versions", "1.22.0", "bin", "go")
44+
// Verify versions were created using ListInstalledVersions
45+
versions, err := f.Manager.ListInstalledVersions()
46+
if err != nil {
47+
t.Fatalf("Failed to list installed versions: %v", err)
48+
}
4749

48-
if utils.FileNotExists(v1Path) {
49-
t.Errorf("Version 1.21.0 binary not created at %s", v1Path)
50+
expectedVersions := []string{"1.21.0", "1.22.0", "1.23.0"}
51+
if len(versions) != len(expectedVersions) {
52+
t.Errorf("Expected %d versions, got %d", len(expectedVersions), len(versions))
5053
}
51-
if utils.FileNotExists(v2Path) {
52-
t.Errorf("Version 1.22.0 binary not created at %s", v2Path)
54+
55+
for _, expected := range expectedVersions {
56+
found := false
57+
for _, v := range versions {
58+
if v == expected {
59+
found = true
60+
break
61+
}
62+
}
63+
if !found {
64+
t.Errorf("Expected version %s not found in installed versions", expected)
65+
}
5366
}
5467
}
5568

@@ -60,16 +73,16 @@ func TestFixtureWithTools(t *testing.T) {
6073
f.WithVersions("1.21.0").
6174
WithTools("1.21.0", "gopls", "staticcheck")
6275

63-
// Verify tools were created
76+
// Verify tools were created using FindExecutable (handles .bat/.exe on Windows)
6477
toolDir := filepath.Join(f.Root, "versions", "1.21.0", "gopath", "bin")
65-
goplsPath := filepath.Join(toolDir, "gopls")
66-
staticcheckPath := filepath.Join(toolDir, "staticcheck")
6778

68-
if utils.FileNotExists(goplsPath) {
69-
t.Errorf("gopls not created at %s", goplsPath)
79+
goplsPath := filepath.Join(toolDir, "gopls")
80+
if _, err := utils.FindExecutable(toolDir, "gopls"); err != nil {
81+
t.Errorf("gopls not found in %s: %v", toolDir, err)
7082
}
71-
if utils.FileNotExists(staticcheckPath) {
72-
t.Errorf("staticcheck not created at %s", staticcheckPath)
83+
84+
if _, err := utils.FindExecutable(toolDir, "staticcheck"); err != nil {
85+
t.Errorf("staticcheck not found in %s: %v", toolDir, err)
7386
}
7487
}
7588

@@ -184,12 +197,13 @@ func TestToolScenario(t *testing.T) {
184197
// Verify version and tools
185198
f.AssertVersionInstalled("1.21.0")
186199

200+
// Verify tools were created using FindExecutable (handles .bat/.exe on Windows)
187201
toolDir := filepath.Join(f.Root, "versions", "1.21.0", "gopath", "bin")
188-
if utils.FileNotExists(filepath.Join(toolDir, "gopls")) {
189-
t.Error("gopls not created in ToolScenario")
202+
if _, err := utils.FindExecutable(toolDir, "gopls"); err != nil {
203+
t.Errorf("gopls not created in ToolScenario: %v", err)
190204
}
191-
if utils.FileNotExists(filepath.Join(toolDir, "staticcheck")) {
192-
t.Error("staticcheck not created in ToolScenario")
205+
if _, err := utils.FindExecutable(toolDir, "staticcheck"); err != nil {
206+
t.Errorf("staticcheck not created in ToolScenario: %v", err)
193207
}
194208
}
195209

@@ -247,19 +261,19 @@ func TestVersionBuilder(t *testing.T) {
247261
WithGoMod().
248262
Build()
249263

250-
// Verify binaries
264+
// Verify binaries using FindExecutable (handles .bat/.exe on Windows)
251265
binDir := filepath.Join(f.Root, "versions", "1.21.0", "bin")
252-
if utils.FileNotExists(filepath.Join(binDir, "go")) {
253-
t.Error("go binary not created")
266+
if _, err := utils.FindExecutable(binDir, "go"); err != nil {
267+
t.Errorf("go binary not created: %v", err)
254268
}
255-
if utils.FileNotExists(filepath.Join(binDir, "gofmt")) {
256-
t.Error("gofmt binary not created")
269+
if _, err := utils.FindExecutable(binDir, "gofmt"); err != nil {
270+
t.Errorf("gofmt binary not created: %v", err)
257271
}
258272

259-
// Verify tools
273+
// Verify tools using FindExecutable (handles .bat/.exe on Windows)
260274
toolDir := filepath.Join(f.Root, "versions", "1.21.0", "gopath", "bin")
261-
if utils.FileNotExists(filepath.Join(toolDir, "gopls")) {
262-
t.Error("gopls tool not created")
275+
if _, err := utils.FindExecutable(toolDir, "gopls"); err != nil {
276+
t.Errorf("gopls tool not created: %v", err)
263277
}
264278

265279
// Verify pkg directory
@@ -291,14 +305,14 @@ func TestScenarioBuilder(t *testing.T) {
291305
// Local should override global
292306
f.AssertCurrentVersion("1.22.0")
293307

294-
// Verify tools
308+
// Verify tools using FindExecutable (handles .bat/.exe on Windows)
295309
toolDir1 := filepath.Join(f.Root, "versions", "1.21.0", "gopath", "bin")
296-
if utils.FileNotExists(filepath.Join(toolDir1, "gopls")) {
297-
t.Error("gopls not created for 1.21.0")
310+
if _, err := utils.FindExecutable(toolDir1, "gopls"); err != nil {
311+
t.Errorf("gopls not created for 1.21.0: %v", err)
298312
}
299313

300314
toolDir2 := filepath.Join(f.Root, "versions", "1.22.0", "gopath", "bin")
301-
if utils.FileNotExists(filepath.Join(toolDir2, "staticcheck")) {
302-
t.Error("staticcheck not created for 1.22.0")
315+
if _, err := utils.FindExecutable(toolDir2, "staticcheck"); err != nil {
316+
t.Errorf("staticcheck not created for 1.22.0: %v", err)
303317
}
304318
}

internal/shell/profile/profile_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ func TestHasGoenvInit(t *testing.T) {
5858
}
5959

6060
func TestGetProfile(t *testing.T) {
61+
// This test is Unix-specific (tests .bashrc)
62+
if utils.IsWindows() {
63+
t.Skip("Skipping Unix shell profile test on Windows")
64+
}
65+
6166
// Create temporary test directory
6267
tmpDir := t.TempDir()
6368
home := filepath.Join(tmpDir, "home")
@@ -147,6 +152,11 @@ func TestDetectPathResets(t *testing.T) {
147152
}
148153

149154
func TestGetAllProfiles(t *testing.T) {
155+
// This test is Unix-specific (tests .zshrc, .zprofile)
156+
if utils.IsWindows() {
157+
t.Skip("Skipping Unix shell profile test on Windows")
158+
}
159+
150160
// Create temporary test directory
151161
tmpDir := t.TempDir()
152162
home := filepath.Join(tmpDir, "home")

0 commit comments

Comments
 (0)