|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { runInNewContext } from "node:vm"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | + |
| 6 | +const RELEASE_TITLE = "release(version): Release 2.18.0"; |
| 7 | +const RELEASE_PR = 265; |
| 8 | +const WORKFLOW_PATHS = [ |
| 9 | + ".github/workflows/release-trigger.yml", |
| 10 | + ".github/workflows/release.yml", |
| 11 | +] as const; |
| 12 | + |
| 13 | +type MergeSubjectValidator = ( |
| 14 | + message: string, |
| 15 | + expectedTitle: string, |
| 16 | + expectedPullNumber: number, |
| 17 | +) => boolean; |
| 18 | + |
| 19 | +async function readMergeSubjectValidator(workflowPath: string) { |
| 20 | + const workflow = await fs.readFile(path.resolve(workflowPath), "utf8"); |
| 21 | + const functionStart = workflow.indexOf(" function isAllowedReleaseMergeSubject"); |
| 22 | + expect( |
| 23 | + functionStart, |
| 24 | + `${workflowPath} must define the merge-subject validator`, |
| 25 | + ).toBeGreaterThanOrEqual(0); |
| 26 | + const functionEnd = workflow.indexOf("\n }", functionStart); |
| 27 | + expect(functionEnd, `${workflowPath} must close the merge-subject validator`).toBeGreaterThan( |
| 28 | + functionStart, |
| 29 | + ); |
| 30 | + const source = workflow |
| 31 | + .slice(functionStart, functionEnd + "\n }".length) |
| 32 | + .replace(/^ {12}/gm, ""); |
| 33 | + return runInNewContext(`(${source})`) as MergeSubjectValidator; |
| 34 | +} |
| 35 | + |
| 36 | +describe.each(WORKFLOW_PATHS)("%s release merge-subject contract", (workflowPath) => { |
| 37 | + it.each([ |
| 38 | + [RELEASE_TITLE, true], |
| 39 | + [`${RELEASE_TITLE}\n\npodnotes-release-commit schema=1 version=2.18.0`, true], |
| 40 | + [`${RELEASE_TITLE} (#${RELEASE_PR})`, true], |
| 41 | + [`${RELEASE_TITLE} (#${RELEASE_PR})\n\nGitHub-generated body`, true], |
| 42 | + [`${RELEASE_TITLE} (#264)`, false], |
| 43 | + [`${RELEASE_TITLE} (#${RELEASE_PR}) extra`, false], |
| 44 | + [` ${RELEASE_TITLE}`, false], |
| 45 | + [`${RELEASE_TITLE} `, false], |
| 46 | + ["release(version): Release 2.18.1", false], |
| 47 | + ])("validates the exact merge subject in %j", async (message, expected) => { |
| 48 | + const validate = await readMergeSubjectValidator(workflowPath); |
| 49 | + expect(validate(message, RELEASE_TITLE, RELEASE_PR)).toBe(expected); |
| 50 | + }); |
| 51 | + |
| 52 | + it("uses the validator for the fetched release commit", async () => { |
| 53 | + const workflow = await fs.readFile(path.resolve(workflowPath), "utf8"); |
| 54 | + expect(workflow).toContain( |
| 55 | + "if (!isAllowedReleaseMergeSubject(releaseCommit.data.message, title, pullNumber)) {", |
| 56 | + ); |
| 57 | + }); |
| 58 | +}); |
0 commit comments