feat(FN-4222): complete Step 2 — extend git ops for finalize

Fusion-Task-Id: FN-4222
Fusion-Task-Lineage: 7de6b18c-36d5-49e8-bbd7-084707e88d6a
This commit is contained in:
Fusion
2026-05-14 03:30:44 -07:00
committed by gsxdsm
parent 7ac32a5a6c
commit b8c5cf4f72
2 changed files with 122 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { execSync } from "node:child_process";
import { describe, expect, it } from "vitest";
import { defaultGitOps } from "../experiment/git-ops.js";
function createRepo() {
const dir = mkdtempSync(join(tmpdir(), "finalize-git-ops-"));
execSync("git init", { cwd: dir });
execSync("git config user.email test@example.com", { cwd: dir });
execSync("git config user.name Test", { cwd: dir });
writeFileSync(join(dir, "file.txt"), "one\n");
execSync("git add file.txt", { cwd: dir });
execSync('git commit -m "first"', { cwd: dir });
writeFileSync(join(dir, "file.txt"), "two\n");
execSync("git add file.txt", { cwd: dir });
execSync('git commit -m "second"', { cwd: dir });
return dir;
}
describe.skipIf(process.platform === "win32")("finalize git ops", () => {
it("supports mergeBase/branch/checkout/currentBranch/deleteBranch/cherryPick", async () => {
const cwd = createRepo();
try {
const git = defaultGitOps(cwd);
const head = await git.head();
const initialBranch = (await git.currentBranch()) ?? "main";
await git.createBranch("feature/a", head);
expect(await git.branchExists("feature/a")).toBe(true);
await git.checkout("feature/a");
expect(await git.currentBranch()).toBe("feature/a");
writeFileSync(join(cwd, "file.txt"), "feature\n");
await git.add(["file.txt"]);
const featureCommit = await git.commit("feature change");
await git.checkout(initialBranch);
writeFileSync(join(cwd, "main.txt"), "main\n");
await git.add(["main.txt"]);
await git.commit("main change");
const mergeBase = await git.mergeBase(initialBranch, "feature/a");
expect(mergeBase).toBe(head);
await git.checkout(initialBranch);
await git.cherryPick(featureCommit);
expect((await git.statusPorcelain()).trim()).toBe("");
await git.deleteBranch("feature/a", { force: true });
expect(await git.branchExists("feature/a")).toBe(false);
} finally {
rmSync(cwd, { recursive: true, force: true });
}
});
});