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