38 lines
1.1 KiB
JavaScript
38 lines
1.1 KiB
JavaScript
import { execFileSync } from 'node:child_process'
|
|
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import { dirname, join } from 'node:path'
|
|
|
|
export function tempDir(prefix = 'dsh-harness-') {
|
|
return mkdtempSync(join(tmpdir(), prefix))
|
|
}
|
|
|
|
export function write(path, content) {
|
|
mkdirSync(dirname(path), { recursive: true })
|
|
writeFileSync(path, content, 'utf8')
|
|
}
|
|
|
|
export function git(cwd, args) {
|
|
return execFileSync('git', args, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim()
|
|
}
|
|
|
|
export function makeRepo() {
|
|
const repo = tempDir('dsh-harness-repo-')
|
|
try {
|
|
git(repo, ['init', '-b', 'main'])
|
|
} catch {
|
|
git(repo, ['init'])
|
|
git(repo, ['checkout', '-b', 'main'])
|
|
}
|
|
git(repo, ['config', 'user.email', 'dsh-harness@example.invalid'])
|
|
git(repo, ['config', 'user.name', 'DSH Harness Test'])
|
|
write(join(repo, 'README.md'), '# test\n')
|
|
git(repo, ['add', 'README.md'])
|
|
git(repo, ['commit', '-m', 'init'])
|
|
return repo
|
|
}
|
|
|
|
export function cleanup(path) {
|
|
rmSync(path, { recursive: true, force: true })
|
|
}
|