Skip to content

Commit c5cfefe

Browse files
authored
test: Generate golden files for all (visible) CLI commands (#5479)
1 parent c7ce3e7 commit c5cfefe

File tree

53 files changed

+1754
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1754
-6
lines changed

cli/root_test.go

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package cli_test
33
import (
44
"bytes"
55
"flag"
6+
"fmt"
67
"net/http"
78
"net/http/httptest"
89
"os"
910
"path/filepath"
11+
"runtime"
1012
"strings"
1113
"testing"
1214

@@ -30,28 +32,48 @@ var updateGoldenFiles = flag.Bool("update", false, "update .golden files")
3032
func TestCommandHelp(t *testing.T) {
3133
t.Parallel()
3234

33-
tests := []struct {
35+
commonEnv := map[string]string{
36+
"CODER_CONFIG_DIR": "/tmp/coder-cli-test-config",
37+
}
38+
39+
type testCase struct {
3440
name string
3541
cmd []string
3642
env map[string]string
37-
}{
43+
}
44+
tests := []testCase{
3845
{
3946
name: "coder --help",
4047
cmd: []string{"--help"},
41-
env: map[string]string{
42-
"CODER_CONFIG_DIR": "/tmp/coder-cli-test-config",
43-
},
4448
},
4549
{
4650
name: "coder server --help",
4751
cmd: []string{"server", "--help"},
4852
env: map[string]string{
49-
"CODER_CONFIG_DIR": "/tmp/coder-cli-test-config",
5053
"CODER_CACHE_DIRECTORY": "/tmp/coder-cli-test-cache",
5154
},
5255
},
5356
}
5457

58+
root := cli.Root(cli.AGPL())
59+
ExtractCommandPathsLoop:
60+
for _, cp := range extractVisibleCommandPaths(nil, root.Commands()) {
61+
name := fmt.Sprintf("coder %s --help", strings.Join(cp, " "))
62+
cmd := append(cp, "--help")
63+
for _, tt := range tests {
64+
if tt.name == name {
65+
continue ExtractCommandPathsLoop
66+
}
67+
}
68+
tests = append(tests, testCase{name: name, cmd: cmd})
69+
}
70+
71+
wd, err := os.Getwd()
72+
require.NoError(t, err)
73+
if runtime.GOOS == "windows" {
74+
wd = strings.ReplaceAll(wd, "\\", "\\\\")
75+
}
76+
5577
for _, tt := range tests {
5678
tt := tt
5779
t.Run(tt.name, func(t *testing.T) {
@@ -63,6 +85,14 @@ func TestCommandHelp(t *testing.T) {
6385
}
6486
}
6587
// Override environment variables for a reproducible test.
88+
for k, v := range commonEnv {
89+
if tt.env != nil {
90+
if _, ok := tt.env[k]; ok {
91+
continue
92+
}
93+
}
94+
t.Setenv(k, v)
95+
}
6696
for k, v := range tt.env {
6797
t.Setenv(k, v)
6898
}
@@ -79,6 +109,10 @@ func TestCommandHelp(t *testing.T) {
79109
// Remove CRLF newlines (Windows).
80110
got = bytes.ReplaceAll(got, []byte{'\r', '\n'}, []byte{'\n'})
81111

112+
// The `coder templates create --help` command prints the path
113+
// to the working directory (--directory flag default value).
114+
got = bytes.ReplaceAll(got, []byte(wd), []byte("/tmp/coder-cli-test-workdir"))
115+
82116
gf := filepath.Join("testdata", strings.Replace(tt.name, " ", "_", -1)+".golden")
83117
if *updateGoldenFiles {
84118
t.Logf("update golden file for: %q: %s", tt.name, gf)
@@ -95,6 +129,19 @@ func TestCommandHelp(t *testing.T) {
95129
}
96130
}
97131

132+
func extractVisibleCommandPaths(cmdPath []string, cmds []*cobra.Command) [][]string {
133+
var cmdPaths [][]string
134+
for _, c := range cmds {
135+
if c.Hidden {
136+
continue
137+
}
138+
cmdPath := append(cmdPath, c.Name())
139+
cmdPaths = append(cmdPaths, cmdPath)
140+
cmdPaths = append(cmdPaths, extractVisibleCommandPaths(cmdPath, c.Commands())...)
141+
}
142+
return cmdPaths
143+
}
144+
98145
func TestRoot(t *testing.T) {
99146
t.Parallel()
100147
t.Run("FormatCobraError", func(t *testing.T) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Add an SSH Host entry for your workspaces "ssh coder.workspace"
2+
3+
Usage:
4+
coder config-ssh [flags]
5+
6+
Get Started:
7+
- You can use -o (or --ssh-option) so set SSH options to be used for all your
8+
workspaces:
9+
10+
$ coder config-ssh -o ForwardAgent=yes
11+
12+
- You can use --dry-run (or -n) to see the changes that would be made:
13+
14+
$ coder config-ssh --dry-run
15+
16+
Flags:
17+
-n, --dry-run Perform a trial run with no changes made, showing a diff at
18+
the end.
19+
-h, --help help for config-ssh
20+
--ssh-config-file string Specifies the path to an SSH config.
21+
Consumes $CODER_SSH_CONFIG_FILE (default "~/.ssh/config")
22+
-o, --ssh-option stringArray Specifies additional SSH options to embed in each host stanza.
23+
--use-previous-options Specifies whether or not to keep options from previous run of
24+
config-ssh.
25+
Consumes $CODER_SSH_USE_PREVIOUS_OPTIONS
26+
-y, --yes Bypass prompts
27+
28+
Global Flags:
29+
--global-config coder Path to the global coder config directory.
30+
Consumes $CODER_CONFIG_DIR (default "/tmp/coder-cli-test-config")
31+
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
32+
Consumes $CODER_HEADER
33+
--no-feature-warning Suppress warnings about unlicensed features.
34+
Consumes $CODER_NO_FEATURE_WARNING
35+
--no-version-warning Suppress warning when client and server versions do not match.
36+
Consumes $CODER_NO_VERSION_WARNING
37+
--token string Specify an authentication token. For security reasons setting
38+
CODER_SESSION_TOKEN is preferred.
39+
Consumes $CODER_SESSION_TOKEN
40+
--url string URL to a deployment.
41+
Consumes $CODER_URL
42+
-v, --verbose Enable verbose output.
43+
Consumes $CODER_VERBOSE
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Create a workspace
2+
3+
Usage:
4+
coder create [name] [flags]
5+
6+
Flags:
7+
-h, --help help for create
8+
--parameter-file string Specify a file path with parameter values.
9+
Consumes $CODER_PARAMETER_FILE
10+
--start-at coder schedule start --help Specify the workspace autostart schedule. Check
11+
coder schedule start --help for the syntax.
12+
Consumes $CODER_WORKSPACE_START_AT
13+
--stop-after duration Specify a duration after which the workspace
14+
should shut down (e.g. 8h).
15+
Consumes $CODER_WORKSPACE_STOP_AFTER (default
16+
8h0m0s)
17+
-t, --template string Specify a template name.
18+
Consumes $CODER_TEMPLATE_NAME
19+
-y, --yes Bypass prompts
20+
21+
Global Flags:
22+
--global-config coder Path to the global coder config directory.
23+
Consumes $CODER_CONFIG_DIR (default "/tmp/coder-cli-test-config")
24+
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
25+
Consumes $CODER_HEADER
26+
--no-feature-warning Suppress warnings about unlicensed features.
27+
Consumes $CODER_NO_FEATURE_WARNING
28+
--no-version-warning Suppress warning when client and server versions do not match.
29+
Consumes $CODER_NO_VERSION_WARNING
30+
--token string Specify an authentication token. For security reasons setting
31+
CODER_SESSION_TOKEN is preferred.
32+
Consumes $CODER_SESSION_TOKEN
33+
--url string URL to a deployment.
34+
Consumes $CODER_URL
35+
-v, --verbose Enable verbose output.
36+
Consumes $CODER_VERBOSE
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Delete a workspace
2+
3+
Usage:
4+
coder delete <workspace> [flags]
5+
6+
Aliases:
7+
delete, rm
8+
9+
Flags:
10+
-h, --help help for delete
11+
--orphan Delete a workspace without deleting its resources. This can delete a
12+
workspace in a broken state, but may also lead to unaccounted cloud resources.
13+
-y, --yes Bypass prompts
14+
15+
Global Flags:
16+
--global-config coder Path to the global coder config directory.
17+
Consumes $CODER_CONFIG_DIR (default "/tmp/coder-cli-test-config")
18+
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
19+
Consumes $CODER_HEADER
20+
--no-feature-warning Suppress warnings about unlicensed features.
21+
Consumes $CODER_NO_FEATURE_WARNING
22+
--no-version-warning Suppress warning when client and server versions do not match.
23+
Consumes $CODER_NO_VERSION_WARNING
24+
--token string Specify an authentication token. For security reasons setting
25+
CODER_SESSION_TOKEN is preferred.
26+
Consumes $CODER_SESSION_TOKEN
27+
--url string URL to a deployment.
28+
Consumes $CODER_URL
29+
-v, --verbose Enable verbose output.
30+
Consumes $CODER_VERBOSE
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Checkout and install a dotfiles repository from a Git URL
2+
3+
Usage:
4+
coder dotfiles [git_repo_url] [flags]
5+
6+
Get Started:
7+
- Check out and install a dotfiles repository without prompts:
8+
9+
$ coder dotfiles --yes git@github.com:example/dotfiles.git
10+
11+
Flags:
12+
-h, --help help for dotfiles
13+
--symlink-dir string Specifies the directory for the dotfiles symlink destinations. If
14+
empty will use $HOME.
15+
Consumes $CODER_SYMLINK_DIR
16+
-y, --yes Bypass prompts
17+
18+
Global Flags:
19+
--global-config coder Path to the global coder config directory.
20+
Consumes $CODER_CONFIG_DIR (default "/tmp/coder-cli-test-config")
21+
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
22+
Consumes $CODER_HEADER
23+
--no-feature-warning Suppress warnings about unlicensed features.
24+
Consumes $CODER_NO_FEATURE_WARNING
25+
--no-version-warning Suppress warning when client and server versions do not match.
26+
Consumes $CODER_NO_VERSION_WARNING
27+
--token string Specify an authentication token. For security reasons setting
28+
CODER_SESSION_TOKEN is preferred.
29+
Consumes $CODER_SESSION_TOKEN
30+
--url string URL to a deployment.
31+
Consumes $CODER_URL
32+
-v, --verbose Enable verbose output.
33+
Consumes $CODER_VERBOSE

cli/testdata/coder_list_--help.golden

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
List workspaces
2+
3+
Usage:
4+
coder list [flags]
5+
6+
Aliases:
7+
list, ls
8+
9+
Flags:
10+
-a, --all Specifies whether all workspaces will be listed or not.
11+
-c, --column stringArray Specify a column to filter in the table. Available columns are:
12+
workspace, template, status, last_built, outdated, starts_at,
13+
stops_after
14+
-h, --help help for list
15+
--search string Search for a workspace with a query. (default "owner:me")
16+
17+
Global Flags:
18+
--global-config coder Path to the global coder config directory.
19+
Consumes $CODER_CONFIG_DIR (default "/tmp/coder-cli-test-config")
20+
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
21+
Consumes $CODER_HEADER
22+
--no-feature-warning Suppress warnings about unlicensed features.
23+
Consumes $CODER_NO_FEATURE_WARNING
24+
--no-version-warning Suppress warning when client and server versions do not match.
25+
Consumes $CODER_NO_VERSION_WARNING
26+
--token string Specify an authentication token. For security reasons setting
27+
CODER_SESSION_TOKEN is preferred.
28+
Consumes $CODER_SESSION_TOKEN
29+
--url string URL to a deployment.
30+
Consumes $CODER_URL
31+
-v, --verbose Enable verbose output.
32+
Consumes $CODER_VERBOSE
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Authenticate with Coder deployment
2+
3+
Usage:
4+
coder login <url> [flags]
5+
6+
Flags:
7+
--first-user-email string Specifies an email address to use if creating the first
8+
user for the deployment.
9+
Consumes $CODER_FIRST_USER_EMAIL
10+
--first-user-password string Specifies a password to use if creating the first user
11+
for the deployment.
12+
Consumes $CODER_FIRST_USER_PASSWORD
13+
--first-user-trial Specifies whether a trial license should be provisioned
14+
for the Coder deployment or not.
15+
Consumes $CODER_FIRST_USER_TRIAL
16+
--first-user-username string Specifies a username to use if creating the first user
17+
for the deployment.
18+
Consumes $CODER_FIRST_USER_USERNAME
19+
-h, --help help for login
20+
21+
Global Flags:
22+
--global-config coder Path to the global coder config directory.
23+
Consumes $CODER_CONFIG_DIR (default "/tmp/coder-cli-test-config")
24+
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
25+
Consumes $CODER_HEADER
26+
--no-feature-warning Suppress warnings about unlicensed features.
27+
Consumes $CODER_NO_FEATURE_WARNING
28+
--no-version-warning Suppress warning when client and server versions do not match.
29+
Consumes $CODER_NO_VERSION_WARNING
30+
--token string Specify an authentication token. For security reasons setting
31+
CODER_SESSION_TOKEN is preferred.
32+
Consumes $CODER_SESSION_TOKEN
33+
--url string URL to a deployment.
34+
Consumes $CODER_URL
35+
-v, --verbose Enable verbose output.
36+
Consumes $CODER_VERBOSE
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Unauthenticate your local session
2+
3+
Usage:
4+
coder logout [flags]
5+
6+
Flags:
7+
-h, --help help for logout
8+
-y, --yes Bypass prompts
9+
10+
Global Flags:
11+
--global-config coder Path to the global coder config directory.
12+
Consumes $CODER_CONFIG_DIR (default "/tmp/coder-cli-test-config")
13+
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
14+
Consumes $CODER_HEADER
15+
--no-feature-warning Suppress warnings about unlicensed features.
16+
Consumes $CODER_NO_FEATURE_WARNING
17+
--no-version-warning Suppress warning when client and server versions do not match.
18+
Consumes $CODER_NO_VERSION_WARNING
19+
--token string Specify an authentication token. For security reasons setting
20+
CODER_SESSION_TOKEN is preferred.
21+
Consumes $CODER_SESSION_TOKEN
22+
--url string URL to a deployment.
23+
Consumes $CODER_URL
24+
-v, --verbose Enable verbose output.
25+
Consumes $CODER_VERBOSE

0 commit comments

Comments
 (0)