|
4 | 4 | "bytes" |
5 | 5 | "context" |
6 | 6 | "errors" |
| 7 | + "path/filepath" |
7 | 8 | "strings" |
8 | 9 | "testing" |
9 | 10 |
|
@@ -236,6 +237,59 @@ func TestSelfCmd(t *testing.T) { |
236 | 237 | } |
237 | 238 | }) |
238 | 239 |
|
| 240 | + t.Run("should print user config path", func(t *testing.T) { |
| 241 | + home := t.TempDir() |
| 242 | + t.Setenv("HOME", home) |
| 243 | + bufOut := new(bytes.Buffer) |
| 244 | + |
| 245 | + rootCmd := CreateRootCommand("v0.0.0-test", "") |
| 246 | + rootCmd.SetArgs([]string{"self", "config", "path"}) |
| 247 | + rootCmd.SetOut(bufOut) |
| 248 | + rootCmd.SetErr(bufOut) |
| 249 | + initSelfCmd(rootCmd, "v0.0.0-test", func(string) error { return nil }) |
| 250 | + |
| 251 | + if err := rootCmd.Execute(); err != nil { |
| 252 | + t.Fatalf("unexpected error: %v", err) |
| 253 | + } |
| 254 | + |
| 255 | + expected := filepath.Join(home, ".config", "lets", "config.yaml") + "\n" |
| 256 | + if bufOut.String() != expected { |
| 257 | + t.Fatalf("expected %q, got %q", expected, bufOut.String()) |
| 258 | + } |
| 259 | + }) |
| 260 | + |
| 261 | + t.Run("should open user config in editor", func(t *testing.T) { |
| 262 | + home := t.TempDir() |
| 263 | + t.Setenv("HOME", home) |
| 264 | + bufOut := new(bytes.Buffer) |
| 265 | + called := false |
| 266 | + gotPath := "" |
| 267 | + |
| 268 | + rootCmd := CreateRootCommand("v0.0.0-test", "") |
| 269 | + rootCmd.SetArgs([]string{"self", "config", "edit"}) |
| 270 | + rootCmd.SetOut(bufOut) |
| 271 | + rootCmd.SetErr(bufOut) |
| 272 | + initSelfCmdWithEditor(rootCmd, "v0.0.0-test", func(string) error { return nil }, func(path string) error { |
| 273 | + called = true |
| 274 | + gotPath = path |
| 275 | + |
| 276 | + return nil |
| 277 | + }) |
| 278 | + |
| 279 | + if err := rootCmd.Execute(); err != nil { |
| 280 | + t.Fatalf("unexpected error: %v", err) |
| 281 | + } |
| 282 | + |
| 283 | + expected := filepath.Join(home, ".config", "lets", "config.yaml") |
| 284 | + if !called { |
| 285 | + t.Fatal("expected editor to be called") |
| 286 | + } |
| 287 | + |
| 288 | + if gotPath != expected { |
| 289 | + t.Fatalf("expected editor path %q, got %q", expected, gotPath) |
| 290 | + } |
| 291 | + }) |
| 292 | + |
239 | 293 | t.Run("should open documentation in browser", func(t *testing.T) { |
240 | 294 | bufOut := new(bytes.Buffer) |
241 | 295 | called := false |
|
0 commit comments