|
| 1 | +// Copyright (c) 2026 ObjectStack contributors. Apache-2.0 license. |
| 2 | +// |
| 3 | +// Pins #10322: the printed "Next steps" (and the install-failure remedy) must |
| 4 | +// name the SAME package manager the run actually detected — never a |
| 5 | +// hardcoded `npm` regardless of what ran. Before this fix, a newcomer whose |
| 6 | +// install ran with `pnpm` (confirmed empirically: this scaffolder prefers |
| 7 | +// pnpm and only falls back to npm when pnpm is unreachable — see |
| 8 | +// `detectPackageManager()`) was told to run `npm run dev` / `npm run |
| 9 | +// validate` afterwards — the third of the "three different answers" #10322 |
| 10 | +// measured. `packages/cli/src/commands/init.ts`'s own "Next steps" already |
| 11 | +// threads its detected `chosenPm` through; this file is the same contract for |
| 12 | +// `create-objectstack`. |
| 13 | +// |
| 14 | +// `index.ts` calls `program.parse()` at import time, so it cannot be |
| 15 | +// unit-tested directly — this exercises the real CLI end to end via `tsx`, |
| 16 | +// the same no-build subprocess pattern `scaffold-description.test.ts` uses. |
| 17 | +// `--skip-install` keeps every run here fast and offline: `detectPackageManager()` |
| 18 | +// is a read-only `<pm> --version` probe (see index.ts), so its result — and |
| 19 | +// therefore what "Next steps" prints — does not depend on an install actually |
| 20 | +// following it. The *real* install path (both the pnpm and npm-fallback |
| 21 | +// cases) was additionally verified by hand against the built CLI; see this |
| 22 | +// issue's PR body for the transcripts. |
| 23 | +// |
| 24 | +// Both branches of the detector are exercised by controlling PATH: |
| 25 | +// - pnpm reachable -> "pnpm run dev" / "pnpm run validate" |
| 26 | +// - pnpm unreachable -> "npm run dev" / "npm run validate" (the fallback |
| 27 | +// this scaffolder has always had for machines without pnpm) |
| 28 | +// |
| 29 | +// The "no bare npm when pnpm ran" assertion is deliberately a WORD-BOUNDARY |
| 30 | +// match, not a substring one: the literal text "pnpm run" itself contains the |
| 31 | +// substring "npm run" (p-N-P-M-space-r-u-n has "npm run" starting at its |
| 32 | +// second character), so a naive `.not.toContain('npm run')` would fail |
| 33 | +// against correct pnpm output. |
| 34 | + |
| 35 | +import { describe, it, expect } from 'vitest'; |
| 36 | +import { execFileSync } from 'node:child_process'; |
| 37 | +import fs from 'node:fs'; |
| 38 | +import os from 'node:os'; |
| 39 | +import path from 'node:path'; |
| 40 | +import { fileURLToPath } from 'node:url'; |
| 41 | + |
| 42 | +const HERE = path.dirname(fileURLToPath(import.meta.url)); |
| 43 | +const PKG_ROOT = path.resolve(HERE, '..'); |
| 44 | +const REPO_ROOT = path.resolve(PKG_ROOT, '..', '..'); |
| 45 | +const TSX = path.join(REPO_ROOT, 'node_modules', '.bin', 'tsx'); |
| 46 | +const INDEX_TS = path.join(PKG_ROOT, 'src', 'index.ts'); |
| 47 | + |
| 48 | +function which(cmd: string): string { |
| 49 | + return execFileSync('sh', ['-c', `command -v ${cmd}`], { encoding: 'utf8' }).trim(); |
| 50 | +} |
| 51 | + |
| 52 | +/** A PATH entry with `node` + `npm` reachable and `pnpm` deliberately absent. */ |
| 53 | +function makePnpmlessBin(): string { |
| 54 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'create-objectstack-nopnpm-bin-')); |
| 55 | + fs.symlinkSync(which('node'), path.join(dir, 'node')); |
| 56 | + fs.symlinkSync(which('npm'), path.join(dir, 'npm')); |
| 57 | + return dir; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * The "Next steps:" block of a run's stdout — deliberately narrower than the |
| 62 | + * whole transcript. The "Created files" listing above it names |
| 63 | + * `pnpm-workspace.yaml` regardless of which package manager ran the install |
| 64 | + * (it is a static template file, not install output), so a bare |
| 65 | + * `stdout.not.toMatch(/pnpm/)` would false-positive on that filename in the |
| 66 | + * npm-fallback case. What actually matters is what the run tells the reader |
| 67 | + * to type next. |
| 68 | + */ |
| 69 | +function nextStepsSection(stdout: string): string { |
| 70 | + return stdout.split('Next steps:')[1] ?? ''; |
| 71 | +} |
| 72 | + |
| 73 | +/** Run the real CLI with --skip-install --skip-skills and return its stdout. */ |
| 74 | +function runScaffold(env: NodeJS.ProcessEnv): string { |
| 75 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'create-objectstack-nextsteps-')); |
| 76 | + try { |
| 77 | + return execFileSync( |
| 78 | + TSX, |
| 79 | + [INDEX_TS, 'my-app', '--template', 'blank', '--skip-install', '--skip-skills'], |
| 80 | + { cwd: tmp, env, encoding: 'utf8' }, |
| 81 | + ); |
| 82 | + } finally { |
| 83 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +describe('scaffolder "Next steps" names the package manager it actually detected (#10322)', () => { |
| 88 | + it('with pnpm on PATH: prints pnpm consistently, never bare npm', () => { |
| 89 | + // Sanity: this container really does have pnpm reachable, or the |
| 90 | + // "consistently pnpm" assertion below would be vacuous. |
| 91 | + expect(() => which('pnpm')).not.toThrow(); |
| 92 | + |
| 93 | + const nextSteps = nextStepsSection(runScaffold(process.env)); |
| 94 | + expect(nextSteps).toMatch(/\bpnpm run dev\b/); |
| 95 | + expect(nextSteps).toMatch(/\bpnpm run validate\b/); |
| 96 | + expect(nextSteps).not.toMatch(/\bnpm run\b/); |
| 97 | + expect(nextSteps).not.toMatch(/\bnpm install\b/); |
| 98 | + }, 20_000); |
| 99 | + |
| 100 | + it('with pnpm unreachable: falls back to npm — consistently, not a stale pnpm mention', () => { |
| 101 | + const bin = makePnpmlessBin(); |
| 102 | + try { |
| 103 | + // Sanity: the fake PATH really does hide pnpm (and really does still |
| 104 | + // expose node/npm — otherwise tsx itself could not launch). |
| 105 | + expect(() => |
| 106 | + execFileSync('sh', ['-c', 'command -v pnpm'], { |
| 107 | + env: { ...process.env, PATH: bin }, |
| 108 | + }), |
| 109 | + ).toThrow(); |
| 110 | + |
| 111 | + const nextSteps = nextStepsSection( |
| 112 | + runScaffold({ ...process.env, PATH: `${bin}:/usr/bin:/bin` }), |
| 113 | + ); |
| 114 | + expect(nextSteps).toMatch(/\bnpm run dev\b/); |
| 115 | + expect(nextSteps).toMatch(/\bnpm run validate\b/); |
| 116 | + expect(nextSteps).not.toMatch(/pnpm/); |
| 117 | + } finally { |
| 118 | + fs.rmSync(bin, { recursive: true, force: true }); |
| 119 | + } |
| 120 | + }, 20_000); |
| 121 | + |
| 122 | + it('both branches still name the unskippable validate step (#10322 pt. 3)', () => { |
| 123 | + expect(runScaffold(process.env)).toMatch(/run validate/); |
| 124 | + }, 20_000); |
| 125 | + |
| 126 | + it('the install-failure remedy also names the detected package manager, not a hardcoded npm', () => { |
| 127 | + const source = fs.readFileSync(INDEX_TS, 'utf8'); |
| 128 | + expect(source).toMatch(/Dependency installation failed\. Run .*\$\{pm\} install.* manually\./); |
| 129 | + expect(source).not.toMatch(/Run `npm install` manually/); |
| 130 | + }); |
| 131 | +}); |
0 commit comments