Skip to content

test: Generate golden files for all (visible) CLI commands #5479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions cli/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package cli_test
import (
"bytes"
"flag"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strings"
"testing"

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

tests := []struct {
commonEnv := map[string]string{
"CODER_CONFIG_DIR": "/tmp/coder-cli-test-config",
}

type testCase struct {
name string
cmd []string
env map[string]string
}{
}
tests := []testCase{
{
name: "coder --help",
cmd: []string{"--help"},
env: map[string]string{
"CODER_CONFIG_DIR": "/tmp/coder-cli-test-config",
},
},
{
name: "coder server --help",
cmd: []string{"server", "--help"},
env: map[string]string{
"CODER_CONFIG_DIR": "/tmp/coder-cli-test-config",
"CODER_CACHE_DIRECTORY": "/tmp/coder-cli-test-cache",
},
},
}

root := cli.Root(cli.AGPL())
ExtractCommandPathsLoop:
for _, cp := range extractVisibleCommandPaths(nil, root.Commands()) {
name := fmt.Sprintf("coder %s --help", strings.Join(cp, " "))
cmd := append(cp, "--help")
for _, tt := range tests {
if tt.name == name {
continue ExtractCommandPathsLoop
}
}
tests = append(tests, testCase{name: name, cmd: cmd})
}

wd, err := os.Getwd()
require.NoError(t, err)
if runtime.GOOS == "windows" {
wd = strings.ReplaceAll(wd, "\\", "\\\\")
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -63,6 +85,14 @@ func TestCommandHelp(t *testing.T) {
}
}
// Override environment variables for a reproducible test.
for k, v := range commonEnv {
if tt.env != nil {
if _, ok := tt.env[k]; ok {
continue
}
}
t.Setenv(k, v)
}
for k, v := range tt.env {
t.Setenv(k, v)
}
Expand All @@ -79,6 +109,10 @@ func TestCommandHelp(t *testing.T) {
// Remove CRLF newlines (Windows).
got = bytes.ReplaceAll(got, []byte{'\r', '\n'}, []byte{'\n'})

// The `coder templates create --help` command prints the path
// to the working directory (--directory flag default value).
got = bytes.ReplaceAll(got, []byte(wd), []byte("/tmp/coder-cli-test-workdir"))

gf := filepath.Join("testdata", strings.Replace(tt.name, " ", "_", -1)+".golden")
if *updateGoldenFiles {
t.Logf("update golden file for: %q: %s", tt.name, gf)
Expand All @@ -95,6 +129,19 @@ func TestCommandHelp(t *testing.T) {
}
}

func extractVisibleCommandPaths(cmdPath []string, cmds []*cobra.Command) [][]string {
var cmdPaths [][]string
for _, c := range cmds {
if c.Hidden {
continue
}
cmdPath := append(cmdPath, c.Name())
cmdPaths = append(cmdPaths, cmdPath)
cmdPaths = append(cmdPaths, extractVisibleCommandPaths(cmdPath, c.Commands())...)
}
return cmdPaths
}

func TestRoot(t *testing.T) {
t.Parallel()
t.Run("FormatCobraError", func(t *testing.T) {
Expand Down
43 changes: 43 additions & 0 deletions cli/testdata/coder_config-ssh_--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Add an SSH Host entry for your workspaces "ssh coder.workspace"

Usage:
coder config-ssh [flags]

Get Started:
- You can use -o (or --ssh-option) so set SSH options to be used for all your
workspaces:

$ coder config-ssh -o ForwardAgent=yes

- You can use --dry-run (or -n) to see the changes that would be made:

$ coder config-ssh --dry-run

Flags:
-n, --dry-run Perform a trial run with no changes made, showing a diff at
the end.
-h, --help help for config-ssh
--ssh-config-file string Specifies the path to an SSH config.
Consumes $CODER_SSH_CONFIG_FILE (default "~/.ssh/config")
-o, --ssh-option stringArray Specifies additional SSH options to embed in each host stanza.
--use-previous-options Specifies whether or not to keep options from previous run of
config-ssh.
Consumes $CODER_SSH_USE_PREVIOUS_OPTIONS
-y, --yes Bypass prompts

Global Flags:
--global-config coder Path to the global coder config directory.
Consumes $CODER_CONFIG_DIR (default "/tmp/coder-cli-test-config")
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
Consumes $CODER_HEADER
--no-feature-warning Suppress warnings about unlicensed features.
Consumes $CODER_NO_FEATURE_WARNING
--no-version-warning Suppress warning when client and server versions do not match.
Consumes $CODER_NO_VERSION_WARNING
--token string Specify an authentication token. For security reasons setting
CODER_SESSION_TOKEN is preferred.
Consumes $CODER_SESSION_TOKEN
--url string URL to a deployment.
Consumes $CODER_URL
-v, --verbose Enable verbose output.
Consumes $CODER_VERBOSE
36 changes: 36 additions & 0 deletions cli/testdata/coder_create_--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Create a workspace

Usage:
coder create [name] [flags]

Flags:
-h, --help help for create
--parameter-file string Specify a file path with parameter values.
Consumes $CODER_PARAMETER_FILE
--start-at coder schedule start --help Specify the workspace autostart schedule. Check
coder schedule start --help for the syntax.
Consumes $CODER_WORKSPACE_START_AT
--stop-after duration Specify a duration after which the workspace
should shut down (e.g. 8h).
Consumes $CODER_WORKSPACE_STOP_AFTER (default
8h0m0s)
-t, --template string Specify a template name.
Consumes $CODER_TEMPLATE_NAME
-y, --yes Bypass prompts

Global Flags:
--global-config coder Path to the global coder config directory.
Consumes $CODER_CONFIG_DIR (default "/tmp/coder-cli-test-config")
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
Consumes $CODER_HEADER
--no-feature-warning Suppress warnings about unlicensed features.
Consumes $CODER_NO_FEATURE_WARNING
--no-version-warning Suppress warning when client and server versions do not match.
Consumes $CODER_NO_VERSION_WARNING
--token string Specify an authentication token. For security reasons setting
CODER_SESSION_TOKEN is preferred.
Consumes $CODER_SESSION_TOKEN
--url string URL to a deployment.
Consumes $CODER_URL
-v, --verbose Enable verbose output.
Consumes $CODER_VERBOSE
30 changes: 30 additions & 0 deletions cli/testdata/coder_delete_--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Delete a workspace

Usage:
coder delete <workspace> [flags]

Aliases:
delete, rm

Flags:
-h, --help help for delete
--orphan Delete a workspace without deleting its resources. This can delete a
workspace in a broken state, but may also lead to unaccounted cloud resources.
-y, --yes Bypass prompts

Global Flags:
--global-config coder Path to the global coder config directory.
Consumes $CODER_CONFIG_DIR (default "/tmp/coder-cli-test-config")
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
Consumes $CODER_HEADER
--no-feature-warning Suppress warnings about unlicensed features.
Consumes $CODER_NO_FEATURE_WARNING
--no-version-warning Suppress warning when client and server versions do not match.
Consumes $CODER_NO_VERSION_WARNING
--token string Specify an authentication token. For security reasons setting
CODER_SESSION_TOKEN is preferred.
Consumes $CODER_SESSION_TOKEN
--url string URL to a deployment.
Consumes $CODER_URL
-v, --verbose Enable verbose output.
Consumes $CODER_VERBOSE
33 changes: 33 additions & 0 deletions cli/testdata/coder_dotfiles_--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Checkout and install a dotfiles repository from a Git URL

Usage:
coder dotfiles [git_repo_url] [flags]

Get Started:
- Check out and install a dotfiles repository without prompts:

$ coder dotfiles --yes git@github.com:example/dotfiles.git

Flags:
-h, --help help for dotfiles
--symlink-dir string Specifies the directory for the dotfiles symlink destinations. If
empty will use $HOME.
Consumes $CODER_SYMLINK_DIR
-y, --yes Bypass prompts

Global Flags:
--global-config coder Path to the global coder config directory.
Consumes $CODER_CONFIG_DIR (default "/tmp/coder-cli-test-config")
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
Consumes $CODER_HEADER
--no-feature-warning Suppress warnings about unlicensed features.
Consumes $CODER_NO_FEATURE_WARNING
--no-version-warning Suppress warning when client and server versions do not match.
Consumes $CODER_NO_VERSION_WARNING
--token string Specify an authentication token. For security reasons setting
CODER_SESSION_TOKEN is preferred.
Consumes $CODER_SESSION_TOKEN
--url string URL to a deployment.
Consumes $CODER_URL
-v, --verbose Enable verbose output.
Consumes $CODER_VERBOSE
32 changes: 32 additions & 0 deletions cli/testdata/coder_list_--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
List workspaces

Usage:
coder list [flags]

Aliases:
list, ls

Flags:
-a, --all Specifies whether all workspaces will be listed or not.
-c, --column stringArray Specify a column to filter in the table. Available columns are:
workspace, template, status, last_built, outdated, starts_at,
stops_after
-h, --help help for list
--search string Search for a workspace with a query. (default "owner:me")

Global Flags:
--global-config coder Path to the global coder config directory.
Consumes $CODER_CONFIG_DIR (default "/tmp/coder-cli-test-config")
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
Consumes $CODER_HEADER
--no-feature-warning Suppress warnings about unlicensed features.
Consumes $CODER_NO_FEATURE_WARNING
--no-version-warning Suppress warning when client and server versions do not match.
Consumes $CODER_NO_VERSION_WARNING
--token string Specify an authentication token. For security reasons setting
CODER_SESSION_TOKEN is preferred.
Consumes $CODER_SESSION_TOKEN
--url string URL to a deployment.
Consumes $CODER_URL
-v, --verbose Enable verbose output.
Consumes $CODER_VERBOSE
36 changes: 36 additions & 0 deletions cli/testdata/coder_login_--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Authenticate with Coder deployment

Usage:
coder login <url> [flags]

Flags:
--first-user-email string Specifies an email address to use if creating the first
user for the deployment.
Consumes $CODER_FIRST_USER_EMAIL
--first-user-password string Specifies a password to use if creating the first user
for the deployment.
Consumes $CODER_FIRST_USER_PASSWORD
--first-user-trial Specifies whether a trial license should be provisioned
for the Coder deployment or not.
Consumes $CODER_FIRST_USER_TRIAL
--first-user-username string Specifies a username to use if creating the first user
for the deployment.
Consumes $CODER_FIRST_USER_USERNAME
-h, --help help for login

Global Flags:
--global-config coder Path to the global coder config directory.
Consumes $CODER_CONFIG_DIR (default "/tmp/coder-cli-test-config")
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
Consumes $CODER_HEADER
--no-feature-warning Suppress warnings about unlicensed features.
Consumes $CODER_NO_FEATURE_WARNING
--no-version-warning Suppress warning when client and server versions do not match.
Consumes $CODER_NO_VERSION_WARNING
--token string Specify an authentication token. For security reasons setting
CODER_SESSION_TOKEN is preferred.
Consumes $CODER_SESSION_TOKEN
--url string URL to a deployment.
Consumes $CODER_URL
-v, --verbose Enable verbose output.
Consumes $CODER_VERBOSE
25 changes: 25 additions & 0 deletions cli/testdata/coder_logout_--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Unauthenticate your local session

Usage:
coder logout [flags]

Flags:
-h, --help help for logout
-y, --yes Bypass prompts

Global Flags:
--global-config coder Path to the global coder config directory.
Consumes $CODER_CONFIG_DIR (default "/tmp/coder-cli-test-config")
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
Consumes $CODER_HEADER
--no-feature-warning Suppress warnings about unlicensed features.
Consumes $CODER_NO_FEATURE_WARNING
--no-version-warning Suppress warning when client and server versions do not match.
Consumes $CODER_NO_VERSION_WARNING
--token string Specify an authentication token. For security reasons setting
CODER_SESSION_TOKEN is preferred.
Consumes $CODER_SESSION_TOKEN
--url string URL to a deployment.
Consumes $CODER_URL
-v, --verbose Enable verbose output.
Consumes $CODER_VERBOSE
Loading