2022-04-01 03:11:27 +08:00
import * as cache from '@actions/cache' ;
import * as core from '@actions/core' ;
2023-06-29 04:02:44 +08:00
import * as io from '@actions/io' ;
import fs from 'fs' ;
import path from 'path' ;
2020-12-17 23:03:54 +08:00
import {
validateVersion ,
2022-04-01 03:11:27 +08:00
validatePythonVersionFormatForPyPy ,
2023-06-29 04:02:44 +08:00
isCacheFeatureAvailable ,
getVersionInputFromFile ,
2024-01-04 07:33:23 +08:00
getVersionsInputFromPlainFile ,
2023-10-10 20:59:54 +08:00
getVersionInputFromTomlFile ,
2024-08-06 01:23:34 +08:00
getNextPageUrl ,
2024-10-22 00:42:17 +08:00
isGhes ,
2024-08-06 01:23:34 +08:00
IS_WINDOWS ,
getDownloadFileName
2020-12-17 23:03:54 +08:00
} from '../src/utils' ;
2022-04-01 03:11:27 +08:00
jest . mock ( '@actions/cache' ) ;
jest . mock ( '@actions/core' ) ;
2020-12-17 23:03:54 +08: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-04-01 03:11:27 +08:00
describe ( 'isCacheFeatureAvailable' , ( ) = > {
it ( 'isCacheFeatureAvailable disabled on GHES' , ( ) = > {
jest . spyOn ( cache , 'isFeatureAvailable' ) . mockImplementation ( ( ) = > false ) ;
2022-12-19 21:00:46 +08: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-04-01 03:11:27 +08:00
try {
process . env [ 'GITHUB_SERVER_URL' ] = 'http://example.com' ;
2022-12-19 21:00:46 +08:00
expect ( isCacheFeatureAvailable ( ) ) . toBeFalsy ( ) ;
expect ( infoMock ) . toHaveBeenCalledWith ( message ) ;
2022-04-01 03:11:27 +08: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 ) ;
} ) ;
} ) ;
2023-06-29 04:02:44 +08:00
const tempDir = path . join (
__dirname ,
'runner' ,
path . join ( Math . random ( ) . toString ( 36 ) . substring ( 7 ) ) ,
'temp'
) ;
describe ( 'Version from file test' , ( ) = > {
2024-01-04 07:33:23 +08:00
it . each ( [ getVersionsInputFromPlainFile , getVersionInputFromFile ] ) (
2023-06-29 04:02:44 +08:00
'Version from plain file test' ,
async _fn = > {
await io . mkdirP ( tempDir ) ;
const pythonVersionFileName = 'python-version.file' ;
const pythonVersionFilePath = path . join ( tempDir , pythonVersionFileName ) ;
const pythonVersionFileContent = '3.7' ;
fs . writeFileSync ( pythonVersionFilePath , pythonVersionFileContent ) ;
expect ( _fn ( pythonVersionFilePath ) ) . toEqual ( [ pythonVersionFileContent ] ) ;
}
) ;
2024-01-04 07:33:23 +08:00
it . each ( [ getVersionsInputFromPlainFile , getVersionInputFromFile ] ) (
'Versions from multiline plain file test' ,
async _fn = > {
await io . mkdirP ( tempDir ) ;
const pythonVersionFileName = 'python-version.file' ;
const pythonVersionFilePath = path . join ( tempDir , pythonVersionFileName ) ;
const pythonVersionFileContent = '3.8\r\n3.7' ;
fs . writeFileSync ( pythonVersionFilePath , pythonVersionFileContent ) ;
expect ( _fn ( pythonVersionFilePath ) ) . toEqual ( [ '3.8' , '3.7' ] ) ;
}
) ;
it . each ( [ getVersionsInputFromPlainFile , getVersionInputFromFile ] ) (
'Version from complex plain file test' ,
async _fn = > {
await io . mkdirP ( tempDir ) ;
const pythonVersionFileName = 'python-version.file' ;
const pythonVersionFilePath = path . join ( tempDir , pythonVersionFileName ) ;
const pythonVersionFileContent = '3.10/envs/virtualenv\r# 3.9\n3.8\r\n3.7\r\n 3.6 \r\n' ;
fs . writeFileSync ( pythonVersionFilePath , pythonVersionFileContent ) ;
expect ( _fn ( pythonVersionFilePath ) ) . toEqual ( [ '3.10' , '3.8' , '3.7' , '3.6' ] ) ;
}
) ;
2023-06-29 04:02:44 +08:00
it . each ( [ getVersionInputFromTomlFile , getVersionInputFromFile ] ) (
'Version from standard pyproject.toml test' ,
async _fn = > {
await io . mkdirP ( tempDir ) ;
const pythonVersionFileName = 'pyproject.toml' ;
const pythonVersionFilePath = path . join ( tempDir , pythonVersionFileName ) ;
2023-09-07 21:45:09 +08:00
const pythonVersion = '>=3.7.0' ;
2023-06-29 04:02:44 +08:00
const pythonVersionFileContent = ` [project] \ nrequires-python = " ${ pythonVersion } " ` ;
fs . writeFileSync ( pythonVersionFilePath , pythonVersionFileContent ) ;
expect ( _fn ( pythonVersionFilePath ) ) . toEqual ( [ pythonVersion ] ) ;
}
) ;
it . each ( [ getVersionInputFromTomlFile , getVersionInputFromFile ] ) (
'Version from poetry pyproject.toml test' ,
async _fn = > {
await io . mkdirP ( tempDir ) ;
const pythonVersionFileName = 'pyproject.toml' ;
const pythonVersionFilePath = path . join ( tempDir , pythonVersionFileName ) ;
2023-09-07 21:45:09 +08:00
const pythonVersion = '>=3.7.0' ;
2023-06-29 04:02:44 +08:00
const pythonVersionFileContent = ` [tool.poetry.dependencies] \ npython = " ${ pythonVersion } " ` ;
fs . writeFileSync ( pythonVersionFilePath , pythonVersionFileContent ) ;
expect ( _fn ( pythonVersionFilePath ) ) . toEqual ( [ pythonVersion ] ) ;
}
) ;
it . each ( [ getVersionInputFromTomlFile , getVersionInputFromFile ] ) (
'Version undefined' ,
async _fn = > {
await io . mkdirP ( tempDir ) ;
const pythonVersionFileName = 'pyproject.toml' ;
const pythonVersionFilePath = path . join ( tempDir , pythonVersionFileName ) ;
fs . writeFileSync ( pythonVersionFilePath , ` ` ) ;
expect ( _fn ( pythonVersionFilePath ) ) . toEqual ( [ ] ) ;
}
) ;
} ) ;
2023-10-10 20:59:54 +08:00
describe ( 'getNextPageUrl' , ( ) = > {
it ( 'GitHub API pagination next page is parsed correctly' , ( ) = > {
function generateResponse ( link : string ) {
return {
statusCode : 200 ,
result : null ,
headers : {
link : link
}
} ;
}
const page1Links =
'<https://api.github.com/repositories/129883600/releases?page=2>; rel="next", <https://api.github.com/repositories/129883600/releases?page=3>; rel="last"' ;
expect ( getNextPageUrl ( generateResponse ( page1Links ) ) ) . toStrictEqual (
'https://api.github.com/repositories/129883600/releases?page=2'
) ;
const page2Links =
'<https://api.github.com/repositories/129883600/releases?page=1>; rel="prev", <https://api.github.com/repositories/129883600/releases?page=1>; rel="first"' ;
expect ( getNextPageUrl ( generateResponse ( page2Links ) ) ) . toBeNull ( ) ;
} ) ;
} ) ;
2024-08-06 01:23:34 +08:00
describe ( 'getDownloadFileName' , ( ) = > {
const originalEnv = process . env ;
const tempDir = path . join ( __dirname , 'runner' , 'temp' ) ;
beforeEach ( ( ) = > {
process . env = { . . . originalEnv } ;
} ) ;
afterEach ( ( ) = > {
process . env = originalEnv ;
} ) ;
it ( 'should return the correct path on Windows' , ( ) = > {
if ( IS_WINDOWS ) {
process . env [ 'RUNNER_TEMP' ] = tempDir ;
const downloadUrl =
'https://github.com/actions/sometool/releases/tag/1.2.3-20200402.6/sometool-1.2.3-win32-x64.zip' ;
const expectedPath = path . join (
process . env . RUNNER_TEMP ,
path . basename ( downloadUrl )
) ;
expect ( getDownloadFileName ( downloadUrl ) ) . toBe ( expectedPath ) ;
}
} ) ;
it ( 'should return undefined on non-Windows' , ( ) = > {
if ( ! IS_WINDOWS ) {
const downloadUrl =
'https://github.com/actions/sometool/releases/tag/1.2.3-20200402.6/sometool-1.2.3-linux-x64.tar.gz' ;
expect ( getDownloadFileName ( downloadUrl ) ) . toBeUndefined ( ) ;
}
} ) ;
} ) ;
2024-10-22 00:42:17 +08:00
describe ( 'isGhes' , ( ) = > {
const pristineEnv = process . env ;
beforeEach ( ( ) = > {
jest . resetModules ( ) ;
process . env = { . . . pristineEnv } ;
} ) ;
afterAll ( ( ) = > {
process . env = pristineEnv ;
} ) ;
it ( 'returns false when the GITHUB_SERVER_URL environment variable is not defined' , async ( ) = > {
delete process . env [ 'GITHUB_SERVER_URL' ] ;
expect ( isGhes ( ) ) . toBeFalsy ( ) ;
} ) ;
it ( 'returns false when the GITHUB_SERVER_URL environment variable is set to github.com' , async ( ) = > {
process . env [ 'GITHUB_SERVER_URL' ] = 'https://github.com' ;
expect ( isGhes ( ) ) . toBeFalsy ( ) ;
} ) ;
it ( '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 ( isGhes ( ) ) . toBeFalsy ( ) ;
} ) ;
it ( 'returns false when the GITHUB_SERVER_URL environment variable has a .localhost suffix' , async ( ) = > {
process . env [ 'GITHUB_SERVER_URL' ] = 'https://mock-github.localhost' ;
expect ( isGhes ( ) ) . toBeFalsy ( ) ;
} ) ;
it ( '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 ( isGhes ( ) ) . toBeTruthy ( ) ;
} ) ;
} ) ;