runCLI runs a docker agent CLI command and returns its stdout. The first argument is the command name ("exec", "debug", etc.). Commands that talk to an AI model ("exec", "debug title") automatically get a recording AI proxy. The "exec" command also gets a unique session DB.
(t *testing.T, command string, moreArgs ...string)
| 17 | // Commands that talk to an AI model ("exec", "debug title") automatically |
| 18 | // get a recording AI proxy. The "exec" command also gets a unique session DB. |
| 19 | func runCLI(t *testing.T, command string, moreArgs ...string) string { |
| 20 | t.Helper() |
| 21 | |
| 22 | // Ensure root.Execute takes the standalone path even when the test |
| 23 | // process inherits DOCKER_CLI_PLUGIN_ORIGINAL_CLI_COMMAND from a |
| 24 | // Docker CLI plugin environment (e.g. running inside docker-agent). |
| 25 | // We use os.Unsetenv instead of t.Setenv because some callers run |
| 26 | // with t.Parallel(), and t.Setenv panics in that case. |
| 27 | os.Unsetenv("DOCKER_CLI_PLUGIN_ORIGINAL_CLI_COMMAND") |
| 28 | |
| 29 | args := []string{command} |
| 30 | |
| 31 | // Use .env file to set dummy env vars so config loading doesn't fail. |
| 32 | dotEnv := filepath.Join(t.TempDir(), ".env") |
| 33 | err := os.WriteFile(dotEnv, []byte("OPENAI_API_KEY=DUMMY\nDOCKER_TOKEN=DUMMY"), 0o644) |
| 34 | require.NoError(t, err) |
| 35 | args = append(args, "--env-from-file", dotEnv) |
| 36 | |
| 37 | exec := (command == "run") && (len(moreArgs) > 0) && (moreArgs[0] == "--exec") |
| 38 | |
| 39 | // Commands that talk to an AI model need a recording AI proxy. |
| 40 | needsProxy := exec || (command == "debug" && len(moreArgs) > 0 && moreArgs[0] == "title") |
| 41 | if needsProxy { |
| 42 | svr, _ := startRecordingAIProxy(t) |
| 43 | args = append(args, "--models-gateway", svr.URL) |
| 44 | } |
| 45 | |
| 46 | // The exec command needs a unique session DB per test. |
| 47 | if exec { |
| 48 | sessionDB := filepath.Join(t.TempDir(), "session.db") |
| 49 | args = append(args, "--session-db", sessionDB) |
| 50 | } |
| 51 | |
| 52 | var stdout bytes.Buffer |
| 53 | err = root.Execute(t.Context(), nil, &stdout, io.Discard, append(args, moreArgs...)...) |
| 54 | require.NoError(t, err) |
| 55 | |
| 56 | return stdout.String() |
| 57 | } |
no test coverage detected