|
| 1 | +/** |
| 2 | + * @file Verifies the packed npm artifact after installation from node_modules. |
| 3 | + * Source-level tests cover workspace modules directly, but this catches export, |
| 4 | + * prepack, and package-contents regressions that only appear in the published layout. |
| 5 | + */ |
| 6 | + |
| 7 | +import { execFile } from "node:child_process" |
| 8 | +import { mkdir, rm } from "node:fs/promises" |
| 9 | +import { tmpdir } from "node:os" |
| 10 | +import { dirname, join, resolve } from "node:path" |
| 11 | +import process from "node:process" |
| 12 | +import { fileURLToPath } from "node:url" |
| 13 | +import { promisify } from "node:util" |
| 14 | + |
| 15 | +const execFileAsync = promisify(execFile) |
| 16 | +const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..") |
| 17 | +const workspaceDir = await mkdirTemp() |
| 18 | +const installPrefix = join(workspaceDir, "project") |
| 19 | +const npmCacheDir = join(workspaceDir, "npm-cache") |
| 20 | + |
| 21 | +try { |
| 22 | + await mkdir(installPrefix, { recursive: true }) |
| 23 | + await mkdir(npmCacheDir, { recursive: true }) |
| 24 | + const tarballPath = await packTarball(workspaceDir) |
| 25 | + await installTarball(installPrefix, tarballPath, npmCacheDir) |
| 26 | + await runSmokeCheck(installPrefix) |
| 27 | +} finally { |
| 28 | + await rm(workspaceDir, { recursive: true, force: true }) |
| 29 | +} |
| 30 | + |
| 31 | +async function mkdirTemp() { |
| 32 | + const { mkdtemp } = await import("node:fs/promises") |
| 33 | + return mkdtemp(join(tmpdir(), "structview-npm-smoke-")) |
| 34 | +} |
| 35 | + |
| 36 | +async function packTarball(packDestination) { |
| 37 | + const { stdout } = await execFileAsync( |
| 38 | + "npm", |
| 39 | + ["pack", "--quiet", "--pack-destination", packDestination], |
| 40 | + { |
| 41 | + cwd: repoRoot, |
| 42 | + env: process.env, |
| 43 | + }, |
| 44 | + ) |
| 45 | + const tarballName = stdout.trim().split(/\r?\n/).at(-1) |
| 46 | + |
| 47 | + if (!tarballName) { |
| 48 | + throw new Error("npm pack did not report a tarball name") |
| 49 | + } |
| 50 | + |
| 51 | + return join(packDestination, tarballName) |
| 52 | +} |
| 53 | + |
| 54 | +async function installTarball(installPath, tarballPath, cachePath) { |
| 55 | + await execFileAsync( |
| 56 | + "npm", |
| 57 | + [ |
| 58 | + "install", |
| 59 | + "--ignore-scripts", |
| 60 | + "--no-package-lock", |
| 61 | + "--prefix", |
| 62 | + installPath, |
| 63 | + tarballPath, |
| 64 | + ], |
| 65 | + { |
| 66 | + cwd: repoRoot, |
| 67 | + env: { |
| 68 | + ...process.env, |
| 69 | + npm_config_cache: cachePath, |
| 70 | + }, |
| 71 | + }, |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +async function runSmokeCheck(installPath) { |
| 76 | + await execFileAsync( |
| 77 | + process.execPath, |
| 78 | + [ |
| 79 | + "--input-type=module", |
| 80 | + "-e", |
| 81 | + "import { defineStruct, u8 } from '@rotu/structview'; import { u16be } from '@rotu/structview/bigendian'; class Sample extends defineStruct({ value: u8(0), wide: u16be(1) }) {} const sample = new Sample(new Uint8Array(3)); sample.value = 7; sample.wide = 0x0102; if (sample.value !== 7 || sample.wide !== 0x0102) throw new Error('npm smoke test failed');", |
| 82 | + ], |
| 83 | + { |
| 84 | + cwd: installPath, |
| 85 | + env: process.env, |
| 86 | + }, |
| 87 | + ) |
| 88 | +} |
0 commit comments