/** * KHAELOR * File: tests/repository/fixtures.ts * Description: Test fixtures — temp dirs and real `git init` repositories for repository-module tests. * * Author: Simon-Pierre Boucher * Contact: contact@spboucher.ai */ import { execFileSync } from "node:child_process"; import { mkdtempSync, mkdirSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import * as path from "node:path"; /** Run a real git command in a fixture repo with fully-isolated identity/config. */ export function runGit(dir: string, args: string[]): string { return execFileSync("git", args, { cwd: dir, encoding: "utf8", env: { ...process.env, GIT_CONFIG_NOSYSTEM: "1", GIT_CONFIG_GLOBAL: "/dev/null", GIT_AUTHOR_NAME: "KHAELOR Test", GIT_AUTHOR_EMAIL: "test@spboucher.ai", GIT_COMMITTER_NAME: "KHAELOR Test", GIT_COMMITTER_EMAIL: "test@spboucher.ai", }, }); } /** Create a fresh temp dir (caller removes it in afterEach). */ export function makeTempDir(prefix: string): string { return mkdtempSync(path.join(tmpdir(), prefix)); } /** `git init -b main` in dir. */ export function initRepo(dir: string): void { runGit(dir, ["init", "-qb", "main"]); } /** Stage everything and commit. */ export function commitAll(dir: string, message: string): void { runGit(dir, ["add", "-A"]); runGit(dir, ["commit", "-qm", message, "--no-gpg-sign"]); } /** Write a file, creating parent directories. */ export function writeFixtureFile(dir: string, rel: string, content: string | Buffer): void { const abs = path.join(dir, rel); mkdirSync(path.dirname(abs), { recursive: true }); writeFileSync(abs, content); }