prototypey.org - atproto lexicon typescript toolkit - mirror https://github.com/tylersayshi/prototypey

test util for runCLI

Tyler 8cf91a48 513fc05d

+40 -138
+1 -27
packages/cli/tests/integration/cli.test.ts
··· 1 1 import { expect, test, describe } from "vitest"; 2 - import { spawn } from "node:child_process"; 3 - import { join, dirname } from "node:path"; 4 - import { fileURLToPath } from "node:url"; 2 + import { runCLI } from "../test-utils.js"; 5 3 6 4 describe("CLI Integration", () => { 7 5 test("shows error when called without arguments", async () => { ··· 51 49 ); 52 50 }); 53 51 }); 54 - 55 - function runCLI( 56 - args: string[] = [], 57 - ): Promise<{ stdout: string; stderr: string; code: number }> { 58 - return new Promise((resolve) => { 59 - const cliPath = join(dirname(fileURLToPath(import.meta.url)), "../../lib/index.js"); 60 - const child = spawn("node", [cliPath, ...args]); 61 - 62 - let stdout = ""; 63 - let stderr = ""; 64 - 65 - child.stdout.on("data", (data) => { 66 - stdout += data.toString(); 67 - }); 68 - 69 - child.stderr.on("data", (data) => { 70 - stderr += data.toString(); 71 - }); 72 - 73 - child.on("close", (code) => { 74 - resolve({ stdout, stderr, code: code ?? 0 }); 75 - }); 76 - }); 77 - }
+2 -27
packages/cli/tests/integration/error-handling.test.ts
··· 1 1 import { expect, test, describe, beforeEach, afterEach } from "vitest"; 2 2 import { mkdir, writeFile, rm } from "node:fs/promises"; 3 - import { join, dirname } from "node:path"; 3 + import { join } from "node:path"; 4 4 import { tmpdir } from "node:os"; 5 - import { spawn } from "node:child_process"; 6 - import { fileURLToPath } from "node:url"; 5 + import { runCLI } from "../test-utils.js"; 7 6 8 7 describe("CLI Error Handling", () => { 9 8 let testDir: string; ··· 154 153 expect(stderr).toContain("Error generating inferred types"); 155 154 }); 156 155 }); 157 - 158 - function runCLI( 159 - args: string[], 160 - ): Promise<{ stdout: string; stderr: string; code: number }> { 161 - return new Promise((resolve) => { 162 - const cliPath = join(dirname(fileURLToPath(import.meta.url)), "../../lib/index.js"); 163 - const child = spawn("node", [cliPath, ...args]); 164 - 165 - let stdout = ""; 166 - let stderr = ""; 167 - 168 - child.stdout.on("data", (data) => { 169 - stdout += data.toString(); 170 - }); 171 - 172 - child.stderr.on("data", (data) => { 173 - stderr += data.toString(); 174 - }); 175 - 176 - child.on("close", (code) => { 177 - resolve({ stdout, stderr, code: code ?? 0 }); 178 - }); 179 - }); 180 - }
+2 -27
packages/cli/tests/integration/filesystem.test.ts
··· 7 7 access, 8 8 constants, 9 9 } from "node:fs/promises"; 10 - import { join, dirname } from "node:path"; 10 + import { join } from "node:path"; 11 11 import { tmpdir } from "node:os"; 12 - import { spawn } from "node:child_process"; 13 - import { fileURLToPath } from "node:url"; 12 + import { runCLI } from "../test-utils.js"; 14 13 15 14 describe("CLI File System Handling", () => { 16 15 let testDir: string; ··· 185 184 expect([0, 1]).toContain(code); 186 185 }); 187 186 }); 188 - 189 - function runCLI( 190 - args: string[], 191 - ): Promise<{ stdout: string; stderr: string; code: number }> { 192 - return new Promise((resolve) => { 193 - const cliPath = join(dirname(fileURLToPath(import.meta.url)), "../../lib/index.js"); 194 - const child = spawn("node", [cliPath, ...args]); 195 - 196 - let stdout = ""; 197 - let stderr = ""; 198 - 199 - child.stdout.on("data", (data) => { 200 - stdout += data.toString(); 201 - }); 202 - 203 - child.stderr.on("data", (data) => { 204 - stderr += data.toString(); 205 - }); 206 - 207 - child.on("close", (code) => { 208 - resolve({ stdout, stderr, code: code ?? 0 }); 209 - }); 210 - }); 211 - }
+2 -30
packages/cli/tests/integration/workflow.test.ts
··· 1 1 import { expect, test, describe, beforeEach, afterEach } from "vitest"; 2 2 import { mkdir, writeFile, rm, readFile } from "node:fs/promises"; 3 - import { join, dirname } from "node:path"; 3 + import { join } from "node:path"; 4 4 import { tmpdir } from "node:os"; 5 - import { spawn } from "node:child_process"; 6 - import { fileURLToPath } from "node:url"; 5 + import { runCLI } from "../test-utils.js"; 7 6 8 7 describe("CLI End-to-End Workflow", () => { 9 8 let testDir: string; ··· 190 189 ); 191 190 }); 192 191 }); 193 - 194 - function runCLI( 195 - args: string[], 196 - ): Promise<{ stdout: string; stderr: string; code: number }> { 197 - return new Promise((resolve) => { 198 - const cliPath = join(dirname(fileURLToPath(import.meta.url)), "../../lib/index.js"); 199 - const child = spawn("node", [cliPath, ...args], { 200 - cwd: process.cwd(), 201 - env: process.env, 202 - }); 203 - 204 - let stdout = ""; 205 - let stderr = ""; 206 - 207 - child.stdout.on("data", (data) => { 208 - stdout += data.toString(); 209 - }); 210 - 211 - child.stderr.on("data", (data) => { 212 - stderr += data.toString(); 213 - }); 214 - 215 - child.on("close", (code) => { 216 - resolve({ stdout, stderr, code: code ?? 0 }); 217 - }); 218 - }); 219 - }
+2 -27
packages/cli/tests/performance/large-schema-set.test.ts
··· 1 1 import { expect, test, describe, beforeEach, afterEach } from "vitest"; 2 2 import { mkdir, writeFile, rm } from "node:fs/promises"; 3 - import { join, dirname } from "node:path"; 3 + import { join } from "node:path"; 4 4 import { tmpdir } from "node:os"; 5 - import { spawn } from "node:child_process"; 6 - import { fileURLToPath } from "node:url"; 5 + import { runCLI } from "../test-utils.js"; 7 6 8 7 describe("CLI Performance", () => { 9 8 let testDir: string; ··· 209 208 ); 210 209 }); 211 210 }); 212 - 213 - function runCLI( 214 - args: string[], 215 - ): Promise<{ stdout: string; stderr: string; code: number }> { 216 - return new Promise((resolve) => { 217 - const cliPath = join(dirname(fileURLToPath(import.meta.url)), "../../lib/index.js"); 218 - const child = spawn("node", [cliPath, ...args]); 219 - 220 - let stdout = ""; 221 - let stderr = ""; 222 - 223 - child.stdout.on("data", (data) => { 224 - stdout += data.toString(); 225 - }); 226 - 227 - child.stderr.on("data", (data) => { 228 - stderr += data.toString(); 229 - }); 230 - 231 - child.on("close", (code) => { 232 - resolve({ stdout, stderr, code: code ?? 0 }); 233 - }); 234 - }); 235 - }
+31
packages/cli/tests/test-utils.ts
··· 1 + import { spawn } from "node:child_process"; 2 + import { dirname, join } from "node:path"; 3 + import { fileURLToPath } from "node:url"; 4 + 5 + export function runCLI( 6 + args: string[] = [], 7 + options?: { cwd?: string; env?: NodeJS.ProcessEnv }, 8 + ): Promise<{ stdout: string; stderr: string; code: number }> { 9 + return new Promise((resolve) => { 10 + const cliPath = join(dirname(fileURLToPath(import.meta.url)), "../lib/index.js"); 11 + const child = spawn("node", [cliPath, ...args], { 12 + cwd: options?.cwd ?? process.cwd(), 13 + env: options?.env ?? process.env, 14 + }); 15 + 16 + let stdout = ""; 17 + let stderr = ""; 18 + 19 + child.stdout.on("data", (data) => { 20 + stdout += data.toString(); 21 + }); 22 + 23 + child.stderr.on("data", (data) => { 24 + stderr += data.toString(); 25 + }); 26 + 27 + child.on("close", (code) => { 28 + resolve({ stdout, stderr, code: code ?? 0 }); 29 + }); 30 + }); 31 + }