Skip to content

Commit c814fa3

Browse files
committed
feat: Add golden files to test cli help output
1 parent 3f6c448 commit c814fa3

File tree

4 files changed

+346
-2
lines changed

4 files changed

+346
-2
lines changed

Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,16 @@ gen: \
394394
coderd/database/querier.go \
395395
provisionersdk/proto/provisioner.pb.go \
396396
provisionerd/proto/provisionerd.pb.go \
397-
site/src/api/typesGenerated.ts
397+
site/src/api/typesGenerated.ts \
398+
cli/testdata/golden
398399
.PHONY: gen
399400

400401
# Mark all generated files as fresh so make thinks they're up-to-date. This is
401402
# used during releases so we don't run generation scripts.
402403
gen/mark-fresh:
403404
files="coderd/database/dump.sql coderd/database/querier.go provisionersdk/proto/provisioner.pb.go provisionerd/proto/provisionerd.pb.go site/src/api/typesGenerated.ts"
404-
for file in $$files; do
405+
files=($$files cli/testdata/*.golden)
406+
for file in "$${files[@]}"; do
405407
echo "$$file"
406408
if [ ! -f "$$file" ]; then
407409
echo "File '$$file' does not exist"
@@ -443,6 +445,10 @@ site/src/api/typesGenerated.ts: scripts/apitypings/main.go $(shell find codersdk
443445
cd site
444446
yarn run format:types
445447

448+
cli/testdata/golden: $(wildcard cli/testdata/*.golden)
449+
go test ./cli -run=TestCommandHelp -update
450+
.PHONY: cli/testdata/golden
451+
446452
test: test-clean
447453
gotestsum -- -v -short ./...
448454
.PHONY: test

cli/root_test.go

Lines changed: 71 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,75 @@ 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+
func TestCommandHelp(t *testing.T) {
29+
t.Parallel()
30+
31+
tests := []struct {
32+
name string
33+
cmd []string
34+
env map[string]string
35+
}{
36+
{
37+
name: "coder --help",
38+
cmd: []string{"--help"},
39+
env: map[string]string{
40+
"CODER_CONFIG_DIR": "/tmp/coder-cli-test-config",
41+
},
42+
},
43+
{
44+
name: "coder server --help",
45+
cmd: []string{"server", "--help"},
46+
env: map[string]string{
47+
"CODER_CONFIG_DIR": "/tmp/coder-cli-test-config",
48+
"CODER_CACHE_DIRECTORY": "/tmp/coder-cli-test-cache",
49+
},
50+
},
51+
}
52+
53+
//nolint:paralleltest // These test sets env vars.
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+
gf := filepath.Join("testdata", strings.Replace(tt.name, " ", "_", -1)+".golden")
78+
if *updateGoldenFiles {
79+
t.Logf("update golden file for: %q: %s", tt.name, gf)
80+
err = os.WriteFile(gf, buf.Bytes(), 0o600)
81+
require.NoError(t, err, "update golden file")
82+
}
83+
84+
want, err := os.ReadFile(gf)
85+
require.NoError(t, err, "read golden file, run \"make gen\" and commit the changes")
86+
got := buf.Bytes()
87+
require.Equal(t, string(want), string(got), "golden file mismatch: %s, run \"make gen\", verify and commit the changes", gf)
88+
})
89+
}
90+
}
91+
2192
func TestRoot(t *testing.T) {
2293
t.Parallel()
2394
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)