Files
hyperframes/packages/cli/src/utils/shellArg.test.ts
T
James RussoandClaude Fable 5.2 8bc73a0bd3 fix(cli): show control characters as text in init, skills and render printed lines (#8352)
* fix(cli): show control characters as text in init, skills and render printed lines

Lines that quote a name, a path, a flag value, a tool's reply or a caught
message now pass it through terminalText / terminalLines before printing.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* test(cli): build the expected init path with the platform separator

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* fix(cli): init prints only commands that run as written; one line per render progress update

A project name or old-flag value with a control character is left out of the
printed commands: the next steps say to go into the project folder, and the
renamed-flag examples use a placeholder. A render progress update whose text
has line breaks is written as one line.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* fix(cli): init answers a --json run with a refused value as given

With --json before the command name, a refused --resolution, a missing --video
or --audio file, a folder that is not empty and a failed example now report
the value as it was given in the answer, and as visible text on the terminal.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* fix(cli): init's renamed-flag refusals answer --json with the value as given

The two lines keep the example people see. A --json answer now names the flag
and the value as typed. One entry-point case per init failure line, and a check
that a new failure line cannot be added with a bare console.error.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* fix(cli): answer a --json init failure with the value exactly as given

A failure line's values now reach the answer untouched, line breaks and
trailing spaces included. Status lines wait until the project is made, so
a later failure still answers in JSON, and a run that asked for JSON no
longer stops to ask questions. Next steps for a folder name that cannot
be typed back are printed as bare commands under a prose line.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* fix(cli): hold a converted video's status lines with the rest of init's

With every answer given by flags, init printed the two lines about
converting a video through a spinner, apart from its other status lines.
At a terminal that drew on stdout ahead of the JSON answer to a failed
run. They now go the same way as the other status lines; the run that
asks questions keeps its spinner.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* test(cli): cover a failed project after a converted video in a JSON run

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-21 10:19:08 +00:00

169 lines
5.5 KiB
TypeScript

import { execFileSync, spawnSync } from "node:child_process";
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, win32 } from "node:path";
import { afterAll, describe, expect, it } from "vitest";
import { shellArg } from "./shellArg.js";
const WINDOWS_ROUND_TRIP = [
"my-video",
"my first video",
"it's mine",
"o\u2019neil \u2018draft\u2019 it's",
"\u201Alow\u201B 'n\u2019 \u2018mixed'",
"Q&A (draft); 100% | a<b>c ^",
"100% done",
"서울 여행",
"a,b",
"@scope",
"films\\my film",
"C:\\Users\\Bob\\film",
"C:\\work\\it's\\",
];
// `“ ” „` end a PowerShell double-quoted string; `$` and a backtick expand in PowerShell and Git Bash,
// `!` in interactive Git Bash, and `%NAME%` in cmd.exe.
const WINDOWS_UNPRINTABLE = [
"$(calc)",
"a`b",
"o'neil $x",
"x\u201D;calc;\u201Dy",
"x\u2019;calc;\u2018 $y",
"$(calc)&calc",
"\u201Cquoted\u201D",
"a\nb",
"%USERPROFILE%",
"50% off, 20% more",
"Hello!",
"a!b",
];
const NAMES = [
"my-video",
"서울여행",
"films/a",
"my first video",
"it's mine",
"it's",
"a&b",
"$HOME",
"o'neil $x",
"서울 여행 브이로그",
];
describe("shellArg", () => {
it("leaves a plain name alone and quotes one the shell would split or expand", () => {
expect(NAMES.map((name) => shellArg(name, "darwin"))).toEqual([
"my-video",
"서울여행",
"films/a",
'"my first video"',
`"it's mine"`,
`"it's"`,
'"a&b"',
"'$HOME'",
`'o'\\''neil $x'`,
'"서울 여행 브이로그"',
]);
});
it("uses double quotes on Windows, for a backslash path too", () => {
expect(shellArg("films/a", "win32")).toBe("films/a");
expect(shellArg("films\\a", "win32")).toBe('"films\\a"');
expect(shellArg("C:\\Users\\Bob\\film", "win32")).toBe('"C:\\Users\\Bob\\film"');
expect(shellArg("C:\\", "win32")).toBe('"C:\\\\"');
expect(shellArg("my first video", "win32")).toBe('"my first video"');
expect(shellArg("it's mine", "win32")).toBe(`"it's mine"`);
expect(shellArg("Q&A (draft); 100%", "win32")).toBe('"Q&A (draft); 100%"');
expect(shellArg("서울 여행", "win32")).toBe('"서울 여행"');
});
it("quotes a Windows name that PowerShell would read as an array or a splat", () => {
expect(shellArg("a,b", "win32")).toBe('"a,b"');
expect(shellArg("@scope", "win32")).toBe('"@scope"');
expect(shellArg("films/@scope", "win32")).toBe("films/@scope");
});
// fails without the fix: the word was printed with the character inside its quotes.
it.each(["darwin", "linux", "win32"] as const)(
"prints nothing on %s for a name with a character a terminal does not show",
(platform) => {
for (const name of [
"a\nb",
"a\tb",
`a${String.fromCharCode(7)}b`,
`a${String.fromCharCode(27)}[2Jb`,
])
expect(shellArg(name, platform), JSON.stringify(name)).toBeNull();
},
);
it("prints nothing on Windows for a name no shell there reads literally", () => {
for (const name of WINDOWS_UNPRINTABLE) expect(shellArg(name, "win32"), name).toBeNull();
});
const root = mkdtempSync(join(tmpdir(), "hf-shell-arg-"));
afterAll(() => rmSync(root, { recursive: true, force: true }));
it.skipIf(process.platform === "win32")("prints a cd that a POSIX shell runs as written", () => {
for (const name of NAMES) {
mkdirSync(join(root, name), { recursive: true });
const pwd = execFileSync("/bin/sh", ["-c", `cd ${shellArg(name, "darwin")} && pwd -P`], {
cwd: root,
encoding: "utf8",
});
expect(pwd.trim()).toBe(realpathSync(join(root, name)));
}
});
it.skipIf(process.platform === "win32")(
"prints a Windows word that Git Bash reads as written",
() => {
for (const name of WINDOWS_ROUND_TRIP) {
const shown = execFileSync("/bin/bash", ["-c", `printf %s ${shellArg(name, "win32")}`], {
encoding: "utf8",
});
expect(shown, name).toBe(name);
}
},
);
const argvScript = join(root, "argv.cjs");
writeFileSync(argvScript, "process.stdout.write(JSON.stringify(process.argv.slice(2)));");
const readArgv = (output: string): string[] => JSON.parse(output) as string[];
it.runIf(process.platform === "win32")(
"prints a Windows word that cmd.exe reads as written",
() => {
for (const name of WINDOWS_ROUND_TRIP) {
const line = `"${process.execPath}" "${argvScript}" ${shellArg(name, "win32")}`;
const run = spawnSync("cmd.exe", ["/d", "/s", "/c", `"${line}"`], {
encoding: "utf8",
windowsVerbatimArguments: true,
});
expect(run.status, run.stderr).toBe(0);
expect(readArgv(run.stdout), name).toEqual([name]);
}
},
);
it.runIf(process.platform === "win32")(
"prints a Windows word that PowerShell reads as written",
() => {
for (const name of WINDOWS_ROUND_TRIP) {
const line = `& '${process.execPath}' '${argvScript}' ${shellArg(name, "win32")}`;
const output = execFileSync(
"powershell.exe",
["-NoProfile", "-EncodedCommand", Buffer.from(line, "utf16le").toString("base64")],
{ encoding: "utf8" },
);
// Windows PowerShell hands a space-free word to a program unquoted, so a doubled trailing backslash stays doubled.
expect(
readArgv(output).map((arg) => win32.normalize(arg)),
name,
).toEqual([win32.normalize(name)]);
}
},
);
});