@@ -9,6 +9,7 @@ import os from 'node:os';
99import path from 'node:path' ;
1010import { test } from 'node:test' ;
1111import { runCmdSync } from '../../src/utils/exec.ts' ;
12+ import { STALE_NODE_MODULES_MESSAGE } from '../../src/utils/lockfile-install-sync.ts' ;
1213import { CHECK_CATALOG } from './checks.ts' ;
1314import { DEFAULT_VITEST_MAX_WORKERS } from '../lib/vitest-concurrency.ts' ;
1415import { selectChecks } from './model.ts' ;
@@ -247,3 +248,107 @@ test('runChecks leaves coverage to CI and runs capped related tests once', async
247248 'the coverage gate is GitHub-authoritative and must not run locally' ,
248249 ) ;
249250} ) ;
251+
252+ test ( 'runChecks fails fast on a stale install before running format or any other check' , async ( ) => {
253+ const executed : string [ ] [ ] = [ ] ;
254+ const execute : CommandExecutor = async ( command ) => {
255+ executed . push ( command ) ;
256+ return 0 ;
257+ } ;
258+ const plan = selectChecks ( {
259+ changedFiles : [ 'packages/selectors/src/index.ts' ] ,
260+ packageEntryFiles : [ ] ,
261+ } ) ;
262+ const code = await runChecks ( plan , { scripts : ALL_SCRIPTS } , ARGS , {
263+ execute,
264+ cwd : '.' ,
265+ checkLockfileSync : ( ) => ( { inSync : false , reason : 'stale' } ) ,
266+ } ) ;
267+ assert . equal ( code , 1 ) ;
268+ assert . deepEqual ( executed , [ ] , 'no check — including format — may run against a stale install' ) ;
269+ } ) ;
270+
271+ test ( 'runChecks fails fast when node_modules was never installed in this checkout' , async ( ) => {
272+ const executed : string [ ] [ ] = [ ] ;
273+ const execute : CommandExecutor = async ( command ) => {
274+ executed . push ( command ) ;
275+ return 0 ;
276+ } ;
277+ const plan = selectChecks ( {
278+ changedFiles : [ 'packages/selectors/src/index.ts' ] ,
279+ packageEntryFiles : [ ] ,
280+ } ) ;
281+ const code = await runChecks ( plan , { scripts : ALL_SCRIPTS } , ARGS , {
282+ execute,
283+ cwd : '.' ,
284+ checkLockfileSync : ( ) => ( { inSync : false , reason : 'install-missing' } ) ,
285+ } ) ;
286+ assert . equal ( code , 1 ) ;
287+ assert . deepEqual ( executed , [ ] ) ;
288+ } ) ;
289+
290+ test ( 'runChecks does not block on a missing lockfile — that is a different failure than a stale install' , async ( ) => {
291+ const executed : string [ ] [ ] = [ ] ;
292+ const execute : CommandExecutor = async ( command ) => {
293+ executed . push ( command ) ;
294+ return 0 ;
295+ } ;
296+ const plan = selectChecks ( {
297+ changedFiles : [ 'packages/selectors/src/index.ts' ] ,
298+ packageEntryFiles : [ ] ,
299+ } ) ;
300+ const code = await runChecks ( plan , { scripts : ALL_SCRIPTS } , ARGS , {
301+ execute,
302+ cwd : '.' ,
303+ checkLockfileSync : ( ) => ( { inSync : false , reason : 'lockfile-missing' } ) ,
304+ } ) ;
305+ assert . equal ( code , 0 ) ;
306+ assert . ok ( executed . length > 0 , 'checks still run when there is nothing to compare against' ) ;
307+ } ) ;
308+
309+ test ( 'runChecks proceeds normally when the install is in sync' , async ( ) => {
310+ const executed : string [ ] [ ] = [ ] ;
311+ const execute : CommandExecutor = async ( command ) => {
312+ executed . push ( command ) ;
313+ return 0 ;
314+ } ;
315+ const plan = selectChecks ( {
316+ changedFiles : [ 'packages/selectors/src/index.ts' ] ,
317+ packageEntryFiles : [ ] ,
318+ } ) ;
319+ const code = await runChecks ( plan , { scripts : ALL_SCRIPTS } , ARGS , {
320+ execute,
321+ cwd : '.' ,
322+ checkLockfileSync : ( ) => ( { inSync : true } ) ,
323+ } ) ;
324+ assert . equal ( code , 0 ) ;
325+ assert . ok ( executed . some ( ( command ) => command . includes ( 'format:check' ) ) ) ;
326+ } ) ;
327+
328+ test ( 'runChecks names the real cause on stderr instead of surfacing as an unrelated failure' , async ( ) => {
329+ const stderrChunks : string [ ] = [ ] ;
330+ const originalWrite = process . stderr . write . bind ( process . stderr ) ;
331+ process . stderr . write = ( ( chunk : string | Uint8Array ) => {
332+ stderrChunks . push ( chunk . toString ( ) ) ;
333+ return true ;
334+ } ) as typeof process . stderr . write ;
335+
336+ try {
337+ const plan = selectChecks ( {
338+ changedFiles : [ 'packages/selectors/src/index.ts' ] ,
339+ packageEntryFiles : [ ] ,
340+ } ) ;
341+ const code = await runChecks ( plan , { scripts : ALL_SCRIPTS } , ARGS , {
342+ execute : async ( ) => 0 ,
343+ cwd : '.' ,
344+ checkLockfileSync : ( ) => ( { inSync : false , reason : 'stale' } ) ,
345+ } ) ;
346+ assert . equal ( code , 1 ) ;
347+ assert . ok (
348+ stderrChunks . some ( ( chunk ) => chunk . includes ( STALE_NODE_MODULES_MESSAGE ) ) ,
349+ `expected stderr to name the stale-install cause, got: ${ stderrChunks . join ( '' ) } ` ,
350+ ) ;
351+ } finally {
352+ process . stderr . write = originalWrite ;
353+ }
354+ } ) ;
0 commit comments