|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func executeWithArgs(t *testing.T, args ...string) (string, error) { |
| 11 | + t.Helper() |
| 12 | + |
| 13 | + origOut := rootCmd.OutOrStdout() |
| 14 | + origErr := rootCmd.ErrOrStderr() |
| 15 | + |
| 16 | + buf := new(bytes.Buffer) |
| 17 | + rootCmd.SetOut(buf) |
| 18 | + rootCmd.SetErr(buf) |
| 19 | + rootCmd.SetArgs(args) |
| 20 | + |
| 21 | + err := rootCmd.ExecuteContext(context.Background()) |
| 22 | + out := buf.String() |
| 23 | + |
| 24 | + rootCmd.SetArgs(nil) |
| 25 | + rootCmd.SetOut(origOut) |
| 26 | + rootCmd.SetErr(origErr) |
| 27 | + |
| 28 | + return out, err |
| 29 | +} |
| 30 | + |
| 31 | +func TestRootHelpOutputTemplate(t *testing.T) { |
| 32 | + out, err := executeWithArgs(t, "--help") |
| 33 | + if err != nil { |
| 34 | + t.Fatalf("expected no error, got %v", err) |
| 35 | + } |
| 36 | + |
| 37 | + assertContains(t, out, "Usage:") |
| 38 | + assertContains(t, out, "lstk [options] [command]") |
| 39 | + assertContains(t, out, "LSTK - LocalStack command-line interface") |
| 40 | + assertContains(t, out, "Commands:") |
| 41 | + assertContains(t, out, "Options:") |
| 42 | + assertNotContains(t, out, "Available Commands:") |
| 43 | + assertNotContains(t, out, `Use "lstk [command] --help" for more information about a command.`) |
| 44 | +} |
| 45 | + |
| 46 | +func TestSubcommandHelpUsesSubcommandUsageLine(t *testing.T) { |
| 47 | + out, err := executeWithArgs(t, "start", "--help") |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("expected no error, got %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + assertContains(t, out, "Start emulator and services.") |
| 53 | + assertContains(t, out, "Usage:") |
| 54 | + assertContains(t, out, "lstk start") |
| 55 | + assertContains(t, out, "Options:") |
| 56 | + assertNotContains(t, out, "LSTK - LocalStack command-line interface") |
| 57 | +} |
| 58 | + |
| 59 | +func assertContains(t *testing.T, s, want string) { |
| 60 | + t.Helper() |
| 61 | + if !strings.Contains(s, want) { |
| 62 | + t.Fatalf("expected output to contain %q\noutput:\n%s", want, s) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func assertNotContains(t *testing.T, s, want string) { |
| 67 | + t.Helper() |
| 68 | + if strings.Contains(s, want) { |
| 69 | + t.Fatalf("expected output not to contain %q\noutput:\n%s", want, s) |
| 70 | + } |
| 71 | +} |
0 commit comments