|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "runtime" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/github/gh-stack/internal/config" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAliasCmd_ValidatesName(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + input string |
| 18 | + wantErr bool |
| 19 | + }{ |
| 20 | + {"default", "gs", false}, |
| 21 | + {"alphanumeric", "gst2", false}, |
| 22 | + {"with-hyphen", "my-stack", false}, |
| 23 | + {"with-underscore", "my_stack", false}, |
| 24 | + {"starts-with-digit", "2gs", true}, |
| 25 | + {"has-spaces", "my stack", true}, |
| 26 | + {"has-slash", "my/stack", true}, |
| 27 | + {"empty", "", true}, |
| 28 | + {"special-chars", "gs!", true}, |
| 29 | + } |
| 30 | + |
| 31 | + for _, tt := range tests { |
| 32 | + t.Run(tt.name, func(t *testing.T) { |
| 33 | + assert.Equal(t, !tt.wantErr, validAliasName.MatchString(tt.input)) |
| 34 | + }) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 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. |
| 49 | +func withTmpBinDir(t *testing.T) string { |
| 50 | + t.Helper() |
| 51 | + skipWindows(t) |
| 52 | + tmpDir := t.TempDir() |
| 53 | + orig := localBinDirFunc |
| 54 | + localBinDirFunc = func() (string, error) { return tmpDir, nil } |
| 55 | + t.Cleanup(func() { localBinDirFunc = orig }) |
| 56 | + return tmpDir |
| 57 | +} |
| 58 | + |
| 59 | +// testAliasName is a name unlikely to collide with real commands on PATH. |
| 60 | +const testAliasName = "ghstacktest" |
| 61 | + |
| 62 | +func TestRunAlias_CreatesWrapperScript(t *testing.T) { |
| 63 | + tmpDir := withTmpBinDir(t) |
| 64 | + cfg, _, _ := config.NewTestConfig() |
| 65 | + |
| 66 | + err := runAlias(cfg, testAliasName, tmpDir) |
| 67 | + require.NoError(t, err) |
| 68 | + |
| 69 | + scriptPath := filepath.Join(tmpDir, testAliasName) |
| 70 | + data, err := os.ReadFile(scriptPath) |
| 71 | + require.NoError(t, err) |
| 72 | + assert.Equal(t, markedWrapperContent, string(data)) |
| 73 | + |
| 74 | + info, err := os.Stat(scriptPath) |
| 75 | + require.NoError(t, err) |
| 76 | + assert.True(t, info.Mode()&0o111 != 0, "script should be executable") |
| 77 | +} |
| 78 | + |
| 79 | +func TestRunAlias_Idempotent(t *testing.T) { |
| 80 | + tmpDir := withTmpBinDir(t) |
| 81 | + cfg, _, _ := config.NewTestConfig() |
| 82 | + |
| 83 | + // First install |
| 84 | + require.NoError(t, runAlias(cfg, testAliasName, tmpDir)) |
| 85 | + // Second install should succeed (idempotent) |
| 86 | + require.NoError(t, runAlias(cfg, testAliasName, tmpDir)) |
| 87 | +} |
| 88 | + |
| 89 | +func TestRunAlias_RejectsExistingCommand(t *testing.T) { |
| 90 | + tmpDir := withTmpBinDir(t) |
| 91 | + cfg, _, _ := config.NewTestConfig() |
| 92 | + |
| 93 | + // "ls" exists on every Unix system |
| 94 | + err := runAlias(cfg, "ls", tmpDir) |
| 95 | + assert.ErrorIs(t, err, ErrInvalidArgs) |
| 96 | +} |
| 97 | + |
| 98 | +func TestRunAliasRemove_RemovesWrapper(t *testing.T) { |
| 99 | + tmpDir := withTmpBinDir(t) |
| 100 | + cfg, _, _ := config.NewTestConfig() |
| 101 | + |
| 102 | + require.NoError(t, runAlias(cfg, testAliasName, tmpDir)) |
| 103 | + |
| 104 | + scriptPath := filepath.Join(tmpDir, testAliasName) |
| 105 | + require.FileExists(t, scriptPath) |
| 106 | + |
| 107 | + require.NoError(t, runAliasRemove(cfg, testAliasName, tmpDir)) |
| 108 | + assert.NoFileExists(t, scriptPath) |
| 109 | +} |
| 110 | + |
| 111 | +func TestRunAliasRemove_RefusesNonOurScript(t *testing.T) { |
| 112 | + tmpDir := withTmpBinDir(t) |
| 113 | + cfg, _, _ := config.NewTestConfig() |
| 114 | + |
| 115 | + // Create a file that isn't our wrapper |
| 116 | + scriptPath := filepath.Join(tmpDir, testAliasName) |
| 117 | + require.NoError(t, os.WriteFile(scriptPath, []byte("#!/bin/sh\necho hello\n"), 0o755)) |
| 118 | + |
| 119 | + err := runAliasRemove(cfg, testAliasName, tmpDir) |
| 120 | + assert.Error(t, err) |
| 121 | + assert.FileExists(t, scriptPath) |
| 122 | +} |
| 123 | + |
| 124 | +func TestRunAliasRemove_ErrorsWhenNotFound(t *testing.T) { |
| 125 | + tmpDir := withTmpBinDir(t) |
| 126 | + cfg, _, _ := config.NewTestConfig() |
| 127 | + |
| 128 | + err := runAliasRemove(cfg, testAliasName, tmpDir) |
| 129 | + assert.Error(t, err) |
| 130 | +} |
| 131 | + |
| 132 | +func TestIsOurWrapper(t *testing.T) { |
| 133 | + tmpDir := t.TempDir() |
| 134 | + |
| 135 | + ourPath := filepath.Join(tmpDir, "ours") |
| 136 | + require.NoError(t, os.WriteFile(ourPath, []byte(markedWrapperContent), 0o755)) |
| 137 | + assert.True(t, isOurWrapper(ourPath)) |
| 138 | + |
| 139 | + otherPath := filepath.Join(tmpDir, "other") |
| 140 | + require.NoError(t, os.WriteFile(otherPath, []byte("#!/bin/sh\necho hi\n"), 0o755)) |
| 141 | + assert.False(t, isOurWrapper(otherPath)) |
| 142 | + |
| 143 | + assert.False(t, isOurWrapper(filepath.Join(tmpDir, "nope"))) |
| 144 | +} |
| 145 | + |
| 146 | +func TestDirInPath(t *testing.T) { |
| 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") |
| 156 | + assert.False(t, dirInPath("/nonexistent/path/that/should/not/exist")) |
| 157 | +} |
| 158 | + |
| 159 | +func TestAliasCmd_RemoveFlagWiring(t *testing.T) { |
| 160 | + tmpDir := withTmpBinDir(t) |
| 161 | + cfg, _, _ := config.NewTestConfig() |
| 162 | + |
| 163 | + // Install the alias first via runAlias so there's something to remove. |
| 164 | + require.NoError(t, runAlias(cfg, testAliasName, tmpDir)) |
| 165 | + require.FileExists(t, filepath.Join(tmpDir, testAliasName)) |
| 166 | + |
| 167 | + // Now exercise the cobra command with --remove to verify flag plumbing. |
| 168 | + cmd := AliasCmd(cfg) |
| 169 | + cmd.SetArgs([]string{"--remove", testAliasName}) |
| 170 | + require.NoError(t, cmd.Execute()) |
| 171 | + |
| 172 | + assert.NoFileExists(t, filepath.Join(tmpDir, testAliasName)) |
| 173 | +} |
| 174 | + |
| 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 | + |
| 187 | +func TestValidateAliasName(t *testing.T) { |
| 188 | + cfg, _, _ := config.NewTestConfig() |
| 189 | + |
| 190 | + assert.NoError(t, validateAliasName(cfg, "gs")) |
| 191 | + assert.NoError(t, validateAliasName(cfg, "my-stack")) |
| 192 | + assert.ErrorIs(t, validateAliasName(cfg, ""), ErrInvalidArgs) |
| 193 | + assert.ErrorIs(t, validateAliasName(cfg, "2bad"), ErrInvalidArgs) |
| 194 | + assert.ErrorIs(t, validateAliasName(cfg, "has space"), ErrInvalidArgs) |
| 195 | +} |
0 commit comments