Skip to content

Commit c497c26

Browse files
committed
Add getOverlayDatabaseMode() tests
1 parent c3ebad2 commit c497c26

File tree

2 files changed

+326
-1
lines changed

2 files changed

+326
-1
lines changed

src/config-utils.test.ts

Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import test, { ExecutionContext } from "ava";
66
import * as yaml from "js-yaml";
77
import * as sinon from "sinon";
88

9+
import * as actionsUtil from "./actions-util";
910
import * as api from "./api-client";
1011
import { CachingKind } from "./caching-utils";
1112
import {
@@ -16,15 +17,18 @@ import {
1617
} from "./codeql";
1718
import * as configUtils from "./config-utils";
1819
import { Feature } from "./feature-flags";
20+
import * as gitUtils from "./git-utils";
1921
import { Language } from "./languages";
2022
import { getRunnerLogger } from "./logging";
23+
import { OverlayDatabaseMode } from "./overlay-database-utils";
2124
import { parseRepositoryNwo } from "./repository";
2225
import {
2326
setupTests,
2427
mockLanguagesInRepo as mockLanguagesInRepo,
2528
createFeatures,
2629
getRecordingLogger,
2730
LoggedMessage,
31+
mockCodeQLVersion,
2832
} from "./testing-utils";
2933
import {
3034
GitHubVariant,
@@ -1199,3 +1203,324 @@ for (const { displayName, language, feature } of [
11991203
]);
12001204
});
12011205
}
1206+
1207+
interface OverlayDatabaseModeTestSetup {
1208+
overlayDatabaseEnvVar: string | undefined;
1209+
isFeatureEnabled: boolean;
1210+
isPullRequest: boolean;
1211+
isDefaultBranch: boolean;
1212+
repositoryOwner: string;
1213+
buildMode: BuildMode | undefined;
1214+
languages: Language[];
1215+
codeqlVersion: string;
1216+
gitRoot: string | undefined;
1217+
}
1218+
1219+
const defaultOverlayDatabaseModeTestSetup: OverlayDatabaseModeTestSetup = {
1220+
overlayDatabaseEnvVar: undefined,
1221+
isFeatureEnabled: false,
1222+
isPullRequest: false,
1223+
isDefaultBranch: false,
1224+
repositoryOwner: "github",
1225+
buildMode: BuildMode.None,
1226+
languages: [Language.javascript],
1227+
codeqlVersion: "2.21.0",
1228+
gitRoot: "/some/git/root",
1229+
};
1230+
1231+
const getOverlayDatabaseModeMacro = test.macro({
1232+
exec: async (
1233+
t: ExecutionContext,
1234+
_title: string,
1235+
setupOverrides: Partial<OverlayDatabaseModeTestSetup>,
1236+
expected: {
1237+
overlayDatabaseMode: OverlayDatabaseMode;
1238+
useOverlayDatabaseCaching: boolean;
1239+
},
1240+
) => {
1241+
return await withTmpDir(async (tempDir) => {
1242+
const messages: LoggedMessage[] = [];
1243+
const logger = getRecordingLogger(messages);
1244+
1245+
// Save the original environment
1246+
const originalEnv = { ...process.env };
1247+
1248+
try {
1249+
const setup = {
1250+
...defaultOverlayDatabaseModeTestSetup,
1251+
...setupOverrides,
1252+
};
1253+
1254+
// Set up environment variable if specified
1255+
delete process.env.CODEQL_OVERLAY_DATABASE_MODE;
1256+
if (setup.overlayDatabaseEnvVar !== undefined) {
1257+
process.env.CODEQL_OVERLAY_DATABASE_MODE =
1258+
setup.overlayDatabaseEnvVar;
1259+
}
1260+
1261+
// Mock feature flags
1262+
const features = createFeatures(
1263+
setup.isFeatureEnabled ? [Feature.OverlayAnalysis] : [],
1264+
);
1265+
1266+
// Mock isAnalyzingPullRequest function
1267+
sinon
1268+
.stub(actionsUtil, "isAnalyzingPullRequest")
1269+
.returns(setup.isPullRequest);
1270+
1271+
// Mock repository owner
1272+
const repository = {
1273+
owner: setup.repositoryOwner,
1274+
repo: "test-repo",
1275+
};
1276+
1277+
// Set up CodeQL mock
1278+
const codeql = mockCodeQLVersion(setup.codeqlVersion);
1279+
1280+
// Mock git root detection
1281+
if (setup.gitRoot !== undefined) {
1282+
sinon.stub(gitUtils, "getGitRoot").resolves(setup.gitRoot);
1283+
}
1284+
1285+
// Mock default branch detection
1286+
sinon
1287+
.stub(gitUtils, "isAnalyzingDefaultBranch")
1288+
.resolves(setup.isDefaultBranch);
1289+
1290+
const result = await configUtils.getOverlayDatabaseMode(
1291+
codeql,
1292+
repository,
1293+
features,
1294+
setup.languages,
1295+
tempDir, // sourceRoot
1296+
setup.buildMode,
1297+
logger,
1298+
);
1299+
1300+
t.deepEqual(result, expected);
1301+
} finally {
1302+
// Restore the original environment
1303+
process.env = originalEnv;
1304+
}
1305+
});
1306+
},
1307+
title: (_, title) => `getOverlayDatabaseMode: ${title}`,
1308+
});
1309+
1310+
test(
1311+
getOverlayDatabaseModeMacro,
1312+
"Environment variable override - Overlay",
1313+
{
1314+
overlayDatabaseEnvVar: "overlay",
1315+
},
1316+
{
1317+
overlayDatabaseMode: OverlayDatabaseMode.Overlay,
1318+
useOverlayDatabaseCaching: false,
1319+
},
1320+
);
1321+
1322+
test(
1323+
getOverlayDatabaseModeMacro,
1324+
"Environment variable override - OverlayBase",
1325+
{
1326+
overlayDatabaseEnvVar: "overlay-base",
1327+
},
1328+
{
1329+
overlayDatabaseMode: OverlayDatabaseMode.OverlayBase,
1330+
useOverlayDatabaseCaching: false,
1331+
},
1332+
);
1333+
1334+
test(
1335+
getOverlayDatabaseModeMacro,
1336+
"Environment variable override - None",
1337+
{
1338+
overlayDatabaseEnvVar: "none",
1339+
},
1340+
{
1341+
overlayDatabaseMode: OverlayDatabaseMode.None,
1342+
useOverlayDatabaseCaching: false,
1343+
},
1344+
);
1345+
1346+
test(
1347+
getOverlayDatabaseModeMacro,
1348+
"Ignore invalid environment variable",
1349+
{
1350+
overlayDatabaseEnvVar: "invalid-mode",
1351+
},
1352+
{
1353+
overlayDatabaseMode: OverlayDatabaseMode.None,
1354+
useOverlayDatabaseCaching: false,
1355+
},
1356+
);
1357+
1358+
test(
1359+
getOverlayDatabaseModeMacro,
1360+
"Ignore feature flag when analyzing non-default branch",
1361+
{
1362+
isFeatureEnabled: true,
1363+
},
1364+
{
1365+
overlayDatabaseMode: OverlayDatabaseMode.None,
1366+
useOverlayDatabaseCaching: false,
1367+
},
1368+
);
1369+
1370+
test(
1371+
getOverlayDatabaseModeMacro,
1372+
"Overlay-base database on default branch when feature enabled",
1373+
{
1374+
isFeatureEnabled: true,
1375+
isDefaultBranch: true,
1376+
},
1377+
{
1378+
overlayDatabaseMode: OverlayDatabaseMode.OverlayBase,
1379+
useOverlayDatabaseCaching: true,
1380+
},
1381+
);
1382+
1383+
test(
1384+
getOverlayDatabaseModeMacro,
1385+
"No overlay-base database on default branch when feature disabled",
1386+
{
1387+
isDefaultBranch: true,
1388+
},
1389+
{
1390+
overlayDatabaseMode: OverlayDatabaseMode.None,
1391+
useOverlayDatabaseCaching: false,
1392+
},
1393+
);
1394+
1395+
test(
1396+
getOverlayDatabaseModeMacro,
1397+
"Overlay analysis on PR when feature enabled",
1398+
{
1399+
isFeatureEnabled: true,
1400+
isPullRequest: true,
1401+
},
1402+
{
1403+
overlayDatabaseMode: OverlayDatabaseMode.Overlay,
1404+
useOverlayDatabaseCaching: true,
1405+
},
1406+
);
1407+
1408+
test(
1409+
getOverlayDatabaseModeMacro,
1410+
"No overlay analysis on PR when feature disabled",
1411+
{
1412+
isPullRequest: true,
1413+
},
1414+
{
1415+
overlayDatabaseMode: OverlayDatabaseMode.None,
1416+
useOverlayDatabaseCaching: false,
1417+
},
1418+
);
1419+
1420+
test(
1421+
getOverlayDatabaseModeMacro,
1422+
"Overlay PR analysis by env for dsp-testing",
1423+
{
1424+
overlayDatabaseEnvVar: "overlay",
1425+
repositoryOwner: "dsp-testing",
1426+
},
1427+
{
1428+
overlayDatabaseMode: OverlayDatabaseMode.Overlay,
1429+
useOverlayDatabaseCaching: false,
1430+
},
1431+
);
1432+
1433+
test(
1434+
getOverlayDatabaseModeMacro,
1435+
"Overlay PR analysis by env for other-org",
1436+
{
1437+
overlayDatabaseEnvVar: "overlay",
1438+
repositoryOwner: "other-org",
1439+
},
1440+
{
1441+
overlayDatabaseMode: OverlayDatabaseMode.Overlay,
1442+
useOverlayDatabaseCaching: false,
1443+
},
1444+
);
1445+
1446+
test(
1447+
getOverlayDatabaseModeMacro,
1448+
"Overlay PR analysis by feature flag for dsp-testing",
1449+
{
1450+
isFeatureEnabled: true,
1451+
isPullRequest: true,
1452+
repositoryOwner: "dsp-testing",
1453+
},
1454+
{
1455+
overlayDatabaseMode: OverlayDatabaseMode.Overlay,
1456+
useOverlayDatabaseCaching: true,
1457+
},
1458+
);
1459+
1460+
test(
1461+
getOverlayDatabaseModeMacro,
1462+
"No overlay PR analysis by feature flag for other-org",
1463+
{
1464+
isFeatureEnabled: true,
1465+
isPullRequest: true,
1466+
repositoryOwner: "other-org",
1467+
},
1468+
{
1469+
overlayDatabaseMode: OverlayDatabaseMode.None,
1470+
useOverlayDatabaseCaching: false,
1471+
},
1472+
);
1473+
1474+
test(
1475+
getOverlayDatabaseModeMacro,
1476+
"Fallback due to autobuild with traced language",
1477+
{
1478+
overlayDatabaseEnvVar: "overlay",
1479+
buildMode: BuildMode.Autobuild,
1480+
languages: [Language.java],
1481+
},
1482+
{
1483+
overlayDatabaseMode: OverlayDatabaseMode.None,
1484+
useOverlayDatabaseCaching: false,
1485+
},
1486+
);
1487+
1488+
test(
1489+
getOverlayDatabaseModeMacro,
1490+
"Fallback due to no build mode with traced language",
1491+
{
1492+
overlayDatabaseEnvVar: "overlay",
1493+
buildMode: undefined,
1494+
languages: [Language.java],
1495+
},
1496+
{
1497+
overlayDatabaseMode: OverlayDatabaseMode.None,
1498+
useOverlayDatabaseCaching: false,
1499+
},
1500+
);
1501+
1502+
test(
1503+
getOverlayDatabaseModeMacro,
1504+
"Fallback due to old CodeQL version",
1505+
{
1506+
overlayDatabaseEnvVar: "overlay",
1507+
codeqlVersion: "2.14.0",
1508+
},
1509+
{
1510+
overlayDatabaseMode: OverlayDatabaseMode.None,
1511+
useOverlayDatabaseCaching: false,
1512+
},
1513+
);
1514+
1515+
test(
1516+
getOverlayDatabaseModeMacro,
1517+
"Fallback due to missing git root",
1518+
{
1519+
overlayDatabaseEnvVar: "overlay",
1520+
gitRoot: undefined,
1521+
},
1522+
{
1523+
overlayDatabaseMode: OverlayDatabaseMode.None,
1524+
useOverlayDatabaseCaching: false,
1525+
},
1526+
);

src/config-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ function parseQueriesFromInput(
789789
* @returns An object containing the overlay database mode and whether the
790790
* action should perform overlay-base database caching.
791791
*/
792-
async function getOverlayDatabaseMode(
792+
export async function getOverlayDatabaseMode(
793793
codeql: CodeQL,
794794
repository: RepositoryNwo,
795795
features: FeatureEnablement,

0 commit comments

Comments
 (0)