|
| 1 | +import { spawnSync } from 'node:child_process' |
| 2 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import { dirname, join } from 'node:path' |
| 5 | +import { fileURLToPath } from 'node:url' |
| 6 | +import { afterEach, describe, expect, it } from 'vitest' |
| 7 | + |
| 8 | +const script = fileURLToPath(new URL('../.github/scripts/desktop-live-changes.sh', import.meta.url)) |
| 9 | +const fixtures: string[] = [] |
| 10 | + |
| 11 | +function git(cwd: string, ...args: string[]) { |
| 12 | + const result = spawnSync('git', args, { cwd, encoding: 'utf8' }) |
| 13 | + if (result.status !== 0) throw new Error(`git ${args.join(' ')}: ${result.stderr}`) |
| 14 | + return result.stdout.trim() |
| 15 | +} |
| 16 | + |
| 17 | +function write(repo: string, file: string, content: string) { |
| 18 | + mkdirSync(dirname(join(repo, file)), { recursive: true }) |
| 19 | + writeFileSync(join(repo, file), content) |
| 20 | +} |
| 21 | + |
| 22 | +/** Builds a clone whose base commit holds `apps/sim/x.ts` and whose origin is a local bare repo. */ |
| 23 | +function fixture() { |
| 24 | + const root = mkdtempSync(join(tmpdir(), 'desktop-live-changes-')) |
| 25 | + fixtures.push(root) |
| 26 | + const origin = join(root, 'origin.git') |
| 27 | + const repo = join(root, 'repo') |
| 28 | + git(root, 'init', '--quiet', '--bare', origin) |
| 29 | + git(root, 'init', '--quiet', repo) |
| 30 | + git(repo, 'config', 'user.email', 'test@example.com') |
| 31 | + git(repo, 'config', 'user.name', 'Test') |
| 32 | + git(repo, 'config', 'commit.gpgsign', 'false') |
| 33 | + git(repo, 'remote', 'add', 'origin', origin) |
| 34 | + write(repo, 'apps/sim/x.ts', 'export const x = 1\n'.repeat(20)) |
| 35 | + git(repo, 'add', '-A') |
| 36 | + git(repo, 'commit', '--quiet', '-m', 'base') |
| 37 | + const base = git(repo, 'rev-parse', 'HEAD') |
| 38 | + git(repo, 'push', '--quiet', 'origin', 'HEAD:refs/heads/base') |
| 39 | + return { repo, base } |
| 40 | +} |
| 41 | + |
| 42 | +function detect(repo: string, base: string) { |
| 43 | + return spawnSync('bash', [script, base], { cwd: repo, encoding: 'utf8' }) |
| 44 | +} |
| 45 | + |
| 46 | +afterEach(() => { |
| 47 | + for (const root of fixtures.splice(0)) rmSync(root, { recursive: true, force: true }) |
| 48 | +}) |
| 49 | + |
| 50 | +describe('desktop live change gate', () => { |
| 51 | + it('runs the suite when a live file moves into a skipped path', () => { |
| 52 | + const { repo, base } = fixture() |
| 53 | + mkdirSync(join(repo, 'apps/docs'), { recursive: true }) |
| 54 | + git(repo, 'mv', 'apps/sim/x.ts', 'apps/docs/x.ts') |
| 55 | + git(repo, 'commit', '--quiet', '-m', 'move') |
| 56 | + |
| 57 | + const result = detect(repo, base) |
| 58 | + expect(result.status, result.stderr).toBe(0) |
| 59 | + expect(result.stdout.trim()).toBe('changed=true') |
| 60 | + }) |
| 61 | + |
| 62 | + it('skips the suite when every change is in a skipped path', () => { |
| 63 | + const { repo, base } = fixture() |
| 64 | + write(repo, 'apps/docs/guide.mdx', '# Guide\n') |
| 65 | + git(repo, 'add', '-A') |
| 66 | + git(repo, 'commit', '--quiet', '-m', 'docs') |
| 67 | + |
| 68 | + const result = detect(repo, base) |
| 69 | + expect(result.status, result.stderr).toBe(0) |
| 70 | + expect(result.stdout.trim()).toBe('changed=false') |
| 71 | + }) |
| 72 | + |
| 73 | + it('runs the suite when the base commit cannot be fetched', () => { |
| 74 | + const { repo } = fixture() |
| 75 | + const result = detect(repo, '0'.repeat(40)) |
| 76 | + expect(result.stdout.trim()).toBe('changed=true') |
| 77 | + }) |
| 78 | +}) |
0 commit comments