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" ) ) ;
2023-01-10 13:37:46 +00:00
const api = _ _importStar ( require ( "./api-client" ) ) ;
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" ) ;
2023-01-25 11:09:18 -08:00
const init _1 = require ( "./init" ) ;
2022-05-09 12:02:19 +00:00
const languages _1 = require ( "./languages" ) ;
2020-08-25 16:19:15 +01:00
const logging _1 = require ( "./logging" ) ;
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 ) ;
2020-11-23 14:18:05 +00:00
const sampleApiDetails = {
auth : "token" ,
2020-11-24 11:10:25 +00:00
url : "https://github.com" ,
2023-01-10 12:58:15 +00:00
apiURL : "https://api.github.com" ,
2020-11-23 14:18:05 +00:00
} ;
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
} ;
2023-01-10 13:16:22 +00:00
const SAMPLE _DEFAULT _CLI _VERSION = {
cliVersion : "2.0.0" ,
variant : util . GitHubVariant . DOTCOM ,
} ;
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 : {
injectedMlQueries : false ,
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-01-11 15:25:21 +00:00
/**
* Mocks the API for downloading the bundle tagged `tagName`.
*
* @returns the download URL for the bundle. This can be passed to the tools parameter of
* `codeql.setupCodeQL`.
*/
2023-01-12 20:44:05 +00:00
function mockDownloadApi ( { apiDetails = sampleApiDetails , isPinned , tagName , } ) {
2022-08-16 15:38:55 +01:00
const platform = process . platform === "win32"
? "win64"
: process . platform === "linux"
? "linux64"
: "osx64" ;
2023-01-18 20:00:33 +00:00
const baseUrl = apiDetails ? . url ? ? "https://example.com" ;
2022-08-16 15:38:55 +01:00
const relativeUrl = apiDetails
2023-01-09 18:18:52 +00:00
? ` /github/codeql-action/releases/download/ ${ tagName } /codeql-bundle- ${ platform } .tar.gz `
: ` /download/ ${ tagName } /codeql-bundle.tar.gz ` ;
2022-08-16 15:38:55 +01:00
( 0 , nock _1 . default ) ( baseUrl )
. get ( relativeUrl )
2023-01-09 14:53:52 +00:00
. replyWithFile ( 200 , path _1 . default . join ( _ _dirname , ` /../src/testdata/codeql-bundle ${ isPinned ? "-pinned" : "" } .tar.gz ` ) ) ;
2023-01-09 18:18:52 +00:00
return ` ${ baseUrl } ${ relativeUrl } ` ;
2022-08-16 15:38:55 +01:00
}
2023-01-10 13:37:46 +00:00
async function installIntoToolcache ( { apiDetails = sampleApiDetails , cliVersion , isPinned , tagName , tmpDir , } ) {
2023-01-12 20:44:05 +00:00
const url = mockDownloadApi ( { apiDetails , isPinned , tagName } ) ;
2023-01-10 13:37:46 +00:00
await codeql . setupCodeQL ( cliVersion !== undefined ? undefined : url , apiDetails , tmpDir , util . GitHubVariant . GHES , false , cliVersion !== undefined
? { cliVersion , tagName , variant : util . GitHubVariant . GHES }
: SAMPLE _DEFAULT _CLI _VERSION , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
2023-01-09 18:18:52 +00:00
}
2023-01-12 20:44:05 +00:00
function mockReleaseApi ( { apiDetails = sampleApiDetails , assetNames , tagName , } ) {
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-01-12 20:44:05 +00:00
const url = mockDownloadApi ( {
2023-01-09 18:18:52 +00:00
tagName : ` codeql-bundle- ${ version } ` ,
isPinned : false ,
} ) ;
2023-01-10 13:16:22 +00:00
const result = await codeql . setupCodeQL ( url , sampleApiDetails , tmpDir , util . GitHubVariant . DOTCOM , false , 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-01-25 11:09:18 -08:00
t . is ( result . toolsSource , init _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-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-01-12 20:44:05 +00:00
const url = mockDownloadApi ( {
2023-01-09 18:18:52 +00:00
tagName : "codeql-bundle-20200610" ,
2022-10-05 09:16:42 -07:00
} ) ;
2023-01-10 13:16:22 +00:00
const result = await codeql . setupCodeQL ( url , sampleApiDetails , tmpDir , util . GitHubVariant . DOTCOM , false , 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-01-25 11:09:18 -08:00
t . is ( result . toolsSource , init _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 ) ;
mockApiDetails ( sampleApiDetails ) ;
sinon . stub ( actionsUtil , "isRunningLocalAction" ) . returns ( true ) ;
const releaseApiMock = mockReleaseApi ( {
assetNames : [ ` cli-version- ${ cliVersion } .txt ` ] ,
tagName : "codeql-bundle-20200610" ,
} ) ;
const url = mockDownloadApi ( {
tagName : "codeql-bundle-20200610" ,
} ) ;
const result = await codeql . setupCodeQL ( url , sampleApiDetails , tmpDir , util . GitHubVariant . DOTCOM , false , SAMPLE _DEFAULT _CLI _VERSION , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
t . assert ( releaseApiMock . isDone ( ) , "Releases API should have been called" ) ;
t . assert ( toolcache . find ( "CodeQL" , expectedToolcacheVersion ) ) ;
t . deepEqual ( result . toolsVersion , cliVersion ) ;
2023-01-25 11:09:18 -08:00
t . is ( result . toolsSource , init _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-01-26 11:16:13 +00:00
for ( const { githubReleases , toolcacheVersion } of [
// 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-01-12 15:39:26 +00:00
{
2023-01-26 11:16:13 +00:00
toolcacheVersion : SAMPLE _DEFAULT _CLI _VERSION . cliVersion ,
2023-01-12 15:39:26 +00:00
} ,
{
2023-01-26 11:16:13 +00:00
githubReleases : {
"codeql-bundle-20230101" : ` cli-version- ${ SAMPLE _DEFAULT _CLI _VERSION . cliVersion } .txt ` ,
} ,
toolcacheVersion : "0.0.0-20230101" ,
2023-01-12 15:39:26 +00:00
} ,
{
2023-01-26 11:16:13 +00:00
toolcacheVersion : ` ${ SAMPLE _DEFAULT _CLI _VERSION . cliVersion } -20230101 ` ,
2023-01-12 15:39:26 +00:00
} ,
] ) {
2023-01-26 11:16:13 +00:00
( 0 , ava _1 . default ) ( ` uses tools from toolcache when ${ SAMPLE _DEFAULT _CLI _VERSION . cliVersion } is requested and ` +
` ${ 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 ] ) ;
if ( githubReleases ) {
2023-01-10 13:37:46 +00:00
sinon . stub ( api , "getApiClient" ) . value ( ( ) => ( {
repos : {
listReleases : sinon . stub ( ) . resolves ( undefined ) ,
} ,
2023-01-26 11:16:13 +00:00
paginate : sinon . stub ( ) . resolves ( Object . entries ( githubReleases ) . map ( ( [ releaseTagName , cliVersionMarkerFile ] ) => ( {
assets : [
{
name : cliVersionMarkerFile ,
} ,
] ,
tag _name : releaseTagName ,
} ) ) ) ,
2023-01-10 13:37:46 +00:00
} ) ) ;
}
const result = await codeql . setupCodeQL ( undefined , sampleApiDetails , tmpDir , util . GitHubVariant . DOTCOM , false , SAMPLE _DEFAULT _CLI _VERSION , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
t . is ( result . toolsVersion , SAMPLE _DEFAULT _CLI _VERSION . cliVersion ) ;
2023-01-26 11:16:13 +00:00
t . is ( result . toolsSource , init _1 . ToolsSource . Toolcache ) ;
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 ,
} ) ;
const result = await codeql . setupCodeQL ( undefined , sampleApiDetails , tmpDir , variant , false , {
cliVersion : defaults . cliVersion ,
tagName : defaults . bundleVersion ,
variant ,
} , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
t . deepEqual ( result . toolsVersion , "0.0.0-20200601" ) ;
2023-01-25 11:09:18 -08:00
t . is ( result . toolsSource , init _1 . ToolsSource . Toolcache ) ;
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-01-12 20:44:05 +00:00
mockDownloadApi ( {
2023-01-10 13:16:22 +00:00
tagName : defaults . bundleVersion ,
} ) ;
const result = await codeql . setupCodeQL ( undefined , sampleApiDetails , tmpDir , variant , false , {
cliVersion : defaults . cliVersion ,
tagName : defaults . bundleVersion ,
variant ,
} , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
t . deepEqual ( result . toolsVersion , defaults . cliVersion ) ;
2023-01-25 11:09:18 -08:00
t . is ( result . toolsSource , init _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-01-12 20:44:05 +00:00
mockDownloadApi ( {
2023-01-09 18:18:52 +00:00
tagName : defaults . bundleVersion ,
2022-08-16 15:38:55 +01:00
} ) ;
2023-01-10 13:16:22 +00:00
const result = await codeql . setupCodeQL ( "latest" , sampleApiDetails , tmpDir , util . GitHubVariant . DOTCOM , false , SAMPLE _DEFAULT _CLI _VERSION , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
t . deepEqual ( result . toolsVersion , defaults . cliVersion ) ;
2023-01-25 11:09:18 -08:00
t . is ( result . toolsSource , init _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" ;
const result = await codeql . setupCodeQL ( undefined , sampleGHAEApiDetails , tmpDir , util . GitHubVariant . GHAE , false , {
cliVersion : defaults . cliVersion ,
tagName : defaults . bundleVersion ,
variant : util . GitHubVariant . GHAE ,
} , ( 0 , logging _1 . getRunnerLogger ) ( true ) , false ) ;
t . is ( result . toolsSource , init _1 . ToolsSource . Download ) ;
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
}
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
} ) ;
2021-11-03 13:19:01 -07:00
( 0 , ava _1 . default ) ( "databaseInterpretResults() does not set --sarif-add-query-help for 2.7.0" , async ( t ) => {
const runnerConstructorStub = stubToolRunnerConstructor ( ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
sinon . stub ( codeqlObject , "getVersion" ) . resolves ( "2.7.0" ) ;
2022-12-02 14:05:21 +01:00
// safeWhich throws because of the test CodeQL object.
sinon . stub ( safeWhich , "safeWhich" ) . resolves ( "" ) ;
2022-12-05 14:10:06 +00:00
await codeqlObject . databaseInterpretResults ( "" , [ ] , "" , "" , "" , "-v" , "" ) ;
2021-11-03 13:19:01 -07:00
t . false ( runnerConstructorStub . firstCall . args [ 1 ] . includes ( "--sarif-add-query-help" ) , "--sarif-add-query-help should be absent, but it is present" ) ;
} ) ;
( 0 , ava _1 . default ) ( "databaseInterpretResults() sets --sarif-add-query-help for 2.7.1" , async ( t ) => {
const runnerConstructorStub = stubToolRunnerConstructor ( ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
sinon . stub ( codeqlObject , "getVersion" ) . resolves ( "2.7.1" ) ;
2022-12-02 14:05:21 +01:00
// safeWhich throws because of the test CodeQL object.
sinon . stub ( safeWhich , "safeWhich" ) . resolves ( "" ) ;
2022-12-05 14:10:06 +00:00
await codeqlObject . databaseInterpretResults ( "" , [ ] , "" , "" , "" , "-v" , "" ) ;
2021-11-03 13:19:01 -07:00
t . true ( runnerConstructorStub . firstCall . args [ 1 ] . includes ( "--sarif-add-query-help" ) , "--sarif-add-query-help should be present, but it is absent" ) ;
} ) ;
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 ( ) ;
sinon . stub ( codeqlObject , "getVersion" ) . resolves ( "2.8.1" ) ;
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 : {
injectedMlQueries : false ,
queriesInputCombines : false ,
packsInputCombines : false ,
} ,
} ;
2022-11-14 18:49:17 +00:00
await codeqlObject . databaseInitCluster ( thisStubConfig , "" , undefined , ( 0 , testing _utils _1 . createFeatures ) ( [ ] ) , ( 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=" ) ) ;
t . falsy ( configArg , "Should have injected a codescanning config" ) ;
} ) ;
} ) ;
// 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-01-05 17:34:59 -08:00
. resolves ( feature _flags _1 . featureConfig [ feature _flags _1 . Feature . CliConfigFileEnabled ] . minimumVersion ) ;
2022-10-05 15:54:07 -07:00
const thisStubConfig = {
... stubConfig ,
... configOverride ,
tempDir ,
augmentationProperties ,
} ;
2022-11-14 18:49:17 +00:00
await codeqlObject . databaseInitCluster ( thisStubConfig , "" , undefined , ( 0 , testing _utils _1 . createFeatures ) ( [ feature _flags _1 . Feature . CliConfigFileEnabled ] ) , ( 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 , {
injectedMlQueries : false ,
queriesInputCombines : false ,
packsInputCombines : false ,
} , { } , { } ) ;
( 0 , ava _1 . default ) ( "injected ML queries" , injectedConfigMacro , {
injectedMlQueries : true ,
queriesInputCombines : false ,
packsInputCombines : false ,
} , { } , {
2023-01-05 17:34:59 -08:00
packs : [ "codeql/javascript-experimental-atm-queries@~0.4.0" ] ,
2022-06-19 16:44:24 -07:00
} ) ;
( 0 , ava _1 . default ) ( "injected ML queries with existing packs" , injectedConfigMacro , {
injectedMlQueries : true ,
queriesInputCombines : false ,
packsInputCombines : false ,
} , {
originalUserInput : {
packs : { javascript : [ "codeql/something-else" ] } ,
} ,
} , {
packs : {
javascript : [
"codeql/something-else" ,
2023-01-05 17:34:59 -08:00
"codeql/javascript-experimental-atm-queries@~0.4.0" ,
2022-06-19 16:44:24 -07:00
] ,
} ,
} ) ;
( 0 , ava _1 . default ) ( "injected ML queries with existing packs of different language" , injectedConfigMacro , {
injectedMlQueries : true ,
queriesInputCombines : false ,
packsInputCombines : false ,
} , {
originalUserInput : {
packs : { cpp : [ "codeql/something-else" ] } ,
} ,
} , {
packs : {
cpp : [ "codeql/something-else" ] ,
2023-01-05 17:34:59 -08:00
javascript : [ "codeql/javascript-experimental-atm-queries@~0.4.0" ] ,
2022-06-19 16:44:24 -07:00
} ,
} ) ;
( 0 , ava _1 . default ) ( "injected packs from input" , injectedConfigMacro , {
injectedMlQueries : false ,
queriesInputCombines : false ,
packsInputCombines : false ,
packsInput : [ "xxx" , "yyy" ] ,
} , { } , {
packs : [ "xxx" , "yyy" ] ,
} ) ;
( 0 , ava _1 . default ) ( "injected packs from input with existing packs combines" , injectedConfigMacro , {
injectedMlQueries : false ,
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 , {
injectedMlQueries : false ,
queriesInputCombines : false ,
packsInputCombines : false ,
packsInput : [ "xxx" , "yyy" ] ,
} , {
originalUserInput : {
packs : {
cpp : [ "codeql/something-else" ] ,
} ,
} ,
} , {
packs : [ "xxx" , "yyy" ] ,
} ) ;
( 0 , ava _1 . default ) ( "injected packs from input with existing packs overrides and ML model inject" , injectedConfigMacro , {
injectedMlQueries : true ,
queriesInputCombines : false ,
packsInputCombines : false ,
packsInput : [ "xxx" , "yyy" ] ,
} , {
originalUserInput : {
packs : {
cpp : [ "codeql/something-else" ] ,
} ,
} ,
} , {
2023-01-05 17:34:59 -08:00
packs : [ "xxx" , "yyy" , "codeql/javascript-experimental-atm-queries@~0.4.0" ] ,
2022-06-19 16:44:24 -07:00
} ) ;
// similar, but with queries
( 0 , ava _1 . default ) ( "injected queries from input" , injectedConfigMacro , {
injectedMlQueries : false ,
queriesInputCombines : false ,
packsInputCombines : false ,
queriesInput : [ { uses : "xxx" } , { uses : "yyy" } ] ,
} , { } , {
queries : [
{
uses : "xxx" ,
} ,
{
uses : "yyy" ,
} ,
] ,
} ) ;
( 0 , ava _1 . default ) ( "injected queries from input overrides" , injectedConfigMacro , {
injectedMlQueries : false ,
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 , {
injectedMlQueries : false ,
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 , {
injectedMlQueries : false ,
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 , {
injectedMlQueries : false ,
queriesInputCombines : true ,
packsInputCombines : true ,
queriesInput : [ ] ,
packsInput : [ ] ,
} , {
originalUserInput : {
packs : [ ] ,
queries : [ ] ,
} ,
} , { } ) ;
2022-08-16 13:58:59 +01:00
( 0 , ava _1 . default ) ( "does not use injected config" , async ( t ) => {
2022-06-19 16:44:24 -07:00
const origCODEQL _PASS _CONFIG _TO _CLI = process . env . CODEQL _PASS _CONFIG _TO _CLI ;
process . env [ "CODEQL_PASS_CONFIG_TO_CLI" ] = "false" ;
try {
const runnerConstructorStub = stubToolRunnerConstructor ( ) ;
const codeqlObject = await codeql . getCodeQLForTesting ( ) ;
sinon
. stub ( codeqlObject , "getVersion" )
2023-01-05 17:34:59 -08:00
. resolves ( feature _flags _1 . featureConfig [ feature _flags _1 . Feature . CliConfigFileEnabled ] . minimumVersion ) ;
2022-11-14 18:49:17 +00:00
await codeqlObject . databaseInitCluster ( stubConfig , "" , undefined , ( 0 , testing _utils _1 . createFeatures ) ( [ ] ) , ( 0 , logging _1 . getRunnerLogger ) ( true ) ) ;
2022-06-19 16:44:24 -07:00
const args = runnerConstructorStub . firstCall . args [ 1 ] ;
// should have used an config file
const configArg = args . find ( ( arg ) => arg . startsWith ( "--codescanning-config=" ) ) ;
t . falsy ( configArg , "Should NOT have injected a codescanning config" ) ;
}
finally {
process . env [ "CODEQL_PASS_CONFIG_TO_CLI" ] = origCODEQL _PASS _CONFIG _TO _CLI ;
}
} ) ;
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 ( ) ;
2022-12-05 14:10:06 +00:00
sinon . stub ( codeqlObject , "getVersion" ) . resolves ( "2.11.3" ) ;
2022-12-02 14:05:21 +01:00
// safeWhich throws because of the test CodeQL object.
sinon . stub ( safeWhich , "safeWhich" ) . resolves ( "" ) ;
2022-12-05 14:10:06 +00:00
await codeqlObject . databaseInterpretResults ( "" , [ ] , "" , "" , "" , "-v" , "" ) ;
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 ( ) ;
2022-12-05 14:10:06 +00:00
sinon . stub ( codeqlObject , "getVersion" ) . resolves ( "2.11.2" ) ;
2022-12-02 14:05:21 +01:00
// safeWhich throws because of the test CodeQL object.
sinon . stub ( safeWhich , "safeWhich" ) . resolves ( "" ) ;
2022-12-05 14:10:06 +00:00
await codeqlObject . databaseInterpretResults ( "" , [ ] , "" , "" , "" , "-v" , "" ) ;
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" ) ;
} ) ;
2021-11-03 13:19:01 -07:00
function stubToolRunnerConstructor ( ) {
const runnerObjectStub = sinon . createStubInstance ( toolrunner . ToolRunner ) ;
runnerObjectStub . exec . resolves ( 0 ) ;
const runnerConstructorStub = sinon . stub ( toolrunner , "ToolRunner" ) ;
runnerConstructorStub . returns ( runnerObjectStub ) ;
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