|
| 1 | +import { execFileSync, spawnSync, type SpawnSyncReturns } from 'node:child_process'; |
| 2 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { join, resolve } from 'node:path'; |
| 5 | + |
| 6 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 7 | + |
| 8 | +const checkScript = resolve(import.meta.dirname, '../../../../scripts/check-nix-hash-fresh.mjs'); |
| 9 | +const tempRoots: string[] = []; |
| 10 | + |
| 11 | +afterEach(() => { |
| 12 | + for (const root of tempRoots.splice(0)) rmSync(root, { recursive: true, force: true }); |
| 13 | +}); |
| 14 | + |
| 15 | +describe('check-nix-hash-fresh', () => { |
| 16 | + it('uses current origin/main when a release branch tracks a stale upstream', () => { |
| 17 | + const root = makeRepository('changeset-release/main'); |
| 18 | + const staleRelease = revParse(root, 'HEAD'); |
| 19 | + setRemoteRef(root, 'changeset-release/main', staleRelease); |
| 20 | + |
| 21 | + git(root, ['switch', '-c', 'main']); |
| 22 | + writeFileSync(join(root, 'pnpm-lock.yaml'), 'lock from current main\n'); |
| 23 | + commit(root, 'update lock on main'); |
| 24 | + const currentMain = revParse(root, 'HEAD'); |
| 25 | + setRemoteRef(root, 'main', currentMain); |
| 26 | + |
| 27 | + git(root, ['switch', 'changeset-release/main']); |
| 28 | + git(root, ['reset', '--hard', currentMain]); |
| 29 | + writeFileSync(join(root, 'package.json'), '{"version":"1.0.1"}\n'); |
| 30 | + commit(root, 'version packages'); |
| 31 | + |
| 32 | + const result = runCheck(root); |
| 33 | + expect(result.status, result.stderr).toBe(0); |
| 34 | + }); |
| 35 | + |
| 36 | + it('keeps a lock-only branch change gated after that change reaches its upstream', () => { |
| 37 | + const root = makeRepository('feature'); |
| 38 | + const currentMain = revParse(root, 'HEAD'); |
| 39 | + setRemoteRef(root, 'main', currentMain); |
| 40 | + |
| 41 | + writeFileSync(join(root, 'pnpm-lock.yaml'), 'unmatched lock change\n'); |
| 42 | + commit(root, 'update lock without flake'); |
| 43 | + setRemoteRef(root, 'feature', revParse(root, 'HEAD')); |
| 44 | + writeFileSync(join(root, 'README.md'), 'follow-up\n'); |
| 45 | + commit(root, 'add follow-up'); |
| 46 | + |
| 47 | + const result = runCheck(root); |
| 48 | + expect(result.status, result.stderr).toBe(1); |
| 49 | + expect(result.stderr).toContain('pnpm-lock.yaml changed on this branch but flake.nix did not'); |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +function makeRepository(branch: string): string { |
| 54 | + const root = mkdtempSync(join(tmpdir(), 'pythinker-nix-hash-check-')); |
| 55 | + tempRoots.push(root); |
| 56 | + git(root, ['init', '--initial-branch', branch]); |
| 57 | + git(root, ['config', 'core.hooksPath', '.git/no-hooks']); |
| 58 | + git(root, ['config', 'user.name', 'Test User']); |
| 59 | + git(root, ['config', 'user.email', 'test@example.test']); |
| 60 | + git(root, ['config', 'commit.gpgSign', 'false']); |
| 61 | + git(root, ['remote', 'add', 'origin', join(root, 'unused-origin.git')]); |
| 62 | + writeFileSync(join(root, 'pnpm-lock.yaml'), 'initial lock\n'); |
| 63 | + writeFileSync(join(root, 'flake.nix'), 'initial flake\n'); |
| 64 | + commit(root, 'initial'); |
| 65 | + return root; |
| 66 | +} |
| 67 | + |
| 68 | +function commit(root: string, message: string): void { |
| 69 | + git(root, ['add', '.']); |
| 70 | + git(root, ['commit', '-m', message]); |
| 71 | +} |
| 72 | + |
| 73 | +function setRemoteRef(root: string, branch: string, sha: string): void { |
| 74 | + git(root, ['update-ref', `refs/remotes/origin/${branch}`, sha]); |
| 75 | + git(root, ['config', `branch.${branch}.remote`, 'origin']); |
| 76 | + git(root, ['config', `branch.${branch}.merge`, `refs/heads/${branch}`]); |
| 77 | +} |
| 78 | + |
| 79 | +function revParse(root: string, ref: string): string { |
| 80 | + return git(root, ['rev-parse', ref]); |
| 81 | +} |
| 82 | + |
| 83 | +function git(root: string, args: string[]): string { |
| 84 | + return execFileSync('git', args, { cwd: root, encoding: 'utf8' }).trim(); |
| 85 | +} |
| 86 | + |
| 87 | +function runCheck(root: string): SpawnSyncReturns<string> { |
| 88 | + return spawnSync(process.execPath, [checkScript], { cwd: root, encoding: 'utf8' }); |
| 89 | +} |
0 commit comments