Skip to content

Commit 46d20a0

Browse files
Test proper input stream handling in the CLI.
1 parent 59964fe commit 46d20a0

2 files changed

Lines changed: 23 additions & 13 deletions

File tree

src/cli/cli.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import getStdin from "get-stdin";
22
import * as fs from "node:fs/promises";
33
import * as process from "node:process";
4+
import type { Readable } from "node:stream";
45
import type { CLI } from "../types.js";
56
import { EXIT_ERROR, EXIT_FAIL, EXIT_OK } from "../util/constants.js";
67
import { getErrorMessage } from "../util/getErrorMessage.js";
@@ -132,16 +133,12 @@ export async function formatFile(
132133
}
133134
}
134135

135-
async function mainFormatStdin(cli: CLI, check: boolean): Promise<number> {
136-
const input = await getStdin();
137-
return formatStdin(cli, input, check);
138-
}
139-
140-
export async function formatStdin(
136+
export async function mainFormatStdin(
141137
cli: CLI,
142-
input: string,
143-
check: boolean = false,
138+
check: boolean,
139+
stdin: Readable = process.stdin,
144140
): Promise<number> {
141+
const input = await getStdin({ stdin });
145142
const formatted = await cli.format(input, "stdin");
146143

147144
if (check) {

src/test/cli.test.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as assert from "node:assert";
22
import fs from "node:fs/promises";
33
import os from "node:os";
44
import path from "node:path";
5-
import { formatFile, formatFiles, formatStdin } from "../cli/cli.js";
5+
import { PassThrough } from "node:stream";
6+
import { formatFile, formatFiles, mainFormatStdin } from "../cli/cli.js";
67
import type { CLI, ParsedArgs } from "../types.js";
78
import { EXIT_FAIL, EXIT_OK } from "../util/constants.js";
89
import { parseArgs } from "../util/parseArgs.js";
@@ -108,7 +109,7 @@ suite("CLI", () => {
108109
test("writes formatted stdin to stdout", async () => {
109110
const cli = createCLI((text) => `${text} updated`);
110111
const output = await captureStreamWrite(process.stdout, async () =>
111-
formatStdin(cli, "content"),
112+
readAndFormatStdin(cli, "content"),
112113
);
113114

114115
assert.equal(output.result, EXIT_OK);
@@ -118,7 +119,7 @@ suite("CLI", () => {
118119
test("reports stdin formatting issues to stderr in check mode", async () => {
119120
const cli = createCLI((text) => `${text} updated`);
120121
const output = await captureStreamWrite(process.stderr, async () =>
121-
formatStdin(cli, "content", true),
122+
readAndFormatStdin(cli, "content", true),
122123
);
123124

124125
assert.equal(output.result, EXIT_FAIL);
@@ -128,10 +129,10 @@ suite("CLI", () => {
128129
test("returns success for unchanged stdin in check mode", async () => {
129130
const cli = createCLI((text) => text);
130131
const stderr = await captureStreamWrite(process.stderr, async () =>
131-
formatStdin(cli, "content", true),
132+
readAndFormatStdin(cli, "content", true),
132133
);
133134
const stdout = await captureStreamWrite(process.stdout, async () =>
134-
formatStdin(cli, "content", true),
135+
readAndFormatStdin(cli, "content", true),
135136
);
136137

137138
assert.equal(stderr.result, EXIT_OK);
@@ -195,6 +196,18 @@ function createCLI(format: (text: string) => string | Promise<string>): CLI {
195196
};
196197
}
197198

199+
async function readAndFormatStdin(
200+
cli: CLI,
201+
input: string,
202+
check: boolean = false,
203+
): Promise<number> {
204+
const stdin = new PassThrough();
205+
Object.defineProperty(stdin, "isTTY", { value: false });
206+
const result = mainFormatStdin(cli, check, stdin);
207+
stdin.end(input);
208+
return result;
209+
}
210+
198211
async function captureStreamWrite<T>(
199212
stream: NodeJS.WriteStream,
200213
callback: () => Promise<T>,

0 commit comments

Comments
 (0)