2020-06-26 17:22:19 +01:00
"use strict" ;
2021-07-27 17:59:59 +01:00
var _ _createBinding = ( this && this . _ _createBinding ) || ( Object . create ? ( function ( o , m , k , k2 ) {
if ( k2 === undefined ) k2 = k ;
2023-01-18 20:00:33 +00:00
var desc = Object . getOwnPropertyDescriptor ( m , k ) ;
if ( ! desc || ( "get" in desc ? ! m . _ _esModule : desc . writable || desc . configurable ) ) {
desc = { enumerable : true , get : function ( ) { return m [ k ] ; } } ;
}
Object . defineProperty ( o , k2 , desc ) ;
2021-07-27 17:59:59 +01:00
} ) : ( function ( o , m , k , k2 ) {
if ( k2 === undefined ) k2 = k ;
o [ k2 ] = m [ k ] ;
} ) ) ;
var _ _setModuleDefault = ( this && this . _ _setModuleDefault ) || ( Object . create ? ( function ( o , v ) {
Object . defineProperty ( o , "default" , { enumerable : true , value : v } ) ;
} ) : function ( o , v ) {
o [ "default" ] = v ;
} ) ;
2020-06-26 17:22:19 +01:00
var _ _importStar = ( this && this . _ _importStar ) || function ( mod ) {
if ( mod && mod . _ _esModule ) return mod ;
var result = { } ;
2021-07-27 17:59:59 +01:00
if ( mod != null ) for ( var k in mod ) if ( k !== "default" && Object . prototype . hasOwnProperty . call ( mod , k ) ) _ _createBinding ( result , mod , k ) ;
_ _setModuleDefault ( result , mod ) ;
2020-06-26 17:22:19 +01:00
return result ;
} ;
var _ _importDefault = ( this && this . _ _importDefault ) || function ( mod ) {
return ( mod && mod . _ _esModule ) ? mod : { "default" : mod } ;
} ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
2022-06-27 16:01:48 +00:00
exports . stubToolRunnerConstructor = void 0 ;
2022-06-19 16:44:24 -07:00
const fs = _ _importStar ( require ( "fs" ) ) ;
2023-01-09 14:53:52 +00:00
const path _1 = _ _importDefault ( require ( "path" ) ) ;
2021-11-03 13:19:01 -07:00
const toolrunner = _ _importStar ( require ( "@actions/exec/lib/toolrunner" ) ) ;
2020-06-26 17:22:19 +01:00
const toolcache = _ _importStar ( require ( "@actions/tool-cache" ) ) ;
2022-12-02 14:05:21 +01:00
const safeWhich = _ _importStar ( require ( "@chrisgavin/safe-which" ) ) ;
2020-09-29 14:43:37 +01:00
const ava _1 = _ _importDefault ( require ( "ava" ) ) ;
2022-06-19 16:44:24 -07:00
const del _1 = _ _importDefault ( require ( "del" ) ) ;
const yaml = _ _importStar ( require ( "js-yaml" ) ) ;
2020-10-01 11:03:30 +01:00
const nock _1 = _ _importDefault ( require ( "nock" ) ) ;
2021-11-03 13:19:01 -07:00
const sinon = _ _importStar ( require ( "sinon" ) ) ;
2022-11-23 18:19:25 +00:00
const actionsUtil = _ _importStar ( require ( "./actions-util" ) ) ;
2020-10-01 11:03:30 +01:00
const codeql = _ _importStar ( require ( "./codeql" ) ) ;
const defaults = _ _importStar ( require ( "./defaults.json" ) ) ;
2022-05-09 12:02:19 +00:00
const feature _flags _1 = require ( "./feature-flags" ) ;
const languages _1 = require ( "./languages" ) ;
2020-08-25 16:19:15 +01:00
const logging _1 = require ( "./logging" ) ;
2023-07-19 17:30:23 +01:00
const setup _codeql _1 = require ( "./setup-codeql" ) ;
2020-06-26 17:22:19 +01:00
const testing _utils _1 = require ( "./testing-utils" ) ;
2020-10-01 11:03:30 +01:00
const util = _ _importStar ( require ( "./util" ) ) ;
2021-06-01 14:49:07 -07:00
const util _1 = require ( "./util" ) ;
2021-09-10 13:53:13 -07:00
( 0 , testing _utils _1 . setupTests ) ( ava _1 . default ) ;
2021-03-07 09:27:19 +00:00
const sampleGHAEApiDetails = {
auth : "token" ,
url : "https://example.githubenterprise.com" ,
2023-01-10 12:58:15 +00:00
apiURL : "https://example.githubenterprise.com/api/v3" ,
2021-03-07 09:27:19 +00:00
} ;
2022-06-19 16:44:24 -07:00
let stubConfig ;
2021-05-20 15:20:32 -07:00
ava _1 . default . beforeEach ( ( ) => {
2022-11-14 16:37:48 +00:00
( 0 , util _1 . initializeEnvironment ) ( "1.2.3" ) ;
2022-06-19 16:44:24 -07:00
stubConfig = {
languages : [ languages _1 . Language . cpp ] ,
queries : { } ,
pathsIgnore : [ ] ,
paths : [ ] ,
originalUserInput : { } ,
tempDir : "" ,
codeQLCmd : "" ,
gitHubVersion : {
type : util . GitHubVariant . DOTCOM ,
} ,
dbLocation : "" ,
packs : { } ,
debugMode : false ,
debugArtifactName : util . DEFAULT _DEBUG _ARTIFACT _NAME ,
debugDatabaseName : util . DEFAULT _DEBUG _DATABASE _NAME ,
augmentationProperties : {
packsInputCombines : false ,
queriesInputCombines : false ,
} ,
2022-08-10 15:39:35 -07:00
trapCaches : { } ,
2022-08-15 14:44:43 +01:00
trapCacheDownloadTime : 0 ,
2022-06-19 16:44:24 -07:00
} ;
2021-05-20 15:20:32 -07:00
} ) ;
2023-05-16 14:47:12 +01:00
async function installIntoToolcache ( { apiDetails = testing _utils _1 . SAMPLE _DOTCOM _API _DETAILS , cliVersion , isPinned , tagName , tmpDir , } ) {
const url = ( 0 , testing _utils _1 . mockBundleDownloadApi ) ( { apiDetails , isPinned , tagName } ) ;
2023-02-08 15:20:51 +00:00
await codeql . setupCodeQL ( cliVersion !== undefined ? undefined : url , apiDetails , tmpDir , util . GitHubVariant . GHES , cliVersion !== undefined
2023-07-07 15:02:59 +01:00
? { cliVersion , tagName }
2023-05-16 14:47:12 +01:00
: testing _utils _1 . SAMPLE _DEFAULT _CLI _VERSION , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
2023-01-09 18:18:52 +00:00
}
2023-05-16 14:47:12 +01:00
function mockReleaseApi ( { apiDetails = testing _utils _1 . SAMPLE _DOTCOM _API _DETAILS , assetNames , tagName , } ) {
2023-01-12 20:44:05 +00:00
return ( 0 , nock _1 . default ) ( apiDetails . apiURL )
. get ( ` /repos/github/codeql-action/releases/tags/ ${ tagName } ` )
. reply ( 200 , {
assets : assetNames . map ( ( name ) => ( {
name ,
} ) ) ,
tag _name : tagName ,
} ) ;
}
function mockApiDetails ( apiDetails ) {
// This is a workaround to mock `api.getApiDetails()` since it doesn't seem to be possible to
// mock this directly. The difficulty is that `getApiDetails()` is called locally in
// `api-client.ts`, but `sinon.stub(api, "getApiDetails")` only affects calls to
// `getApiDetails()` via an imported `api` module.
sinon
. stub ( actionsUtil , "getRequiredInput" )
. withArgs ( "token" )
. returns ( apiDetails . auth ) ;
const requiredEnvParamStub = sinon . stub ( util , "getRequiredEnvParam" ) ;
requiredEnvParamStub . withArgs ( "GITHUB_SERVER_URL" ) . returns ( apiDetails . url ) ;
requiredEnvParamStub
. withArgs ( "GITHUB_API_URL" )
. returns ( apiDetails . apiURL || "" ) ;
2023-01-09 18:18:52 +00:00
}
( 0 , ava _1 . default ) ( "downloads and caches explicitly requested bundles that aren't in the toolcache" , async ( t ) => {
2020-06-26 17:22:19 +01:00
await util . withTmpDir ( async ( tmpDir ) => {
2021-09-10 13:53:13 -07:00
( 0 , testing _utils _1 . setupActionsVars ) ( tmpDir , tmpDir ) ;
2020-09-14 10:44:43 +01:00
const versions = [ "20200601" , "20200610" ] ;
2020-06-26 17:22:19 +01:00
for ( let i = 0 ; i < versions . length ; i ++ ) {
const version = versions [ i ] ;
2023-05-16 14:47:12 +01:00
const url = ( 0 , testing _utils _1 . mockBundleDownloadApi ) ( {
2023-01-09 18:18:52 +00:00
tagName : ` codeql-bundle- ${ version } ` ,
isPinned : false ,
} ) ;
2023-05-16 14:47:12 +01:00
const result = await codeql . setupCodeQL ( url , testing _utils _1 . SAMPLE _DOTCOM _API _DETAILS , tmpDir , util . GitHubVariant . DOTCOM , testing _utils _1 . SAMPLE _DEFAULT _CLI _VERSION , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
2020-09-14 10:44:43 +01:00
t . assert ( toolcache . find ( "CodeQL" , ` 0.0.0- ${ version } ` ) ) ;
2023-01-10 13:16:22 +00:00
t . is ( result . toolsVersion , ` 0.0.0- ${ version } ` ) ;
2023-07-19 17:30:23 +01:00
t . is ( result . toolsSource , setup _codeql _1 . ToolsSource . Download ) ;
2023-01-27 01:03:57 -08:00
t . assert ( Number . isInteger ( result . toolsDownloadDurationMs ) ) ;
2020-06-26 17:22:19 +01:00
}
2022-08-16 15:38:55 +01:00
t . is ( toolcache . findAllVersions ( "CodeQL" ) . length , 2 ) ;
2020-09-22 14:38:27 +01:00
} ) ;
} ) ;
2023-08-10 14:36:04 +01:00
( 0 , ava _1 . default ) ( "caches semantically versioned bundles using their semantic version number" , async ( t ) => {
await util . withTmpDir ( async ( tmpDir ) => {
( 0 , testing _utils _1 . setupActionsVars ) ( tmpDir , tmpDir ) ;
const url = ( 0 , testing _utils _1 . mockBundleDownloadApi ) ( {
tagName : ` codeql-bundle-v2.14.0 ` ,
isPinned : false ,
} ) ;
const result = await codeql . setupCodeQL ( url , testing _utils _1 . SAMPLE _DOTCOM _API _DETAILS , tmpDir , util . GitHubVariant . DOTCOM , testing _utils _1 . SAMPLE _DEFAULT _CLI _VERSION , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
t . is ( toolcache . findAllVersions ( "CodeQL" ) . length , 1 ) ;
t . assert ( toolcache . find ( "CodeQL" , ` 2.14.0 ` ) ) ;
t . is ( result . toolsVersion , ` 2.14.0 ` ) ;
t . is ( result . toolsSource , setup _codeql _1 . ToolsSource . Download ) ;
t . assert ( Number . isInteger ( result . toolsDownloadDurationMs ) ) ;
} ) ;
} ) ;
2023-01-09 18:18:52 +00:00
( 0 , ava _1 . default ) ( "downloads an explicitly requested bundle even if a different version is cached" , async ( t ) => {
2020-09-22 14:38:27 +01:00
await util . withTmpDir ( async ( tmpDir ) => {
2021-09-10 13:53:13 -07:00
( 0 , testing _utils _1 . setupActionsVars ) ( tmpDir , tmpDir ) ;
2023-01-09 18:18:52 +00:00
await installIntoToolcache ( {
tagName : "codeql-bundle-20200601" ,
2022-08-16 15:38:55 +01:00
isPinned : true ,
tmpDir ,
} ) ;
2023-05-16 14:47:12 +01:00
const url = ( 0 , testing _utils _1 . mockBundleDownloadApi ) ( {
2023-01-09 18:18:52 +00:00
tagName : "codeql-bundle-20200610" ,
2022-10-05 09:16:42 -07:00
} ) ;
2023-05-16 14:47:12 +01:00
const result = await codeql . setupCodeQL ( url , testing _utils _1 . SAMPLE _DOTCOM _API _DETAILS , tmpDir , util . GitHubVariant . DOTCOM , testing _utils _1 . SAMPLE _DEFAULT _CLI _VERSION , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
2020-09-22 14:38:27 +01:00
t . assert ( toolcache . find ( "CodeQL" , "0.0.0-20200610" ) ) ;
2023-01-10 13:16:22 +00:00
t . deepEqual ( result . toolsVersion , "0.0.0-20200610" ) ;
2023-07-19 17:30:23 +01:00
t . is ( result . toolsSource , setup _codeql _1 . ToolsSource . Download ) ;
2023-01-27 01:03:57 -08:00
t . assert ( Number . isInteger ( result . toolsDownloadDurationMs ) ) ;
2020-09-22 14:38:27 +01:00
} ) ;
} ) ;
2023-01-18 15:55:23 +00:00
const EXPLICITLY _REQUESTED _BUNDLE _TEST _CASES = [
{
cliVersion : "2.10.0" ,
expectedToolcacheVersion : "2.10.0-20200610" ,
} ,
{
cliVersion : "2.10.0-pre" ,
expectedToolcacheVersion : "0.0.0-20200610" ,
} ,
2023-01-20 15:30:57 +00:00
{
cliVersion : "2.10.0+202006100101" ,
expectedToolcacheVersion : "0.0.0-20200610" ,
} ,
2023-01-18 15:55:23 +00:00
] ;
for ( const { cliVersion , expectedToolcacheVersion , } of EXPLICITLY _REQUESTED _BUNDLE _TEST _CASES ) {
( 0 , ava _1 . default ) ( ` caches an explicitly requested bundle containing CLI ${ cliVersion } as ${ expectedToolcacheVersion } ` , async ( t ) => {
await util . withTmpDir ( async ( tmpDir ) => {
( 0 , testing _utils _1 . setupActionsVars ) ( tmpDir , tmpDir ) ;
2023-05-16 14:47:12 +01:00
mockApiDetails ( testing _utils _1 . SAMPLE _DOTCOM _API _DETAILS ) ;
2023-01-18 15:55:23 +00:00
sinon . stub ( actionsUtil , "isRunningLocalAction" ) . returns ( true ) ;
const releaseApiMock = mockReleaseApi ( {
assetNames : [ ` cli-version- ${ cliVersion } .txt ` ] ,
tagName : "codeql-bundle-20200610" ,
} ) ;
2023-05-16 14:47:12 +01:00
const url = ( 0 , testing _utils _1 . mockBundleDownloadApi ) ( {
2023-01-18 15:55:23 +00:00
tagName : "codeql-bundle-20200610" ,
} ) ;
2023-05-16 14:47:12 +01:00
const result = await codeql . setupCodeQL ( url , testing _utils _1 . SAMPLE _DOTCOM _API _DETAILS , tmpDir , util . GitHubVariant . DOTCOM , testing _utils _1 . SAMPLE _DEFAULT _CLI _VERSION , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
2023-01-18 15:55:23 +00:00
t . assert ( releaseApiMock . isDone ( ) , "Releases API should have been called" ) ;
t . assert ( toolcache . find ( "CodeQL" , expectedToolcacheVersion ) ) ;
t . deepEqual ( result . toolsVersion , cliVersion ) ;
2023-07-19 17:30:23 +01:00
t . is ( result . toolsSource , setup _codeql _1 . ToolsSource . Download ) ;
2023-01-27 01:03:57 -08:00
t . assert ( Number . isInteger ( result . toolsDownloadDurationMs ) ) ;
2022-08-16 15:38:55 +01:00
} ) ;
2020-09-22 14:38:27 +01:00
} ) ;
2023-01-18 15:55:23 +00:00
}
2023-07-07 14:52:52 +01:00
for ( const toolcacheVersion of [
2023-01-26 11:16:13 +00:00
// Test that we use the tools from the toolcache when `SAMPLE_DEFAULT_CLI_VERSION` is requested
// and `SAMPLE_DEFAULT_CLI_VERSION-` is in the toolcache.
2023-07-07 14:52:52 +01:00
testing _utils _1 . SAMPLE _DEFAULT _CLI _VERSION . cliVersion ,
` ${ testing _utils _1 . SAMPLE _DEFAULT _CLI _VERSION . cliVersion } -20230101 ` ,
2023-01-12 15:39:26 +00:00
] ) {
2023-05-16 14:47:12 +01:00
( 0 , ava _1 . default ) ( ` uses tools from toolcache when ${ testing _utils _1 . SAMPLE _DEFAULT _CLI _VERSION . cliVersion } is requested and ` +
2023-01-26 11:16:13 +00:00
` ${ toolcacheVersion } is installed ` , async ( t ) => {
2023-01-10 13:37:46 +00:00
await util . withTmpDir ( async ( tmpDir ) => {
( 0 , testing _utils _1 . setupActionsVars ) ( tmpDir , tmpDir ) ;
2023-01-26 11:16:13 +00:00
sinon
. stub ( toolcache , "find" )
. withArgs ( "CodeQL" , toolcacheVersion )
. returns ( "path/to/cached/codeql" ) ;
sinon . stub ( toolcache , "findAllVersions" ) . returns ( [ toolcacheVersion ] ) ;
2023-05-16 14:47:12 +01:00
const result = await codeql . setupCodeQL ( undefined , testing _utils _1 . SAMPLE _DOTCOM _API _DETAILS , tmpDir , util . GitHubVariant . DOTCOM , testing _utils _1 . SAMPLE _DEFAULT _CLI _VERSION , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
t . is ( result . toolsVersion , testing _utils _1 . SAMPLE _DEFAULT _CLI _VERSION . cliVersion ) ;
2023-07-19 17:30:23 +01:00
t . is ( result . toolsSource , setup _codeql _1 . ToolsSource . Toolcache ) ;
2023-01-26 11:16:13 +00:00
t . is ( result . toolsDownloadDurationMs , undefined ) ;
2022-10-05 09:16:42 -07:00
} ) ;
2023-01-10 13:37:46 +00:00
} ) ;
}
2023-01-10 13:16:22 +00:00
for ( const variant of [ util . GitHubVariant . GHAE , util . GitHubVariant . GHES ] ) {
( 0 , ava _1 . default ) ( ` uses a cached bundle when no tools input is given on ${ util . GitHubVariant [ variant ] } ` , async ( t ) => {
await util . withTmpDir ( async ( tmpDir ) => {
( 0 , testing _utils _1 . setupActionsVars ) ( tmpDir , tmpDir ) ;
await installIntoToolcache ( {
tagName : "codeql-bundle-20200601" ,
isPinned : true ,
tmpDir ,
} ) ;
2023-05-16 14:47:12 +01:00
const result = await codeql . setupCodeQL ( undefined , testing _utils _1 . SAMPLE _DOTCOM _API _DETAILS , tmpDir , variant , {
2023-01-10 13:16:22 +00:00
cliVersion : defaults . cliVersion ,
tagName : defaults . bundleVersion ,
} , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
t . deepEqual ( result . toolsVersion , "0.0.0-20200601" ) ;
2023-07-19 17:30:23 +01:00
t . is ( result . toolsSource , setup _codeql _1 . ToolsSource . Toolcache ) ;
2023-01-25 11:09:18 -08:00
t . is ( result . toolsDownloadDurationMs , undefined ) ;
2023-01-10 13:16:22 +00:00
const cachedVersions = toolcache . findAllVersions ( "CodeQL" ) ;
t . is ( cachedVersions . length , 1 ) ;
2022-08-16 15:38:55 +01:00
} ) ;
2020-09-22 14:38:27 +01:00
} ) ;
2023-01-10 13:16:22 +00:00
( 0 , ava _1 . default ) ( ` downloads bundle if only an unpinned version is cached on ${ util . GitHubVariant [ variant ] } ` , async ( t ) => {
await util . withTmpDir ( async ( tmpDir ) => {
( 0 , testing _utils _1 . setupActionsVars ) ( tmpDir , tmpDir ) ;
await installIntoToolcache ( {
tagName : "codeql-bundle-20200601" ,
isPinned : false ,
tmpDir ,
} ) ;
2023-05-16 14:47:12 +01:00
( 0 , testing _utils _1 . mockBundleDownloadApi ) ( {
2023-01-10 13:16:22 +00:00
tagName : defaults . bundleVersion ,
} ) ;
2023-05-16 14:47:12 +01:00
const result = await codeql . setupCodeQL ( undefined , testing _utils _1 . SAMPLE _DOTCOM _API _DETAILS , tmpDir , variant , {
2023-01-10 13:16:22 +00:00
cliVersion : defaults . cliVersion ,
tagName : defaults . bundleVersion ,
} , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
t . deepEqual ( result . toolsVersion , defaults . cliVersion ) ;
2023-07-19 17:30:23 +01:00
t . is ( result . toolsSource , setup _codeql _1 . ToolsSource . Download ) ;
2023-01-27 01:03:57 -08:00
t . assert ( Number . isInteger ( result . toolsDownloadDurationMs ) ) ;
2023-01-10 13:16:22 +00:00
const cachedVersions = toolcache . findAllVersions ( "CodeQL" ) ;
t . is ( cachedVersions . length , 2 ) ;
2022-08-16 15:38:55 +01:00
} ) ;
2020-09-22 14:38:27 +01:00
} ) ;
2023-01-10 13:16:22 +00:00
}
2023-01-09 18:18:52 +00:00
( 0 , ava _1 . default ) ( 'downloads bundle if "latest" tools specified but not cached' , async ( t ) => {
2020-09-22 14:38:27 +01:00
await util . withTmpDir ( async ( tmpDir ) => {
2021-09-10 13:53:13 -07:00
( 0 , testing _utils _1 . setupActionsVars ) ( tmpDir , tmpDir ) ;
2023-01-09 18:18:52 +00:00
await installIntoToolcache ( {
tagName : "codeql-bundle-20200601" ,
2022-08-16 15:38:55 +01:00
isPinned : true ,
tmpDir ,
} ) ;
2023-05-16 14:47:12 +01:00
( 0 , testing _utils _1 . mockBundleDownloadApi ) ( {
2023-01-09 18:18:52 +00:00
tagName : defaults . bundleVersion ,
2022-08-16 15:38:55 +01:00
} ) ;
2023-05-16 14:47:12 +01:00
const result = await codeql . setupCodeQL ( "latest" , testing _utils _1 . SAMPLE _DOTCOM _API _DETAILS , tmpDir , util . GitHubVariant . DOTCOM , testing _utils _1 . SAMPLE _DEFAULT _CLI _VERSION , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
2023-01-10 13:16:22 +00:00
t . deepEqual ( result . toolsVersion , defaults . cliVersion ) ;
2023-07-19 17:30:23 +01:00
t . is ( result . toolsSource , setup _codeql _1 . ToolsSource . Download ) ;
2023-01-27 01:03:57 -08:00
t . assert ( Number . isInteger ( result . toolsDownloadDurationMs ) ) ;
2020-09-22 14:38:27 +01:00
const cachedVersions = toolcache . findAllVersions ( "CodeQL" ) ;
2020-06-26 17:22:19 +01:00
t . is ( cachedVersions . length , 2 ) ;
} ) ;
} ) ;
2023-02-03 19:11:40 +00:00
for ( const isBundleVersionInUrl of [ true , false ] ) {
const inclusionString = isBundleVersionInUrl
? "includes"
: "does not include" ;
( 0 , ava _1 . default ) ( ` download codeql bundle from github ae endpoint (URL ${ inclusionString } bundle version) ` , async ( t ) => {
await util . withTmpDir ( async ( tmpDir ) => {
( 0 , testing _utils _1 . setupActionsVars ) ( tmpDir , tmpDir ) ;
const bundleAssetID = 10 ;
const platform = process . platform === "win32"
? "win64"
: process . platform === "linux"
? "linux64"
: "osx64" ;
const codeQLBundleName = ` codeql-bundle- ${ platform } .tar.gz ` ;
const eventualDownloadUrl = isBundleVersionInUrl
? ` https://example.githubenterprise.com/github/codeql-action/releases/download/ ${ defaults . bundleVersion } / ${ codeQLBundleName } `
: ` https://example.githubenterprise.com/api/v3/repos/github/codeql-action/releases/assets/ ${ bundleAssetID } ` ;
( 0 , nock _1 . default ) ( "https://example.githubenterprise.com" )
. get ( ` /api/v3/enterprise/code-scanning/codeql-bundle/find/ ${ defaults . bundleVersion } ` )
. reply ( 200 , {
assets : { [ codeQLBundleName ] : bundleAssetID } ,
} ) ;
( 0 , nock _1 . default ) ( "https://example.githubenterprise.com" )
. get ( ` /api/v3/enterprise/code-scanning/codeql-bundle/download/ ${ bundleAssetID } ` )
. reply ( 200 , {
url : eventualDownloadUrl ,
} ) ;
( 0 , nock _1 . default ) ( "https://example.githubenterprise.com" )
. get ( eventualDownloadUrl . replace ( "https://example.githubenterprise.com" , "" ) )
. replyWithFile ( 200 , path _1 . default . join ( _ _dirname , ` /../src/testdata/codeql-bundle-pinned.tar.gz ` ) ) ;
mockApiDetails ( sampleGHAEApiDetails ) ;
sinon . stub ( actionsUtil , "isRunningLocalAction" ) . returns ( false ) ;
process . env [ "GITHUB_ACTION_REPOSITORY" ] = "github/codeql-action" ;
2023-02-08 15:20:51 +00:00
const result = await codeql . setupCodeQL ( undefined , sampleGHAEApiDetails , tmpDir , util . GitHubVariant . GHAE , {
2023-02-03 19:11:40 +00:00
cliVersion : defaults . cliVersion ,
tagName : defaults . bundleVersion ,
} , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
2023-07-19 17:30:23 +01:00
t . is ( result . toolsSource , setup _codeql _1 . ToolsSource . Download ) ;
2023-02-03 19:11:40 +00:00
t . assert ( Number . isInteger ( result . toolsDownloadDurationMs ) ) ;
const cachedVersions = toolcache . findAllVersions ( "CodeQL" ) ;
t . is ( cachedVersions . length , 1 ) ;
2021-03-07 09:27:19 +00:00
} ) ;
} ) ;
2023-02-03 19:11:40 +00:00
}
2023-02-06 16:12:29 +00:00
( 0 , ava _1 . default ) ( "bundle URL from another repo is cached as 0.0.0-bundleVersion" , async ( t ) => {
await util . withTmpDir ( async ( tmpDir ) => {
( 0 , testing _utils _1 . setupActionsVars ) ( tmpDir , tmpDir ) ;
2023-05-16 14:47:12 +01:00
mockApiDetails ( testing _utils _1 . SAMPLE _DOTCOM _API _DETAILS ) ;
2023-02-06 16:12:29 +00:00
sinon . stub ( actionsUtil , "isRunningLocalAction" ) . returns ( true ) ;
const releasesApiMock = mockReleaseApi ( {
assetNames : [ "cli-version-2.12.2.txt" ] ,
tagName : "codeql-bundle-20230203" ,
} ) ;
2023-05-16 14:47:12 +01:00
( 0 , testing _utils _1 . mockBundleDownloadApi ) ( {
2023-04-04 12:21:07 -07:00
repo : "codeql-testing/codeql-cli-nightlies" ,
2023-02-06 16:12:29 +00:00
platformSpecific : false ,
tagName : "codeql-bundle-20230203" ,
} ) ;
2023-05-16 14:47:12 +01:00
const result = await codeql . setupCodeQL ( "https://github.com/codeql-testing/codeql-cli-nightlies/releases/download/codeql-bundle-20230203/codeql-bundle.tar.gz" , testing _utils _1 . SAMPLE _DOTCOM _API _DETAILS , tmpDir , util . GitHubVariant . DOTCOM , testing _utils _1 . SAMPLE _DEFAULT _CLI _VERSION , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
2023-02-06 16:12:29 +00:00
t . is ( result . toolsVersion , "0.0.0-20230203" ) ;
2023-07-19 17:30:23 +01:00
t . is ( result . toolsSource , setup _codeql _1 . ToolsSource . Download ) ;
2023-02-06 16:12:29 +00:00
t . true ( Number . isInteger ( result . toolsDownloadDurationMs ) ) ;
const cachedVersions = toolcache . findAllVersions ( "CodeQL" ) ;
t . is ( cachedVersions . length , 1 ) ;
t . is ( cachedVersions [ 0 ] , "0.0.0-20230203" ) ;
t . false ( releasesApiMock . isDone ( ) ) ;
} ) ;
} ) ;
2021-09-10 13:53:13 -07:00
( 0 , ava _1 . default ) ( "getExtraOptions works for explicit paths" , ( t ) => {
2020-09-14 10:44:43 +01:00
t . deepEqual ( codeql . getExtraOptions ( { } , [ "foo" ] , [ ] ) , [ ] ) ;
t . deepEqual ( codeql . getExtraOptions ( { foo : [ 42 ] } , [ "foo" ] , [ ] ) , [ "42" ] ) ;
t . deepEqual ( codeql . getExtraOptions ( { foo : { bar : [ 42 ] } } , [ "foo" , "bar" ] , [ ] ) , [ "42" ] ) ;
2020-08-10 09:25:14 +02:00
} ) ;
2021-09-10 13:53:13 -07:00
( 0 , ava _1 . default ) ( "getExtraOptions works for wildcards" , ( t ) => {
2020-09-14 10:44:43 +01:00
t . deepEqual ( codeql . getExtraOptions ( { "*" : [ 42 ] } , [ "foo" ] , [ ] ) , [ "42" ] ) ;
2020-08-10 09:25:14 +02:00
} ) ;
2021-09-10 13:53:13 -07:00
( 0 , ava _1 . default ) ( "getExtraOptions works for wildcards and explicit paths" , ( t ) => {
2020-09-14 10:44:43 +01:00
const o1 = { "*" : [ 42 ] , foo : [ 87 ] } ;
t . deepEqual ( codeql . getExtraOptions ( o1 , [ "foo" ] , [ ] ) , [ "42" , "87" ] ) ;
const o2 = { "*" : [ 42 ] , foo : [ 87 ] } ;
t . deepEqual ( codeql . getExtraOptions ( o2 , [ "foo" , "bar" ] , [ ] ) , [ "42" ] ) ;
const o3 = { "*" : [ 42 ] , foo : { "*" : [ 87 ] , bar : [ 99 ] } } ;
const p = [ "foo" , "bar" ] ;
t . deepEqual ( codeql . getExtraOptions ( o3 , p , [ ] ) , [ "42" , "87" , "99" ] ) ;
2020-08-10 09:25:14 +02:00
} ) ;
2021-09-10 13:53:13 -07:00
( 0 , ava _1 . default ) ( "getExtraOptions throws for bad content" , ( t ) => {
2020-09-14 10:44:43 +01:00
t . throws ( ( ) => codeql . getExtraOptions ( { "*" : 42 } , [ "foo" ] , [ ] ) ) ;
t . throws ( ( ) => codeql . getExtraOptions ( { foo : 87 } , [ "foo" ] , [ ] ) ) ;
t . throws ( ( ) => codeql . getExtraOptions ( { "*" : [ 42 ] , foo : { "*" : 87 , bar : [ 99 ] } } , [ "foo" , "bar" ] , [ ] ) ) ;
2020-08-10 09:25:14 +02:00
} ) ;
2022-06-19 16:44:24 -07:00
( 0 , ava _1 . default ) ( "databaseInitCluster() without injected codescanning config" , async ( t ) => {
await util . withTmpDir ( async ( tempDir ) => {
const runnerConstructorStub = stubToolRunnerConstructor ( ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
2023-10-04 15:54:34 +01:00
sinon . stub ( codeqlObject , "getVersion" ) . resolves ( ( 0 , testing _utils _1 . makeVersionInfo ) ( "2.10.5" ) ) ;
2022-12-02 14:05:21 +01:00
// safeWhich throws because of the test CodeQL object.
sinon . stub ( safeWhich , "safeWhich" ) . resolves ( "" ) ;
2022-06-19 16:44:24 -07:00
const thisStubConfig = {
... stubConfig ,
tempDir ,
augmentationProperties : {
queriesInputCombines : false ,
packsInputCombines : false ,
} ,
} ;
2023-02-07 10:40:49 -08:00
await codeqlObject . databaseInitCluster ( thisStubConfig , "" , undefined , ( 0 , testing _utils _1 . createFeatures ) ( [ ] ) , "/path/to/qlconfig.yml" , ( 0 , logging _1 . getRunnerLogger ) ( true ) ) ;
2022-06-19 16:44:24 -07:00
const args = runnerConstructorStub . firstCall . args [ 1 ] ;
// should NOT have used an config file
const configArg = args . find ( ( arg ) => arg . startsWith ( "--codescanning-config=" ) ) ;
2023-02-07 10:40:49 -08:00
t . falsy ( configArg , "Should NOT have injected a codescanning config" ) ;
2022-06-19 16:44:24 -07:00
} ) ;
} ) ;
// Test macro for ensuring different variants of injected augmented configurations
const injectedConfigMacro = ava _1 . default . macro ( {
exec : async ( t , augmentationProperties , configOverride , expectedConfig ) => {
2022-10-05 15:54:07 -07:00
await util . withTmpDir ( async ( tempDir ) => {
const runnerConstructorStub = stubToolRunnerConstructor ( ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
sinon
. stub ( codeqlObject , "getVersion" )
2023-10-04 15:54:34 +01:00
. resolves ( ( 0 , testing _utils _1 . makeVersionInfo ) ( feature _flags _1 . featureConfig [ feature _flags _1 . Feature . CliConfigFileEnabled ] . minimumVersion ||
2023-10-04 11:28:28 +01:00
"1.0.0" ) ) ;
2022-10-05 15:54:07 -07:00
const thisStubConfig = {
... stubConfig ,
... configOverride ,
tempDir ,
augmentationProperties ,
} ;
2023-02-07 10:40:49 -08:00
await codeqlObject . databaseInitCluster ( thisStubConfig , "" , undefined , ( 0 , testing _utils _1 . createFeatures ) ( [ feature _flags _1 . Feature . CliConfigFileEnabled ] ) , undefined , ( 0 , logging _1 . getRunnerLogger ) ( true ) ) ;
2022-10-05 15:54:07 -07:00
const args = runnerConstructorStub . firstCall . args [ 1 ] ;
// should have used an config file
const configArg = args . find ( ( arg ) => arg . startsWith ( "--codescanning-config=" ) ) ;
t . truthy ( configArg , "Should have injected a codescanning config" ) ;
const configFile = configArg . split ( "=" ) [ 1 ] ;
const augmentedConfig = yaml . load ( fs . readFileSync ( configFile , "utf8" ) ) ;
t . deepEqual ( augmentedConfig , expectedConfig ) ;
await ( 0 , del _1 . default ) ( configFile , { force : true } ) ;
} ) ;
2022-06-19 16:44:24 -07:00
} ,
title : ( providedTitle = "" ) => ` databaseInitCluster() injected config: ${ providedTitle } ` ,
} ) ;
( 0 , ava _1 . default ) ( "basic" , injectedConfigMacro , {
queriesInputCombines : false ,
packsInputCombines : false ,
} , { } , { } ) ;
( 0 , ava _1 . default ) ( "injected packs from input" , injectedConfigMacro , {
queriesInputCombines : false ,
packsInputCombines : false ,
packsInput : [ "xxx" , "yyy" ] ,
} , { } , {
packs : [ "xxx" , "yyy" ] ,
} ) ;
( 0 , ava _1 . default ) ( "injected packs from input with existing packs combines" , injectedConfigMacro , {
queriesInputCombines : false ,
packsInputCombines : true ,
packsInput : [ "xxx" , "yyy" ] ,
} , {
originalUserInput : {
packs : {
cpp : [ "codeql/something-else" ] ,
} ,
} ,
} , {
packs : {
cpp : [ "codeql/something-else" , "xxx" , "yyy" ] ,
} ,
} ) ;
( 0 , ava _1 . default ) ( "injected packs from input with existing packs overrides" , injectedConfigMacro , {
queriesInputCombines : false ,
packsInputCombines : false ,
packsInput : [ "xxx" , "yyy" ] ,
} , {
originalUserInput : {
packs : {
cpp : [ "codeql/something-else" ] ,
} ,
} ,
} , {
packs : [ "xxx" , "yyy" ] ,
} ) ;
// similar, but with queries
( 0 , ava _1 . default ) ( "injected queries from input" , injectedConfigMacro , {
queriesInputCombines : false ,
packsInputCombines : false ,
queriesInput : [ { uses : "xxx" } , { uses : "yyy" } ] ,
} , { } , {
queries : [
{
uses : "xxx" ,
} ,
{
uses : "yyy" ,
} ,
] ,
} ) ;
( 0 , ava _1 . default ) ( "injected queries from input overrides" , injectedConfigMacro , {
queriesInputCombines : false ,
packsInputCombines : false ,
queriesInput : [ { uses : "xxx" } , { uses : "yyy" } ] ,
} , {
originalUserInput : {
queries : [ { uses : "zzz" } ] ,
} ,
} , {
queries : [
{
uses : "xxx" ,
} ,
{
uses : "yyy" ,
} ,
] ,
} ) ;
( 0 , ava _1 . default ) ( "injected queries from input combines" , injectedConfigMacro , {
queriesInputCombines : true ,
packsInputCombines : false ,
queriesInput : [ { uses : "xxx" } , { uses : "yyy" } ] ,
} , {
originalUserInput : {
queries : [ { uses : "zzz" } ] ,
} ,
} , {
queries : [
{
uses : "zzz" ,
} ,
{
uses : "xxx" ,
} ,
{
uses : "yyy" ,
} ,
] ,
} ) ;
( 0 , ava _1 . default ) ( "injected queries from input combines 2" , injectedConfigMacro , {
queriesInputCombines : true ,
packsInputCombines : true ,
queriesInput : [ { uses : "xxx" } , { uses : "yyy" } ] ,
} , { } , {
queries : [
{
uses : "xxx" ,
} ,
{
uses : "yyy" ,
} ,
] ,
} ) ;
( 0 , ava _1 . default ) ( "injected queries and packs, but empty" , injectedConfigMacro , {
queriesInputCombines : true ,
packsInputCombines : true ,
queriesInput : [ ] ,
packsInput : [ ] ,
} , {
originalUserInput : {
packs : [ ] ,
queries : [ ] ,
} ,
} , { } ) ;
2023-02-09 11:19:27 -08:00
( 0 , ava _1 . default ) ( "does not pass a code scanning config or qlconfig file to the CLI when CLI config passing is disabled" , async ( t ) => {
2023-03-06 10:23:28 +00:00
await util . withTmpDir ( async ( tempDir ) => {
const runnerConstructorStub = stubToolRunnerConstructor ( ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
// stubbed version doesn't matter. It just needs to be valid semver.
2023-10-04 15:54:34 +01:00
sinon . stub ( codeqlObject , "getVersion" ) . resolves ( ( 0 , testing _utils _1 . makeVersionInfo ) ( "0.0.0" ) ) ;
2023-03-06 10:23:28 +00:00
await codeqlObject . databaseInitCluster ( { ... stubConfig , tempDir } , "" , undefined , ( 0 , testing _utils _1 . createFeatures ) ( [ ] ) , "/path/to/qlconfig.yml" , ( 0 , logging _1 . getRunnerLogger ) ( true ) ) ;
const args = runnerConstructorStub . firstCall . args [ 1 ] ;
// should not have used a config file
const hasConfigArg = args . some ( ( arg ) => arg . startsWith ( "--codescanning-config=" ) ) ;
t . false ( hasConfigArg , "Should NOT have injected a codescanning config" ) ;
// should not have passed a qlconfig file
2023-03-06 10:46:36 +00:00
const hasQlconfigArg = args . some ( ( arg ) => arg . startsWith ( "--qlconfig-file=" ) ) ;
2023-03-06 10:23:28 +00:00
t . false ( hasQlconfigArg , "Should NOT have passed a qlconfig file" ) ;
} ) ;
2023-02-07 10:40:49 -08:00
} ) ;
2023-02-09 11:19:27 -08:00
( 0 , ava _1 . default ) ( "passes a code scanning config AND qlconfig to the CLI when CLI config passing is enabled" , async ( t ) => {
2023-03-06 10:23:28 +00:00
await util . withTmpDir ( async ( tempDir ) => {
const runnerConstructorStub = stubToolRunnerConstructor ( ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
sinon
. stub ( codeqlObject , "getVersion" )
2023-10-04 15:54:34 +01:00
. resolves ( ( 0 , testing _utils _1 . makeVersionInfo ) ( codeql . CODEQL _VERSION _INIT _WITH _QLCONFIG ) ) ;
2023-03-06 10:23:28 +00:00
await codeqlObject . databaseInitCluster ( { ... stubConfig , tempDir } , "" , undefined , ( 0 , testing _utils _1 . createFeatures ) ( [ feature _flags _1 . Feature . CliConfigFileEnabled ] ) , "/path/to/qlconfig.yml" , ( 0 , logging _1 . getRunnerLogger ) ( true ) ) ;
const args = runnerConstructorStub . firstCall . args [ 1 ] ;
// should have used a config file
const hasCodeScanningConfigArg = args . some ( ( arg ) => arg . startsWith ( "--codescanning-config=" ) ) ;
t . true ( hasCodeScanningConfigArg , "Should have injected a qlconfig" ) ;
// should have passed a qlconfig file
2023-03-06 10:46:36 +00:00
const hasQlconfigArg = args . some ( ( arg ) => arg . startsWith ( "--qlconfig-file=" ) ) ;
2023-03-06 10:23:28 +00:00
t . truthy ( hasQlconfigArg , "Should have injected a codescanning config" ) ;
} ) ;
2023-02-09 11:19:27 -08:00
} ) ;
( 0 , ava _1 . default ) ( "passes a code scanning config BUT NOT a qlconfig to the CLI when CLI config passing is enabled" , async ( t ) => {
2023-03-06 10:23:28 +00:00
await util . withTmpDir ( async ( tempDir ) => {
const runnerConstructorStub = stubToolRunnerConstructor ( ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
2023-10-04 15:54:34 +01:00
sinon . stub ( codeqlObject , "getVersion" ) . resolves ( ( 0 , testing _utils _1 . makeVersionInfo ) ( "2.12.2" ) ) ;
2023-03-06 10:23:28 +00:00
await codeqlObject . databaseInitCluster ( { ... stubConfig , tempDir } , "" , undefined , ( 0 , testing _utils _1 . createFeatures ) ( [ feature _flags _1 . Feature . CliConfigFileEnabled ] ) , "/path/to/qlconfig.yml" , ( 0 , logging _1 . getRunnerLogger ) ( true ) ) ;
const args = runnerConstructorStub . firstCall . args [ 1 ] ;
// should have used a config file
const hasCodeScanningConfigArg = args . some ( ( arg ) => arg . startsWith ( "--codescanning-config=" ) ) ;
2023-03-06 10:26:34 +00:00
t . true ( hasCodeScanningConfigArg , "Should have injected a codescanning config" ) ;
// should not have passed a qlconfig file
2023-03-06 10:46:36 +00:00
const hasQlconfigArg = args . some ( ( arg ) => arg . startsWith ( "--qlconfig-file=" ) ) ;
2023-03-06 10:26:34 +00:00
t . false ( hasQlconfigArg , "should NOT have injected a qlconfig" ) ;
} ) ;
} ) ;
( 0 , ava _1 . default ) ( "does not pass a qlconfig to the CLI when it is undefined" , async ( t ) => {
await util . withTmpDir ( async ( tempDir ) => {
const runnerConstructorStub = stubToolRunnerConstructor ( ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
sinon
. stub ( codeqlObject , "getVersion" )
2023-10-04 15:54:34 +01:00
. resolves ( ( 0 , testing _utils _1 . makeVersionInfo ) ( codeql . CODEQL _VERSION _INIT _WITH _QLCONFIG ) ) ;
2023-03-06 10:26:34 +00:00
await codeqlObject . databaseInitCluster ( { ... stubConfig , tempDir } , "" , undefined , ( 0 , testing _utils _1 . createFeatures ) ( [ feature _flags _1 . Feature . CliConfigFileEnabled ] ) , undefined , // undefined qlconfigFile
( 0 , logging _1 . getRunnerLogger ) ( true ) ) ;
const args = runnerConstructorStub . firstCall . args [ 1 ] ;
2023-03-06 10:46:36 +00:00
const hasQlconfigArg = args . some ( ( arg ) => arg . startsWith ( "--qlconfig-file=" ) ) ;
2023-03-06 10:26:34 +00:00
t . false ( hasQlconfigArg , "should NOT have injected a qlconfig" ) ;
2023-03-06 10:23:28 +00:00
} ) ;
2022-06-19 16:44:24 -07:00
} ) ;
2022-12-05 14:10:06 +00:00
( 0 , ava _1 . default ) ( "databaseInterpretResults() sets --sarif-add-baseline-file-info for 2.11.3" , async ( t ) => {
2022-10-26 16:14:02 +01:00
const runnerConstructorStub = stubToolRunnerConstructor ( ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
2023-10-04 15:54:34 +01:00
sinon . stub ( codeqlObject , "getVersion" ) . resolves ( ( 0 , testing _utils _1 . makeVersionInfo ) ( "2.11.3" ) ) ;
2022-12-02 14:05:21 +01:00
// safeWhich throws because of the test CodeQL object.
sinon . stub ( safeWhich , "safeWhich" ) . resolves ( "" ) ;
2023-03-24 20:14:00 +00:00
await codeqlObject . databaseInterpretResults ( "" , [ ] , "" , "" , "" , "-v" , "" , stubConfig , ( 0 , testing _utils _1 . createFeatures ) ( [ ] ) , ( 0 , logging _1 . getRunnerLogger ) ( true ) ) ;
2022-10-26 16:14:02 +01:00
t . true ( runnerConstructorStub . firstCall . args [ 1 ] . includes ( "--sarif-add-baseline-file-info" ) , "--sarif-add-baseline-file-info should be present, but it is absent" ) ;
} ) ;
2022-12-05 14:10:06 +00:00
( 0 , ava _1 . default ) ( "databaseInterpretResults() does not set --sarif-add-baseline-file-info for 2.11.2" , async ( t ) => {
2022-10-26 16:14:02 +01:00
const runnerConstructorStub = stubToolRunnerConstructor ( ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
2023-10-04 15:54:34 +01:00
sinon . stub ( codeqlObject , "getVersion" ) . resolves ( ( 0 , testing _utils _1 . makeVersionInfo ) ( "2.11.2" ) ) ;
2022-12-02 14:05:21 +01:00
// safeWhich throws because of the test CodeQL object.
sinon . stub ( safeWhich , "safeWhich" ) . resolves ( "" ) ;
2023-03-24 20:14:00 +00:00
await codeqlObject . databaseInterpretResults ( "" , [ ] , "" , "" , "" , "-v" , "" , stubConfig , ( 0 , testing _utils _1 . createFeatures ) ( [ ] ) , ( 0 , logging _1 . getRunnerLogger ) ( true ) ) ;
2022-10-26 16:14:02 +01:00
t . false ( runnerConstructorStub . firstCall . args [ 1 ] . includes ( "--sarif-add-baseline-file-info" ) , "--sarif-add-baseline-file-info must be absent, but it is present" ) ;
} ) ;
2023-07-18 13:28:06 +01:00
const NEW _ANALYSIS _SUMMARY _TEST _CASES = [
{
featureEnabled : true ,
2023-10-10 16:42:34 +01:00
codeqlVersion : "2.15.0" ,
2023-07-18 13:28:06 +01:00
flagPassed : true ,
negativeFlagPassed : false ,
} ,
{
featureEnabled : false ,
2023-10-10 16:42:34 +01:00
codeqlVersion : "2.15.0" ,
2023-07-18 13:28:06 +01:00
flagPassed : false ,
negativeFlagPassed : true ,
} ,
{
featureEnabled : false ,
2023-10-10 16:42:34 +01:00
codeqlVersion : "2.14.6" ,
2023-07-18 13:28:06 +01:00
flagPassed : false ,
negativeFlagPassed : false ,
} ,
] ;
for ( const { featureEnabled , codeqlVersion , flagPassed , negativeFlagPassed , } of NEW _ANALYSIS _SUMMARY _TEST _CASES ) {
( 0 , ava _1 . default ) ( ` database interpret-results passes ${ flagPassed
? "--new-analysis-summary"
: negativeFlagPassed
? "--no-new-analysis-summary"
: "nothing" } for CodeQL CLI v ${ codeqlVersion } when the new analysis summary feature is ${ featureEnabled ? "enabled" : "disabled" } ` , async ( t ) => {
const runnerConstructorStub = stubToolRunnerConstructor ( ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
2023-10-04 11:28:28 +01:00
sinon
. stub ( codeqlObject , "getVersion" )
2023-10-04 15:54:34 +01:00
. resolves ( ( 0 , testing _utils _1 . makeVersionInfo ) ( codeqlVersion ) ) ;
2023-07-18 13:28:06 +01:00
// safeWhich throws because of the test CodeQL object.
sinon . stub ( safeWhich , "safeWhich" ) . resolves ( "" ) ;
2023-08-11 15:51:44 +01:00
await codeqlObject . databaseInterpretResults ( "" , [ ] , "" , "" , "" , "-v" , "" , stubConfig , ( 0 , testing _utils _1 . createFeatures ) ( featureEnabled ? [ feature _flags _1 . Feature . AnalysisSummaryV2Enabled ] : [ ] ) , ( 0 , logging _1 . getRunnerLogger ) ( true ) ) ;
2023-07-18 13:28:06 +01:00
t . is ( runnerConstructorStub . firstCall . args [ 1 ] . includes ( "--new-analysis-summary" ) , flagPassed , ` --new-analysis-summary should ${ flagPassed ? "" : "n't" } be passed ` ) ;
t . is ( runnerConstructorStub . firstCall . args [ 1 ] . includes ( "--no-new-analysis-summary" ) , negativeFlagPassed , ` --no-new-analysis-summary should ${ negativeFlagPassed ? "" : "n't" } be passed ` ) ;
} ) ;
}
2023-07-20 18:16:02 +01:00
( 0 , ava _1 . default ) ( "database finalize recognises JavaScript no code found error on CodeQL 2.11.6" , async ( t ) => {
stubToolRunnerConstructor ( 1 , ` 2020-09-07T17:39:53.9050522Z [2020-09-07 17:39:53] [build] Done extracting /opt/hostedtoolcache/CodeQL/0.0.0-20200630/x64/codeql/javascript/tools/data/externs/web/ie_vml.js (3 ms)
2020-09-07T17:39:53.9051849Z [2020-09-07 17:39:53] [build-err] No JavaScript or TypeScript code found.
2020-09-07T17:39:53.9052444Z [2020-09-07 17:39:53] [build-err] No JavaScript or TypeScript code found.
2020-09-07T17:39:53.9251124Z [2020-09-07 17:39:53] [ERROR] Spawned process exited abnormally (code 255; tried to run: [/opt/hostedtoolcache/CodeQL/0.0.0-20200630/x64/codeql/javascript/tools/autobuild.sh]) ` ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
2023-10-04 15:54:34 +01:00
sinon . stub ( codeqlObject , "getVersion" ) . resolves ( ( 0 , testing _utils _1 . makeVersionInfo ) ( "2.11.6" ) ) ;
2023-07-20 18:16:02 +01:00
// safeWhich throws because of the test CodeQL object.
sinon . stub ( safeWhich , "safeWhich" ) . resolves ( "" ) ;
await t . throwsAsync ( async ( ) => await codeqlObject . finalizeDatabase ( "" , "" , "" ) , {
message : "No code found during the build. Please see: " +
"https://gh.io/troubleshooting-code-scanning/no-source-code-seen-during-build" ,
} ) ;
} ) ;
2023-07-20 18:41:28 +01:00
( 0 , ava _1 . default ) ( "database finalize overrides no code found error on CodeQL 2.11.6" , async ( t ) => {
stubToolRunnerConstructor ( 32 ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
2023-10-04 15:54:34 +01:00
sinon . stub ( codeqlObject , "getVersion" ) . resolves ( ( 0 , testing _utils _1 . makeVersionInfo ) ( "2.11.6" ) ) ;
2023-07-20 18:41:28 +01:00
// safeWhich throws because of the test CodeQL object.
sinon . stub ( safeWhich , "safeWhich" ) . resolves ( "" ) ;
await t . throwsAsync ( async ( ) => await codeqlObject . finalizeDatabase ( "" , "" , "" ) , {
message : "No code found during the build. Please see: " +
"https://gh.io/troubleshooting-code-scanning/no-source-code-seen-during-build" ,
} ) ;
} ) ;
( 0 , ava _1 . default ) ( "database finalize does not override no code found error on CodeQL 2.12.4" , async ( t ) => {
const cliMessage = "CodeQL did not detect any code written in languages supported by CodeQL. Review our troubleshooting guide at " +
"https://gh.io/troubleshooting-code-scanning/no-source-code-seen-during-build." ;
stubToolRunnerConstructor ( 32 , cliMessage ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
2023-10-04 15:54:34 +01:00
sinon . stub ( codeqlObject , "getVersion" ) . resolves ( ( 0 , testing _utils _1 . makeVersionInfo ) ( "2.12.4" ) ) ;
2023-07-20 18:41:28 +01:00
// safeWhich throws because of the test CodeQL object.
sinon . stub ( safeWhich , "safeWhich" ) . resolves ( "" ) ;
2023-07-20 18:58:37 +01:00
await t . throwsAsync ( async ( ) => await codeqlObject . finalizeDatabase ( "db" , "--threads=2" , "--ram=2048" ) , {
2023-07-21 11:40:07 +01:00
message : 'Encountered a fatal error while running "codeql-for-testing database finalize --finalize-dataset --threads=2 --ram=2048 db". ' +
2023-10-06 15:53:15 +01:00
` Exit code was 32 and last log line was: ${ cliMessage } See the logs for more details. ` ,
2023-07-20 18:41:28 +01:00
} ) ;
} ) ;
2023-07-21 15:27:57 +01:00
( 0 , ava _1 . default ) ( "runTool summarizes several fatal errors" , async ( t ) => {
const heapError = "A fatal error occurred: Evaluator heap must be at least 384.00 MiB" ;
const datasetImportError = "A fatal error occurred: Dataset import for /home/runner/work/_temp/codeql_databases/javascript/db-javascript failed with code 2" ;
const cliStderr = ` Running TRAP import for CodeQL database at /home/runner/work/_temp/codeql_databases/javascript... \n ` +
` ${ heapError } \n ${ datasetImportError } . ` ;
stubToolRunnerConstructor ( 32 , cliStderr ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
2023-10-04 15:54:34 +01:00
sinon . stub ( codeqlObject , "getVersion" ) . resolves ( ( 0 , testing _utils _1 . makeVersionInfo ) ( "2.12.4" ) ) ;
2023-07-21 15:27:57 +01:00
// safeWhich throws because of the test CodeQL object.
sinon . stub ( safeWhich , "safeWhich" ) . resolves ( "" ) ;
await t . throwsAsync ( async ( ) => await codeqlObject . finalizeDatabase ( "db" , "--threads=2" , "--ram=2048" ) , {
message : 'Encountered a fatal error while running "codeql-for-testing database finalize --finalize-dataset --threads=2 --ram=2048 db". ' +
2023-10-06 15:53:15 +01:00
` Exit code was 32 and error was: ${ datasetImportError } . Context: ${ heapError } . See the logs for more details. ` ,
} ) ;
} ) ;
( 0 , ava _1 . default ) ( "runTool outputs last line of stderr if fatal error could not be found" , async ( t ) => {
const cliStderr = "line1\nline2\nline3\nline4\nline5" ;
stubToolRunnerConstructor ( 32 , cliStderr ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
sinon . stub ( codeqlObject , "getVersion" ) . resolves ( ( 0 , testing _utils _1 . makeVersionInfo ) ( "2.12.4" ) ) ;
// safeWhich throws because of the test CodeQL object.
sinon . stub ( safeWhich , "safeWhich" ) . resolves ( "" ) ;
await t . throwsAsync ( async ( ) => await codeqlObject . finalizeDatabase ( "db" , "--threads=2" , "--ram=2048" ) , {
message : 'Encountered a fatal error while running "codeql-for-testing database finalize --finalize-dataset --threads=2 --ram=2048 db". ' +
"Exit code was 32 and last log line was: line5. See the logs for more details." ,
2023-07-21 15:27:57 +01:00
} ) ;
} ) ;
2023-07-20 18:16:02 +01:00
function stubToolRunnerConstructor ( exitCode = 0 , stderr ) {
2021-11-03 13:19:01 -07:00
const runnerObjectStub = sinon . createStubInstance ( toolrunner . ToolRunner ) ;
const runnerConstructorStub = sinon . stub ( toolrunner , "ToolRunner" ) ;
2023-07-20 18:16:02 +01:00
let stderrListener = undefined ;
runnerConstructorStub . callsFake ( ( _cmd , _args , options ) => {
stderrListener = options . listeners ? . stderr ;
return runnerObjectStub ;
} ) ;
runnerObjectStub . exec . callsFake ( async ( ) => {
if ( stderrListener !== undefined && stderr !== undefined ) {
stderrListener ( Buffer . from ( stderr ) ) ;
}
return exitCode ;
} ) ;
2021-11-03 13:19:01 -07:00
return runnerConstructorStub ;
}
2022-06-27 16:01:48 +00:00
exports . stubToolRunnerConstructor = stubToolRunnerConstructor ;
2020-06-26 17:22:19 +01:00
//# sourceMappingURL=codeql.test.js.map