From 9308bcd6bbf87c64808dde10aa2819dfcc6c5acd Mon Sep 17 00:00:00 2001 From: Henry Mercer Date: Tue, 27 Jan 2026 13:55:22 +0000 Subject: [PATCH] Add unit tests for file coverage enablement --- lib/init-action.js | 20 +++++++-------- src/init-action.ts | 24 +++-------------- src/init.test.ts | 64 ++++++++++++++++++++++++++++++++++++++++++++++ src/init.ts | 31 ++++++++++++++++++++-- 4 files changed, 106 insertions(+), 33 deletions(-) diff --git a/lib/init-action.js b/lib/init-action.js index c970552e4..d50f4de68 100644 --- a/lib/init-action.js +++ b/lib/init-action.js @@ -92028,6 +92028,16 @@ function cleanupDatabaseClusterDirectory(config, logger, options = {}, rmSync2 = } } } +async function getFileCoverageInformationEnabled(debugMode, repositoryNwo, features) { + return ( + // Always enable file coverage information in debug mode + debugMode || // We're most interested in speeding up PRs, and we want to keep + // submitting file coverage information for the default branch since + // it is used to populate the status page. + !isAnalyzingPullRequest() || // For now, restrict this feature to the GitHub org + repositoryNwo.owner !== "github" || !await features.getValue("skip_file_coverage_on_prs" /* SkipFileCoverageOnPrs */) + ); +} // src/status-report.ts var os5 = __toESM(require("os")); @@ -92960,16 +92970,6 @@ function getTrapCachingEnabled() { if (!isHostedRunner()) return false; return true; } -async function getFileCoverageInformationEnabled(debugMode, repositoryNwo, features) { - return ( - // Always enable file coverage information in debug mode - debugMode || // We're most interested in speeding up PRs, and we want to keep - // submitting file coverage information for the default branch since - // it is used to populate the status page. - !isAnalyzingPullRequest() || // For now, restrict this feature to the GitHub org - repositoryNwo.owner !== "github" || !await features.getValue("skip_file_coverage_on_prs" /* SkipFileCoverageOnPrs */) - ); -} async function recordZstdAvailability(config, zstdAvailability) { addDiagnostic( config, diff --git a/src/init-action.ts b/src/init-action.ts index 485346738..0d92f46ef 100644 --- a/src/init-action.ts +++ b/src/init-action.ts @@ -13,7 +13,6 @@ import { getOptionalInput, getRequiredInput, getTemporaryDirectory, - isAnalyzingPullRequest, persistInputs, } from "./actions-util"; import { AnalysisKind, getAnalysisKinds } from "./analyses"; @@ -37,12 +36,13 @@ import { makeTelemetryDiagnostic, } from "./diagnostics"; import { EnvVar } from "./environment"; -import { Feature, FeatureEnablement, Features } from "./feature-flags"; +import { Feature, Features } from "./feature-flags"; import { loadPropertiesFromApi } from "./feature-flags/properties"; import { checkInstallPython311, checkPacksForOverlayCompatibility, cleanupDatabaseClusterDirectory, + getFileCoverageInformationEnabled, initCodeQL, initConfig, runDatabaseInitCluster, @@ -54,7 +54,7 @@ import { OverlayBaseDatabaseDownloadStats, OverlayDatabaseMode, } from "./overlay-database-utils"; -import { getRepositoryNwo, RepositoryNwo } from "./repository"; +import { getRepositoryNwo } from "./repository"; import { ToolsSource } from "./setup-codeql"; import { ActionName, @@ -794,24 +794,6 @@ function getTrapCachingEnabled(): boolean { return true; } -async function getFileCoverageInformationEnabled( - debugMode: boolean, - repositoryNwo: RepositoryNwo, - features: FeatureEnablement, -): Promise { - return ( - // Always enable file coverage information in debug mode - debugMode || - // We're most interested in speeding up PRs, and we want to keep - // submitting file coverage information for the default branch since - // it is used to populate the status page. - !isAnalyzingPullRequest() || - // For now, restrict this feature to the GitHub org - repositoryNwo.owner !== "github" || - !(await features.getValue(Feature.SkipFileCoverageOnPrs)) - ); -} - async function recordZstdAvailability( config: configUtils.Config, zstdAvailability: ZstdAvailability, diff --git a/src/init.test.ts b/src/init.test.ts index 6640e081f..8106a78f9 100644 --- a/src/init.test.ts +++ b/src/init.test.ts @@ -2,14 +2,20 @@ import * as fs from "fs"; import path from "path"; import test, { ExecutionContext } from "ava"; +import * as sinon from "sinon"; +import * as actionsUtil from "./actions-util"; import { createStubCodeQL } from "./codeql"; +import { Feature } from "./feature-flags"; import { checkPacksForOverlayCompatibility, cleanupDatabaseClusterDirectory, + getFileCoverageInformationEnabled, } from "./init"; import { KnownLanguage } from "./languages"; +import { parseRepositoryNwo } from "./repository"; import { + createFeatures, LoggedMessage, createTestConfig, getRecordingLogger, @@ -442,3 +448,61 @@ test( expectedResult: true, }, ); + +test("file coverage information enabled when debugMode is true", async (t) => { + t.true( + await getFileCoverageInformationEnabled( + true, // debugMode + parseRepositoryNwo("github/codeql-action"), + createFeatures([Feature.SkipFileCoverageOnPrs]), + ), + ); +}); + +test("file coverage information enabled when not analyzing a pull request", async (t) => { + sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(false); + + t.true( + await getFileCoverageInformationEnabled( + false, // debugMode + parseRepositoryNwo("github/codeql-action"), + createFeatures([Feature.SkipFileCoverageOnPrs]), + ), + ); +}); + +test("file coverage information enabled when owner is not 'github'", async (t) => { + sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true); + + t.true( + await getFileCoverageInformationEnabled( + false, // debugMode + parseRepositoryNwo("other-org/some-repo"), + createFeatures([Feature.SkipFileCoverageOnPrs]), + ), + ); +}); + +test("file coverage information enabled when feature flag is not enabled", async (t) => { + sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true); + + t.true( + await getFileCoverageInformationEnabled( + false, // debugMode + parseRepositoryNwo("github/codeql-action"), + createFeatures([]), + ), + ); +}); + +test("file coverage information disabled when all conditions for skipping are met", async (t) => { + sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true); + + t.false( + await getFileCoverageInformationEnabled( + false, // debugMode + parseRepositoryNwo("github/codeql-action"), + createFeatures([Feature.SkipFileCoverageOnPrs]), + ), + ); +}); diff --git a/src/init.ts b/src/init.ts index b399ef8d9..a5c8e9ec0 100644 --- a/src/init.ts +++ b/src/init.ts @@ -5,13 +5,22 @@ import * as toolrunner from "@actions/exec/lib/toolrunner"; import * as io from "@actions/io"; import * as yaml from "js-yaml"; -import { getOptionalInput, isSelfHostedRunner } from "./actions-util"; +import { + getOptionalInput, + isAnalyzingPullRequest, + isSelfHostedRunner, +} from "./actions-util"; import { GitHubApiDetails } from "./api-client"; import { CodeQL, setupCodeQL } from "./codeql"; import * as configUtils from "./config-utils"; -import { CodeQLDefaultVersionInfo, FeatureEnablement } from "./feature-flags"; +import { + CodeQLDefaultVersionInfo, + Feature, + FeatureEnablement, +} from "./feature-flags"; import { KnownLanguage, Language } from "./languages"; import { Logger, withGroupAsync } from "./logging"; +import { RepositoryNwo } from "./repository"; import { ToolsSource } from "./setup-codeql"; import { ZstdAvailability } from "./tar"; import { ToolsDownloadStatusReport } from "./tools-download"; @@ -288,3 +297,21 @@ export function cleanupDatabaseClusterDirectory( } } } + +export async function getFileCoverageInformationEnabled( + debugMode: boolean, + repositoryNwo: RepositoryNwo, + features: FeatureEnablement, +): Promise { + return ( + // Always enable file coverage information in debug mode + debugMode || + // We're most interested in speeding up PRs, and we want to keep + // submitting file coverage information for the default branch since + // it is used to populate the status page. + !isAnalyzingPullRequest() || + // For now, restrict this feature to the GitHub org + repositoryNwo.owner !== "github" || + !(await features.getValue(Feature.SkipFileCoverageOnPrs)) + ); +}