2022-03-31 19:11:27 +00:00
import * as cache from '@actions/cache' ;
import * as core from '@actions/core' ;
2020-12-17 15:03:54 +00:00
import {
2023-01-17 21:33:06 +00:00
parsePythonVersionFile ,
2020-12-17 15:03:54 +00:00
validateVersion ,
2022-03-31 19:11:27 +00:00
validatePythonVersionFormatForPyPy ,
isCacheFeatureAvailable
2020-12-17 15:03:54 +00:00
} from '../src/utils' ;
2022-03-31 19:11:27 +00:00
jest . mock ( '@actions/cache' ) ;
jest . mock ( '@actions/core' ) ;
2023-01-17 21:33:06 +00:00
describe ( 'parsePythonVersionFile' , ( ) = > {
it ( 'handle the content of a .python-version file' , ( ) = > {
2023-01-18 13:00:38 +00:00
expect ( parsePythonVersionFile ( '3.6' ) ) . toEqual ( '3.6' ) ;
2023-01-17 21:33:06 +00:00
} ) ;
it ( 'trims extra spaces at the end of the content' , ( ) = > {
2023-01-18 13:00:38 +00:00
expect ( parsePythonVersionFile ( '3.7 ' ) ) . toEqual ( '3.7' ) ;
2023-01-17 21:33:06 +00:00
} ) ;
it ( 'parses correctly the content of .tool-version files' , ( ) = > {
2023-01-18 13:00:38 +00:00
expect ( parsePythonVersionFile ( 'python 3.7' ) ) . toEqual ( '3.7' ) ;
} ) ;
it ( 'parses correctly pypy version content of the .tool-version files' , ( ) = > {
expect ( parsePythonVersionFile ( 'python pypy3.9-7.3.10' ) ) . toEqual (
'pypy3.9-7.3.10'
) ;
2023-01-17 21:33:06 +00:00
} ) ;
} ) ;
2020-12-17 15:03:54 +00:00
describe ( 'validatePythonVersionFormatForPyPy' , ( ) = > {
it . each ( [
[ '3.6' , true ] ,
[ '3.7' , true ] ,
[ '3.6.x' , false ] ,
[ '3.7.x' , false ] ,
[ '3.x' , false ] ,
[ '3' , false ]
] ) ( '%s -> %s' , ( input , expected ) = > {
expect ( validatePythonVersionFormatForPyPy ( input ) ) . toEqual ( expected ) ;
} ) ;
} ) ;
describe ( 'validateVersion' , ( ) = > {
it . each ( [
[ 'v7.3.3' , true ] ,
[ 'v7.3.x' , true ] ,
[ 'v7.x' , true ] ,
[ 'x' , true ] ,
[ 'v7.3.3-rc.1' , true ] ,
[ 'nightly' , true ] ,
[ 'v7.3.b' , false ] ,
[ '3.6' , true ] ,
[ '3.b' , false ] ,
[ '3' , true ]
] ) ( '%s -> %s' , ( input , expected ) = > {
expect ( validateVersion ( input ) ) . toEqual ( expected ) ;
} ) ;
} ) ;
2022-03-31 19:11:27 +00:00
describe ( 'isCacheFeatureAvailable' , ( ) = > {
it ( 'isCacheFeatureAvailable disabled on GHES' , ( ) = > {
jest . spyOn ( cache , 'isFeatureAvailable' ) . mockImplementation ( ( ) = > false ) ;
2022-12-19 13:00:46 +00:00
const infoMock = jest . spyOn ( core , 'warning' ) ;
const message =
'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.' ;
2022-03-31 19:11:27 +00:00
try {
process . env [ 'GITHUB_SERVER_URL' ] = 'http://example.com' ;
2022-12-19 13:00:46 +00:00
expect ( isCacheFeatureAvailable ( ) ) . toBeFalsy ( ) ;
expect ( infoMock ) . toHaveBeenCalledWith ( message ) ;
2022-03-31 19:11:27 +00:00
} finally {
delete process . env [ 'GITHUB_SERVER_URL' ] ;
}
} ) ;
it ( 'isCacheFeatureAvailable disabled on dotcom' , ( ) = > {
jest . spyOn ( cache , 'isFeatureAvailable' ) . mockImplementation ( ( ) = > false ) ;
const infoMock = jest . spyOn ( core , 'warning' ) ;
const message =
'The runner was not able to contact the cache service. Caching will be skipped' ;
try {
process . env [ 'GITHUB_SERVER_URL' ] = 'http://github.com' ;
expect ( isCacheFeatureAvailable ( ) ) . toBe ( false ) ;
expect ( infoMock ) . toHaveBeenCalledWith ( message ) ;
} finally {
delete process . env [ 'GITHUB_SERVER_URL' ] ;
}
} ) ;
it ( 'isCacheFeatureAvailable is enabled' , ( ) = > {
jest . spyOn ( cache , 'isFeatureAvailable' ) . mockImplementation ( ( ) = > true ) ;
expect ( isCacheFeatureAvailable ( ) ) . toBe ( true ) ;
} ) ;
} ) ;