1- import { describe , expect , test } from "bun:test"
2- import { mkdir , mkdtemp , writeFile } from "node:fs/promises"
1+ import { afterAll , describe , expect , test } from "bun:test"
2+ import { mkdir , mkdtemp , rm , writeFile } from "node:fs/promises"
33import { tmpdir } from "node:os"
44import path from "node:path"
55
@@ -8,9 +8,28 @@ const cliEnv = { ...process.env, ALTIMATE_TELEMETRY_DISABLED: "1", ALTIMATE_TOOL
88// These drive the CLI through a shell pipeline, which is what makes the reader late.
99const posixOnly = test . skipIf ( process . platform === "win32" )
1010
11+ const pipeDirs : string [ ] = [ ]
12+ async function pipeDir ( ) {
13+ const dir = await mkdtemp ( path . join ( tmpdir ( ) , "tool-cli-" ) )
14+ pipeDirs . push ( dir )
15+ return dir
16+ }
17+ afterAll ( ( ) => Promise . all ( pipeDirs . map ( ( dir ) => rm ( dir , { recursive : true , force : true } ) ) ) )
18+
19+ /** Runs a shell line with the CLI as `"$0" --conditions=browser "$1"`, and returns the CLI's exit code from code.txt. */
20+ async function inShell ( dir : string , line : string ) {
21+ const proc = Bun . spawn ( [ "sh" , "-c" , line , process . execPath , entry ] , { cwd : dir , stderr : "ignore" , env : cliEnv , timeout : 50_000 } )
22+ try {
23+ await proc . exited
24+ return Number ( ( await Bun . file ( path . join ( dir , "code.txt" ) ) . text ( ) ) . trim ( ) )
25+ } finally {
26+ proc . kill ( )
27+ }
28+ }
29+
1130/** A manifest whose hub has 450 readers, so its `dbt_lineage` graph is far over a pipe's 64 KiB. */
1231async function bigAnswerProject ( ) {
13- const dir = await mkdtemp ( path . join ( tmpdir ( ) , "tool-cli-" ) )
32+ const dir = await pipeDir ( )
1433 const model = ( name : string , deps : string [ ] ) => ( {
1534 resource_type : "model" ,
1635 unique_id : `model.shop.${ name } ` ,
@@ -44,18 +63,11 @@ async function bigAnswerProject() {
4463 * Runs the CLI with a reader that waits for it to exit, up to 20 s, as a busy event loop can: the pipe is full
4564 * until then. A CLI that drops what the pipe can't take exits at once and leaves 64 KiB; one that writes it all waits.
4665 */
47- async function lateReader ( dir : string , command : string ) {
48- const proc = Bun . spawn (
49- [
50- "sh" ,
51- "-c" ,
52- `{ "$0" --conditions=browser "$1" ${ command } ; touch done; } | { i=0; until [ -e done ] || [ $i -ge 200 ]; do sleep 0.1; i=$((i+1)); done; cat > out.json; }` ,
53- process . execPath ,
54- entry ,
55- ] ,
56- { cwd : dir , stderr : "ignore" , env : cliEnv } ,
66+ function lateReader ( dir : string , command : string ) {
67+ return inShell (
68+ dir ,
69+ `{ "$0" --conditions=browser "$1" ${ command } ; echo $? > code.txt; touch done; } | { i=0; until [ -e done ] || [ $i -ge 200 ]; do sleep 0.1; i=$((i+1)); done; cat > out.json; }` ,
5770 )
58- return proc . exited
5971}
6072
6173async function cli ( args : string [ ] , stdin ?: string ) {
@@ -91,30 +103,36 @@ describe("tool CLI", () => {
91103 } , 60000 )
92104
93105 posixOnly ( "tool list --json reaches a reader that starts late whole" , async ( ) => {
94- const dir = await mkdtemp ( path . join ( tmpdir ( ) , "tool-cli-" ) )
106+ const dir = await pipeDir ( )
95107 expect ( await lateReader ( dir , "tool list --json" ) ) . toBe ( 0 )
96108 const stdout = await Bun . file ( path . join ( dir , "out.json" ) ) . text ( )
97- expect ( stdout . length ) . toBeGreaterThan ( 65_536 )
109+ expect ( stdout . endsWith ( "]\n" ) ) . toBe ( true )
98110 expect ( ( JSON . parse ( stdout ) as unknown [ ] ) . length ) . toBeGreaterThan ( 0 )
99111 } , 60000 )
100112
101113 posixOnly ( "a reader that leaves early is not an error" , async ( ) => {
102114 const dir = await bigAnswerProject ( )
103- const proc = Bun . spawn (
104- [
105- "sh" ,
106- "-c" ,
107- `{ "$0" --conditions=browser "$1" tool run dbt_lineage --args - --json < args.json 2> err.txt; echo $? > code.txt; } | head -c 10 > /dev/null` ,
108- process . execPath ,
109- entry ,
110- ] ,
111- { cwd : dir , stderr : "ignore" , env : cliEnv } ,
115+ const code = await inShell (
116+ dir ,
117+ `{ "$0" --conditions=browser "$1" tool run dbt_lineage --args - --json < args.json 2> err.txt; echo $? > code.txt; } | head -c 10 > /dev/null` ,
112118 )
113- await proc . exited
114- expect ( ( await Bun . file ( path . join ( dir , "code.txt" ) ) . text ( ) ) . trim ( ) ) . toBe ( "0" )
119+ expect ( code ) . toBe ( 0 )
115120 expect ( await Bun . file ( path . join ( dir , "err.txt" ) ) . text ( ) ) . toBe ( "" )
116121 } , 60000 )
117122
123+ posixOnly ( "an error stays an error when the reader has left" , async ( ) => {
124+ const dir = await pipeDir ( )
125+ const code = await inShell ( dir , `{ "$0" --conditions=browser "$1" tool run nope --args '{}' --json 2> err.txt; echo $? > code.txt; } | true` )
126+ expect ( code ) . toBe ( 1 )
127+ } , 60000 )
128+
129+ posixOnly ( "a write that really fails exits non-zero and says why" , async ( ) => {
130+ const dir = await pipeDir ( )
131+ const code = await inShell ( dir , `"$0" --conditions=browser "$1" tool list --json 1</dev/null 2> err.txt; echo $? > code.txt` )
132+ expect ( code ) . toBe ( 1 )
133+ expect ( await Bun . file ( path . join ( dir , "err.txt" ) ) . text ( ) ) . toContain ( "EBADF" )
134+ } , 60000 )
135+
118136 test ( "rejects --args - with empty stdin" , async ( ) => {
119137 const { stdout, code } = await cli ( [ "tool" , "run" , "sql_format" , "--args" , "-" , "--json" ] , "" )
120138 expect ( code ) . toBe ( 1 )
0 commit comments