From 6a813d91efafbd9c8b2e5571df26cdbf214624dd Mon Sep 17 00:00:00 2001 From: Sameen Karim Date: Fri, 27 Mar 2026 03:58:34 -0400 Subject: [PATCH] skip unix tests on windows --- cmd/alias.go | 7 +++++++ cmd/alias_test.go | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/cmd/alias.go b/cmd/alias.go index 1b5db5f..a3ecf8f 100644 --- a/cmd/alias.go +++ b/cmd/alias.go @@ -102,6 +102,13 @@ func runAlias(cfg *config.Config, name string, binDir string) error { return ErrInvalidArgs } + // Guard against overwriting an existing file that isn't on PATH + if _, err := os.Stat(scriptPath); err == nil { + cfg.Errorf("a file already exists at %s", scriptPath) + cfg.Printf("Choose a different alias name, for example: %s", cfg.ColorCyan("gh stack alias gst")) + return ErrInvalidArgs + } + // Ensure the bin directory exists. if err := os.MkdirAll(binDir, 0o755); err != nil { cfg.Errorf("failed to create directory %s: %s", binDir, err) diff --git a/cmd/alias_test.go b/cmd/alias_test.go index d1ff762..f116504 100644 --- a/cmd/alias_test.go +++ b/cmd/alias_test.go @@ -3,6 +3,7 @@ package cmd import ( "os" "path/filepath" + "runtime" "testing" "github.com/github/gh-stack/internal/config" @@ -34,10 +35,20 @@ func TestAliasCmd_ValidatesName(t *testing.T) { } } -// withTmpBinDir overrides localBinDirFunc to use a temp directory and restores -// it when the test completes. +// 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 } @@ -133,7 +144,15 @@ func TestIsOurWrapper(t *testing.T) { } func TestDirInPath(t *testing.T) { - assert.True(t, dirInPath("/usr/bin") || dirInPath("/bin"), "expected at least /usr/bin or /bin in PATH") + // 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")) } @@ -153,6 +172,18 @@ func TestAliasCmd_RemoveFlagWiring(t *testing.T) { 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()