|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off -- Resolves the real CLI entrypoint for subprocess tests. |
| 2 | +import * as NodeURL from "node:url"; |
| 3 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | +import { expect, it } from "@effect/vitest"; |
| 5 | +import * as Effect from "effect/Effect"; |
| 6 | +import * as FileSystem from "effect/FileSystem"; |
| 7 | +import * as Stream from "effect/Stream"; |
| 8 | +import { ChildProcess, ChildProcessSpawner } from "effect/process"; |
| 9 | + |
| 10 | +it.live("enables CLI caching while respecting overrides and unavailable storage", () => |
| 11 | + Effect.gen(function* () { |
| 12 | + const fs = yield* FileSystem.FileSystem; |
| 13 | + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 14 | + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-cache-test-" }); |
| 15 | + const report = `import { getCompileCacheDir } from 'node:module'; process.once('exit', () => console.error('CACHE=' + (getCompileCacheDir() ?? 'disabled')));`; |
| 16 | + const run = Effect.fn("cliCompileCache.test.run")(function* (env: Record<string, string>) { |
| 17 | + const child = yield* spawner.spawn( |
| 18 | + ChildProcess.make( |
| 19 | + process.execPath, |
| 20 | + [ |
| 21 | + "--experimental-strip-types", |
| 22 | + "--import", |
| 23 | + `data:text/javascript,${encodeURIComponent(report)}`, |
| 24 | + NodeURL.fileURLToPath(new URL("./bin.ts", import.meta.url)), |
| 25 | + "--help", |
| 26 | + ], |
| 27 | + { |
| 28 | + env: { |
| 29 | + XDG_CACHE_HOME: `${root}/cache`, |
| 30 | + T3CODE_HOME: `${root}/home`, |
| 31 | + NODE_COMPILE_CACHE: undefined, |
| 32 | + NODE_DISABLE_COMPILE_CACHE: undefined, |
| 33 | + APPIMAGE: "", |
| 34 | + ...env, |
| 35 | + }, |
| 36 | + }, |
| 37 | + ), |
| 38 | + ); |
| 39 | + return yield* Effect.all( |
| 40 | + { |
| 41 | + stdout: child.stdout.pipe(Stream.decodeText(), Stream.mkString), |
| 42 | + stderr: child.stderr.pipe(Stream.decodeText(), Stream.mkString), |
| 43 | + exitCode: child.exitCode.pipe(Effect.map(Number)), |
| 44 | + }, |
| 45 | + { concurrency: "unbounded" }, |
| 46 | + ); |
| 47 | + }, Effect.scoped); |
| 48 | + const normal = yield* run({}); |
| 49 | + expect(normal.exitCode).toBe(0); |
| 50 | + expect(normal.stderr).not.toContain("CACHE=disabled"); |
| 51 | + const existing = yield* run({ NODE_COMPILE_CACHE: `${root}/explicit` }); |
| 52 | + expect(existing.exitCode).toBe(0); |
| 53 | + expect(existing.stderr.replaceAll("\\", "/")).toContain( |
| 54 | + `${root}/explicit`.replaceAll("\\", "/"), |
| 55 | + ); |
| 56 | + const disabled = yield* run({ NODE_DISABLE_COMPILE_CACHE: "1" }); |
| 57 | + expect(disabled.exitCode).toBe(0); |
| 58 | + expect(disabled.stderr).toContain("CACHE=disabled"); |
| 59 | + yield* fs.writeFileString(`${root}/blocked`, "not a directory"); |
| 60 | + const unavailable = yield* run({ XDG_CACHE_HOME: `${root}/blocked` }); |
| 61 | + expect(unavailable.exitCode).toBe(0); |
| 62 | + expect(unavailable.stderr).toContain("CACHE=disabled"); |
| 63 | + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 64 | +); |
0 commit comments