@@ -2,7 +2,8 @@ import * as assert from "node:assert";
22import fs from "node:fs/promises" ;
33import os from "node:os" ;
44import 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" ;
67import type { CLI , ParsedArgs } from "../types.js" ;
78import { EXIT_FAIL , EXIT_OK } from "../util/constants.js" ;
89import { 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+
198211async function captureStreamWrite < T > (
199212 stream : NodeJS . WriteStream ,
200213 callback : ( ) => Promise < T > ,
0 commit comments