|
| 1 | +import { MockGithub } from "@kie/mock-github"; |
| 2 | +import path from "path"; |
| 3 | +import { exec as execCb } from "node:child_process"; |
| 4 | +import { promisify } from "node:util"; |
| 5 | + |
| 6 | +const exec = promisify(execCb); |
| 7 | + |
| 8 | +const SCRIPT = path.resolve(__dirname, "../..", ".github/scripts/resolve-base-branch.sh"); |
| 9 | + |
| 10 | +let github: MockGithub; |
| 11 | +let repoPath: string; |
| 12 | +const devChain: string[] = []; |
| 13 | + |
| 14 | +async function git(args: string): Promise<string> { |
| 15 | + const { stdout } = await exec(`git -C "${repoPath}" ${args}`); |
| 16 | + return stdout.trim(); |
| 17 | +} |
| 18 | + |
| 19 | +async function createDev(name: string) { |
| 20 | + const base = devChain[devChain.length - 1]; |
| 21 | + await git(base ? `checkout -b ${name} ${base}` : `checkout -b ${name}`); |
| 22 | + devChain.push(name); |
| 23 | +} |
| 24 | + |
| 25 | +async function commitOn(branch: string, message: string) { |
| 26 | + await git(`checkout ${branch}`); |
| 27 | + await git(`commit --allow-empty -m "${message}"`); |
| 28 | + |
| 29 | + // Waterfall to every newer dev branch in the chain |
| 30 | + const idx = devChain.indexOf(branch); |
| 31 | + if (idx < 0) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + let prev = branch; |
| 36 | + for (let i = idx + 1; i < devChain.length; i++) { |
| 37 | + const next = devChain[i]; |
| 38 | + await git(`checkout ${next}`); |
| 39 | + await git(`merge --no-ff ${prev}`); |
| 40 | + prev = next; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +async function branchFrom(name: string, base: string, message: string) { |
| 45 | + await git(`checkout -b ${name} ${base}`); |
| 46 | + await git(`commit --allow-empty -m "${message}"`); |
| 47 | +} |
| 48 | + |
| 49 | +async function runScript(): Promise<string> { |
| 50 | + const { stdout } = await exec(SCRIPT, { cwd: repoPath }); |
| 51 | + return stdout.trim(); |
| 52 | +} |
| 53 | + |
| 54 | +beforeAll(async () => { |
| 55 | + github = new MockGithub({ |
| 56 | + repo: { zenko: {} }, |
| 57 | + }); |
| 58 | + |
| 59 | + await github.setup(); |
| 60 | + repoPath = github.repo.getPath("zenko") as string; |
| 61 | + |
| 62 | + await createDev("development/2.11"); |
| 63 | + await commitOn("development/2.11", "2.11: A1"); |
| 64 | + await createDev("development/2.12"); |
| 65 | + await createDev("development/2.13"); |
| 66 | + |
| 67 | + await commitOn("development/2.11", "2.11: A2"); |
| 68 | + await commitOn("development/2.12", "2.12: B1"); |
| 69 | + await branchFrom("feat-on-outdated-2.12", "development/2.12", "feat-outdated: K1"); |
| 70 | + await commitOn("development/2.13", "2.13: C1"); |
| 71 | + |
| 72 | + await commitOn("development/2.11", "2.11: A3"); |
| 73 | + await commitOn("development/2.12", "2.12: B2"); |
| 74 | + await commitOn("development/2.13", "2.13: C2"); |
| 75 | + |
| 76 | + await git("push origin development/2.11 development/2.12 development/2.13"); |
| 77 | + |
| 78 | + await branchFrom("feat-on-2.13", "development/2.13", "feat-on-2.13: F1"); |
| 79 | + await branchFrom("stacked-on-2.13", "feat-on-2.13", "stacked: H1"); |
| 80 | + await branchFrom("feat-on-2.12", "development/2.12", "feat-on-2.12: G1"); |
| 81 | + await branchFrom("feat-on-2.11", "development/2.11", "feat-on-2.11: I1"); |
| 82 | +}); |
| 83 | + |
| 84 | +afterAll(async () => { |
| 85 | + await github.teardown(); |
| 86 | +}); |
| 87 | + |
| 88 | +describe("resolve-base-branch.sh", () => { |
| 89 | + it("resolves feature branched off latest dev", async () => { |
| 90 | + await git("checkout feat-on-2.13"); |
| 91 | + expect(await runScript()).toBe("development/2.13"); |
| 92 | + }); |
| 93 | + |
| 94 | + it("resolves feature branched off middle dev", async () => { |
| 95 | + await git("checkout feat-on-2.12"); |
| 96 | + expect(await runScript()).toBe("development/2.12"); |
| 97 | + }); |
| 98 | + |
| 99 | + it("resolves feature branched off oldest dev", async () => { |
| 100 | + await git("checkout feat-on-2.11"); |
| 101 | + expect(await runScript()).toBe("development/2.11"); |
| 102 | + }); |
| 103 | + |
| 104 | + it("resolves stacked branch (feature off feature off dev)", async () => { |
| 105 | + await git("checkout stacked-on-2.13"); |
| 106 | + expect(await runScript()).toBe("development/2.13"); |
| 107 | + }); |
| 108 | + |
| 109 | + it("resolves a dev branch itself to itself", async () => { |
| 110 | + await git("checkout development/2.12"); |
| 111 | + expect(await runScript()).toBe("development/2.12"); |
| 112 | + }); |
| 113 | + |
| 114 | + it("resolves feature branched off an outdated dev commit", async () => { |
| 115 | + await git("checkout feat-on-outdated-2.12"); |
| 116 | + expect(await runScript()).toBe("development/2.12"); |
| 117 | + }); |
| 118 | +}); |
0 commit comments