Skip to content

Commit b7177b2

Browse files
committed
fix(ci): scope mutation diff to merge result base
1 parent 9973630 commit b7177b2

2 files changed

Lines changed: 48 additions & 9 deletions

File tree

‎.github/workflows/mutation-testing.yml‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@ jobs:
3636
fetch-depth: 0
3737
persist-credentials: false
3838

39-
- name: Fetch pull request base
40-
if: github.event_name == 'pull_request'
41-
env:
42-
BASE_REPOSITORY_URL: ${{ github.server_url }}/${{ github.repository }}.git
43-
BASE_SHA: ${{ github.event.pull_request.base.sha }}
44-
run: git fetch --no-tags "$BASE_REPOSITORY_URL" "$BASE_SHA"
45-
4639
- name: Setup Node.js and pnpm
4740
if: github.event_name == 'pull_request'
4841
uses: ./.github/actions/setup-node-pnpm
@@ -56,9 +49,10 @@ jobs:
5649
- name: Enforce executable-line scope and run advisory mutation testing
5750
if: github.event_name == 'pull_request'
5851
env:
59-
BASE_SHA: ${{ github.event.pull_request.base.sha }}
6052
HEAD_SHA: ${{ github.sha }}
61-
run: node scripts/stryker-diff.mjs ci --base "$BASE_SHA" --head "$HEAD_SHA"
53+
run: |
54+
BASE_SHA="$(git rev-parse "$HEAD_SHA^1")"
55+
node scripts/stryker-diff.mjs ci --base "$BASE_SHA" --head "$HEAD_SHA"
6256
6357
- name: Upload mutation reports
6458
id: mutation_report

‎scripts/stryker-diff.test.mjs‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ describe("mutation testing workflow", () => {
5757
assert.ok(!workflow.includes("ref: ${{ github.event.pull_request.head.sha }}"))
5858
assert.ok(workflow.includes("HEAD_SHA: ${{ github.sha }}"))
5959
assert.ok(!workflow.includes("HEAD_SHA: ${{ github.event.pull_request.head.sha }}"))
60+
assert.ok(workflow.includes('BASE_SHA="$(git rev-parse "$HEAD_SHA^1")"'))
61+
assert.ok(!workflow.includes("github.event.pull_request.base.sha"))
6062
assert.ok(workflow.includes("steps.mutation_report.outputs.artifact-url"))
6163
assert.ok(workflow.includes("open the package's mutation.html file"))
6264
assert.ok(workflow.includes("Enforce executable-line scope and run advisory mutation testing"))
@@ -444,6 +446,49 @@ describe("selectFromGit", () => {
444446
fs.rmSync(repo, { recursive: true, force: true })
445447
}
446448
})
449+
450+
it("does not charge intervening base-branch changes to the pull request", () => {
451+
const repo = fs.mkdtempSync(path.join(os.tmpdir(), "stryker-stale-base-"))
452+
const runGit = (...args) => execFileSync("git", args, { cwd: repo, encoding: "utf8" }).trim()
453+
454+
try {
455+
runGit("init", "--initial-branch=main")
456+
runGit("config", "user.name", "Mutation Test")
457+
runGit("config", "user.email", "[email protected]")
458+
fs.mkdirSync(path.join(repo, "packages/core/src"), { recursive: true })
459+
fs.writeFileSync(path.join(repo, "packages/core/src/pr.ts"), "export const pr = false\n")
460+
fs.writeFileSync(path.join(repo, "packages/core/src/base.ts"), "export const base = false\n")
461+
runGit("add", ".")
462+
runGit("commit", "-m", "initial")
463+
const staleBaseSha = runGit("rev-parse", "HEAD")
464+
465+
runGit("checkout", "-b", "feature")
466+
fs.writeFileSync(path.join(repo, "packages/core/src/pr.ts"), "export const pr = true\n")
467+
runGit("commit", "-am", "change pull request")
468+
469+
runGit("checkout", "main")
470+
fs.writeFileSync(path.join(repo, "packages/core/src/base.ts"), "export const base = true\n")
471+
runGit("commit", "-am", "advance base branch")
472+
const currentBaseSha = runGit("rev-parse", "HEAD")
473+
runGit("merge", "--no-ff", "feature", "-m", "synthetic pull request merge")
474+
const mergeSha = runGit("rev-parse", "HEAD")
475+
const mergeResultBaseSha = runGit("rev-parse", `${mergeSha}^1`)
476+
assert.equal(mergeResultBaseSha, currentBaseSha)
477+
478+
assert.deepEqual(
479+
selectFromGit(repo, staleBaseSha, mergeSha).packages[0].files.map(({ path: filePath }) => filePath),
480+
["packages/core/src/base.ts", "packages/core/src/pr.ts"],
481+
)
482+
assert.deepEqual(
483+
selectFromGit(repo, mergeResultBaseSha, mergeSha).packages[0].files.map(
484+
({ path: filePath }) => filePath,
485+
),
486+
["packages/core/src/pr.ts"],
487+
)
488+
} finally {
489+
fs.rmSync(repo, { recursive: true, force: true })
490+
}
491+
})
447492
})
448493

449494
describe("mutation exclusions", () => {

0 commit comments

Comments
 (0)