@@ -6,12 +6,14 @@ import test, { ExecutionContext } from "ava";
66import * as yaml from "js-yaml" ;
77import * as sinon from "sinon" ;
88
9+ import { ActionState } from "./action-common" ;
910import * as actionsUtil from "./actions-util" ;
1011import { AnalysisKind , supportedAnalysisKinds } from "./analyses" ;
1112import * as api from "./api-client" ;
1213import { CachingKind } from "./caching-utils" ;
1314import { createStubCodeQL } from "./codeql" ;
1415import { UserConfig } from "./config/db-config" ;
16+ import * as file from "./config/file" ;
1517import * as configUtils from "./config-utils" ;
1618import * as errorMessages from "./error-messages" ;
1719import { Feature } from "./feature-flags" ;
@@ -38,6 +40,7 @@ import {
3840 makeMacro ,
3941 initAllState ,
4042 callee ,
43+ SAMPLE_DOTCOM_API_DETAILS ,
4144} from "./testing-utils" ;
4245import {
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+ } ) ;
0 commit comments