Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,8 @@ async function main(): Promise<void> {
case "completions": {
// `pty completions <shell>` — print a fish/bash/zsh completion script.
const { cmdCompletions } = await import("./completions.ts");
process.exit(cmdCompletions(args.slice(1)));
process.exitCode = cmdCompletions(args.slice(1));
return;
}

case "version":
Expand Down
22 changes: 21 additions & 1 deletion tests/completions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { describe, it, expect } from "vitest";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import { fileURLToPath, pathToFileURL } from "node:url";
import { spawnSync } from "node:child_process";
import { execSync } from "node:child_process";
import { COMMANDS } from "../src/completions.ts";
Expand Down Expand Up @@ -186,6 +186,26 @@ printf '%s\n' "\${COMPREPLY[@]}"`;
}
});

it("lets piped stdout drain before exiting", () => {
const delayedStdout = pathToFileURL(
path.join(__dirname, "fixtures", "delayed-stdout.mjs"),
).href;
for (const shell of ["fish", "bash", "zsh"]) {
const r = spawnSync(
nodeBin,
["--import", delayedStdout, cliPath, "completions", shell],
{ encoding: "utf8" },
);
const checkedIn = fs.readFileSync(
path.join(__dirname, "..", "completions", `pty.${shell}`),
"utf8",
);

expect(r.status, r.stderr).toBe(0);
expect(r.stdout, `${shell} output was truncated`).toBe(checkedIn);
}
});

it("prints usage and exits non-zero for an unknown shell", () => {
const r = spawnSync(nodeBin, [cliPath, "completions", "tcsh"], {
encoding: "utf8",
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/delayed-stdout.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// The natural race depends on the Node version, operating system, and pipe
// scheduling, so it does not reproduce on every supported setup. Delay the
// write so a forced exit loses the text on every platform, independent of
// pipe size or scheduling luck.
const write = process.stdout.write.bind(process.stdout);

process.stdout.write = (chunk, encoding, callback) => {
setTimeout(() => write(chunk, encoding, callback), 25);
return false;
};
Loading