|
| 1 | +import assert from 'node:assert' |
| 2 | +import { describe, it } from 'node:test' |
| 3 | +import { mkdir, writeFile, readFile } from 'fs/promises' |
| 4 | +import { existsSync } from 'fs' |
| 5 | +import * as Path from 'path' |
| 6 | +import { exec } from 'dugite' |
| 7 | + |
| 8 | +import { |
| 9 | + readWorktreeIncludePatterns, |
| 10 | + getIgnoredFilesMatchingPatterns, |
| 11 | + copyWorktreeIncludeFiles, |
| 12 | +} from '../../../src/lib/git/worktree-include' |
| 13 | +import { createTempDirectory } from '../../helpers/temp' |
| 14 | +import { setupEmptyRepository } from '../../helpers/repositories' |
| 15 | + |
| 16 | +describe('git/worktree-include', () => { |
| 17 | + describe('readWorktreeIncludePatterns', () => { |
| 18 | + it('returns empty array when file does not exist', async t => { |
| 19 | + const dir = await createTempDirectory(t) |
| 20 | + const patterns = await readWorktreeIncludePatterns(dir) |
| 21 | + assert.deepStrictEqual(patterns, []) |
| 22 | + }) |
| 23 | + |
| 24 | + it('parses patterns from file', async t => { |
| 25 | + const dir = await createTempDirectory(t) |
| 26 | + await writeFile( |
| 27 | + Path.join(dir, '.worktreeinclude'), |
| 28 | + '.env\n.env.local\nconfig/secrets.json\n' |
| 29 | + ) |
| 30 | + const patterns = await readWorktreeIncludePatterns(dir) |
| 31 | + assert.deepStrictEqual(patterns, [ |
| 32 | + '.env', |
| 33 | + '.env.local', |
| 34 | + 'config/secrets.json', |
| 35 | + ]) |
| 36 | + }) |
| 37 | + |
| 38 | + it('skips blank lines and comments', async t => { |
| 39 | + const dir = await createTempDirectory(t) |
| 40 | + await writeFile( |
| 41 | + Path.join(dir, '.worktreeinclude'), |
| 42 | + '# This is a comment\n\n.env\n\n# Another comment\n.env.local\n' |
| 43 | + ) |
| 44 | + const patterns = await readWorktreeIncludePatterns(dir) |
| 45 | + assert.deepStrictEqual(patterns, ['.env', '.env.local']) |
| 46 | + }) |
| 47 | + |
| 48 | + it('returns empty array for a file with only comments and blanks', async t => { |
| 49 | + const dir = await createTempDirectory(t) |
| 50 | + await writeFile(Path.join(dir, '.worktreeinclude'), '# comment\n\n \n') |
| 51 | + const patterns = await readWorktreeIncludePatterns(dir) |
| 52 | + assert.deepStrictEqual(patterns, []) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + describe('getIgnoredFilesMatchingPatterns', () => { |
| 57 | + it('returns empty array when patterns is empty', async t => { |
| 58 | + const repo = await setupEmptyRepository(t) |
| 59 | + const files = await getIgnoredFilesMatchingPatterns(repo, []) |
| 60 | + assert.deepStrictEqual(files, []) |
| 61 | + }) |
| 62 | + |
| 63 | + it('returns gitignored files matching the patterns', async t => { |
| 64 | + const repo = await setupEmptyRepository(t) |
| 65 | + |
| 66 | + await exec(['config', 'user.email', 'test@example.com'], repo.path) |
| 67 | + await exec(['config', 'user.name', 'Test User'], repo.path) |
| 68 | + |
| 69 | + await writeFile(Path.join(repo.path, '.gitignore'), '.env\n') |
| 70 | + await exec(['add', '.gitignore'], repo.path) |
| 71 | + await exec(['commit', '-m', 'add gitignore'], repo.path) |
| 72 | + |
| 73 | + await writeFile(Path.join(repo.path, '.env'), 'SECRET=value\n') |
| 74 | + await writeFile(Path.join(repo.path, 'readme.txt'), 'hello\n') |
| 75 | + await exec(['add', 'readme.txt'], repo.path) |
| 76 | + |
| 77 | + const files = await getIgnoredFilesMatchingPatterns(repo, ['.env']) |
| 78 | + assert.deepStrictEqual(files, ['.env']) |
| 79 | + }) |
| 80 | + |
| 81 | + it('does not return tracked files even if pattern matches', async t => { |
| 82 | + const repo = await setupEmptyRepository(t) |
| 83 | + await exec(['config', 'user.email', 'test@example.com'], repo.path) |
| 84 | + await exec(['config', 'user.name', 'Test User'], repo.path) |
| 85 | + |
| 86 | + await writeFile(Path.join(repo.path, '.gitignore'), '') |
| 87 | + await writeFile(Path.join(repo.path, 'tracked.txt'), 'content\n') |
| 88 | + await exec(['add', 'tracked.txt', '.gitignore'], repo.path) |
| 89 | + await exec(['commit', '-m', 'initial'], repo.path) |
| 90 | + |
| 91 | + const files = await getIgnoredFilesMatchingPatterns(repo, ['tracked.txt']) |
| 92 | + assert.deepStrictEqual(files, []) |
| 93 | + }) |
| 94 | + |
| 95 | + it('does not return gitignored files that do not match the pattern', async t => { |
| 96 | + const repo = await setupEmptyRepository(t) |
| 97 | + await exec(['config', 'user.email', 'test@example.com'], repo.path) |
| 98 | + await exec(['config', 'user.name', 'Test User'], repo.path) |
| 99 | + |
| 100 | + await writeFile( |
| 101 | + Path.join(repo.path, '.gitignore'), |
| 102 | + '.env\nsecrets.json\n' |
| 103 | + ) |
| 104 | + await exec(['add', '.gitignore'], repo.path) |
| 105 | + await exec(['commit', '-m', 'gitignore'], repo.path) |
| 106 | + |
| 107 | + await writeFile(Path.join(repo.path, '.env'), 'SECRET=1\n') |
| 108 | + await writeFile(Path.join(repo.path, 'secrets.json'), '{}') |
| 109 | + |
| 110 | + const files = await getIgnoredFilesMatchingPatterns(repo, ['.env']) |
| 111 | + assert.deepStrictEqual(files, ['.env']) |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + describe('copyWorktreeIncludeFiles', () => { |
| 116 | + it('copies files preserving directory structure', async t => { |
| 117 | + const source = await createTempDirectory(t) |
| 118 | + const dest = await createTempDirectory(t) |
| 119 | + |
| 120 | + await mkdir(Path.join(source, 'config'), { recursive: true }) |
| 121 | + await writeFile(Path.join(source, '.env'), 'SECRET=1\n') |
| 122 | + await writeFile(Path.join(source, 'config', 'secrets.json'), '{}') |
| 123 | + |
| 124 | + await copyWorktreeIncludeFiles(source, dest, [ |
| 125 | + '.env', |
| 126 | + 'config/secrets.json', |
| 127 | + ]) |
| 128 | + |
| 129 | + const envContent = await readFile(Path.join(dest, '.env'), 'utf8') |
| 130 | + assert.strictEqual(envContent, 'SECRET=1\n') |
| 131 | + |
| 132 | + const secretsContent = await readFile( |
| 133 | + Path.join(dest, 'config', 'secrets.json'), |
| 134 | + 'utf8' |
| 135 | + ) |
| 136 | + assert.strictEqual(secretsContent, '{}') |
| 137 | + }) |
| 138 | + |
| 139 | + it('skips files that do not exist at the source', async t => { |
| 140 | + const source = await createTempDirectory(t) |
| 141 | + const dest = await createTempDirectory(t) |
| 142 | + |
| 143 | + await writeFile(Path.join(source, '.env'), 'SECRET=1\n') |
| 144 | + |
| 145 | + await copyWorktreeIncludeFiles(source, dest, ['.env', 'missing.txt']) |
| 146 | + |
| 147 | + assert.ok(existsSync(Path.join(dest, '.env'))) |
| 148 | + assert.ok(!existsSync(Path.join(dest, 'missing.txt'))) |
| 149 | + }) |
| 150 | + |
| 151 | + it('does not copy files with path traversal patterns', async t => { |
| 152 | + const source = await createTempDirectory(t) |
| 153 | + const dest = await createTempDirectory(t) |
| 154 | + |
| 155 | + await writeFile(Path.join(source, '.env'), 'SECRET=1\n') |
| 156 | + |
| 157 | + await copyWorktreeIncludeFiles(source, dest, ['../../../etc/passwd']) |
| 158 | + |
| 159 | + const destContents = await import('fs/promises').then(fs => |
| 160 | + fs.readdir(dest) |
| 161 | + ) |
| 162 | + assert.strictEqual(destContents.length, 0) |
| 163 | + }) |
| 164 | + }) |
| 165 | +}) |
0 commit comments