|
| 1 | +// Copyright (c) 2026 Circle Internet Services, Inc. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +// SOFTWARE. |
| 20 | +// |
| 21 | +// SPDX-License-Identifier: MIT |
| 22 | + |
| 23 | +package acceptance_test |
| 24 | + |
| 25 | +import ( |
| 26 | + "os" |
| 27 | + "path/filepath" |
| 28 | + "runtime" |
| 29 | + "strings" |
| 30 | + "testing" |
| 31 | + |
| 32 | + "gotest.tools/v3/assert" |
| 33 | + "gotest.tools/v3/assert/cmp" |
| 34 | + "gotest.tools/v3/skip" |
| 35 | + |
| 36 | + "github.com/CircleCI-Public/circleci-cli-v2/internal/testing/binary" |
| 37 | + testenv "github.com/CircleCI-Public/circleci-cli-v2/internal/testing/env" |
| 38 | +) |
| 39 | + |
| 40 | +// buildExtension writes a shell script as a fake circleci-<name> extension |
| 41 | +// into extDir and returns the full path. The script prints its positional args |
| 42 | +// and the CIRCLE_TOKEN / CIRCLE_URL env vars, then exits 0. |
| 43 | +func buildExtension(t *testing.T, extDir, name string) string { |
| 44 | + t.Helper() |
| 45 | + skip.If(t, runtime.GOOS == "windows", "shell-script extensions are not supported on Windows") |
| 46 | + |
| 47 | + path := filepath.Join(extDir, "circleci-"+name) |
| 48 | + script := "#!/bin/sh\nprintf 'args:%s\\n' \"$*\"\nprintf 'token:%s\\n' \"$CIRCLE_TOKEN\"\nprintf 'url:%s\\n' \"$CIRCLE_URL\"\n" |
| 49 | + err := os.WriteFile(path, []byte(script), 0o755) |
| 50 | + assert.NilError(t, err) |
| 51 | + return path |
| 52 | +} |
| 53 | + |
| 54 | +// withExtDir prepends extDir to PATH in the given environ slice. |
| 55 | +func withExtDir(environ []string, extDir string) []string { |
| 56 | + out := make([]string, 0, len(environ)+1) |
| 57 | + out = append(out, environ...) |
| 58 | + for i, v := range out { |
| 59 | + if strings.HasPrefix(v, "PATH=") { |
| 60 | + out[i] = "PATH=" + extDir + string(os.PathListSeparator) + v[len("PATH="):] |
| 61 | + return out |
| 62 | + } |
| 63 | + } |
| 64 | + return append(out, "PATH="+extDir) |
| 65 | +} |
| 66 | + |
| 67 | +// TestExtensionDispatch verifies that an unknown command is transparently |
| 68 | +// dispatched to circleci-<name> when the binary exists in PATH. |
| 69 | +func TestExtensionDispatch(t *testing.T) { |
| 70 | + extDir := t.TempDir() |
| 71 | + buildExtension(t, extDir, "hello") |
| 72 | + |
| 73 | + env := testenv.New(t) |
| 74 | + env.Token = testToken |
| 75 | + |
| 76 | + result := binary.RunCLI(t, binary.RunOpts{ |
| 77 | + Binary: binaryPath, |
| 78 | + Args: []string{"hello", "arg1", "arg2"}, |
| 79 | + Env: withExtDir(env.Environ(), extDir), |
| 80 | + WorkDir: t.TempDir(), |
| 81 | + }) |
| 82 | + |
| 83 | + assert.Equal(t, result.ExitCode, 0, "stderr: %s", result.Stderr) |
| 84 | + assert.Check(t, cmp.Contains(result.Stdout, "args:arg1 arg2"), |
| 85 | + "extension did not receive correct args; stdout: %q", result.Stdout) |
| 86 | +} |
| 87 | + |
| 88 | +// TestExtensionEnvInjection verifies that CIRCLE_TOKEN and CIRCLE_URL are |
| 89 | +// injected into the extension's environment from the CLI's resolved config. |
| 90 | +func TestExtensionEnvInjection(t *testing.T) { |
| 91 | + extDir := t.TempDir() |
| 92 | + buildExtension(t, extDir, "hello") |
| 93 | + |
| 94 | + env := testenv.New(t) |
| 95 | + env.Token = testToken |
| 96 | + |
| 97 | + result := binary.RunCLI(t, binary.RunOpts{ |
| 98 | + Binary: binaryPath, |
| 99 | + Args: []string{"hello"}, |
| 100 | + Env: withExtDir(env.Environ(), extDir), |
| 101 | + WorkDir: t.TempDir(), |
| 102 | + }) |
| 103 | + |
| 104 | + assert.Equal(t, result.ExitCode, 0, "stderr: %s", result.Stderr) |
| 105 | + assert.Check(t, cmp.Contains(result.Stdout, "token:"+testToken), |
| 106 | + "CIRCLE_TOKEN not injected; stdout: %q", result.Stdout) |
| 107 | +} |
| 108 | + |
| 109 | +// TestExtensionExitCodePropagated verifies that the extension's exit code is |
| 110 | +// propagated back to the caller unchanged. |
| 111 | +func TestExtensionExitCodePropagated(t *testing.T) { |
| 112 | + skip.If(t, runtime.GOOS == "windows", "shell-script extensions are not supported on Windows") |
| 113 | + |
| 114 | + extDir := t.TempDir() |
| 115 | + path := filepath.Join(extDir, "circleci-fail") |
| 116 | + err := os.WriteFile(path, []byte("#!/bin/sh\nexit 42\n"), 0o755) |
| 117 | + assert.NilError(t, err) |
| 118 | + |
| 119 | + env := testenv.New(t) |
| 120 | + |
| 121 | + result := binary.RunCLI(t, binary.RunOpts{ |
| 122 | + Binary: binaryPath, |
| 123 | + Args: []string{"fail"}, |
| 124 | + Env: withExtDir(env.Environ(), extDir), |
| 125 | + WorkDir: t.TempDir(), |
| 126 | + }) |
| 127 | + |
| 128 | + assert.Equal(t, result.ExitCode, 42, "expected exit code 42 from extension") |
| 129 | +} |
| 130 | + |
| 131 | +// TestExtensionNotFoundShowsOriginalError verifies that when no matching |
| 132 | +// extension exists, the original "unknown command" error from Cobra is shown. |
| 133 | +func TestExtensionNotFoundShowsOriginalError(t *testing.T) { |
| 134 | + env := testenv.New(t) |
| 135 | + |
| 136 | + result := binary.RunCLI(t, binary.RunOpts{ |
| 137 | + Binary: binaryPath, |
| 138 | + Args: []string{"no-such-command-xyz"}, |
| 139 | + Env: env.Environ(), |
| 140 | + WorkDir: t.TempDir(), |
| 141 | + }) |
| 142 | + |
| 143 | + assert.Check(t, result.ExitCode != 0, "expected non-zero exit for unknown command") |
| 144 | + assert.Check(t, cmp.Contains(result.Stderr, "unknown command"), |
| 145 | + "expected 'unknown command' in stderr; got: %q", result.Stderr) |
| 146 | +} |
| 147 | + |
| 148 | +// TestExtensionNestedUnknownNotIntercepted verifies that unknown subcommands |
| 149 | +// within a known group (e.g. "circleci pipeline foo") are not dispatched to |
| 150 | +// any extension — only top-level unknown commands are intercepted. |
| 151 | +func TestExtensionNestedUnknownNotIntercepted(t *testing.T) { |
| 152 | + skip.If(t, runtime.GOOS == "windows", "shell-script extensions are not supported on Windows") |
| 153 | + |
| 154 | + // Put a circleci-foo extension on PATH to confirm it is NOT invoked. |
| 155 | + extDir := t.TempDir() |
| 156 | + buildExtension(t, extDir, "foo") |
| 157 | + |
| 158 | + env := testenv.New(t) |
| 159 | + env.Token = testToken |
| 160 | + |
| 161 | + result := binary.RunCLI(t, binary.RunOpts{ |
| 162 | + Binary: binaryPath, |
| 163 | + Args: []string{"pipeline", "foo"}, |
| 164 | + Env: withExtDir(env.Environ(), extDir), |
| 165 | + WorkDir: t.TempDir(), |
| 166 | + }) |
| 167 | + |
| 168 | + // The key assertion: the extension script prints "args:" — if that appears, |
| 169 | + // the extension was wrongly invoked. Whether the group shows help or an error |
| 170 | + // is pre-existing behavior outside this feature's scope. |
| 171 | + assert.Check(t, !strings.Contains(result.Stdout, "args:"), |
| 172 | + "extension should NOT have been invoked for a nested unknown command; stdout: %q", result.Stdout) |
| 173 | +} |
0 commit comments