Skip to content

Commit 04b7ae7

Browse files
committed
test: test options CLI output
1 parent c812453 commit 04b7ae7

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

options_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package envbuilder_test
22

33
import (
44
"bytes"
5+
"flag"
6+
"os"
57
"testing"
68

79
"github.com/coder/envbuilder"
@@ -63,6 +65,40 @@ func TestEnvOptionParsing(t *testing.T) {
6365
})
6466
}
6567

68+
// UpdateGoldenFiles indicates golden files should be updated.
69+
var updateCLIOutputGoldenFiles = flag.Bool("update", false, "update options CLI output .golden files")
70+
71+
// TestCLIOutput tests that the default CLI output is as expected.
72+
func TestCLIOutput(t *testing.T) {
73+
var o envbuilder.Options
74+
cmd := serpent.Command{
75+
Use: "envbuilder",
76+
Options: o.CLI(),
77+
Handler: func(inv *serpent.Invocation) error {
78+
return nil
79+
},
80+
}
81+
82+
var b ioBufs
83+
i := cmd.Invoke("--help")
84+
i.Stdout = &b.Stdout
85+
i.Stderr = &b.Stderr
86+
i.Stdin = &b.Stdin
87+
88+
err := i.Run()
89+
require.NoError(t, err)
90+
91+
if *updateCLIOutputGoldenFiles {
92+
err = os.WriteFile("testdata/options.golden", b.Stdout.Bytes(), 0644)
93+
require.NoError(t, err)
94+
t.Logf("updated golden file: testdata/options.golden")
95+
} else {
96+
golden, err := os.ReadFile("testdata/options.golden")
97+
require.NoError(t, err)
98+
require.Equal(t, string(golden), b.Stdout.String())
99+
}
100+
}
101+
66102
func runCLI() envbuilder.Options {
67103
var o envbuilder.Options
68104
cmd := serpent.Command{

testdata/options.golden

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
USAGE:
2+
envbuilder
3+
4+
OPTIONS:
5+
--base-image-cache-dir string, $BASE_IMAGE_CACHE_DIR
6+
The path to a directory where the base image can be found. This should
7+
be a read-only directory solely mounted for the purpose of caching the
8+
base image.
9+
10+
--build-context-path string, $BUILD_CONTEXT_PATH
11+
Can be specified when a DockerfilePath is specified outside the base
12+
WorkspaceFolder. This path MUST be relative to the WorkspaceFolder
13+
path into which the repo is cloned.
14+
15+
--cache-repo string, $CACHE_REPO
16+
The name of the container registry to push the cache image to. If this
17+
is empty, the cache will not be pushed.
18+
19+
--cache-ttl-days int, $CACHE_TTL_DAYS
20+
The number of days to use cached layers before expiring them. Defaults
21+
to 7 days.
22+
23+
--devcontainer-dir string, $DEVCONTAINER_DIR
24+
The path to the folder containing the devcontainer.json file that will
25+
be used to build the workspace and can either be an absolute path or a
26+
path relative to the workspace folder. If not provided, defaults to
27+
`.devcontainer`.
28+
29+
--devcontainer-json-path string, $DEVCONTAINER_JSON_PATH
30+
The path to a devcontainer.json file that is either an absolute path
31+
or a path relative to DevcontainerDir. This can be used in cases where
32+
one wants to substitute an edited devcontainer.json file for the one
33+
that exists in the repo.
34+
35+
--docker-config-base64 string, $DOCKER_CONFIG_BASE64
36+
The base64 encoded Docker config file that will be used to pull images
37+
from private container registries.
38+
39+
--dockerfile-path string, $DOCKERFILE_PATH
40+
The relative path to the Dockerfile that will be used to build the
41+
workspace. This is an alternative to using a devcontainer that some
42+
might find simpler.
43+
44+
--exit-on-build-failure bool, $EXIT_ON_BUILD_FAILURE
45+
Terminates the container upon a build failure. This is handy when
46+
preferring the FALLBACK_IMAGE in cases where no devcontainer.json or
47+
image is provided. However, it ensures that the container stops if the
48+
build process encounters an error.
49+
50+
--export-env-file string, $EXPORT_ENV_FILE
51+
Optional file path to a .env file where envbuilder will dump
52+
environment variables from devcontainer.json and the built container
53+
image.
54+
55+
--fallback-image string, $FALLBACK_IMAGE
56+
Specifies an alternative image to use when neither an image is
57+
declared in the devcontainer.json file nor a Dockerfile is present. If
58+
there's a build failure (from a faulty Dockerfile) or a
59+
misconfiguration, this image will be the substitute. Set
60+
ExitOnBuildFailure to true to halt the container if the build faces an
61+
issue.
62+
63+
--force-safe bool, $FORCE_SAFE
64+
Ignores any filesystem safety checks. This could cause serious harm to
65+
your system! This is used in cases where bypass is needed to unblock
66+
customers.
67+
68+
--git-clone-depth int, $GIT_CLONE_DEPTH
69+
The depth to use when cloning the Git repository.
70+
71+
--git-clone-single-branch bool, $GIT_CLONE_SINGLE_BRANCH
72+
Clone only a single branch of the Git repository.
73+
74+
--git-http-proxy-url string, $GIT_HTTP_PROXY_URL
75+
The URL for the HTTP proxy. This is optional.
76+
77+
--git-password string, $GIT_PASSWORD
78+
The password to use for Git authentication. This is optional.
79+
80+
--git-url string, $GIT_URL
81+
The URL of the Git repository to clone. This is optional.
82+
83+
--git-username string, $GIT_USERNAME
84+
The username to use for Git authentication. This is optional.
85+
86+
--ignore-paths string-array, $IGNORE_PATHS (default: /var/run)
87+
The comma separated list of paths to ignore when building the
88+
workspace.
89+
90+
--init-args string, $INIT_ARGS
91+
The arguments to pass to the init command. They are split according to
92+
/bin/sh rules with https://github.com/kballard/go-shellquote.
93+
94+
--init-command string, $INIT_COMMAND (default: /bin/sh)
95+
The command to run to initialize the workspace.
96+
97+
--init-script string, $INIT_SCRIPT (default: sleep infinity)
98+
The script to run to initialize the workspace.
99+
100+
--insecure bool, $INSECURE
101+
Bypass TLS verification when cloning and pulling from container
102+
registries.
103+
104+
--layer-cache-dir string, $LAYER_CACHE_DIR
105+
The path to a directory where built layers will be stored. This spawns
106+
an in-memory registry to serve the layers from.
107+
108+
--post-start-script-path string, $POST_START_SCRIPT_PATH
109+
The path to a script that will be created by envbuilder based on the
110+
postStartCommand in devcontainer.json, if any is specified (otherwise
111+
the script is not created). If this is set, the specified InitCommand
112+
should check for the presence of this script and execute it after
113+
successful startup.
114+
115+
--setup-script string, $SETUP_SCRIPT
116+
The script to run before the init script. It runs as the root user
117+
regardless of the user specified in the devcontainer.json file.
118+
SetupScript is ran as the root user prior to the init script. It is
119+
used to configure envbuilder dynamically during the runtime. e.g.
120+
specifying whether to start systemd or tiny init for PID 1.
121+
122+
--skip-rebuild bool, $SKIP_REBUILD
123+
Skip building if the MagicFile exists. This is used to skip building
124+
when a container is restarting. e.g. docker stop -> docker start This
125+
value can always be set to true - even if the container is being
126+
started for the first time.
127+
128+
--ssl-cert-base64 string, $SSL_CERT_BASE64
129+
The content of an SSL cert file. This is useful for self-signed
130+
certificates.
131+
132+
--workspace-folder string, $WORKSPACE_FOLDER
133+
The path to the workspace folder that will be built. This is optional.
134+

0 commit comments

Comments
 (0)