-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathv2shim_test.go
More file actions
175 lines (158 loc) · 4.46 KB
/
Copy pathv2shim_test.go
File metadata and controls
175 lines (158 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package migration
import (
"os"
"path/filepath"
"runtime"
"testing"
)
func TestRemoveStaleV2Shim(t *testing.T) {
// Create a temporary directory for testing
tmpDir, err := os.MkdirTemp("", "goenv-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
shimsDir := filepath.Join(tmpDir, "shims")
if err := os.MkdirAll(shimsDir, 0755); err != nil {
t.Fatalf("Failed to create shims dir: %v", err)
}
t.Run("removes v2 shim with forward slash", func(t *testing.T) {
// Create a v2 shim with forward slash
shimPath := filepath.Join(shimsDir, "goenv")
v2Content := `#!/usr/bin/env bash
exec "/opt/homebrew/Cellar/goenv/2.2.38_1/libexec/goenv" "$@"
`
if err := os.WriteFile(shimPath, []byte(v2Content), 0755); err != nil {
t.Fatalf("Failed to create shim: %v", err)
}
removed, err := RemoveStaleV2Shim(shimsDir)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !removed {
t.Error("Expected shim to be removed")
}
if _, err := os.Stat(shimPath); !os.IsNotExist(err) {
t.Error("Shim file should not exist")
}
})
t.Run("removes v2 shim with backslash", func(t *testing.T) {
// Create a v2 shim with backslash (Windows-style)
shimPath := filepath.Join(shimsDir, "goenv")
v2Content := `@echo off
"C:\Program Files\goenv\libexec\goenv" %*
`
if err := os.WriteFile(shimPath, []byte(v2Content), 0755); err != nil {
t.Fatalf("Failed to create shim: %v", err)
}
removed, err := RemoveStaleV2Shim(shimsDir)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !removed {
t.Error("Expected shim to be removed")
}
if _, err := os.Stat(shimPath); !os.IsNotExist(err) {
t.Error("Shim file should not exist")
}
})
t.Run("does not remove non-v2 shim", func(t *testing.T) {
// Create a non-v2 shim
shimPath := filepath.Join(shimsDir, "goenv")
v3Content := `#!/usr/bin/env bash
# This is a v3 shim
exec "goenv" "$@"
`
if err := os.WriteFile(shimPath, []byte(v3Content), 0755); err != nil {
t.Fatalf("Failed to create shim: %v", err)
}
removed, err := RemoveStaleV2Shim(shimsDir)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if removed {
t.Error("Expected shim to NOT be removed")
}
if _, err := os.Stat(shimPath); os.IsNotExist(err) {
t.Error("Shim file should still exist")
}
})
t.Run("handles missing shim file", func(t *testing.T) {
// Ensure no shim exists
shimPath := filepath.Join(shimsDir, "goenv")
os.Remove(shimPath)
removed, err := RemoveStaleV2Shim(shimsDir)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if removed {
t.Error("Expected no removal when file doesn't exist")
}
})
t.Run("returns error when removal fails", func(t *testing.T) {
// Skip on Windows - Unix permission model doesn't apply
if runtime.GOOS == "windows" {
t.Skip("Skipping permission test on Windows - different permission model")
}
shimPath := filepath.Join(shimsDir, "goenv")
v2Content := `#!/usr/bin/env bash
exec "/opt/homebrew/Cellar/goenv/2.2.38_1/libexec/goenv" "$@"
`
if err := os.WriteFile(shimPath, []byte(v2Content), 0755); err != nil {
t.Fatalf("Failed to create shim: %v", err)
}
// Make directory read-only (Unix-like systems only)
if err := os.Chmod(shimsDir, 0555); err != nil {
t.Skipf("Cannot test permission error: %v", err)
}
defer os.Chmod(shimsDir, 0755) // Restore permissions
removed, err := RemoveStaleV2Shim(shimsDir)
if err == nil {
t.Error("Expected error when removal fails")
}
if removed {
t.Error("Should not report as removed when error occurs")
}
})
}
func TestV2ShimPattern(t *testing.T) {
tests := []struct {
name string
content string
matches bool
}{
{
name: "forward slash",
content: `exec "/opt/homebrew/Cellar/goenv/2.2.38_1/libexec/goenv" "$@"`,
matches: true,
},
{
name: "backslash",
content: `"C:\Program Files\goenv\libexec\goenv" %*`,
matches: true,
},
{
name: "no match",
content: `exec "goenv" "$@"`,
matches: false,
},
{
name: "libexec only",
content: `exec "/usr/local/libexec/goenv-other" "$@"`,
matches: false,
},
{
name: "goenv only",
content: `exec "/usr/local/bin/goenv" "$@"`,
matches: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
matches := V2ShimPattern.MatchString(tt.content)
if matches != tt.matches {
t.Errorf("Expected match=%v, got match=%v for content: %s", tt.matches, matches, tt.content)
}
})
}
}