2020-05-04 20:28:01 +03:00
"use strict" ;
2021-07-27 19:59:59 +03:00
var _ _createBinding = ( this && this . _ _createBinding ) || ( Object . create ? ( function ( o , m , k , k2 ) {
if ( k2 === undefined ) k2 = k ;
2023-01-18 23:00:33 +03: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 19:59:59 +03: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-05-04 20:28:01 +03:00
var _ _importStar = ( this && this . _ _importStar ) || function ( mod ) {
if ( mod && mod . _ _esModule ) return mod ;
var result = { } ;
2021-07-27 19:59:59 +03: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-05-04 20:28:01 +03:00
return result ;
} ;
2020-09-29 16:43:37 +03:00
var _ _importDefault = ( this && this . _ _importDefault ) || function ( mod ) {
return ( mod && mod . _ _esModule ) ? mod : { "default" : mod } ;
} ;
2020-05-04 20:28:01 +03:00
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
const fs = _ _importStar ( require ( "fs" ) ) ;
2020-06-22 18:17:25 +03:00
const os = _ _importStar ( require ( "os" ) ) ;
2022-08-10 15:57:57 +03:00
const path _1 = _ _importDefault ( require ( "path" ) ) ;
2020-09-29 16:43:37 +03:00
const ava _1 = _ _importDefault ( require ( "ava" ) ) ;
2023-09-05 14:14:47 +03:00
const environment _1 = require ( "./environment" ) ;
2020-09-01 16:13:10 +03:00
const logging _1 = require ( "./logging" ) ;
2020-06-23 15:24:41 +03:00
const testing _utils _1 = require ( "./testing-utils" ) ;
2020-05-04 20:28:01 +03:00
const util = _ _importStar ( require ( "./util" ) ) ;
2021-09-10 23:53:13 +03:00
( 0 , testing _utils _1 . setupTests ) ( ava _1 . default ) ;
( 0 , ava _1 . default ) ( "getToolNames" , ( t ) => {
2020-09-14 12:44:43 +03:00
const input = fs . readFileSync ( ` ${ _ _dirname } /../src/testdata/tool-names.sarif ` , "utf8" ) ;
2022-01-13 02:26:34 +03:00
const toolNames = util . getToolNames ( JSON . parse ( input ) ) ;
2020-05-04 20:28:01 +03:00
t . deepEqual ( toolNames , [ "CodeQL command-line toolchain" , "ESLint" ] ) ;
} ) ;
2023-07-21 19:35:34 +03:00
const GET _MEMORY _FLAG _TESTS = [
{
input : undefined ,
totalMemoryMb : 8 * 1024 ,
platform : "linux" ,
expectedMemoryValue : 7 * 1024 ,
} ,
{
input : undefined ,
totalMemoryMb : 8 * 1024 ,
platform : "win32" ,
expectedMemoryValue : 6.5 * 1024 ,
} ,
{
input : "" ,
totalMemoryMb : 8 * 1024 ,
platform : "linux" ,
expectedMemoryValue : 7 * 1024 ,
} ,
{
input : "512" ,
totalMemoryMb : 8 * 1024 ,
platform : "linux" ,
expectedMemoryValue : 512 ,
} ,
{
input : undefined ,
totalMemoryMb : 64 * 1024 ,
platform : "linux" ,
2023-09-05 15:30:56 +03:00
expectedMemoryValue : 61644 , // Math.floor(1024 * (64 - 1 - 0.05 * (64 - 8)))
2023-07-21 19:35:34 +03:00
} ,
{
input : undefined ,
totalMemoryMb : 64 * 1024 ,
platform : "win32" ,
2023-09-05 15:30:56 +03:00
expectedMemoryValue : 61132 , // Math.floor(1024 * (64 - 1.5 - 0.05 * (64 - 8)))
2023-07-21 19:35:34 +03:00
} ,
2023-09-05 14:14:47 +03:00
{
input : undefined ,
totalMemoryMb : 64 * 1024 ,
platform : "linux" ,
2023-11-21 01:35:28 +03:00
expectedMemoryValue : 58777 , // Math.floor(1024 * (64 - 1 - 0.1 * (64 - 8)))
2023-09-05 14:14:47 +03:00
reservedPercentageValue : "10" ,
} ,
2023-07-21 19:35:34 +03:00
] ;
2023-09-05 15:30:56 +03:00
for ( const { input , totalMemoryMb , platform , expectedMemoryValue , reservedPercentageValue , } of GET _MEMORY _FLAG _TESTS ) {
( 0 , ava _1 . default ) ( ` Memory flag value is ${ expectedMemoryValue } for ${ input ? ? "no user input" } on ${ platform } with ${ totalMemoryMb } MB total system RAM ${ reservedPercentageValue
? ` and reserved percentage env var set to ${ reservedPercentageValue } `
: "" } ` , async (t) => {
2023-09-05 14:14:47 +03:00
process . env [ environment _1 . EnvVar . SCALING _RESERVED _RAM _PERCENTAGE ] =
reservedPercentageValue || undefined ;
2023-09-05 15:30:56 +03:00
const flag = util . getMemoryFlagValueForPlatform ( input , totalMemoryMb * 1024 * 1024 , platform ) ;
t . deepEqual ( flag , expectedMemoryValue ) ;
2023-07-21 19:35:34 +03:00
} ) ;
}
2023-07-07 14:13:57 +03:00
( 0 , ava _1 . default ) ( "getMemoryFlag() throws if the ram input is < 0 or NaN" , async ( t ) => {
2020-06-22 18:17:25 +03:00
for ( const input of [ "-1" , "hello!" ] ) {
2023-09-18 14:43:52 +03:00
t . throws ( ( ) => util . getMemoryFlag ( input , ( 0 , logging _1 . getRunnerLogger ) ( true ) ) ) ;
2020-06-22 18:17:25 +03:00
}
} ) ;
2021-09-10 23:53:13 +03:00
( 0 , ava _1 . default ) ( "getAddSnippetsFlag() should return the correct flag" , ( t ) => {
2020-09-10 19:18:02 +03:00
t . deepEqual ( util . getAddSnippetsFlag ( true ) , "--sarif-add-snippets" ) ;
t . deepEqual ( util . getAddSnippetsFlag ( "true" ) , "--sarif-add-snippets" ) ;
t . deepEqual ( util . getAddSnippetsFlag ( false ) , "--no-sarif-add-snippets" ) ;
t . deepEqual ( util . getAddSnippetsFlag ( undefined ) , "--no-sarif-add-snippets" ) ;
t . deepEqual ( util . getAddSnippetsFlag ( "false" ) , "--no-sarif-add-snippets" ) ;
t . deepEqual ( util . getAddSnippetsFlag ( "foo bar" ) , "--no-sarif-add-snippets" ) ;
} ) ;
2021-09-10 23:53:13 +03:00
( 0 , ava _1 . default ) ( "getThreadsFlag() should return the correct --threads flag" , ( t ) => {
2020-06-22 18:17:25 +03:00
const numCpus = os . cpus ( ) . length ;
2020-09-15 20:42:23 +03:00
const tests = [
[ "0" , "--threads=0" ] ,
[ "1" , "--threads=1" ] ,
[ undefined , ` --threads= ${ numCpus } ` ] ,
[ "" , ` --threads= ${ numCpus } ` ] ,
[ ` ${ numCpus + 1 } ` , ` --threads= ${ numCpus } ` ] ,
[ ` ${ - numCpus - 1 } ` , ` --threads= ${ - numCpus } ` ] ,
] ;
for ( const [ input , expectedFlag ] of tests ) {
2021-09-10 23:53:13 +03:00
const flag = util . getThreadsFlag ( input , ( 0 , logging _1 . getRunnerLogger ) ( true ) ) ;
2020-06-22 18:17:25 +03:00
t . deepEqual ( flag , expectedFlag ) ;
}
} ) ;
2021-09-10 23:53:13 +03:00
( 0 , ava _1 . default ) ( "getThreadsFlag() throws if the threads input is not an integer" , ( t ) => {
t . throws ( ( ) => util . getThreadsFlag ( "hello!" , ( 0 , logging _1 . getRunnerLogger ) ( true ) ) ) ;
2020-06-22 18:17:25 +03:00
} ) ;
2021-09-10 23:53:13 +03:00
( 0 , ava _1 . default ) ( "getExtraOptionsEnvParam() succeeds on valid JSON with invalid options (for now)" , ( t ) => {
2020-08-10 10:25:14 +03:00
const origExtraOptions = process . env . CODEQL _ACTION _EXTRA _OPTIONS ;
const options = { foo : 42 } ;
process . env . CODEQL _ACTION _EXTRA _OPTIONS = JSON . stringify ( options ) ;
t . deepEqual ( util . getExtraOptionsEnvParam ( ) , options ) ;
process . env . CODEQL _ACTION _EXTRA _OPTIONS = origExtraOptions ;
} ) ;
2021-09-10 23:53:13 +03:00
( 0 , ava _1 . default ) ( "getExtraOptionsEnvParam() succeeds on valid options" , ( t ) => {
2020-08-10 10:25:14 +03:00
const origExtraOptions = process . env . CODEQL _ACTION _EXTRA _OPTIONS ;
const options = { database : { init : [ "--debug" ] } } ;
2020-09-14 12:44:43 +03:00
process . env . CODEQL _ACTION _EXTRA _OPTIONS = JSON . stringify ( options ) ;
2020-08-10 10:25:14 +03:00
t . deepEqual ( util . getExtraOptionsEnvParam ( ) , options ) ;
process . env . CODEQL _ACTION _EXTRA _OPTIONS = origExtraOptions ;
} ) ;
2021-09-10 23:53:13 +03:00
( 0 , ava _1 . default ) ( "getExtraOptionsEnvParam() fails on invalid JSON" , ( t ) => {
2020-08-10 10:25:14 +03:00
const origExtraOptions = process . env . CODEQL _ACTION _EXTRA _OPTIONS ;
process . env . CODEQL _ACTION _EXTRA _OPTIONS = "{{invalid-json}}" ;
t . throws ( util . getExtraOptionsEnvParam ) ;
process . env . CODEQL _ACTION _EXTRA _OPTIONS = origExtraOptions ;
} ) ;
2021-09-10 23:53:13 +03:00
( 0 , ava _1 . default ) ( "parseGitHubUrl" , ( t ) => {
2021-02-28 09:55:55 +03:00
t . deepEqual ( util . parseGitHubUrl ( "github.com" ) , "https://github.com" ) ;
t . deepEqual ( util . parseGitHubUrl ( "https://github.com" ) , "https://github.com" ) ;
t . deepEqual ( util . parseGitHubUrl ( "https://api.github.com" ) , "https://github.com" ) ;
t . deepEqual ( util . parseGitHubUrl ( "https://github.com/foo/bar" ) , "https://github.com" ) ;
t . deepEqual ( util . parseGitHubUrl ( "github.example.com" ) , "https://github.example.com/" ) ;
t . deepEqual ( util . parseGitHubUrl ( "https://github.example.com" ) , "https://github.example.com/" ) ;
t . deepEqual ( util . parseGitHubUrl ( "https://api.github.example.com" ) , "https://github.example.com/" ) ;
t . deepEqual ( util . parseGitHubUrl ( "https://github.example.com/api/v3" ) , "https://github.example.com/" ) ;
t . deepEqual ( util . parseGitHubUrl ( "https://github.example.com:1234" ) , "https://github.example.com:1234/" ) ;
t . deepEqual ( util . parseGitHubUrl ( "https://api.github.example.com:1234" ) , "https://github.example.com:1234/" ) ;
t . deepEqual ( util . parseGitHubUrl ( "https://github.example.com:1234/api/v3" ) , "https://github.example.com:1234/" ) ;
t . deepEqual ( util . parseGitHubUrl ( "https://github.example.com/base/path" ) , "https://github.example.com/base/path/" ) ;
t . deepEqual ( util . parseGitHubUrl ( "https://github.example.com/base/path/api/v3" ) , "https://github.example.com/base/path/" ) ;
t . throws ( ( ) => util . parseGitHubUrl ( "" ) , {
2020-09-28 20:28:46 +03:00
message : '"" is not a valid URL' ,
} ) ;
2021-02-28 09:55:55 +03:00
t . throws ( ( ) => util . parseGitHubUrl ( "ssh://github.com" ) , {
2020-09-28 20:28:46 +03:00
message : '"ssh://github.com" is not a http or https URL' ,
} ) ;
2021-02-28 09:55:55 +03:00
t . throws ( ( ) => util . parseGitHubUrl ( "http:///::::433" ) , {
2020-09-28 20:28:46 +03:00
message : '"http:///::::433" is not a valid URL' ,
} ) ;
} ) ;
2021-09-10 23:53:13 +03:00
( 0 , ava _1 . default ) ( "allowed API versions" , async ( t ) => {
2020-11-26 20:54:34 +03:00
t . is ( util . apiVersionInRange ( "1.33.0" , "1.33" , "2.0" ) , undefined ) ;
t . is ( util . apiVersionInRange ( "1.33.1" , "1.33" , "2.0" ) , undefined ) ;
t . is ( util . apiVersionInRange ( "1.34.0" , "1.33" , "2.0" ) , undefined ) ;
t . is ( util . apiVersionInRange ( "2.0.0" , "1.33" , "2.0" ) , undefined ) ;
t . is ( util . apiVersionInRange ( "2.0.1" , "1.33" , "2.0" ) , undefined ) ;
t . is ( util . apiVersionInRange ( "1.32.0" , "1.33" , "2.0" ) , util . DisallowedAPIVersionReason . ACTION _TOO _NEW ) ;
t . is ( util . apiVersionInRange ( "2.1.0" , "1.33" , "2.0" ) , util . DisallowedAPIVersionReason . ACTION _TOO _OLD ) ;
} ) ;
2022-08-11 14:45:26 +03:00
( 0 , ava _1 . default ) ( "doesDirectoryExist" , async ( t ) => {
2022-08-11 17:08:06 +03:00
// Returns false if no file/dir of this name exists
2022-08-11 16:09:44 +03:00
t . false ( util . doesDirectoryExist ( "non-existent-file.txt" ) ) ;
2022-08-11 14:45:26 +03:00
await util . withTmpDir ( async ( tmpDir ) => {
2022-08-11 17:08:06 +03:00
// Returns false if file
const testFile = ` ${ tmpDir } /test-file.txt ` ;
fs . writeFileSync ( testFile , "" ) ;
t . false ( util . doesDirectoryExist ( testFile ) ) ;
// Returns true if directory
2022-08-11 14:45:26 +03:00
fs . writeFileSync ( ` ${ tmpDir } /nested-test-file.txt ` , "" ) ;
t . true ( util . doesDirectoryExist ( tmpDir ) ) ;
} ) ;
2022-08-10 15:57:57 +03:00
} ) ;
2022-08-11 14:45:26 +03:00
( 0 , ava _1 . default ) ( "listFolder" , async ( t ) => {
2022-08-11 16:09:44 +03:00
// Returns empty if not a directory
t . deepEqual ( util . listFolder ( "not-a-directory" ) , [ ] ) ;
// Returns empty if directory is empty
await util . withTmpDir ( async ( emptyTmpDir ) => {
2022-08-11 14:45:26 +03:00
t . deepEqual ( util . listFolder ( emptyTmpDir ) , [ ] ) ;
2022-08-11 16:09:44 +03:00
} ) ;
// Returns all file names in directory
await util . withTmpDir ( async ( tmpDir ) => {
const nestedDir = fs . mkdtempSync ( path _1 . default . join ( tmpDir , "nested-" ) ) ;
2022-08-11 17:46:56 +03:00
fs . writeFileSync ( path _1 . default . resolve ( nestedDir , "nested-test-file.txt" ) , "" ) ;
fs . writeFileSync ( path _1 . default . resolve ( tmpDir , "test-file-1.txt" ) , "" ) ;
fs . writeFileSync ( path _1 . default . resolve ( tmpDir , "test-file-2.txt" ) , "" ) ;
fs . writeFileSync ( path _1 . default . resolve ( tmpDir , "test-file-3.txt" ) , "" ) ;
2022-08-11 14:45:26 +03:00
t . deepEqual ( util . listFolder ( tmpDir ) , [
2022-08-11 17:46:56 +03:00
path _1 . default . resolve ( nestedDir , "nested-test-file.txt" ) ,
path _1 . default . resolve ( tmpDir , "test-file-1.txt" ) ,
path _1 . default . resolve ( tmpDir , "test-file-2.txt" ) ,
path _1 . default . resolve ( tmpDir , "test-file-3.txt" ) ,
2022-08-11 14:45:26 +03:00
] ) ;
} ) ;
2022-08-10 15:57:57 +03:00
} ) ;
2022-09-30 12:44:36 +03:00
const longTime = 999999 ;
const shortTime = 10 ;
( 0 , ava _1 . default ) ( "withTimeout on long task" , async ( t ) => {
let longTaskTimedOut = false ;
const longTask = new Promise ( ( resolve ) => {
setTimeout ( ( ) => {
resolve ( 42 ) ;
} , longTime ) ;
} ) ;
const result = await util . withTimeout ( shortTime , longTask , ( ) => {
longTaskTimedOut = true ;
} ) ;
t . deepEqual ( longTaskTimedOut , true ) ;
t . deepEqual ( result , undefined ) ;
} ) ;
( 0 , ava _1 . default ) ( "withTimeout on short task" , async ( t ) => {
let shortTaskTimedOut = false ;
const shortTask = new Promise ( ( resolve ) => {
setTimeout ( ( ) => {
resolve ( 99 ) ;
} , shortTime ) ;
} ) ;
const result = await util . withTimeout ( longTime , shortTask , ( ) => {
shortTaskTimedOut = true ;
} ) ;
t . deepEqual ( shortTaskTimedOut , false ) ;
t . deepEqual ( result , 99 ) ;
} ) ;
2022-10-11 12:04:21 +03:00
( 0 , ava _1 . default ) ( "withTimeout doesn't call callback if promise resolves" , async ( t ) => {
let shortTaskTimedOut = false ;
const shortTask = new Promise ( ( resolve ) => {
setTimeout ( ( ) => {
resolve ( 99 ) ;
} , shortTime ) ;
} ) ;
const result = await util . withTimeout ( 100 , shortTask , ( ) => {
shortTaskTimedOut = true ;
} ) ;
await new Promise ( ( r ) => setTimeout ( r , 200 ) ) ;
t . deepEqual ( shortTaskTimedOut , false ) ;
t . deepEqual ( result , 99 ) ;
} ) ;
2023-03-24 23:01:35 +03:00
function createMockSarifWithNotification ( locations ) {
return {
runs : [
{
tool : {
driver : {
name : "CodeQL" ,
} ,
} ,
invocations : [
{
toolExecutionNotifications : [
{
locations ,
} ,
] ,
} ,
] ,
} ,
] ,
} ;
}
const stubLocation = {
physicalLocation : {
artifactLocation : {
uri : "file1" ,
} ,
} ,
} ;
( 0 , ava _1 . default ) ( "fixInvalidNotifications leaves notifications with unique locations alone" , ( t ) => {
const messages = [ ] ;
const result = util . fixInvalidNotifications ( createMockSarifWithNotification ( [ stubLocation ] ) , ( 0 , testing _utils _1 . getRecordingLogger ) ( messages ) ) ;
t . deepEqual ( result , createMockSarifWithNotification ( [ stubLocation ] ) ) ;
2023-04-04 18:46:45 +03:00
t . is ( messages . length , 1 ) ;
t . deepEqual ( messages [ 0 ] , {
type : "debug" ,
message : "No duplicate locations found in SARIF notification objects." ,
} ) ;
2023-03-24 23:01:35 +03:00
} ) ;
( 0 , ava _1 . default ) ( "fixInvalidNotifications removes duplicate locations" , ( t ) => {
const messages = [ ] ;
const result = util . fixInvalidNotifications ( createMockSarifWithNotification ( [ stubLocation , stubLocation ] ) , ( 0 , testing _utils _1 . getRecordingLogger ) ( messages ) ) ;
t . deepEqual ( result , createMockSarifWithNotification ( [ stubLocation ] ) ) ;
t . is ( messages . length , 1 ) ;
t . deepEqual ( messages [ 0 ] , {
type : "info" ,
message : "Removed 1 duplicate locations from SARIF notification objects." ,
} ) ;
} ) ;
2020-05-13 18:31:24 +03:00
//# sourceMappingURL=util.test.js.map