|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "io" |
5 | 6 | "os" |
6 | 7 | "testing" |
@@ -110,6 +111,94 @@ func TestInit_PrefixStoredInStack(t *testing.T) { |
110 | 111 | assert.Equal(t, "feat", sf.Stacks[0].Prefix) |
111 | 112 | } |
112 | 113 |
|
| 114 | +func TestInit_PrefixAppliedToExplicitBranches(t *testing.T) { |
| 115 | + gitDir := t.TempDir() |
| 116 | + var created []string |
| 117 | + restore := git.SetOps(&git.MockOps{ |
| 118 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 119 | + DefaultBranchFn: func() (string, error) { return "main", nil }, |
| 120 | + CurrentBranchFn: func() (string, error) { return "main", nil }, |
| 121 | + CreateBranchFn: func(name, base string) error { |
| 122 | + created = append(created, name) |
| 123 | + return nil |
| 124 | + }, |
| 125 | + }) |
| 126 | + defer restore() |
| 127 | + |
| 128 | + cfg, outR, errR := config.NewTestConfig() |
| 129 | + err := runInit(cfg, &initOptions{branches: []string{"b1", "b2"}, prefix: "feat"}) |
| 130 | + output := collectOutput(cfg, outR, errR) |
| 131 | + |
| 132 | + require.NoError(t, err, "runInit should succeed") |
| 133 | + require.NotContains(t, output, "\u2717", "unexpected error") |
| 134 | + assert.Equal(t, []string{"feat/b1", "feat/b2"}, created, "branches should be created with prefix") |
| 135 | + |
| 136 | + sf, err := stack.Load(gitDir) |
| 137 | + require.NoError(t, err, "loading stack") |
| 138 | + names := sf.Stacks[0].BranchNames() |
| 139 | + assert.Equal(t, []string{"feat/b1", "feat/b2"}, names, "stack should store prefixed branch names") |
| 140 | +} |
| 141 | + |
| 142 | +func TestInit_InvalidPrefixRejectedBeforeBranchCreation(t *testing.T) { |
| 143 | + gitDir := t.TempDir() |
| 144 | + var created []string |
| 145 | + restore := git.SetOps(&git.MockOps{ |
| 146 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 147 | + DefaultBranchFn: func() (string, error) { return "main", nil }, |
| 148 | + CurrentBranchFn: func() (string, error) { return "main", nil }, |
| 149 | + ValidateRefNameFn: func(name string) error { |
| 150 | + return fmt.Errorf("invalid ref name: %s", name) |
| 151 | + }, |
| 152 | + CreateBranchFn: func(name, base string) error { |
| 153 | + created = append(created, name) |
| 154 | + return nil |
| 155 | + }, |
| 156 | + }) |
| 157 | + defer restore() |
| 158 | + |
| 159 | + cfg, outR, errR := config.NewTestConfig() |
| 160 | + err := runInit(cfg, &initOptions{branches: []string{"mybranch"}, prefix: "bad..prefix"}) |
| 161 | + output := collectOutput(cfg, outR, errR) |
| 162 | + |
| 163 | + assert.ErrorIs(t, err, ErrInvalidArgs, "should reject invalid prefix") |
| 164 | + assert.Contains(t, output, "invalid prefix") |
| 165 | + assert.Empty(t, created, "no branches should be created when prefix is invalid") |
| 166 | +} |
| 167 | + |
| 168 | +func TestInit_AdoptRejectsPrefix(t *testing.T) { |
| 169 | + gitDir := t.TempDir() |
| 170 | + restore := git.SetOps(&git.MockOps{ |
| 171 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 172 | + DefaultBranchFn: func() (string, error) { return "main", nil }, |
| 173 | + CurrentBranchFn: func() (string, error) { return "main", nil }, |
| 174 | + }) |
| 175 | + defer restore() |
| 176 | + |
| 177 | + cfg, outR, errR := config.NewTestConfig() |
| 178 | + err := runInit(cfg, &initOptions{adopt: true, branches: []string{"b1"}, prefix: "feat"}) |
| 179 | + output := collectOutput(cfg, outR, errR) |
| 180 | + |
| 181 | + assert.ErrorIs(t, err, ErrInvalidArgs) |
| 182 | + assert.Contains(t, output, "--adopt cannot be combined with --prefix or --numbered") |
| 183 | +} |
| 184 | + |
| 185 | +func TestInit_AdoptRejectsNumbered(t *testing.T) { |
| 186 | + gitDir := t.TempDir() |
| 187 | + restore := git.SetOps(&git.MockOps{ |
| 188 | + GitDirFn: func() (string, error) { return gitDir, nil }, |
| 189 | + DefaultBranchFn: func() (string, error) { return "main", nil }, |
| 190 | + CurrentBranchFn: func() (string, error) { return "main", nil }, |
| 191 | + }) |
| 192 | + defer restore() |
| 193 | + |
| 194 | + cfg, outR, errR := config.NewTestConfig() |
| 195 | + err := runInit(cfg, &initOptions{adopt: true, branches: []string{"b1"}, numbered: true}) |
| 196 | + output := collectOutput(cfg, outR, errR) |
| 197 | + |
| 198 | + assert.ErrorIs(t, err, ErrInvalidArgs) |
| 199 | + assert.Contains(t, output, "--adopt cannot be combined with --prefix or --numbered") |
| 200 | +} |
| 201 | + |
113 | 202 | func TestInit_RerereAlreadyEnabled(t *testing.T) { |
114 | 203 | gitDir := t.TempDir() |
115 | 204 | enableRerereCalled := false |
|
0 commit comments