import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { afterAll, afterEach, beforeAll, beforeEach, expect } from 'vitest'; export const FIXTURE_MARKER = 'fixture.cfg'; /** * A plugin Stim ships, `@stim-cli/plugin-fixture`, that no `plugins` entry names: its manifest declares a Cargo-shaped * `target/` beside `fixture.cfg`, a cache under `~/.cache/fixture` and an iOS headroom of 17 GB, and its module writes * `sentinel` when imported. A test file routes `../shipped-plugins.ts` to `shipped.dir` with `vi.mock` and calls this * at its top level; every test then proves the data applies, and the file ends by asserting the module never loaded. */ export function useShippedFixture(shipped: { dir: string }): { sentinel: () => string } { let sentinel = ''; beforeAll(() => { shipped.dir = mkdtempSync(join(tmpdir(), 'stim-shipped-fixture-')); sentinel = join(shipped.dir, 'imported'); writeFileSync( join(shipped.dir, 'plugin.mjs'), `import { writeFileSync } from 'node:fs';\nwriteFileSync(${JSON.stringify(sentinel)}, '');\nexport const createProjectPlugin = () => ({});\n`, ); writeFileSync( join(shipped.dir, 'package.json'), JSON.stringify({ name: '@stim-cli/plugin-fixture', files: ['plugin.mjs'], stimPlugin: { kinds: { project: 2 }, project: { id: 'fixture', name: 'Fixture', markers: [FIXTURE_MARKER], module: 'plugin.mjs', targets: { ios: {} }, outputs: [ { dir: 'target', beside: [FIXTURE_MARKER], deletedWhole: true, notCopied: 'Fixture refuses a target/ built in another checkout', ownedEntries: ['fixture-receipts'], keptFiles: ['fixture.lock'], unenteredDirs: ['fixture-locks'], notProfiles: ['fixture-swept'], useRecord: 'fixture-ledger.json', }, ], caches: [ { inHome: ['.cache', 'fixture'], label: "Fixture's shared cache", note: '(when no Fixture build runs)' }, ], headroomGb: { ios: 17 }, }, }, }), ); }); afterAll(() => { expect(existsSync(sentinel)).toBe(false); rmSync(shipped.dir, { recursive: true, force: true }); }); return { sentinel: () => sentinel }; } /** The source of the Exact plugin Stim ships, `packages/plugin-exact`. */ export const EXACT_PLUGIN_DIR: string = fileURLToPath(new URL('../../../plugin-exact', import.meta.url)); /** * Points `STIM_HOME` at a new home whose `plugins` turns on the shipped Exact plugin, for each test of the file that * calls it at its top level. */ export function useExactPlugin(): void { let home = ''; beforeEach(() => { home = mkdtempSync(join(tmpdir(), 'stim-exact-home-')); writeFileSync(join(home, 'config.json'), JSON.stringify({ plugins: ['exact'] })); process.env.STIM_HOME = home; }); afterEach(() => { delete process.env.STIM_HOME; rmSync(home, { recursive: true, force: true }); }); } /** A `../shipped-plugins.ts` mock that ships the fixture at `shipped.dir` and the real Exact plugin. */ export function shippedFixtureAndExact(shipped: { dir: string }): { shippedPluginDir(name: string): string | null; shippedPluginNames(): string[]; } { return { shippedPluginDir: (name) => (name === 'fixture' ? shipped.dir : name === 'exact' ? EXACT_PLUGIN_DIR : null), shippedPluginNames: () => ['exact', 'fixture'], }; }