Skip to content

Commit 9a86f34

Browse files
authored
fix(cli): report completion install outcomes (#129320)
1 parent f05a7dd commit 9a86f34

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

‎src/cli/completion-cli.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export function registerCompletionCli(program: Command) {
203203
// introduce unrelated plugin failures before the cache can be checked or installed.
204204
if (options.install && !options.writeState) {
205205
const targetShell = options.shell ?? resolveShellFromEnv();
206-
await installCompletion(targetShell, Boolean(options.yes), program.name());
206+
await installCompletion(targetShell, false, program.name());
207207
return;
208208
}
209209

@@ -239,7 +239,7 @@ export function registerCompletionCli(program: Command) {
239239

240240
if (options.install) {
241241
const targetShell = options.shell ?? resolveShellFromEnv();
242-
await installCompletion(targetShell, Boolean(options.yes), program.name());
242+
await installCompletion(targetShell, false, program.name());
243243
return;
244244
}
245245

‎src/cli/completion-cli.write-state.test.ts‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
77
import { withEnvAsync } from "../test-utils/env.js";
88
import {
99
COMPLETION_SHELLS,
10+
formatCompletionReloadCommand,
1011
resolveCompletionCachePath,
12+
resolveCompletionProfileHint,
1113
resolveCompletionProfilePath,
1214
type CompletionShell,
1315
} from "./completion-runtime.js";
@@ -144,6 +146,7 @@ describe("completion-cli write-state", () => {
144146

145147
afterEach(async () => {
146148
restoreStderrWriteSpy?.();
149+
vi.restoreAllMocks();
147150
});
148151

149152
it.each(COMPLETION_SHELLS)(
@@ -291,25 +294,35 @@ describe("completion-cli write-state", () => {
291294
);
292295

293296
it.each(COMPLETION_SHELLS)(
294-
"installs cached %s completion without registering commands or plugins",
297+
"installs cached %s completion visibly and idempotently without registering commands or plugins",
295298
async (shell) => {
296299
const { registerCompletionCli } = await import("./completion-cli.js");
297300

298301
await withIsolatedCompletionState(async () => {
299302
const cachePath = resolveCompletionCachePath(shell, "openclaw");
303+
const profilePath = resolveCompletionProfilePath(shell);
304+
const log = vi.spyOn(console, "log").mockImplementation(() => {});
305+
vi.spyOn(console, "warn").mockImplementation(() => {});
300306
await fs.mkdir(path.dirname(cachePath), { recursive: true });
301307
await fs.writeFile(cachePath, "# cached completion\n", "utf8");
302308

303309
const program = new Command().name("openclaw");
304310
registerCompletionCli(program);
305-
await program.parseAsync(["completion", "--shell", shell, "--install", "--yes"], {
306-
from: "user",
307-
});
311+
const args = ["completion", "--shell", shell, "--install", "--yes"];
312+
await program.parseAsync(args, { from: "user" });
308313

309-
await expect(fs.readFile(resolveCompletionProfilePath(shell), "utf8")).resolves.toContain(
310-
cachePath,
314+
const installedProfile = await fs.readFile(profilePath, "utf8");
315+
expect(installedProfile).toContain(cachePath);
316+
expect(log).toHaveBeenCalledWith(
317+
`Completion installed. Restart your shell or run: ${formatCompletionReloadCommand(shell, resolveCompletionProfileHint(shell))}`,
311318
);
312319
await expect(fs.readFile(cachePath, "utf8")).resolves.toBe("# cached completion\n");
320+
321+
log.mockClear();
322+
await program.parseAsync(args, { from: "user" });
323+
324+
expect(log).toHaveBeenCalledWith(`Completion already installed in ${profilePath}`);
325+
await expect(fs.readFile(profilePath, "utf8")).resolves.toBe(installedProfile);
313326
expectCompletionInstallationToSkipRegistration();
314327
});
315328
},
@@ -366,6 +379,8 @@ describe("completion-cli write-state", () => {
366379
const { registerCompletionCli } = await import("./completion-cli.js");
367380

368381
await withIsolatedCompletionState(async () => {
382+
const log = vi.spyOn(console, "log").mockImplementation(() => {});
383+
vi.spyOn(console, "warn").mockImplementation(() => {});
369384
const program = new Command().name("openclaw");
370385
registerCompletionCli(program);
371386
await program.parseAsync(
@@ -378,6 +393,9 @@ describe("completion-cli write-state", () => {
378393
await expect(fs.readFile(resolveCompletionProfilePath("zsh"), "utf8")).resolves.toContain(
379394
cachePath,
380395
);
396+
expect(log).toHaveBeenCalledWith(
397+
"Completion installed. Restart your shell or run: source ~/.zshrc",
398+
);
381399
expect(registerSubCliByNameMock.mock.calls).toEqual([
382400
[program, "qa", process.argv, { purpose: "completion" }],
383401
]);

0 commit comments

Comments
 (0)