import { test } from 'node:test'; import assert from 'node:assert/strict'; import { execFileSync } from 'node:child_process'; import { mkdtempSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { checkout, type MrInfo } from '../src/gitlab.ts'; const git = (cwd: string, ...args: string[]) => execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', ...args], { cwd, encoding: 'utf8' }).trim(); test('an empty checkout folder inside another repository never touches that repository', async () => { delete process.env.GITLAB_TOKEN; const root = mkdtempSync(path.join(os.tmpdir(), 'labuh-co-')); // The reviewed project, with the MR head under GitLab's ref name. const src = path.join(root, 'src'); mkdirSync(src); git(src, 'init', '-q'); writeFileSync(path.join(src, 'app.txt'), 'reviewed\n'); git(src, 'add', '.'); git(src, 'commit', '-qm', 'mr'); const sha = git(src, 'rev-parse', 'HEAD'); git(src, 'update-ref', 'refs/merge-requests/7/head', sha); // LABUH_HOME is itself a repository, and the work folder already has an empty repo/. const home = path.join(root, 'home'); mkdirSync(home); git(home, 'init', '-q'); writeFileSync(path.join(home, 'own.txt'), 'labuh\n'); git(home, 'add', '.'); git(home, 'commit', '-qm', 'home'); const homeHead = git(home, 'rev-parse', 'HEAD'); const dir = path.join(home, '.work', 'p-7', 'repo'); mkdirSync(dir, { recursive: true }); await checkout({ iid: 7, sha, sshUrl: src, httpUrl: src } as MrInfo, dir); assert.equal(git(dir, 'rev-parse', '--show-toplevel'), realpathSync(dir), 'the checkout is its own repository'); assert.equal(git(dir, 'rev-parse', 'HEAD'), sha); assert.equal(readFileSync(path.join(dir, 'app.txt'), 'utf8'), 'reviewed\n'); assert.equal(git(home, 'rev-parse', 'HEAD'), homeHead, 'the outer repository keeps its HEAD'); assert.equal(git(home, 'remote'), '', 'the outer repository gets no remote'); assert.equal(readFileSync(path.join(home, 'own.txt'), 'utf8'), 'labuh\n'); });