-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathalias_test.go
More file actions
195 lines (157 loc) · 5.45 KB
/
Copy pathalias_test.go
File metadata and controls
195 lines (157 loc) · 5.45 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package cmd
import (
"os"
"path/filepath"
"runtime"
"testing"
"github.com/github/gh-stack/internal/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAliasCmd_ValidatesName(t *testing.T) {
tests := []struct {
name string
input string
wantErr bool
}{
{"default", "gs", false},
{"alphanumeric", "gst2", false},
{"with-hyphen", "my-stack", false},
{"with-underscore", "my_stack", false},
{"starts-with-digit", "2gs", true},
{"has-spaces", "my stack", true},
{"has-slash", "my/stack", true},
{"empty", "", true},
{"special-chars", "gs!", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, !tt.wantErr, validAliasName.MatchString(tt.input))
})
}
}
// skipWindows skips the current test on Windows since the alias command
// creates Unix shell scripts.
func skipWindows(t *testing.T) {
t.Helper()
if runtime.GOOS == "windows" {
t.Skip("alias command uses shell scripts; not supported on Windows")
}
}
// withTmpBinDir skips on Windows, overrides localBinDirFunc to use a temp
// directory, and restores it when the test completes.
func withTmpBinDir(t *testing.T) string {
t.Helper()
skipWindows(t)
tmpDir := t.TempDir()
orig := localBinDirFunc
localBinDirFunc = func() (string, error) { return tmpDir, nil }
t.Cleanup(func() { localBinDirFunc = orig })
return tmpDir
}
// testAliasName is a name unlikely to collide with real commands on PATH.
const testAliasName = "ghstacktest"
func TestRunAlias_CreatesWrapperScript(t *testing.T) {
tmpDir := withTmpBinDir(t)
cfg, _, _ := config.NewTestConfig()
err := runAlias(cfg, testAliasName, tmpDir)
require.NoError(t, err)
scriptPath := filepath.Join(tmpDir, testAliasName)
data, err := os.ReadFile(scriptPath)
require.NoError(t, err)
assert.Equal(t, markedWrapperContent, string(data))
info, err := os.Stat(scriptPath)
require.NoError(t, err)
assert.True(t, info.Mode()&0o111 != 0, "script should be executable")
}
func TestRunAlias_Idempotent(t *testing.T) {
tmpDir := withTmpBinDir(t)
cfg, _, _ := config.NewTestConfig()
// First install
require.NoError(t, runAlias(cfg, testAliasName, tmpDir))
// Second install should succeed (idempotent)
require.NoError(t, runAlias(cfg, testAliasName, tmpDir))
}
func TestRunAlias_RejectsExistingCommand(t *testing.T) {
tmpDir := withTmpBinDir(t)
cfg, _, _ := config.NewTestConfig()
// "ls" exists on every Unix system
err := runAlias(cfg, "ls", tmpDir)
assert.ErrorIs(t, err, ErrInvalidArgs)
}
func TestRunAliasRemove_RemovesWrapper(t *testing.T) {
tmpDir := withTmpBinDir(t)
cfg, _, _ := config.NewTestConfig()
require.NoError(t, runAlias(cfg, testAliasName, tmpDir))
scriptPath := filepath.Join(tmpDir, testAliasName)
require.FileExists(t, scriptPath)
require.NoError(t, runAliasRemove(cfg, testAliasName, tmpDir))
assert.NoFileExists(t, scriptPath)
}
func TestRunAliasRemove_RefusesNonOurScript(t *testing.T) {
tmpDir := withTmpBinDir(t)
cfg, _, _ := config.NewTestConfig()
// Create a file that isn't our wrapper
scriptPath := filepath.Join(tmpDir, testAliasName)
require.NoError(t, os.WriteFile(scriptPath, []byte("#!/bin/sh\necho hello\n"), 0o755))
err := runAliasRemove(cfg, testAliasName, tmpDir)
assert.Error(t, err)
assert.FileExists(t, scriptPath)
}
func TestRunAliasRemove_ErrorsWhenNotFound(t *testing.T) {
tmpDir := withTmpBinDir(t)
cfg, _, _ := config.NewTestConfig()
err := runAliasRemove(cfg, testAliasName, tmpDir)
assert.Error(t, err)
}
func TestIsOurWrapper(t *testing.T) {
tmpDir := t.TempDir()
ourPath := filepath.Join(tmpDir, "ours")
require.NoError(t, os.WriteFile(ourPath, []byte(markedWrapperContent), 0o755))
assert.True(t, isOurWrapper(ourPath))
otherPath := filepath.Join(tmpDir, "other")
require.NoError(t, os.WriteFile(otherPath, []byte("#!/bin/sh\necho hi\n"), 0o755))
assert.False(t, isOurWrapper(otherPath))
assert.False(t, isOurWrapper(filepath.Join(tmpDir, "nope")))
}
func TestDirInPath(t *testing.T) {
// Use a directory we know is in PATH on any platform.
found := false
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
if dirInPath(dir) {
found = true
break
}
}
assert.True(t, found, "expected at least one PATH entry to be found by dirInPath")
assert.False(t, dirInPath("/nonexistent/path/that/should/not/exist"))
}
func TestAliasCmd_RemoveFlagWiring(t *testing.T) {
tmpDir := withTmpBinDir(t)
cfg, _, _ := config.NewTestConfig()
// Install the alias first via runAlias so there's something to remove.
require.NoError(t, runAlias(cfg, testAliasName, tmpDir))
require.FileExists(t, filepath.Join(tmpDir, testAliasName))
// Now exercise the cobra command with --remove to verify flag plumbing.
cmd := AliasCmd(cfg)
cmd.SetArgs([]string{"--remove", testAliasName})
require.NoError(t, cmd.Execute())
assert.NoFileExists(t, filepath.Join(tmpDir, testAliasName))
}
func TestAliasCmd_WindowsReturnsError(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Windows-only test")
}
cfg, _, _ := config.NewTestConfig()
cmd := AliasCmd(cfg)
cmd.SetArgs([]string{testAliasName})
assert.Error(t, cmd.Execute())
}
func TestValidateAliasName(t *testing.T) {
cfg, _, _ := config.NewTestConfig()
assert.NoError(t, validateAliasName(cfg, "gs"))
assert.NoError(t, validateAliasName(cfg, "my-stack"))
assert.ErrorIs(t, validateAliasName(cfg, ""), ErrInvalidArgs)
assert.ErrorIs(t, validateAliasName(cfg, "2bad"), ErrInvalidArgs)
assert.ErrorIs(t, validateAliasName(cfg, "has space"), ErrInvalidArgs)
}