|
1 | | -//go:build devel |
2 | | - |
3 | 1 | // Shared scaffolding for the package's integration tests. |
4 | 2 | // |
5 | | -// All integration tests build under the `devel` build tag (so the |
6 | | -// production-version-check HTTP call from src/version is stubbed to a no-op) |
7 | | -// and run via `make test-integration`. They drive the CLI in-process through |
8 | | -// cmdBuilder.RunRootCmd, point the REST client at an httptest.Server, and |
9 | | -// isolate per-test state by setting ZEROPS_CLI_DATA_FILE_PATH, |
10 | | -// ZEROPS_CLI_LOG_FILE_PATH, and ZEROPS_CLI_YAML_FILE_PATH to per-test temp |
11 | | -// files. yamlReader's package-level cache is reset before and after each test. |
12 | | -// See pushDeploy_helpers_test.go for push/deploy-specific helpers. |
| 3 | +// Integration tests drive the CLI in-process through cmdBuilder.RunRootCmd, |
| 4 | +// point the REST client at an httptest.Server, and isolate per-test state by |
| 5 | +// setting ZEROPS_CLI_DATA_FILE_PATH, ZEROPS_CLI_LOG_FILE_PATH, and |
| 6 | +// ZEROPS_CLI_YAML_FILE_PATH to per-test temp files. ZEROPS_VERSION_API_URL is |
| 7 | +// pointed at the test server so the background version check never reaches the |
| 8 | +// real API. yamlReader's package-level cache is reset before and after each |
| 9 | +// test. See pushDeploy_helpers_test.go for push/deploy-specific helpers. |
13 | 10 |
|
14 | 11 | package cmd |
15 | 12 |
|
@@ -63,6 +60,9 @@ func newFixture(t *testing.T) *fixture { |
63 | 60 | t.Setenv(constants.CliLogFilePathEnvVar, logPath) |
64 | 61 | t.Setenv(constants.CliZcliYamlFilePathEnvVar, yamlPath) |
65 | 62 | t.Setenv(constants.CliTokenEnvVar, "") |
| 63 | + // Keep the background version check off the real network. Tests that |
| 64 | + // exercise the version API re-point this at a registered handler. |
| 65 | + t.Setenv(constants.VersionApiUrlEnvVar, server.URL+"/__version_check__") |
66 | 66 |
|
67 | 67 | return &fixture{ |
68 | 68 | t: t, |
@@ -117,13 +117,16 @@ type result struct { |
117 | 117 | ExitCode int |
118 | 118 | } |
119 | 119 |
|
120 | | -// Run executes the CLI in-process with the given args. ctx defaults to |
121 | | -// context.Background() when nil. |
122 | | -func (f *fixture) Run(ctx context.Context, args ...string) result { |
| 120 | +// Run executes the CLI in-process with the given args, using the test's |
| 121 | +// context. Use RunCtx when a specific (e.g. canceled) context is needed. |
| 122 | +func (f *fixture) Run(args ...string) result { |
| 123 | + f.t.Helper() |
| 124 | + return f.RunCtx(f.t.Context(), args...) |
| 125 | +} |
| 126 | + |
| 127 | +// RunCtx is Run with an explicit context. |
| 128 | +func (f *fixture) RunCtx(ctx context.Context, args ...string) result { |
123 | 129 | f.t.Helper() |
124 | | - if ctx == nil { |
125 | | - ctx = context.Background() |
126 | | - } |
127 | 130 | var stdout, stderr bytes.Buffer |
128 | 131 | code := cmdBuilder.RunRootCmd( |
129 | 132 | ctx, |
|
0 commit comments