fix(cli): say stdin was empty when a JSON flag reads - and nothing is piped (#6852)

This commit is contained in:
Miguel Ángel
2026-10-30 18:29:50 +00:00
committed by GitHub
parent aafbd96b77
commit ef9155fee1
2 changed files with 10 additions and 1 deletions
+6
View File
@@ -35,6 +35,12 @@ describe("readJsonFlag", () => {
expect(() => readJsonFlag("--grading", "C:\\clips\\grade.json", io)).toThrow(/no such file$/);
});
it("says stdin was empty instead of a JSON parse error", () => {
expect(() => readJsonFlag("--grading", "-", { ...io, readStdin: () => "\n" })).toThrow(
/Could not read --grading: stdin: nothing was piped in/,
);
});
it("names the flag and path when the file cannot be read", () => {
expect(() => readJsonFlag("--grading", "missing.json", io)).toThrow(
/Could not read --grading: missing\.json: ENOENT/,
+4 -1
View File
@@ -51,7 +51,10 @@ export function readJsonFlagText(value: string, partialIo: Partial<JsonFlagIO> =
if (trimmed.startsWith("{") || trimmed.startsWith("[") || isJsonScalar(trimmed)) return value;
const source = trimmed === "-" ? "stdin" : value;
try {
return trimmed === "-" ? io.readStdin() : io.readFile(value);
if (trimmed !== "-") return io.readFile(value);
const piped = io.readStdin();
if (!piped.trim()) throw new Error("nothing was piped in");
return piped;
} catch (error: unknown) {
throw new JsonFlagReadError(source, errorText(error));
}