Skip to content

Commit 587924f

Browse files
authored
feat: Add golden files to test cli help output (#4897)
1 parent 70048ac commit 587924f

File tree

5 files changed

+355
-0
lines changed

5 files changed

+355
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ gotests.coverage
1818
.gitpod.yml
1919
.DS_Store
2020

21+
# Make target for updating golden files.
22+
cli/testdata/.gen-golden
23+
2124
# Front-end ignore
2225
.next/
2326
site/.eslintcache

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,15 @@ site/src/api/typesGenerated.ts: scripts/apitypings/main.go $(shell find codersdk
443443
cd site
444444
yarn run format:types
445445

446+
update-golden-files: cli/testdata/.gen-golden
447+
.PHONY: update-golden-files
448+
449+
cli/testdata/.gen-golden: $(wildcard cli/testdata/*.golden) \
450+
$(shell find . -not -path './vendor/*' -type f -name '*.go')
451+
452+
go test ./cli -run=TestCommandHelp -update
453+
touch "$@"
454+
446455
test: test-clean
447456
gotestsum -- -v -short ./...
448457
.PHONY: test

cli/root_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package cli_test
22

33
import (
44
"bytes"
5+
"flag"
56
"net/http"
67
"net/http/httptest"
8+
"os"
9+
"path/filepath"
10+
"strings"
711
"testing"
812

913
"github.com/spf13/cobra"
@@ -16,8 +20,80 @@ import (
1620
"github.com/coder/coder/cli/cliflag"
1721
"github.com/coder/coder/cli/clitest"
1822
"github.com/coder/coder/codersdk"
23+
"github.com/coder/coder/testutil"
1924
)
2025

26+
var updateGoldenFiles = flag.Bool("update", false, "update .golden files")
27+
28+
//nolint:tparallel,paralleltest // These test sets env vars.
29+
func TestCommandHelp(t *testing.T) {
30+
t.Parallel()
31+
32+
tests := []struct {
33+
name string
34+
cmd []string
35+
env map[string]string
36+
}{
37+
{
38+
name: "coder --help",
39+
cmd: []string{"--help"},
40+
env: map[string]string{
41+
"CODER_CONFIG_DIR": "/tmp/coder-cli-test-config",
42+
},
43+
},
44+
{
45+
name: "coder server --help",
46+
cmd: []string{"server", "--help"},
47+
env: map[string]string{
48+
"CODER_CONFIG_DIR": "/tmp/coder-cli-test-config",
49+
"CODER_CACHE_DIRECTORY": "/tmp/coder-cli-test-cache",
50+
},
51+
},
52+
}
53+
54+
for _, tt := range tests {
55+
tt := tt
56+
t.Run(tt.name, func(t *testing.T) {
57+
// Unset all CODER_ environment variables for a clean slate.
58+
for _, kv := range os.Environ() {
59+
name := strings.Split(kv, "=")[0]
60+
if _, ok := tt.env[name]; !ok && strings.HasPrefix(name, "CODER_") {
61+
t.Setenv(name, "")
62+
}
63+
}
64+
// Override environment variables for a reproducible test.
65+
for k, v := range tt.env {
66+
t.Setenv(k, v)
67+
}
68+
69+
ctx, _ := testutil.Context(t)
70+
71+
var buf bytes.Buffer
72+
root, _ := clitest.New(t, tt.cmd...)
73+
root.SetOut(&buf)
74+
err := root.ExecuteContext(ctx)
75+
require.NoError(t, err)
76+
77+
got := buf.Bytes()
78+
// Remove CRLF newlines (Windows).
79+
got = bytes.ReplaceAll(got, []byte{'\r', '\n'}, []byte{'\n'})
80+
81+
gf := filepath.Join("testdata", strings.Replace(tt.name, " ", "_", -1)+".golden")
82+
if *updateGoldenFiles {
83+
t.Logf("update golden file for: %q: %s", tt.name, gf)
84+
err = os.WriteFile(gf, got, 0o600)
85+
require.NoError(t, err, "update golden file")
86+
}
87+
88+
want, err := os.ReadFile(gf)
89+
require.NoError(t, err, "read golden file, run \"make update-golden-files\" and commit the changes")
90+
// Remove CRLF newlines (Windows).
91+
want = bytes.ReplaceAll(want, []byte{'\r', '\n'}, []byte{'\n'})
92+
require.Equal(t, string(want), string(got), "golden file mismatch: %s, run \"make update-golden-files\", verify and commit the changes", gf)
93+
})
94+
}
95+
}
96+
2197
func TestRoot(t *testing.T) {
2298
t.Parallel()
2399
t.Run("FormatCobraError", func(t *testing.T) {

cli/testdata/coder_--help.golden

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
Coder v0.0.0-devel — A tool for provisioning self-hosted development environments with Terraform.
2+
3+
Usage:
4+
coder [flags]
5+
6+
coder [command]
7+
8+
Get Started:
9+
- Start a Coder server:
10+
11+
$ coder server
12+
13+
- Get started by creating a template from an example:
14+
15+
$ coder templates init
16+
17+
Commands:
18+
completion Generate the autocompletion script for the specified shell
19+
dotfiles Checkout and install a dotfiles repository from a Git URL
20+
help Help about any command
21+
login Authenticate with Coder deployment
22+
logout Unauthenticate your local session
23+
port-forward Forward ports from machine to a workspace
24+
publickey Output your Coder public key used for Git operations
25+
reset-password Directly connect to the database to reset a user's password
26+
server Start a Coder server
27+
state Manually manage Terraform state to fix broken workspaces
28+
templates Manage templates
29+
tokens Manage personal access tokens
30+
users Manage users
31+
version Show coder version
32+
33+
Workspace Commands:
34+
config-ssh Add an SSH Host entry for your workspaces "ssh coder.workspace"
35+
create Create a workspace
36+
delete Delete a workspace
37+
list List workspaces
38+
schedule Schedule automated start and stop times for workspaces
39+
show Display details of a workspace's resources and agents
40+
speedtest Run upload and download tests from your machine to a workspace
41+
ssh Start a shell into a workspace
42+
start Start a workspace
43+
stop Stop a workspace
44+
update Update a workspace
45+
46+
Flags:
47+
--experimental Enable experimental features. Experimental features are not
48+
ready for production.
49+
Consumes $CODER_EXPERIMENTAL
50+
--global-config coder Path to the global coder config directory.
51+
Consumes $CODER_CONFIG_DIR (default "/tmp/coder-cli-test-config")
52+
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
53+
Consumes $CODER_HEADER
54+
-h, --help help for coder
55+
--no-feature-warning Suppress warnings about unlicensed features.
56+
Consumes $CODER_NO_FEATURE_WARNING
57+
--no-version-warning Suppress warning when client and server versions do not match.
58+
Consumes $CODER_NO_VERSION_WARNING
59+
--token string Specify an authentication token. For security reasons setting
60+
CODER_SESSION_TOKEN is preferred.
61+
Consumes $CODER_SESSION_TOKEN
62+
--url string URL to a deployment.
63+
Consumes $CODER_URL
64+
-v, --verbose Enable verbose output.
65+
Consumes $CODER_VERBOSE
66+
67+
Use "coder [command] --help" for more information about a command.

0 commit comments

Comments
 (0)