11import test , { ExecutionContext } from "ava" ;
22
3+ import { AnalysisKind } from "../analyses" ;
34import { RepositoryProperties } from "../feature-flags/properties" ;
45import { BuiltInLanguage , Language } from "../languages" ;
56import { getRunnerLogger } from "../logging" ;
@@ -14,6 +15,117 @@ import { ConfigurationError, prettyPrintPack } from "../util";
1415
1516import * as dbConfig from "./db-config" ;
1617
18+ function makeUserConfigWithActionExtensions ( options ?: {
19+ analysisConfig ?: dbConfig . UserConfig ;
20+ analysisKinds ?: AnalysisKind [ ] ;
21+ baseConfig ?: dbConfig . UserConfig ;
22+ } ) {
23+ const testConfig = { ...options ?. baseConfig } ;
24+
25+ const allAnalysisKinds = Object . values ( AnalysisKind ) ;
26+ const analysisKinds = options ?. analysisKinds ?? allAnalysisKinds ;
27+ for ( const analysisKind of analysisKinds ) {
28+ testConfig [ analysisKind ] = { ...options ?. analysisConfig } ;
29+ }
30+
31+ return testConfig as dbConfig . UserConfigWithActionExtensions ;
32+ }
33+
34+ test ( "hasAnalysisKindKey - finds `AnalysisKind` sections" , async ( t ) => {
35+ const testConfig = makeUserConfigWithActionExtensions ( ) ;
36+
37+ for ( const analysisKind of Object . values ( AnalysisKind ) ) {
38+ // Check that it finds the `analysisKind`-specific section among multiple.
39+ t . true ( dbConfig . hasAnalysisKindKey ( analysisKind , testConfig ) ) ;
40+ // And for just the `analysisKind`-specific section.
41+ t . true ( dbConfig . hasAnalysisKindKey ( analysisKind , { [ analysisKind ] : { } } ) ) ;
42+ }
43+ } ) ;
44+
45+ test ( "hasAnalysisKindKey - returns false if there is no relevant section" , async ( t ) => {
46+ const testConfig = makeUserConfigWithActionExtensions ( ) ;
47+
48+ for ( const analysisKind of Object . values ( AnalysisKind ) ) {
49+ // Clone the `testConfig` and remove the `analysisKind`-specific key.
50+ const testConfigClone = { ...testConfig } ;
51+ delete testConfigClone [ analysisKind ] ;
52+
53+ // Check that it doesn't find an `analysisKind`-specific section.
54+ t . false ( dbConfig . hasAnalysisKindKey ( analysisKind , testConfigClone ) ) ;
55+ }
56+ } ) ;
57+
58+ test ( "applyAnalysisKindConfig - applies analysis-specific settings" , async ( t ) => {
59+ for ( const analysisKind of Object . values ( AnalysisKind ) ) {
60+ const testConfig = makeUserConfigWithActionExtensions ( {
61+ analysisKinds : [ analysisKind ] ,
62+ analysisConfig : {
63+ "paths-ignore" : [ ] ,
64+ } ,
65+ baseConfig : {
66+ "paths-ignore" : [ "a" , "b" , "c" ] ,
67+ "threat-models" : [ "local" ] ,
68+ } ,
69+ } ) ;
70+ const clonedTestConfig = { ...testConfig } ;
71+
72+ const result = dbConfig . applyAnalysisKindConfig ( analysisKind , testConfig ) ;
73+
74+ // The `analysisKind`-specific section should have been removed.
75+ // `threat-models` should be unchanged from the `baseConfig`.
76+ // `paths-ignore` should have been overwritten with the `analysisKind`-specific config.
77+ t . deepEqual ( result , {
78+ "threat-models" : [ "local" ] ,
79+ "paths-ignore" : [ ] ,
80+ } ) ;
81+
82+ // The input `testConfig` should not have changed.
83+ t . deepEqual ( testConfig , clonedTestConfig ) ;
84+ }
85+ } ) ;
86+
87+ test ( "applyAnalysisKindConfig - removes all `AnalysisKind`-specific keys" , async ( t ) => {
88+ for ( const analysisKind of Object . values ( AnalysisKind ) ) {
89+ const testConfig = makeUserConfigWithActionExtensions ( {
90+ analysisConfig : {
91+ "paths-ignore" : [ "a" , "b" , "c" ] ,
92+ } ,
93+ baseConfig : { } ,
94+ } ) ;
95+
96+ const result = dbConfig . applyAnalysisKindConfig ( analysisKind , testConfig ) ;
97+
98+ // All `AnalysisKind`-specific sections should have been removed.
99+ // `paths-ignore` should have been overwritten with the analysis-specific config.
100+ t . deepEqual ( result , {
101+ "paths-ignore" : [ "a" , "b" , "c" ] ,
102+ } ) ;
103+ }
104+ } ) ;
105+
106+ test ( "applyAnalysisKindConfig - returns the `baseConfig` if there is no `analysisKind`-specific key" , async ( t ) => {
107+ const baseConfig = {
108+ "disable-default-queries" : true ,
109+ "paths-ignore" : [ "a" , "b" , "c" ] ,
110+ queries : [ { name : "foo" , uses : "something" } ] ,
111+ packs : { test : [ "bar" ] } ,
112+ "default-setup" : { org : { "model-packs" : [ "pack" ] } } ,
113+ } satisfies dbConfig . UserConfig ;
114+
115+ const testConfig = makeUserConfigWithActionExtensions ( {
116+ analysisKinds : [ ] ,
117+ baseConfig,
118+ } ) ;
119+
120+ const result = dbConfig . applyAnalysisKindConfig (
121+ AnalysisKind . CodeScanning ,
122+ testConfig ,
123+ ) ;
124+
125+ // The result should be the same as the `baseConfig`.
126+ t . deepEqual ( result , baseConfig ) ;
127+ } ) ;
128+
17129/**
18130 * Test macro for ensuring the packs block is valid
19131 */
0 commit comments