Skip to content

Commit ca80299

Browse files
committed
Add tests for loadUserConfig
1 parent 29d5e4c commit ca80299

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

‎src/config-utils.test.ts‎

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

9+
import { ActionState } from "./action-common";
910
import * as actionsUtil from "./actions-util";
1011
import { AnalysisKind, supportedAnalysisKinds } from "./analyses";
1112
import * as api from "./api-client";
1213
import { CachingKind } from "./caching-utils";
1314
import { createStubCodeQL } from "./codeql";
1415
import { UserConfig } from "./config/db-config";
16+
import * as file from "./config/file";
1517
import * as configUtils from "./config-utils";
1618
import * as errorMessages from "./error-messages";
1719
import { Feature } from "./feature-flags";
@@ -38,6 +40,7 @@ import {
3840
makeMacro,
3941
initAllState,
4042
callee,
43+
SAMPLE_DOTCOM_API_DETAILS,
4144
} from "./testing-utils";
4245
import {
4346
GitHubVariant,
@@ -2529,3 +2532,69 @@ test("determineUserConfig - ignores config file input outside Default Setup if F
25292532
});
25302533
});
25312534
});
2535+
2536+
test("loadUserConfig - loads local configuration files", async (t) => {
2537+
await withTmpDir(async (workspaceDir) => {
2538+
await withTmpDir(async (tmpDir) => {
2539+
// Construct the test target.
2540+
const loadUserConfig = (
2541+
actionState: ActionState<["Logger", "Env", "FeatureFlags"]>,
2542+
filePath: string,
2543+
) =>
2544+
configUtils.loadUserConfig(
2545+
actionState,
2546+
filePath,
2547+
workspaceDir,
2548+
SAMPLE_DOTCOM_API_DETAILS,
2549+
tmpDir,
2550+
);
2551+
const target = callee(loadUserConfig);
2552+
2553+
// `loadUserConfig` should load local configuration files if they are inside the workspace:
2554+
const insideOfWorkspace = path.join(workspaceDir, "some-file.yml");
2555+
fs.writeFileSync(insideOfWorkspace, "test-key: present", "utf8");
2556+
2557+
await target
2558+
.withArgs(insideOfWorkspace)
2559+
.passes(t.deepEqual, { "test-key": "present" });
2560+
2561+
// `loadUserConfig` should normally throw if the path is outside of the workspace:
2562+
const outsideOfWorkspace = path.join(
2563+
tmpDir,
2564+
"not-the-generated-file.yml",
2565+
);
2566+
fs.writeFileSync(outsideOfWorkspace, "test-key: present", "utf8");
2567+
2568+
await target
2569+
.withArgs(outsideOfWorkspace)
2570+
.throws(t, { instanceOf: ConfigurationError });
2571+
2572+
// `loadUserConfig` does not throw if the path is the result of `userConfigFromActionPath`:
2573+
const generatedPath = configUtils.userConfigFromActionPath(tmpDir);
2574+
fs.writeFileSync(generatedPath, "test-key: present", "utf8");
2575+
2576+
await target
2577+
.withArgs(generatedPath)
2578+
.passes(t.deepEqual, { "test-key": "present" });
2579+
});
2580+
});
2581+
});
2582+
2583+
test.serial("loadUserConfig - loads remote configuration files", async (t) => {
2584+
await withTmpDir(async (tmpDir) => {
2585+
const getRemoteConfig = sinon.stub(file, "getRemoteConfig").resolves({});
2586+
2587+
const remoteAddress = "owner/repo/file@ref";
2588+
await callee(configUtils.loadUserConfig)
2589+
.withArgs(remoteAddress, tmpDir, SAMPLE_DOTCOM_API_DETAILS, tmpDir)
2590+
.passes(t.deepEqual, {});
2591+
2592+
t.true(
2593+
getRemoteConfig.calledOnceWithExactly(
2594+
sinon.match.any,
2595+
remoteAddress,
2596+
SAMPLE_DOTCOM_API_DETAILS,
2597+
),
2598+
);
2599+
});
2600+
});

‎src/config-utils.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ async function downloadCacheWithTime(
478478
* @param tempDir The temporary directory which may contain a CodeQL Action-generated configuration file.
479479
* @returns The loaded configuration file, if successful.
480480
*/
481-
async function loadUserConfig(
481+
export async function loadUserConfig(
482482
actionState: ActionState<["Logger", "Env", "FeatureFlags"]>,
483483
configFile: string,
484484
workspacePath: string,

0 commit comments

Comments
 (0)