Skip to content

Commit 9308bcd

Browse files
committed
Add unit tests for file coverage enablement
1 parent bf20b3e commit 9308bcd

File tree

4 files changed

+106
-33
lines changed

4 files changed

+106
-33
lines changed

lib/init-action.js

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/init-action.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
getOptionalInput,
1414
getRequiredInput,
1515
getTemporaryDirectory,
16-
isAnalyzingPullRequest,
1716
persistInputs,
1817
} from "./actions-util";
1918
import { AnalysisKind, getAnalysisKinds } from "./analyses";
@@ -37,12 +36,13 @@ import {
3736
makeTelemetryDiagnostic,
3837
} from "./diagnostics";
3938
import { EnvVar } from "./environment";
40-
import { Feature, FeatureEnablement, Features } from "./feature-flags";
39+
import { Feature, Features } from "./feature-flags";
4140
import { loadPropertiesFromApi } from "./feature-flags/properties";
4241
import {
4342
checkInstallPython311,
4443
checkPacksForOverlayCompatibility,
4544
cleanupDatabaseClusterDirectory,
45+
getFileCoverageInformationEnabled,
4646
initCodeQL,
4747
initConfig,
4848
runDatabaseInitCluster,
@@ -54,7 +54,7 @@ import {
5454
OverlayBaseDatabaseDownloadStats,
5555
OverlayDatabaseMode,
5656
} from "./overlay-database-utils";
57-
import { getRepositoryNwo, RepositoryNwo } from "./repository";
57+
import { getRepositoryNwo } from "./repository";
5858
import { ToolsSource } from "./setup-codeql";
5959
import {
6060
ActionName,
@@ -794,24 +794,6 @@ function getTrapCachingEnabled(): boolean {
794794
return true;
795795
}
796796

797-
async function getFileCoverageInformationEnabled(
798-
debugMode: boolean,
799-
repositoryNwo: RepositoryNwo,
800-
features: FeatureEnablement,
801-
): Promise<boolean> {
802-
return (
803-
// Always enable file coverage information in debug mode
804-
debugMode ||
805-
// We're most interested in speeding up PRs, and we want to keep
806-
// submitting file coverage information for the default branch since
807-
// it is used to populate the status page.
808-
!isAnalyzingPullRequest() ||
809-
// For now, restrict this feature to the GitHub org
810-
repositoryNwo.owner !== "github" ||
811-
!(await features.getValue(Feature.SkipFileCoverageOnPrs))
812-
);
813-
}
814-
815797
async function recordZstdAvailability(
816798
config: configUtils.Config,
817799
zstdAvailability: ZstdAvailability,

src/init.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ import * as fs from "fs";
22
import path from "path";
33

44
import test, { ExecutionContext } from "ava";
5+
import * as sinon from "sinon";
56

7+
import * as actionsUtil from "./actions-util";
68
import { createStubCodeQL } from "./codeql";
9+
import { Feature } from "./feature-flags";
710
import {
811
checkPacksForOverlayCompatibility,
912
cleanupDatabaseClusterDirectory,
13+
getFileCoverageInformationEnabled,
1014
} from "./init";
1115
import { KnownLanguage } from "./languages";
16+
import { parseRepositoryNwo } from "./repository";
1217
import {
18+
createFeatures,
1319
LoggedMessage,
1420
createTestConfig,
1521
getRecordingLogger,
@@ -442,3 +448,61 @@ test(
442448
expectedResult: true,
443449
},
444450
);
451+
452+
test("file coverage information enabled when debugMode is true", async (t) => {
453+
t.true(
454+
await getFileCoverageInformationEnabled(
455+
true, // debugMode
456+
parseRepositoryNwo("github/codeql-action"),
457+
createFeatures([Feature.SkipFileCoverageOnPrs]),
458+
),
459+
);
460+
});
461+
462+
test("file coverage information enabled when not analyzing a pull request", async (t) => {
463+
sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(false);
464+
465+
t.true(
466+
await getFileCoverageInformationEnabled(
467+
false, // debugMode
468+
parseRepositoryNwo("github/codeql-action"),
469+
createFeatures([Feature.SkipFileCoverageOnPrs]),
470+
),
471+
);
472+
});
473+
474+
test("file coverage information enabled when owner is not 'github'", async (t) => {
475+
sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true);
476+
477+
t.true(
478+
await getFileCoverageInformationEnabled(
479+
false, // debugMode
480+
parseRepositoryNwo("other-org/some-repo"),
481+
createFeatures([Feature.SkipFileCoverageOnPrs]),
482+
),
483+
);
484+
});
485+
486+
test("file coverage information enabled when feature flag is not enabled", async (t) => {
487+
sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true);
488+
489+
t.true(
490+
await getFileCoverageInformationEnabled(
491+
false, // debugMode
492+
parseRepositoryNwo("github/codeql-action"),
493+
createFeatures([]),
494+
),
495+
);
496+
});
497+
498+
test("file coverage information disabled when all conditions for skipping are met", async (t) => {
499+
sinon.stub(actionsUtil, "isAnalyzingPullRequest").returns(true);
500+
501+
t.false(
502+
await getFileCoverageInformationEnabled(
503+
false, // debugMode
504+
parseRepositoryNwo("github/codeql-action"),
505+
createFeatures([Feature.SkipFileCoverageOnPrs]),
506+
),
507+
);
508+
});

src/init.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@ import * as toolrunner from "@actions/exec/lib/toolrunner";
55
import * as io from "@actions/io";
66
import * as yaml from "js-yaml";
77

8-
import { getOptionalInput, isSelfHostedRunner } from "./actions-util";
8+
import {
9+
getOptionalInput,
10+
isAnalyzingPullRequest,
11+
isSelfHostedRunner,
12+
} from "./actions-util";
913
import { GitHubApiDetails } from "./api-client";
1014
import { CodeQL, setupCodeQL } from "./codeql";
1115
import * as configUtils from "./config-utils";
12-
import { CodeQLDefaultVersionInfo, FeatureEnablement } from "./feature-flags";
16+
import {
17+
CodeQLDefaultVersionInfo,
18+
Feature,
19+
FeatureEnablement,
20+
} from "./feature-flags";
1321
import { KnownLanguage, Language } from "./languages";
1422
import { Logger, withGroupAsync } from "./logging";
23+
import { RepositoryNwo } from "./repository";
1524
import { ToolsSource } from "./setup-codeql";
1625
import { ZstdAvailability } from "./tar";
1726
import { ToolsDownloadStatusReport } from "./tools-download";
@@ -288,3 +297,21 @@ export function cleanupDatabaseClusterDirectory(
288297
}
289298
}
290299
}
300+
301+
export async function getFileCoverageInformationEnabled(
302+
debugMode: boolean,
303+
repositoryNwo: RepositoryNwo,
304+
features: FeatureEnablement,
305+
): Promise<boolean> {
306+
return (
307+
// Always enable file coverage information in debug mode
308+
debugMode ||
309+
// We're most interested in speeding up PRs, and we want to keep
310+
// submitting file coverage information for the default branch since
311+
// it is used to populate the status page.
312+
!isAnalyzingPullRequest() ||
313+
// For now, restrict this feature to the GitHub org
314+
repositoryNwo.owner !== "github" ||
315+
!(await features.getValue(Feature.SkipFileCoverageOnPrs))
316+
);
317+
}

0 commit comments

Comments
 (0)