From 5164a9e438ce2ad73b6b063fac1aea1ff7820d74 Mon Sep 17 00:00:00 2001 From: Todd Evanoff Date: Fri, 1 Dec 2023 14:09:43 -0800 Subject: [PATCH] add some tests --- node-src/git/getParentCommits.test.ts | 94 +++++++++++++++++++++------ node-src/git/mocks/double-loop.ts | 23 +++++++ 2 files changed, 98 insertions(+), 19 deletions(-) create mode 100644 node-src/git/mocks/double-loop.ts diff --git a/node-src/git/getParentCommits.test.ts b/node-src/git/getParentCommits.test.ts index ab8bbb659..70e09fc18 100644 --- a/node-src/git/getParentCommits.test.ts +++ b/node-src/git/getParentCommits.test.ts @@ -10,6 +10,7 @@ import generateGitRepository from './generateGitRepository'; import longLineDescription from './mocks/long-line'; import longLoopDescription from './mocks/long-loop'; +import doubleLoopDescription from './mocks/double-loop'; import createMockIndex from './mocks/mock-index'; import simpleLoopDescription from './mocks/simple-loop'; import threeParentsDescription from './mocks/three-parents'; @@ -19,6 +20,7 @@ const descriptions = { simpleLoop: simpleLoopDescription, longLine: longLineDescription, longLoop: longLoopDescription, + doubleLoop: doubleLoopDescription, threeParents: threeParentsDescription, twoRoots: twoRootsDescription, }; @@ -615,7 +617,7 @@ describe('getParentCommits', () => { it(`also includes PR head commits that were squashed to this commit`, async () => { // // - // [D] [branch, squash merged into E] + // [D] [branch, squash merged into E] // / \ // A - B -[C] F // \ / @@ -641,9 +643,9 @@ describe('getParentCommits', () => { it(`also finds squash merge commits that were on previous commits without builds`, async () => { // []: has build, <>: is squash merge, (): current commit // - // B -[D] [branch] + // B -[D] [branch] // / - // A - [C]--(G) [main] + // A -[C]--(G) [main] const repository = repositories.longLoop; await checkoutCommit('G', 'main', repository); const client = createClient({ @@ -664,7 +666,7 @@ describe('getParentCommits', () => { it(`deals with situations where the last build on the squashed branch isn't the last commit`, async () => { // []: has build, <>: is squash merge, (): current commit // - // [B] - D [branch] + // [B]- D [branch] // / // A -[C]--(G) [main] const repository = repositories.longLoop; @@ -681,28 +683,82 @@ describe('getParentCommits', () => { const git = { branch: 'main', ...(await getCommit()) }; const parentCommits = await getParentCommits({ client, log, git, options } as any); - // This doesn't include 'C' as D "covers" it. expectCommitsToEqualNames(parentCommits, ['C', 'B'], repository); }); - // it(`deals with situations where squashed branches have no builds`) - // [X] - // / \ - // P - Q - R - // / - //[A] - B - C - M - // it(`deals with situations where squashed branches no longer exist in the repo but have a build`) - // it(`deals with situations where squashed branches no longer exist in the repo and have no build`) + it(`deals with situations where squashed branches have no builds`, async () => { + // []: has build, <>: is squash merge, (): current commit + // + // B - D [branch] + // / + // A -[C]--(G) [main] + const repository = repositories.longLoop; + await checkoutCommit('G', 'main', repository); + const client = createClient({ + repository, + builds: [ + ['C', 'main'], + ], + // Talking to GH (etc) tells us that commit E is the merge commit for "branch" + prs: [['E', 'branch']], + }); + const git = { branch: 'main', ...(await getCommit()) }; - // it(`deals with situations where squashed branches themselves have squash merge commits`) + const parentCommits = await getParentCommits({ client, log, git, options } as any); + expectCommitsToEqualNames(parentCommits, ['C'], repository); + }); - // P -[Q] - // \ - // X - [Y] - (squash merge of Q) - // \ - // A - - (squash merge of Z) + it(`deals with situations where squashed branches no longer exist in the repo but have a build`, async () => { + // []: has build, <>: is squash merge, (): current commit + // + // [B] [no longer in repo] + // + // [A]- C -<(D)> [main] + const repository = repositories.twoRoots; + await checkoutCommit('D', 'main', repository); + const client = createClient({ + repository, + builds: [ + ['A', 'main'], + ['B', 'xxx'], + ], + // Talking to GH (etc) tells us that commit D is the merge commit for "branch" (which no longer exists) + prs: [['D', 'branch']], + }); + const git = { branch: 'main', ...(await getCommit()) }; + + const parentCommits = await getParentCommits({ client, log, git, options } as any); + // This is A and not B because we do not know anything about the deleted branch and its commits + expectCommitsToEqualNames(parentCommits, ['A'], repository); + }); + + it(`deals with situations where squashed branches themselves have squash merge commits`, async () => { + // []: has build, <>: is squash merge, (): current commit + // + // [F] [branch2] + // / + // [C]- [branch] + // / + // A -[B]-- G [main] + const repository = repositories.doubleLoop; + await checkoutCommit('G', 'main', repository); + const client = createClient({ + repository, + builds: [ + ['B', 'main'], + ['C', 'branch'], + ['F', 'branch2'], + ], + // Talking to GH (etc) tells us that commit G is the merge commit for "branch" + prs: [['D', 'branch'], ['E', 'branch2']], + }); + const git = { branch: 'main', ...(await getCommit()) }; + + const parentCommits = await getParentCommits({ client, log, git, options } as any); + expectCommitsToEqualNames(parentCommits, ['C', 'B'], repository); + }); }); }); diff --git a/node-src/git/mocks/double-loop.ts b/node-src/git/mocks/double-loop.ts new file mode 100644 index 000000000..5514f7f31 --- /dev/null +++ b/node-src/git/mocks/double-loop.ts @@ -0,0 +1,23 @@ +// A +// | \ +// B C +// | | \ +// D E F +// | | / +// G H +// | / +// I + +// [commit, parent(s)] +export default [ + // prettier-ignore + ['A', false], + ['B', 'A'], + ['C', 'A'], + ['D', 'B'], + ['E', 'C'], + ['F', 'C'], + ['G', 'D'], + ['H', 'E'], + ['I', ['G', 'H']], +];