2022-03-30 18:16:49 +08:00
import * as cache from "@actions/cache" ;
2019-11-14 05:13:00 +08:00
import * as core from "@actions/core" ;
2022-12-21 22:08:44 +08:00
import { Events , RefKey } from "../src/constants" ;
2019-11-14 05:13:00 +08:00
import * as actionUtils from "../src/utils/actionUtils" ;
2020-06-02 23:21:03 +08:00
import * as testUtils from "../src/utils/testUtils" ;
2019-11-14 05:13:00 +08:00
jest . mock ( "@actions/core" ) ;
2022-03-30 18:16:49 +08:00
jest . mock ( "@actions/cache" ) ;
2020-03-21 04:02:11 +08:00
2024-10-19 01:22:08 +08:00
let pristineEnv : NodeJS.ProcessEnv ;
2020-06-02 23:21:03 +08:00
beforeAll ( ( ) = > {
2024-10-19 01:22:08 +08:00
pristineEnv = process . env ;
2020-06-02 23:21:03 +08:00
jest . spyOn ( core , "getInput" ) . mockImplementation ( ( name , options ) = > {
return jest . requireActual ( "@actions/core" ) . getInput ( name , options ) ;
} ) ;
} ) ;
2024-10-19 01:22:08 +08:00
beforeEach ( ( ) = > {
jest . resetModules ( ) ;
process . env = pristineEnv ;
2019-11-14 05:13:00 +08:00
delete process . env [ Events . Key ] ;
2020-04-18 03:46:46 +08:00
delete process . env [ RefKey ] ;
2019-11-14 05:13:00 +08:00
} ) ;
2024-10-19 01:22:08 +08:00
afterAll ( ( ) = > {
process . env = pristineEnv ;
} ) ;
2020-09-30 01:36:19 +08:00
test ( "isGhes returns true if server url is not github.com" , ( ) = > {
try {
process . env [ "GITHUB_SERVER_URL" ] = "http://example.com" ;
expect ( actionUtils . isGhes ( ) ) . toBe ( true ) ;
} finally {
process . env [ "GITHUB_SERVER_URL" ] = undefined ;
}
} ) ;
2022-02-07 13:20:40 +08:00
test ( "isGhes returns false when server url is github.com" , ( ) = > {
2020-09-30 01:36:19 +08:00
try {
process . env [ "GITHUB_SERVER_URL" ] = "http://github.com" ;
expect ( actionUtils . isGhes ( ) ) . toBe ( false ) ;
} finally {
process . env [ "GITHUB_SERVER_URL" ] = undefined ;
}
} ) ;
2020-05-15 05:27:38 +08:00
test ( "isExactKeyMatch with undefined cache key returns false" , ( ) = > {
2019-11-14 05:13:00 +08:00
const key = "linux-rust" ;
2020-05-15 05:27:38 +08:00
const cacheKey = undefined ;
2019-11-14 05:13:00 +08:00
2020-05-15 05:27:38 +08:00
expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( false ) ;
2019-11-14 05:13:00 +08:00
} ) ;
2020-05-15 05:27:38 +08:00
test ( "isExactKeyMatch with empty cache key returns false" , ( ) = > {
2019-11-14 05:13:00 +08:00
const key = "linux-rust" ;
2020-05-15 05:27:38 +08:00
const cacheKey = "" ;
2019-11-14 05:13:00 +08:00
2020-05-15 05:27:38 +08:00
expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( false ) ;
2019-11-14 05:13:00 +08:00
} ) ;
test ( "isExactKeyMatch with different keys returns false" , ( ) = > {
const key = "linux-rust" ;
2020-05-15 05:27:38 +08:00
const cacheKey = "linux-" ;
2019-11-14 05:13:00 +08:00
2020-05-15 05:27:38 +08:00
expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( false ) ;
2019-11-14 05:13:00 +08:00
} ) ;
test ( "isExactKeyMatch with different key accents returns false" , ( ) = > {
const key = "linux-áccent" ;
2020-05-15 05:27:38 +08:00
const cacheKey = "linux-accent" ;
2019-11-14 05:13:00 +08:00
2020-05-15 05:27:38 +08:00
expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( false ) ;
2019-11-14 05:13:00 +08:00
} ) ;
test ( "isExactKeyMatch with same key returns true" , ( ) = > {
const key = "linux-rust" ;
2020-05-15 05:27:38 +08:00
const cacheKey = "linux-rust" ;
2019-11-14 05:13:00 +08:00
2020-05-15 05:27:38 +08:00
expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( true ) ;
2019-11-14 05:13:00 +08:00
} ) ;
test ( "isExactKeyMatch with same key and different casing returns true" , ( ) = > {
const key = "linux-rust" ;
2020-05-15 05:27:38 +08:00
const cacheKey = "LINUX-RUST" ;
2019-11-14 05:13:00 +08:00
2020-05-15 05:27:38 +08:00
expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( true ) ;
2019-11-14 05:13:00 +08:00
} ) ;
2019-11-22 03:37:54 +08:00
test ( "logWarning logs a message with a warning prefix" , ( ) = > {
const message = "A warning occurred." ;
const infoMock = jest . spyOn ( core , "info" ) ;
actionUtils . logWarning ( message ) ;
expect ( infoMock ) . toHaveBeenCalledWith ( ` [warning] ${ message } ` ) ;
} ) ;
2020-04-18 03:46:46 +08:00
test ( "isValidEvent returns false for event that does not have a branch or tag" , ( ) = > {
2019-11-14 05:13:00 +08:00
const event = "foo" ;
process . env [ Events . Key ] = event ;
const isValidEvent = actionUtils . isValidEvent ( ) ;
expect ( isValidEvent ) . toBe ( false ) ;
} ) ;
2020-04-18 03:46:46 +08:00
test ( "isValidEvent returns true for event that has a ref" , ( ) = > {
2019-11-14 05:13:00 +08:00
const event = Events . Push ;
process . env [ Events . Key ] = event ;
2020-04-18 03:46:46 +08:00
process . env [ RefKey ] = "ref/heads/feature" ;
2019-11-14 05:13:00 +08:00
const isValidEvent = actionUtils . isValidEvent ( ) ;
expect ( isValidEvent ) . toBe ( true ) ;
} ) ;
2020-06-02 23:21:03 +08:00
test ( "getInputAsArray returns empty array if not required and missing" , ( ) = > {
expect ( actionUtils . getInputAsArray ( "foo" ) ) . toEqual ( [ ] ) ;
} ) ;
test ( "getInputAsArray throws error if required and missing" , ( ) = > {
expect ( ( ) = >
actionUtils . getInputAsArray ( "foo" , { required : true } )
) . toThrowError ( ) ;
} ) ;
test ( "getInputAsArray handles single line correctly" , ( ) = > {
testUtils . setInput ( "foo" , "bar" ) ;
expect ( actionUtils . getInputAsArray ( "foo" ) ) . toEqual ( [ "bar" ] ) ;
} ) ;
test ( "getInputAsArray handles multiple lines correctly" , ( ) = > {
testUtils . setInput ( "foo" , "bar\nbaz" ) ;
expect ( actionUtils . getInputAsArray ( "foo" ) ) . toEqual ( [ "bar" , "baz" ] ) ;
} ) ;
test ( "getInputAsArray handles different new lines correctly" , ( ) = > {
testUtils . setInput ( "foo" , "bar\r\nbaz" ) ;
expect ( actionUtils . getInputAsArray ( "foo" ) ) . toEqual ( [ "bar" , "baz" ] ) ;
} ) ;
test ( "getInputAsArray handles empty lines correctly" , ( ) = > {
testUtils . setInput ( "foo" , "\n\nbar\n\nbaz\n\n" ) ;
expect ( actionUtils . getInputAsArray ( "foo" ) ) . toEqual ( [ "bar" , "baz" ] ) ;
} ) ;
2020-10-02 22:59:55 +08:00
2020-11-29 03:34:06 +08:00
test ( "getInputAsArray removes spaces after ! at the beginning" , ( ) = > {
testUtils . setInput (
"foo" ,
"! bar\n! baz\n! qux\n!quux\ncorge\ngrault! garply\n!\r\t waldo"
) ;
expect ( actionUtils . getInputAsArray ( "foo" ) ) . toEqual ( [
"!bar" ,
"!baz" ,
"!qux" ,
2022-10-03 14:39:10 +08:00
"!quux" ,
2020-11-29 03:34:06 +08:00
"corge" ,
2022-10-03 14:39:10 +08:00
"grault! garply" ,
"!waldo"
2020-11-29 03:34:06 +08:00
] ) ;
} ) ;
2020-10-02 22:59:55 +08:00
test ( "getInputAsInt returns undefined if input not set" , ( ) = > {
2020-10-02 23:55:30 +08:00
expect ( actionUtils . getInputAsInt ( "undefined" ) ) . toBeUndefined ( ) ;
2020-10-02 22:59:55 +08:00
} ) ;
test ( "getInputAsInt returns value if input is valid" , ( ) = > {
testUtils . setInput ( "foo" , "8" ) ;
expect ( actionUtils . getInputAsInt ( "foo" ) ) . toBe ( 8 ) ;
} ) ;
test ( "getInputAsInt returns undefined if input is invalid or NaN" , ( ) = > {
testUtils . setInput ( "foo" , "bar" ) ;
expect ( actionUtils . getInputAsInt ( "foo" ) ) . toBeUndefined ( ) ;
} ) ;
2020-10-02 23:55:30 +08:00
test ( "getInputAsInt throws if required and value missing" , ( ) = > {
expect ( ( ) = >
actionUtils . getInputAsInt ( "undefined" , { required : true } )
) . toThrowError ( ) ;
} ) ;
2022-03-30 18:16:49 +08:00
2023-01-05 19:19:13 +08:00
test ( "getInputAsBool returns false if input not set" , ( ) = > {
expect ( actionUtils . getInputAsBool ( "undefined" ) ) . toBe ( false ) ;
} ) ;
test ( "getInputAsBool returns value if input is valid" , ( ) = > {
testUtils . setInput ( "foo" , "true" ) ;
expect ( actionUtils . getInputAsBool ( "foo" ) ) . toBe ( true ) ;
} ) ;
test ( "getInputAsBool returns false if input is invalid or NaN" , ( ) = > {
testUtils . setInput ( "foo" , "bar" ) ;
expect ( actionUtils . getInputAsBool ( "foo" ) ) . toBe ( false ) ;
} ) ;
test ( "getInputAsBool throws if required and value missing" , ( ) = > {
expect ( ( ) = >
actionUtils . getInputAsBool ( "undefined2" , { required : true } )
) . toThrowError ( ) ;
} ) ;
2022-03-30 18:16:49 +08:00
test ( "isCacheFeatureAvailable for ac enabled" , ( ) = > {
jest . spyOn ( cache , "isFeatureAvailable" ) . mockImplementation ( ( ) = > true ) ;
expect ( actionUtils . isCacheFeatureAvailable ( ) ) . toBe ( true ) ;
} ) ;
test ( "isCacheFeatureAvailable for ac disabled on GHES" , ( ) = > {
jest . spyOn ( cache , "isFeatureAvailable" ) . mockImplementation ( ( ) = > false ) ;
2022-09-20 13:17:27 +08:00
const message = ` Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.
Otherwise please upgrade to GHES version >= 3.5 and If you are also using Github Connect , please unretire the actions / cache namespace before upgrade ( see https : //docs.github.com/en/enterprise-server@3.5/admin/github-actions/managing-access-to-actions-from-githubcom/enabling-automatic-access-to-githubcom-actions-using-github-connect#automatic-retirement-of-namespaces-for-actions-accessed-on-githubcom)`;
2022-03-30 18:16:49 +08:00
const infoMock = jest . spyOn ( core , "info" ) ;
try {
process . env [ "GITHUB_SERVER_URL" ] = "http://example.com" ;
expect ( actionUtils . isCacheFeatureAvailable ( ) ) . toBe ( false ) ;
expect ( infoMock ) . toHaveBeenCalledWith ( ` [warning] ${ message } ` ) ;
} finally {
delete process . env [ "GITHUB_SERVER_URL" ] ;
}
} ) ;
test ( "isCacheFeatureAvailable for ac disabled on dotcom" , ( ) = > {
jest . spyOn ( cache , "isFeatureAvailable" ) . mockImplementation ( ( ) = > false ) ;
const message =
"An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions." ;
const infoMock = jest . spyOn ( core , "info" ) ;
try {
process . env [ "GITHUB_SERVER_URL" ] = "http://github.com" ;
expect ( actionUtils . isCacheFeatureAvailable ( ) ) . toBe ( false ) ;
expect ( infoMock ) . toHaveBeenCalledWith ( ` [warning] ${ message } ` ) ;
} finally {
delete process . env [ "GITHUB_SERVER_URL" ] ;
}
} ) ;
2024-10-19 01:22:08 +08:00
test ( "isGhes returns false when the GITHUB_SERVER_URL environment variable is not defined" , async ( ) = > {
delete process . env [ "GITHUB_SERVER_URL" ] ;
expect ( actionUtils . isGhes ( ) ) . toBeFalsy ( ) ;
} ) ;
test ( "isGhes returns false when the GITHUB_SERVER_URL environment variable is set to github.com" , async ( ) = > {
process . env [ "GITHUB_SERVER_URL" ] = "https://github.com" ;
expect ( actionUtils . isGhes ( ) ) . toBeFalsy ( ) ;
} ) ;
test ( "isGhes returns false when the GITHUB_SERVER_URL environment variable is set to a GitHub Enterprise Cloud-style URL" , async ( ) = > {
process . env [ "GITHUB_SERVER_URL" ] = "https://contoso.ghe.com" ;
expect ( actionUtils . isGhes ( ) ) . toBeFalsy ( ) ;
} ) ;
test ( "isGhes returns false when the GITHUB_SERVER_URL environment variable has a .localhost suffix" , async ( ) = > {
process . env [ "GITHUB_SERVER_URL" ] = "https://mock-github.localhost" ;
expect ( actionUtils . isGhes ( ) ) . toBeFalsy ( ) ;
} ) ;
test ( "isGhes returns true when the GITHUB_SERVER_URL environment variable is set to some other URL" , async ( ) = > {
process . env [ "GITHUB_SERVER_URL" ] = "https://src.onpremise.fabrikam.com" ;
expect ( actionUtils . isGhes ( ) ) . toBeTruthy ( ) ;
} ) ;