|
| 1 | +import { mkdtempSync, writeFileSync } from 'node:fs' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import { join } from 'node:path' |
1 | 4 | import { describe, expect, it } from 'vitest' |
2 | 5 | import { buildPrepareScript } from '../../src/cli-helpers.ts' |
3 | 6 | import { editJsonProperty } from '../../src/core/package-json.ts' |
4 | 7 |
|
| 8 | +function makeTempCwd(hasSkilld: boolean): string { |
| 9 | + const dir = mkdtempSync(join(tmpdir(), 'prepare-hook-')) |
| 10 | + const pkg: Record<string, any> = { name: 'test-pkg' } |
| 11 | + if (hasSkilld) |
| 12 | + pkg.devDependencies = { skilld: '^1.0.0' } |
| 13 | + writeFileSync(join(dir, 'package.json'), JSON.stringify(pkg)) |
| 14 | + return dir |
| 15 | +} |
| 16 | + |
5 | 17 | describe('prepare hook script building', () => { |
6 | | - const buildPrepare = buildPrepareScript |
| 18 | + const cwdWithSkilld = makeTempCwd(true) |
| 19 | + const cwdWithout = makeTempCwd(false) |
7 | 20 | const standalone = 'skilld prepare || true' |
| 21 | + const npxStandalone = 'npx skilld prepare || true' |
| 22 | + |
| 23 | + it('uses skilld when installed as dependency', () => { |
| 24 | + expect(buildPrepareScript(undefined, cwdWithSkilld)).toBe(standalone) |
| 25 | + }) |
| 26 | + |
| 27 | + it('uses npx skilld when not installed as dependency', () => { |
| 28 | + expect(buildPrepareScript(undefined, cwdWithout)).toBe(npxStandalone) |
| 29 | + }) |
8 | 30 |
|
9 | 31 | it('returns standalone when no existing script', () => { |
10 | | - expect(buildPrepare(undefined)).toBe(standalone) |
| 32 | + expect(buildPrepareScript(undefined, cwdWithSkilld)).toBe(standalone) |
11 | 33 | }) |
12 | 34 |
|
13 | 35 | it('returns standalone when existing script is empty', () => { |
14 | | - expect(buildPrepare('')).toBe(standalone) |
15 | | - expect(buildPrepare(' ')).toBe(standalone) |
| 36 | + expect(buildPrepareScript('', cwdWithSkilld)).toBe(standalone) |
| 37 | + expect(buildPrepareScript(' ', cwdWithSkilld)).toBe(standalone) |
16 | 38 | }) |
17 | 39 |
|
18 | 40 | it('appends with && and parens to existing script', () => { |
19 | | - expect(buildPrepare('husky')).toBe('husky && (skilld prepare || true)') |
| 41 | + expect(buildPrepareScript('husky', cwdWithSkilld)).toBe('husky && (skilld prepare || true)') |
20 | 42 | }) |
21 | 43 |
|
22 | 44 | it('handles existing script with multiple commands', () => { |
23 | | - expect(buildPrepare('husky && lint-staged')).toBe('husky && lint-staged && (skilld prepare || true)') |
| 45 | + expect(buildPrepareScript('husky && lint-staged', cwdWithSkilld)).toBe('husky && lint-staged && (skilld prepare || true)') |
24 | 46 | }) |
25 | 47 |
|
26 | 48 | it('strips trailing && from existing script', () => { |
27 | | - expect(buildPrepare('husky &&')).toBe('husky && (skilld prepare || true)') |
28 | | - expect(buildPrepare('husky && ')).toBe('husky && (skilld prepare || true)') |
| 49 | + expect(buildPrepareScript('husky &&', cwdWithSkilld)).toBe('husky && (skilld prepare || true)') |
| 50 | + expect(buildPrepareScript('husky && ', cwdWithSkilld)).toBe('husky && (skilld prepare || true)') |
29 | 51 | }) |
30 | 52 |
|
31 | 53 | it('strips trailing ; from existing script', () => { |
32 | | - expect(buildPrepare('husky;')).toBe('husky && (skilld prepare || true)') |
| 54 | + expect(buildPrepareScript('husky;', cwdWithSkilld)).toBe('husky && (skilld prepare || true)') |
33 | 55 | }) |
34 | 56 |
|
35 | 57 | it('strips trailing || from existing script', () => { |
36 | | - expect(buildPrepare('husky ||')).toBe('husky && (skilld prepare || true)') |
| 58 | + expect(buildPrepareScript('husky ||', cwdWithSkilld)).toBe('husky && (skilld prepare || true)') |
37 | 59 | }) |
38 | 60 |
|
39 | 61 | it('handles only operators as existing script', () => { |
40 | | - expect(buildPrepare('&&')).toBe(standalone) |
41 | | - expect(buildPrepare(';')).toBe(standalone) |
| 62 | + expect(buildPrepareScript('&&', cwdWithSkilld)).toBe(standalone) |
| 63 | + expect(buildPrepareScript(';', cwdWithSkilld)).toBe(standalone) |
| 64 | + }) |
| 65 | + |
| 66 | + it('appends npx variant to existing script when not installed', () => { |
| 67 | + expect(buildPrepareScript('husky', cwdWithout)).toBe('husky && (npx skilld prepare || true)') |
42 | 68 | }) |
43 | 69 |
|
44 | 70 | describe('surgical package.json editing', () => { |
|
0 commit comments