|
| 1 | +import * as fs from "fs"; |
| 2 | +import * as path from "path"; |
| 3 | +import { customLinterCheckTest } from "tests"; |
| 4 | +import { TrunkLintDriver } from "tests/driver"; |
| 5 | +import { TEST_DATA } from "tests/utils"; |
| 6 | + |
| 7 | +const moveWorkflowFile = |
| 8 | + (filename: string, disableGhAuth = false) => |
| 9 | + async (driver: TrunkLintDriver) => { |
| 10 | + if (disableGhAuth) { |
| 11 | + process.env.PINACT_DISABLE_GH_AUTH = "1"; |
| 12 | + } else { |
| 13 | + delete process.env.PINACT_DISABLE_GH_AUTH; |
| 14 | + } |
| 15 | + driver.moveFile(path.join(TEST_DATA, filename), path.join(".github/workflows", filename)); |
| 16 | + await driver.gitDriver?.add(".").commit("moved"); |
| 17 | + }; |
| 18 | + |
| 19 | +const moveWorkflowFiles = |
| 20 | + (sourceDir = TEST_DATA) => |
| 21 | + async (driver: TrunkLintDriver) => { |
| 22 | + delete process.env.PINACT_DISABLE_GH_AUTH; |
| 23 | + |
| 24 | + fs.readdirSync(path.resolve(driver.getSandbox(), sourceDir), { withFileTypes: true }) |
| 25 | + .filter((file) => file.isFile()) |
| 26 | + .forEach((file) => { |
| 27 | + driver.moveFile(path.join(sourceDir, file.name), path.join(".github/workflows", file.name)); |
| 28 | + }); |
| 29 | + await driver.gitDriver?.add(".").commit("moved"); |
| 30 | + }; |
| 31 | + |
| 32 | +const enablePinactCommand = |
| 33 | + (command: string, preCheck?: (driver: TrunkLintDriver) => Promise<void>) => |
| 34 | + async (driver: TrunkLintDriver) => { |
| 35 | + delete process.env.PINACT_DISABLE_GH_AUTH; |
| 36 | + |
| 37 | + const trunkYamlPath = ".trunk/trunk.yaml"; |
| 38 | + const currentContents = driver.readFile(trunkYamlPath); |
| 39 | + const pinactRegex = /- pinact@(.+)\n/; |
| 40 | + |
| 41 | + driver.writeFile( |
| 42 | + trunkYamlPath, |
| 43 | + currentContents.replace(pinactRegex, `- pinact@$1:\n commands: [${command}]\n`), |
| 44 | + ); |
| 45 | + |
| 46 | + if (preCheck) { |
| 47 | + await preCheck(driver); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | +const skipIfMissingGitHubToken = () => { |
| 52 | + if (!process.env.GH_TOKEN && !process.env.GITHUB_TOKEN && !process.env.PINACT_GITHUB_TOKEN) { |
| 53 | + console.log( |
| 54 | + "Skipping pinact online audit test because GH_TOKEN, GITHUB_TOKEN, and PINACT_GITHUB_TOKEN are not set.", |
| 55 | + ); |
| 56 | + return true; |
| 57 | + } |
| 58 | + return false; |
| 59 | +}; |
| 60 | + |
| 61 | +customLinterCheckTest({ |
| 62 | + linterName: "pinact", |
| 63 | + testName: "missing_version_comment", |
| 64 | + args: ".github", |
| 65 | + preCheck: moveWorkflowFile("missing_version_comment.in.yaml", true), |
| 66 | +}); |
| 67 | + |
| 68 | +customLinterCheckTest({ |
| 69 | + linterName: "pinact", |
| 70 | + testName: "unpinned", |
| 71 | + args: ".github", |
| 72 | + preCheck: moveWorkflowFile("unpinned.in.yaml", true), |
| 73 | +}); |
| 74 | + |
| 75 | +customLinterCheckTest({ |
| 76 | + linterName: "pinact", |
| 77 | + testName: "version_comment", |
| 78 | + args: ".github", |
| 79 | + preCheck: moveWorkflowFiles(path.join(TEST_DATA, "online")), |
| 80 | + skipTestIf: skipIfMissingGitHubToken, |
| 81 | +}); |
| 82 | + |
| 83 | +customLinterCheckTest({ |
| 84 | + linterName: "pinact", |
| 85 | + testName: "upgrade", |
| 86 | + args: ".github", |
| 87 | + preCheck: enablePinactCommand("upgrade", moveWorkflowFile("unpinned.in.yaml")), |
| 88 | + skipTestIf: skipIfMissingGitHubToken, |
| 89 | +}); |
0 commit comments