2021-06-16 07:52:44 +01:00
import * as core from '@actions/core' ;
2022-03-31 20:10:37 +01:00
import * as cache from '@actions/cache' ;
2021-06-16 07:52:44 +01:00
import path from 'path' ;
import * as utils from '../src/cache-utils' ;
2022-03-31 20:10:37 +01:00
import { PackageManagerInfo , isCacheFeatureAvailable } from '../src/cache-utils' ;
2021-06-16 07:52:44 +01:00
describe ( 'cache-utils' , ( ) = > {
const versionYarn1 = '1.2.3' ;
let debugSpy : jest.SpyInstance ;
let getCommandOutputSpy : jest.SpyInstance ;
2022-03-31 20:10:37 +01:00
let isFeatureAvailable : jest.SpyInstance ;
let info : jest.SpyInstance ;
let warningSpy : jest.SpyInstance ;
2021-06-16 07:52:44 +01:00
beforeEach ( ( ) = > {
process . env [ 'GITHUB_WORKSPACE' ] = path . join ( __dirname , 'data' ) ;
debugSpy = jest . spyOn ( core , 'debug' ) ;
debugSpy . mockImplementation ( msg = > { } ) ;
2022-03-31 20:10:37 +01:00
info = jest . spyOn ( core , 'info' ) ;
warningSpy = jest . spyOn ( core , 'warning' ) ;
isFeatureAvailable = jest . spyOn ( cache , 'isFeatureAvailable' ) ;
2021-06-16 07:52:44 +01:00
getCommandOutputSpy = jest . spyOn ( utils , 'getCommandOutput' ) ;
} ) ;
describe ( 'getPackageManagerInfo' , ( ) = > {
2021-06-29 11:34:35 +01:00
it . each < [ string , PackageManagerInfo | null ] > ( [
2021-06-16 07:52:44 +01:00
[ 'npm' , utils . supportedPackageManagers . npm ] ,
2021-06-30 16:44:51 +01:00
[ 'pnpm' , utils . supportedPackageManagers . pnpm ] ,
2021-06-16 07:52:44 +01:00
[ 'yarn' , utils . supportedPackageManagers . yarn1 ] ,
[ 'yarn1' , null ] ,
[ 'yarn2' , null ] ,
[ 'npm7' , null ]
] ) ( 'getPackageManagerInfo for %s is %o' , async ( packageManager , result ) = > {
getCommandOutputSpy . mockImplementationOnce ( ( ) = > versionYarn1 ) ;
await expect ( utils . getPackageManagerInfo ( packageManager ) ) . resolves . toBe (
result
) ;
} ) ;
} ) ;
2022-03-31 20:10:37 +01:00
it ( 'isCacheFeatureAvailable for GHES is false' , ( ) = > {
isFeatureAvailable . mockImplementation ( ( ) = > false ) ;
process . env [ 'GITHUB_SERVER_URL' ] = 'https://www.test.com' ;
expect ( ( ) = > isCacheFeatureAvailable ( ) ) . toThrowError (
'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.'
) ;
} ) ;
it ( 'isCacheFeatureAvailable for GHES has an interhal error' , ( ) = > {
isFeatureAvailable . mockImplementation ( ( ) = > false ) ;
process . env [ 'GITHUB_SERVER_URL' ] = '' ;
isCacheFeatureAvailable ( ) ;
expect ( warningSpy ) . toHaveBeenCalledWith (
'The runner was not able to contact the cache service. Caching will be skipped'
) ;
} ) ;
it ( 'isCacheFeatureAvailable for GHES is available' , ( ) = > {
isFeatureAvailable . mockImplementation ( ( ) = > true ) ;
expect ( isCacheFeatureAvailable ( ) ) . toStrictEqual ( true ) ;
} ) ;
2021-06-16 07:52:44 +01:00
afterEach ( ( ) = > {
2022-03-31 20:10:37 +01:00
process . env [ 'GITHUB_SERVER_URL' ] = '' ;
2021-06-16 07:52:44 +01:00
jest . resetAllMocks ( ) ;
jest . clearAllMocks ( ) ;
} ) ;
} ) ;