Skip to content

Commit 6a813d9

Browse files
committed
skip unix tests on windows
1 parent 2f228a6 commit 6a813d9

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

cmd/alias.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ func runAlias(cfg *config.Config, name string, binDir string) error {
102102
return ErrInvalidArgs
103103
}
104104

105+
// Guard against overwriting an existing file that isn't on PATH
106+
if _, err := os.Stat(scriptPath); err == nil {
107+
cfg.Errorf("a file already exists at %s", scriptPath)
108+
cfg.Printf("Choose a different alias name, for example: %s", cfg.ColorCyan("gh stack alias gst"))
109+
return ErrInvalidArgs
110+
}
111+
105112
// Ensure the bin directory exists.
106113
if err := os.MkdirAll(binDir, 0o755); err != nil {
107114
cfg.Errorf("failed to create directory %s: %s", binDir, err)

cmd/alias_test.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"os"
55
"path/filepath"
6+
"runtime"
67
"testing"
78

89
"github.com/github/gh-stack/internal/config"
@@ -34,10 +35,20 @@ func TestAliasCmd_ValidatesName(t *testing.T) {
3435
}
3536
}
3637

37-
// withTmpBinDir overrides localBinDirFunc to use a temp directory and restores
38-
// it when the test completes.
38+
// skipWindows skips the current test on Windows since the alias command
39+
// creates Unix shell scripts.
40+
func skipWindows(t *testing.T) {
41+
t.Helper()
42+
if runtime.GOOS == "windows" {
43+
t.Skip("alias command uses shell scripts; not supported on Windows")
44+
}
45+
}
46+
47+
// withTmpBinDir skips on Windows, overrides localBinDirFunc to use a temp
48+
// directory, and restores it when the test completes.
3949
func withTmpBinDir(t *testing.T) string {
4050
t.Helper()
51+
skipWindows(t)
4152
tmpDir := t.TempDir()
4253
orig := localBinDirFunc
4354
localBinDirFunc = func() (string, error) { return tmpDir, nil }
@@ -133,7 +144,15 @@ func TestIsOurWrapper(t *testing.T) {
133144
}
134145

135146
func TestDirInPath(t *testing.T) {
136-
assert.True(t, dirInPath("/usr/bin") || dirInPath("/bin"), "expected at least /usr/bin or /bin in PATH")
147+
// Use a directory we know is in PATH on any platform.
148+
found := false
149+
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
150+
if dirInPath(dir) {
151+
found = true
152+
break
153+
}
154+
}
155+
assert.True(t, found, "expected at least one PATH entry to be found by dirInPath")
137156
assert.False(t, dirInPath("/nonexistent/path/that/should/not/exist"))
138157
}
139158

@@ -153,6 +172,18 @@ func TestAliasCmd_RemoveFlagWiring(t *testing.T) {
153172
assert.NoFileExists(t, filepath.Join(tmpDir, testAliasName))
154173
}
155174

175+
func TestAliasCmd_WindowsReturnsError(t *testing.T) {
176+
if runtime.GOOS != "windows" {
177+
t.Skip("Windows-only test")
178+
}
179+
180+
cfg, _, _ := config.NewTestConfig()
181+
182+
cmd := AliasCmd(cfg)
183+
cmd.SetArgs([]string{testAliasName})
184+
assert.Error(t, cmd.Execute())
185+
}
186+
156187
func TestValidateAliasName(t *testing.T) {
157188
cfg, _, _ := config.NewTestConfig()
158189

0 commit comments

Comments
 (0)