91 lines
4.1 KiB
TypeScript
91 lines
4.1 KiB
TypeScript
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
import { mergeTargetIntoBranch } from "../src/ci_sync";
|
|
|
|
/**
|
|
* 分支保鲜对着真 git 跑:一个 bare 远端 + 一份「节点检出」+ 一份「别人」的检出。
|
|
* 关键是节点检出的工作区全程不动(agent 可能正在里面干活)。
|
|
*/
|
|
let root = "";
|
|
const git = (cwd: string, ...args: string[]) => {
|
|
const r = Bun.spawnSync(["git", ...args], {
|
|
cwd,
|
|
env: { ...process.env, GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "t@x", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "t@x" },
|
|
});
|
|
if (r.exitCode !== 0) throw new Error(`git ${args.join(" ")}: ${r.stderr.toString()}`);
|
|
return r.stdout.toString().trim();
|
|
};
|
|
let remote = "", node = "", other = "";
|
|
|
|
beforeAll(() => {
|
|
root = mkdtempSync(path.join(tmpdir(), "ci-sync-"));
|
|
remote = path.join(root, "remote.git");
|
|
node = path.join(root, "node");
|
|
other = path.join(root, "other");
|
|
git(root, "init", "--bare", "-b", "develop", remote);
|
|
git(root, "clone", remote, other);
|
|
writeFileSync(path.join(other, "a.txt"), "a\n");
|
|
writeFileSync(path.join(other, "b.txt"), "b\n");
|
|
git(other, "add", ".");
|
|
git(other, "commit", "-m", "base");
|
|
git(other, "push", "origin", "HEAD:develop");
|
|
// 任务分支:改 a.txt
|
|
git(other, "checkout", "-b", "bugfix/x");
|
|
writeFileSync(path.join(other, "a.txt"), "a fixed\n");
|
|
git(other, "commit", "-am", "fix");
|
|
git(other, "push", "origin", "bugfix/x");
|
|
// 冲突分支:也改 a.txt
|
|
git(other, "checkout", "-b", "bugfix/conflict", "develop");
|
|
writeFileSync(path.join(other, "a.txt"), "a other\n");
|
|
git(other, "commit", "-am", "other");
|
|
git(other, "push", "origin", "bugfix/conflict");
|
|
git(root, "clone", remote, node);
|
|
});
|
|
|
|
afterAll(() => { if (root) rmSync(root, { recursive: true, force: true }); });
|
|
|
|
describe("mergeTargetIntoBranch", () => {
|
|
test("目标分支没动过 → up_to_date,不推任何东西", async () => {
|
|
const r = await mergeTargetIntoBranch(node, { branch: "bugfix/x", targetBranch: "develop", commitAuthor: null });
|
|
expect(r.status).toBe("up_to_date");
|
|
});
|
|
|
|
test("目标分支前进了 → 造合并提交、普通 push,工作区不动", async () => {
|
|
git(other, "checkout", "develop");
|
|
writeFileSync(path.join(other, "b.txt"), "b newer\n");
|
|
git(other, "commit", "-am", "develop moves on");
|
|
git(other, "push", "origin", "develop");
|
|
const headBefore = git(node, "rev-parse", "HEAD");
|
|
const r = await mergeTargetIntoBranch(node, { branch: "bugfix/x", targetBranch: "develop", commitAuthor: { name: "Bot", email: "bot@x" } });
|
|
expect(r.status).toBe("merged");
|
|
const remoteHead = git(remote, "rev-parse", "bugfix/x");
|
|
expect(remoteHead).toBe(r.mergedSha!);
|
|
expect(git(remote, "log", "-1", "--format=%P", "bugfix/x").split(" ")).toHaveLength(2);
|
|
expect(git(remote, "log", "-1", "--format=%ae", "bugfix/x")).toBe("bot@x");
|
|
expect(git(remote, "show", "bugfix/x:a.txt")).toBe("a fixed");
|
|
expect(git(remote, "show", "bugfix/x:b.txt")).toBe("b newer");
|
|
// 节点检出的 HEAD 和工作区没被碰
|
|
expect(git(node, "rev-parse", "HEAD")).toBe(headBefore);
|
|
expect(git(node, "status", "--porcelain")).toBe("");
|
|
});
|
|
|
|
test("有冲突 → conflict + 冲突文件,远端不变", async () => {
|
|
const before = git(remote, "rev-parse", "bugfix/conflict");
|
|
git(other, "checkout", "develop");
|
|
writeFileSync(path.join(other, "a.txt"), "a develop\n");
|
|
git(other, "commit", "-am", "develop touches a");
|
|
git(other, "push", "origin", "develop");
|
|
const r = await mergeTargetIntoBranch(node, { branch: "bugfix/conflict", targetBranch: "develop", commitAuthor: null });
|
|
expect(r.status).toBe("conflict");
|
|
expect(r.detail).toContain("a.txt");
|
|
expect(git(remote, "rev-parse", "bugfix/conflict")).toBe(before);
|
|
});
|
|
|
|
test("不合法的分支名直接拒绝", async () => {
|
|
const r = await mergeTargetIntoBranch(node, { branch: "x; rm -rf /", targetBranch: "develop", commitAuthor: null });
|
|
expect(r.status).toBe("failed");
|
|
});
|
|
});
|