Files
hyperframes/packages/cli/src/cli.dotEnv.test.ts
T
James RussoandClaude Fable 5.2 9a618b798f fix(cli): read --json before the command name the same as after it (#8365)
* fix(cli): read --json before the command name the same as after it

`hyperframes --json <command>` turned on the JSON failure answer but never
reached the command, so the command printed for people. The flag now moves
behind the command's own arguments before anything reads them, so both
spellings are one run: a command with a `json` flag answers in JSON, and a
command without one refuses the flag as it already did after its name.

A `--json` behind `--` is an argument and no longer turns the JSON failure
answer on.

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

* test(cli): follow --json to its one meaning in the entry-point cases

Rows that put --json before the command name to see a command's lines
for people now run without it, and the word-tier catalog case checks
the ready-to-paste command.

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

* fix(cli): drop init's handling for a JSON run that can no longer start

init has no JSON output, and --json is now refused in both positions before
init runs. The status holding, the JSON-run check and init's own answer text
had no caller left. Its tests run without --json and assert the printed line.

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

* test(cli): let the command table load preview where the studio packages are not built

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

* fix(cli): read --json before `help <command>` the same as after it

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

* test(cli): cover command words run with no subcommand in the --json table

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

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-21 13:31:44 +00:00

90 lines
3.2 KiB
TypeScript

import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, expect, it, vi } from "vitest";
import { mockTelemetry } from "./cliDispatchTestUtils.js";
const originalArgv = [...process.argv];
const originalExitCode = process.exitCode;
const dirs: string[] = [];
const eventNames = ["exit", "uncaughtException", "unhandledRejection"] as const;
const listeners = eventNames.map((event) => [event, process.listeners(event)] as const);
const streams = [process.stdout, process.stderr].map(
(stream) => [stream, stream.listeners("error")] as const,
);
afterEach(async () => {
await vi.dynamicImportSettled();
process.argv = [...originalArgv];
process.exitCode = originalExitCode;
vi.restoreAllMocks();
vi.unstubAllEnvs();
for (const path of [
"./commands/capture.js",
"./commands/init.js",
"./telemetry/index.js",
"./telemetry/events.js",
])
vi.doUnmock(path);
vi.resetModules();
for (const [event, original] of listeners) {
for (const listener of process.listeners(event)) {
if (!original.includes(listener)) process.removeListener(event, listener);
}
}
for (const [stream, original] of streams) {
for (const listener of stream.listeners("error")) {
if (!original.includes(listener)) stream.removeListener("error", listener);
}
}
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
});
it.each(
(
[
["capture", false, undefined, "fixture-key", undefined],
["capture", true, undefined, undefined, undefined],
["init", false, undefined, "fixture-key", undefined],
["init", true, undefined, "fixture-key", undefined],
["capture", false, process.execPath, "fixture-key", process.execPath],
] as const
).flatMap((test) =>
[[], ["--json"], ["--json", "--verbose"]].map((prefix) => [...test, prefix] as const),
),
)(
"applies CWD dotenv policy at real CLI startup: %s, opt-out=%s",
async (command, skip, browser, key, inheritedBrowser, prefix) => {
const dir = mkdtempSync(join(tmpdir(), "hf-cli-dotenv-"));
dirs.push(dir);
writeFileSync(
join(dir, ".env"),
`HYPERFRAMES_BROWSER_PATH=${process.execPath}\nNODE_OPTIONS=--no-warnings\nGEMINI_API_KEY=fixture-key\n`,
);
vi.spyOn(process, "cwd").mockReturnValue(dir);
vi.stubEnv("HYPERFRAMES_BROWSER_PATH", inheritedBrowser);
vi.stubEnv("NODE_OPTIONS", undefined);
vi.stubEnv("GEMINI_API_KEY", undefined);
vi.stubEnv("HYPERFRAMES_NO_CAPTURE_DOTENV", skip ? "1" : undefined);
let observed: unknown;
const definition = {
default: {
meta: { name: command },
args: { json: { type: "boolean" } },
run: () => {
observed = [
process.env.HYPERFRAMES_BROWSER_PATH,
process.env.NODE_OPTIONS,
process.env.GEMINI_API_KEY,
];
},
},
};
vi.doMock("./commands/capture.js", () => definition);
vi.doMock("./commands/init.js", () => definition);
mockTelemetry();
process.argv = ["node", "cli.ts", ...prefix, command];
await import("./cli.js");
expect(observed).toEqual([browser, undefined, key]);
},
);