@@ -6,15 +6,101 @@ import * as sinon from "sinon";
66
77import { AnalysisKind , getAnalysisConfig } from "./analyses" ;
88import { getCodeQLForTesting } from "./codeql" ;
9+ import * as codeql from "./codeql" ;
910import { getRunnerLogger } from "./logging" ;
10- import { createFeatures , setupTests } from "./testing-utils" ;
11+ import { createFeatures , createTestConfig , setupTests } from "./testing-utils" ;
1112import { UploadResult } from "./upload-lib" ;
1213import * as uploadLib from "./upload-lib" ;
13- import { postProcessAndUploadSarif } from "./upload-sarif" ;
14+ import {
15+ getOrInitCodeQL ,
16+ postProcessAndUploadSarif ,
17+ UploadSarifState ,
18+ } from "./upload-sarif" ;
1419import * as util from "./util" ;
1520
1621setupTests ( test ) ;
1722
23+ test ( "getOrInitCodeQL - gets cached CodeQL instance when available" , async ( t ) => {
24+ const cachedCodeQL = await getCodeQLForTesting ( ) ;
25+ const getCodeQL = sinon . stub ( codeql , "getCodeQL" ) . resolves ( undefined ) ;
26+ const minimalInitCodeQL = sinon
27+ . stub ( uploadLib , "minimalInitCodeQL" )
28+ . resolves ( undefined ) ;
29+
30+ const result = await getOrInitCodeQL (
31+ { cachedCodeQL } ,
32+ getRunnerLogger ( true ) ,
33+ { type : util . GitHubVariant . GHES , version : "3.0" } ,
34+ createFeatures ( [ ] ) ,
35+ undefined ,
36+ ) ;
37+
38+ // Neither of the two functions to get a CodeQL instance were called.
39+ t . true ( getCodeQL . notCalled ) ;
40+ t . true ( minimalInitCodeQL . notCalled ) ;
41+
42+ // But we have an instance that refers to the same object as the one we put into the state.
43+ t . truthy ( result ) ;
44+ t . is ( result , cachedCodeQL ) ;
45+ } ) ;
46+
47+ test ( "getOrInitCodeQL - uses minimalInitCodeQL when there's no config" , async ( t ) => {
48+ const newInstance = await getCodeQLForTesting ( ) ;
49+ const getCodeQL = sinon . stub ( codeql , "getCodeQL" ) . resolves ( undefined ) ;
50+ const minimalInitCodeQL = sinon
51+ . stub ( uploadLib , "minimalInitCodeQL" )
52+ . resolves ( newInstance ) ;
53+
54+ const state : UploadSarifState = { cachedCodeQL : undefined } ;
55+ const result = await getOrInitCodeQL (
56+ state ,
57+ getRunnerLogger ( true ) ,
58+ { type : util . GitHubVariant . GHES , version : "3.0" } ,
59+ createFeatures ( [ ] ) ,
60+ undefined ,
61+ ) ;
62+
63+ // Check that the right function was called.
64+ t . true ( getCodeQL . notCalled ) ;
65+ t . true ( minimalInitCodeQL . calledOnce ) ;
66+
67+ // And that we received the instance that we expected.
68+ t . truthy ( result ) ;
69+ t . is ( result , newInstance ) ;
70+
71+ // And that it was cached.
72+ t . is ( state . cachedCodeQL , newInstance ) ;
73+ } ) ;
74+
75+ test ( "getOrInitCodeQL - uses getCodeQL when there's a config" , async ( t ) => {
76+ const newInstance = await getCodeQLForTesting ( ) ;
77+ const getCodeQL = sinon . stub ( codeql , "getCodeQL" ) . resolves ( newInstance ) ;
78+ const minimalInitCodeQL = sinon
79+ . stub ( uploadLib , "minimalInitCodeQL" )
80+ . resolves ( undefined ) ;
81+ const config = createTestConfig ( { } ) ;
82+
83+ const state : UploadSarifState = { cachedCodeQL : undefined } ;
84+ const result = await getOrInitCodeQL (
85+ state ,
86+ getRunnerLogger ( true ) ,
87+ { type : util . GitHubVariant . GHES , version : "3.0" } ,
88+ createFeatures ( [ ] ) ,
89+ config ,
90+ ) ;
91+
92+ // Check that the right function was called.
93+ t . true ( getCodeQL . calledOnce ) ;
94+ t . true ( minimalInitCodeQL . notCalled ) ;
95+
96+ // And that we received the instance that we expected.
97+ t . truthy ( result ) ;
98+ t . is ( result , newInstance ) ;
99+
100+ // And that it was cached.
101+ t . is ( state . cachedCodeQL , newInstance ) ;
102+ } ) ;
103+
18104interface UploadSarifExpectedResult {
19105 uploadResult ?: UploadResult ;
20106 expectedFiles ?: string [ ] ;
0 commit comments