|
| 1 | +import { spawnSync } from 'node:child_process'; |
| 2 | +import { promises as fs } from 'node:fs'; |
| 3 | +import { mkdtemp, rm } from 'node:fs/promises'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { join } from 'node:path'; |
| 6 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 7 | +import { createWorktree, removeWorktree } from './index.js'; |
| 8 | + |
| 9 | +async function makeRepo(): Promise<string> { |
| 10 | + const dir = await mkdtemp(join(tmpdir(), 'dc-wt-src-')); |
| 11 | + spawnSync('git', ['init', '-q', '-b', 'main'], { cwd: dir }); |
| 12 | + spawnSync('git', ['config', 'user.email', 't@t'], { cwd: dir }); |
| 13 | + spawnSync('git', ['config', 'user.name', 't'], { cwd: dir }); |
| 14 | + await fs.writeFile(join(dir, 'a.txt'), 'A'); |
| 15 | + spawnSync('git', ['add', '.'], { cwd: dir }); |
| 16 | + spawnSync('git', ['commit', '-q', '-m', 'init'], { cwd: dir }); |
| 17 | + return dir; |
| 18 | +} |
| 19 | + |
| 20 | +describe('createWorktree / removeWorktree', () => { |
| 21 | + let src: string; |
| 22 | + let parent: string; |
| 23 | + |
| 24 | + beforeEach(async () => { |
| 25 | + src = await makeRepo(); |
| 26 | + parent = await mkdtemp(join(tmpdir(), 'dc-wt-parent-')); |
| 27 | + }); |
| 28 | + afterEach(async () => { |
| 29 | + await rm(src, { recursive: true, force: true }); |
| 30 | + await rm(parent, { recursive: true, force: true }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('creates a worktree and removes it cleanly', async () => { |
| 34 | + const h = await createWorktree({ source: src, parentDir: parent }); |
| 35 | + expect(h.path).toContain(parent); |
| 36 | + expect(h.branch).toMatch(/^dc\//); |
| 37 | + // File is present in worktree |
| 38 | + expect(await fs.readFile(join(h.path, 'a.txt'), 'utf8')).toBe('A'); |
| 39 | + await removeWorktree(h); |
| 40 | + await expect(fs.access(h.path)).rejects.toThrow(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('honors baseRef from config', async () => { |
| 44 | + // Make a second commit, then branch from the FIRST. |
| 45 | + spawnSync('git', ['-C', src, 'tag', 'v0']); |
| 46 | + await fs.writeFile(join(src, 'b.txt'), 'B'); |
| 47 | + spawnSync('git', ['-C', src, 'add', '.'], {}); |
| 48 | + spawnSync('git', ['-C', src, 'commit', '-q', '-m', 'second'], {}); |
| 49 | + const h = await createWorktree({ |
| 50 | + source: src, |
| 51 | + parentDir: parent, |
| 52 | + config: { baseRef: 'v0' }, |
| 53 | + }); |
| 54 | + try { |
| 55 | + // b.txt should NOT exist at the tag-pinned worktree |
| 56 | + await expect(fs.access(join(h.path, 'b.txt'))).rejects.toThrow(); |
| 57 | + } finally { |
| 58 | + await removeWorktree(h); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + it('creates symlinks for symlinkDirectories', async () => { |
| 63 | + await fs.mkdir(join(src, 'node_modules')); |
| 64 | + await fs.writeFile(join(src, 'node_modules', 'pkg.txt'), 'real'); |
| 65 | + const h = await createWorktree({ |
| 66 | + source: src, |
| 67 | + parentDir: parent, |
| 68 | + config: { symlinkDirectories: ['node_modules'] }, |
| 69 | + }); |
| 70 | + try { |
| 71 | + const stat = await fs.lstat(join(h.path, 'node_modules')); |
| 72 | + expect(stat.isSymbolicLink()).toBe(true); |
| 73 | + } finally { |
| 74 | + await removeWorktree(h); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + it('errors when source is not a git repo', async () => { |
| 79 | + const notARepo = await mkdtemp(join(tmpdir(), 'dc-not-repo-')); |
| 80 | + try { |
| 81 | + await expect( |
| 82 | + createWorktree({ source: notARepo, parentDir: parent }), |
| 83 | + ).rejects.toThrow(/not a git repository/); |
| 84 | + } finally { |
| 85 | + await rm(notARepo, { recursive: true, force: true }); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + it('removeWorktree is idempotent (path already gone)', async () => { |
| 90 | + await removeWorktree({ path: join(parent, 'nope'), branch: 'x', source: src }); |
| 91 | + // should not throw |
| 92 | + }); |
| 93 | +}); |
0 commit comments