|
| 1 | +import test from "ava"; |
| 2 | + |
| 3 | +import { ActionsEnvVars } from "./environment"; |
| 4 | +import { Feature } from "./feature-flags"; |
| 5 | +import { BuiltInLanguage } from "./languages"; |
| 6 | +import { getRunnerLogger } from "./logging"; |
| 7 | +import { |
| 8 | + getPerLanguageBundleLanguage, |
| 9 | + MIN_PER_LANGUAGE_BUNDLE_CLI_VERSION, |
| 10 | + PerLanguageBundleOptions, |
| 11 | +} from "./per-language-bundles"; |
| 12 | +import { createFeatures, setupTests } from "./testing-utils"; |
| 13 | +import { GitHubVariant } from "./util"; |
| 14 | + |
| 15 | +setupTests(test); |
| 16 | + |
| 17 | +/** Options for which we would use a per-language bundle. */ |
| 18 | +const ELIGIBLE_OPTIONS: PerLanguageBundleOptions = { |
| 19 | + rawLanguages: ["java"], |
| 20 | + // Any version at least as new as the minimum will do. |
| 21 | + cliVersion: MIN_PER_LANGUAGE_BUNDLE_CLI_VERSION, |
| 22 | + compressionMethod: "zstd", |
| 23 | + platform: "linux64", |
| 24 | + variant: GitHubVariant.DOTCOM, |
| 25 | +}; |
| 26 | + |
| 27 | +async function checkEligibility( |
| 28 | + overrides: Partial<PerLanguageBundleOptions>, |
| 29 | + enabledFeatures: Feature[] = [Feature.PerLanguageBundles], |
| 30 | +) { |
| 31 | + return getPerLanguageBundleLanguage( |
| 32 | + { ...ELIGIBLE_OPTIONS, ...overrides }, |
| 33 | + createFeatures(enabledFeatures), |
| 34 | + getRunnerLogger(true), |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +test.beforeEach(() => { |
| 39 | + process.env[ActionsEnvVars.RUNNER_ENVIRONMENT] = "github-hosted"; |
| 40 | +}); |
| 41 | + |
| 42 | +test.serial( |
| 43 | + "uses a per-language bundle when everything lines up", |
| 44 | + async (t) => { |
| 45 | + t.is(await checkEligibility({}), BuiltInLanguage.java); |
| 46 | + }, |
| 47 | +); |
| 48 | + |
| 49 | +test.serial("resolves the language names used by default setup", async (t) => { |
| 50 | + // Code Scanning default setup passes the combined language names, which do not match the |
| 51 | + // extractor names that the bundles are published under. |
| 52 | + for (const [rawLanguage, expected] of [ |
| 53 | + ["java-kotlin", BuiltInLanguage.java], |
| 54 | + ["kotlin", BuiltInLanguage.java], |
| 55 | + ["javascript-typescript", BuiltInLanguage.javascript], |
| 56 | + ["typescript", BuiltInLanguage.javascript], |
| 57 | + ["c-cpp", BuiltInLanguage.cpp], |
| 58 | + ["c", BuiltInLanguage.cpp], |
| 59 | + ["c++", BuiltInLanguage.cpp], |
| 60 | + ["c#", BuiltInLanguage.csharp], |
| 61 | + ["actions", BuiltInLanguage.actions], |
| 62 | + ["go", BuiltInLanguage.go], |
| 63 | + ["python", BuiltInLanguage.python], |
| 64 | + ["ruby", BuiltInLanguage.ruby], |
| 65 | + ["rust", BuiltInLanguage.rust], |
| 66 | + ] as const) { |
| 67 | + t.is( |
| 68 | + await checkEligibility({ rawLanguages: [rawLanguage] }), |
| 69 | + expected, |
| 70 | + `Expected '${rawLanguage}' to resolve to '${expected}'`, |
| 71 | + ); |
| 72 | + } |
| 73 | +}); |
| 74 | + |
| 75 | +test.serial("uses the macOS bundle for Swift", async (t) => { |
| 76 | + t.is( |
| 77 | + await checkEligibility({ rawLanguages: ["swift"], platform: "osx64" }), |
| 78 | + BuiltInLanguage.swift, |
| 79 | + ); |
| 80 | + // Swift is only published for macOS. |
| 81 | + t.is( |
| 82 | + await checkEligibility({ rawLanguages: ["swift"], platform: "linux64" }), |
| 83 | + undefined, |
| 84 | + ); |
| 85 | +}); |
| 86 | + |
| 87 | +test.serial("only publishes non-Swift languages for Linux", async (t) => { |
| 88 | + t.is(await checkEligibility({ platform: "osx64" }), undefined); |
| 89 | + t.is(await checkEligibility({ platform: "win64" }), undefined); |
| 90 | + t.is(await checkEligibility({ platform: undefined }), undefined); |
| 91 | +}); |
| 92 | + |
| 93 | +test.serial("requires exactly one language", async (t) => { |
| 94 | + t.is(await checkEligibility({ rawLanguages: undefined }), undefined); |
| 95 | + t.is(await checkEligibility({ rawLanguages: [] }), undefined); |
| 96 | + t.is(await checkEligibility({ rawLanguages: ["java", "python"] }), undefined); |
| 97 | +}); |
| 98 | + |
| 99 | +test.serial("requires a language that CodeQL knows about", async (t) => { |
| 100 | + t.is(await checkEligibility({ rawLanguages: ["cobol"] }), undefined); |
| 101 | +}); |
| 102 | + |
| 103 | +test.serial("requires a zstd bundle", async (t) => { |
| 104 | + t.is(await checkEligibility({ compressionMethod: "gzip" }), undefined); |
| 105 | +}); |
| 106 | + |
| 107 | +test.serial("requires GitHub.com", async (t) => { |
| 108 | + // Other products resolve the combined bundle against their own instance, so asking for a |
| 109 | + // per-language bundle they do not mirror would move the download off that instance. |
| 110 | + for (const variant of [GitHubVariant.GHES, GitHubVariant.GHEC_DR]) { |
| 111 | + t.is(await checkEligibility({ variant }), undefined); |
| 112 | + } |
| 113 | +}); |
| 114 | + |
| 115 | +test.serial("requires a GitHub-hosted runner", async (t) => { |
| 116 | + // A self-hosted runner may have a toolcache that persists between jobs, which is worth more than |
| 117 | + // a smaller download. |
| 118 | + process.env[ActionsEnvVars.RUNNER_ENVIRONMENT] = "self-hosted"; |
| 119 | + t.is(await checkEligibility({}), undefined); |
| 120 | + |
| 121 | + // Self-hosted runners are routinely configured to look like hosted ones, for example by mounting |
| 122 | + // a persistent volume at `/opt/hostedtoolcache`, so we require the service to tell us explicitly. |
| 123 | + delete process.env[ActionsEnvVars.RUNNER_ENVIRONMENT]; |
| 124 | + process.env["RUNNER_TOOL_CACHE"] = "/opt/hostedtoolcache"; |
| 125 | + t.is(await checkEligibility({}), undefined); |
| 126 | +}); |
| 127 | + |
| 128 | +test.serial("requires a new enough CLI version", async (t) => { |
| 129 | + t.is(await checkEligibility({ cliVersion: undefined }), undefined); |
| 130 | + t.is( |
| 131 | + await checkEligibility({ |
| 132 | + cliVersion: decrementPatchVersion(MIN_PER_LANGUAGE_BUNDLE_CLI_VERSION), |
| 133 | + }), |
| 134 | + undefined, |
| 135 | + ); |
| 136 | +}); |
| 137 | + |
| 138 | +test.serial("requires the feature flag", async (t) => { |
| 139 | + t.is(await checkEligibility({}, []), undefined); |
| 140 | +}); |
| 141 | + |
| 142 | +function decrementPatchVersion(version: string): string { |
| 143 | + const [major, minor, patch] = version.split(".").map(Number); |
| 144 | + return `${major}.${minor}.${patch - 1}`; |
| 145 | +} |
0 commit comments