TestCommandHelp will test the help output of the given commands using golden files.
(t *testing.T, getRoot func(t *testing.T) *serpent.Command, cases []CommandHelpCase)
| 53 | // TestCommandHelp will test the help output of the given commands |
| 54 | // using golden files. |
| 55 | func TestCommandHelp(t *testing.T, getRoot func(t *testing.T) *serpent.Command, cases []CommandHelpCase) { |
| 56 | t.Parallel() |
| 57 | rootClient, replacements := prepareTestData(t) |
| 58 | |
| 59 | root := getRoot(t) |
| 60 | |
| 61 | ExtractCommandPathsLoop: |
| 62 | for _, cp := range extractVisibleCommandPaths(nil, root.Children) { |
| 63 | name := fmt.Sprintf("coder %s --help", strings.Join(cp, " ")) |
| 64 | //nolint:gocritic |
| 65 | cmd := append(cp, "--help") |
| 66 | for _, tt := range cases { |
| 67 | if tt.Name == name { |
| 68 | continue ExtractCommandPathsLoop |
| 69 | } |
| 70 | } |
| 71 | cases = append(cases, CommandHelpCase{Name: name, Cmd: cmd}) |
| 72 | } |
| 73 | |
| 74 | for _, tt := range cases { |
| 75 | t.Run(tt.Name, func(t *testing.T) { |
| 76 | t.Parallel() |
| 77 | ctx := testutil.Context(t, testutil.WaitLong) |
| 78 | |
| 79 | var outBuf bytes.Buffer |
| 80 | |
| 81 | caseCmd := getRoot(t) |
| 82 | |
| 83 | inv, cfg := NewWithCommand(t, caseCmd, tt.Cmd...) |
| 84 | inv.Stderr = &outBuf |
| 85 | inv.Stdout = &outBuf |
| 86 | inv.Environ.Set("CODER_URL", rootClient.URL.String()) |
| 87 | inv.Environ.Set("CODER_SESSION_TOKEN", rootClient.SessionToken()) |
| 88 | inv.Environ.Set("CODER_CACHE_DIRECTORY", "~/.cache") |
| 89 | |
| 90 | SetupConfig(t, rootClient, cfg) |
| 91 | |
| 92 | StartWithWaiter(t, inv.WithContext(ctx)).RequireSuccess() |
| 93 | |
| 94 | TestGoldenFile(t, tt.Name, outBuf.Bytes(), replacements) |
| 95 | }) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Output captures stdout and stderr from an invocation and formats them with |
| 100 | // prefixes for golden file testing, preserving their interleaved order. |
no test coverage detected