2022-09-27 19:47:12 +07:00
import * as io from '@actions/io' ;
import * as os from 'os' ;
import fs from 'fs' ;
import path from 'path' ;
import each from 'jest-each' ;
import * as hc from '@actions/http-client' ;
import * as installer from '../src/installer' ;
import { QualityOptions } from '../src/setup-dotnet' ;
import { IS_WINDOWS } from '../src/utils' ;
import { IS_LINUX } from '../src/utils' ;
2019-06-20 03:22:22 +07:00
2022-09-27 19:47:12 +07:00
let toolDir : string ;
if ( IS_WINDOWS ) {
toolDir = path . join ( process . env [ 'PROGRAMFILES' ] + '' , 'dotnet' ) ;
} else if ( IS_LINUX ) {
toolDir = '/usr/share/dotnet' ;
} else {
toolDir = path . join ( process . env [ 'HOME' ] + '' , '.dotnet' ) ;
}
2019-06-20 03:22:22 +07:00
const tempDir = path . join ( __dirname , 'runner' , 'temp' ) ;
2019-07-30 23:53:06 +07:00
process . env [ 'RUNNER_TOOL_CACHE' ] = toolDir ;
process . env [ 'RUNNER_TEMP' ] = tempDir ;
2019-06-21 19:21:08 +07:00
2022-09-27 19:47:12 +07:00
describe ( 'DotnetCoreInstaller tests' , ( ) = > {
2019-06-20 03:22:22 +07:00
beforeAll ( async ( ) = > {
2020-09-15 23:36:09 +07:00
process . env . RUNNER_TOOL_CACHE = toolDir ;
process . env . DOTNET_INSTALL_DIR = toolDir ;
process . env . RUNNER_TEMP = tempDir ;
2020-09-24 22:26:00 +07:00
process . env . DOTNET_ROOT = '' ;
2022-09-27 19:47:12 +07:00
try {
await io . rmRF ( ` ${ toolDir } /* ` ) ;
await io . rmRF ( ` ${ tempDir } /* ` ) ;
} catch ( err ) {
console . log (
` Failed to remove test directories, check the error message: ${ os . EOL } ` ,
err . message
) ;
}
} , 30000 ) ;
2019-06-20 03:22:22 +07:00
2022-09-27 19:47:12 +07:00
afterEach ( async ( ) = > {
2019-06-21 19:21:08 +07:00
try {
2022-09-27 19:47:12 +07:00
await io . rmRF ( ` ${ toolDir } /* ` ) ;
await io . rmRF ( ` ${ tempDir } /* ` ) ;
} catch ( err ) {
console . log (
` Failed to remove test directories, check the error message: ${ os . EOL } ` ,
err . message
) ;
2019-06-21 19:21:08 +07:00
}
2020-09-15 23:36:09 +07:00
} , 30000 ) ;
2019-06-21 19:21:08 +07:00
2021-11-23 17:03:56 +07:00
it ( 'Aquires multiple versions of dotnet' , async ( ) = > {
const versions = [ '2.2.207' , '3.1.120' ] ;
for ( const version of versions ) {
await getDotnet ( version ) ;
}
expect ( fs . existsSync ( path . join ( toolDir , 'sdk' , '2.2.207' ) ) ) . toBe ( true ) ;
expect ( fs . existsSync ( path . join ( toolDir , 'sdk' , '3.1.120' ) ) ) . toBe ( true ) ;
if ( IS_WINDOWS ) {
expect ( fs . existsSync ( path . join ( toolDir , 'dotnet.exe' ) ) ) . toBe ( true ) ;
} else {
expect ( fs . existsSync ( path . join ( toolDir , 'dotnet' ) ) ) . toBe ( true ) ;
}
2023-03-09 19:43:05 +07:00
expect ( process . env . DOTNET_ROOT ) . toBeDefined ( ) ;
expect ( process . env . PATH ) . toBeDefined ( ) ;
2021-11-23 17:03:56 +07:00
expect ( process . env . DOTNET_ROOT ) . toBe ( toolDir ) ;
expect ( process . env . PATH ? . startsWith ( toolDir ) ) . toBe ( true ) ;
} , 600000 ) ;
2019-06-21 19:21:08 +07:00
it ( 'Acquires version of dotnet if no matching version is installed' , async ( ) = > {
2020-09-15 23:36:09 +07:00
await getDotnet ( '3.1.201' ) ;
expect ( fs . existsSync ( path . join ( toolDir , 'sdk' , '3.1.201' ) ) ) . toBe ( true ) ;
2019-06-21 19:21:08 +07:00
if ( IS_WINDOWS ) {
2020-09-15 23:36:09 +07:00
expect ( fs . existsSync ( path . join ( toolDir , 'dotnet.exe' ) ) ) . toBe ( true ) ;
2019-06-21 19:21:08 +07:00
} else {
2020-09-15 23:36:09 +07:00
expect ( fs . existsSync ( path . join ( toolDir , 'dotnet' ) ) ) . toBe ( true ) ;
2019-06-21 19:21:08 +07:00
}
2019-08-12 23:14:43 +07:00
2023-03-09 19:43:05 +07:00
expect ( process . env . DOTNET_ROOT ) . toBeDefined ( ) ;
expect ( process . env . PATH ) . toBeDefined ( ) ;
2020-09-24 22:26:00 +07:00
expect ( process . env . DOTNET_ROOT ) . toBe ( toolDir ) ;
expect ( process . env . PATH ? . startsWith ( toolDir ) ) . toBe ( true ) ;
} , 600000 ) ; //This needs some time to download on "slower" internet connections
it ( 'Acquires generic version of dotnet if no matching version is installed' , async ( ) = > {
await getDotnet ( '3.1' ) ;
2023-03-09 19:43:05 +07:00
const directory = fs
2020-09-24 22:26:00 +07:00
. readdirSync ( path . join ( toolDir , 'sdk' ) )
. filter ( fn = > fn . startsWith ( '3.1.' ) ) ;
expect ( directory . length > 0 ) . toBe ( true ) ;
2019-08-12 23:14:43 +07:00
if ( IS_WINDOWS ) {
2020-09-15 23:36:09 +07:00
expect ( fs . existsSync ( path . join ( toolDir , 'dotnet.exe' ) ) ) . toBe ( true ) ;
2019-08-12 23:14:43 +07:00
} else {
2020-09-15 23:36:09 +07:00
expect ( fs . existsSync ( path . join ( toolDir , 'dotnet' ) ) ) . toBe ( true ) ;
2019-08-12 23:14:43 +07:00
}
2020-09-24 22:26:00 +07:00
2023-03-09 19:43:05 +07:00
expect ( process . env . DOTNET_ROOT ) . toBeDefined ( ) ;
expect ( process . env . PATH ) . toBeDefined ( ) ;
2020-09-24 22:26:00 +07:00
expect ( process . env . DOTNET_ROOT ) . toBe ( toolDir ) ;
expect ( process . env . PATH ? . startsWith ( toolDir ) ) . toBe ( true ) ;
} , 600000 ) ; //This needs some time to download on "slower" internet connections
2019-08-12 23:14:43 +07:00
2022-09-29 22:45:25 +07:00
it ( 'Returns string with installed SDK version' , async ( ) = > {
const version = '3.1.120' ;
2023-03-09 19:43:05 +07:00
const installedVersion = await getDotnet ( version ) ;
2022-09-29 22:45:25 +07:00
expect ( installedVersion ) . toBe ( '3.1.120' ) ;
} , 600000 ) ;
2019-06-21 19:21:08 +07:00
it ( 'Throws if no location contains correct dotnet version' , async ( ) = > {
2022-09-27 19:47:12 +07:00
await expect ( async ( ) = > {
2023-05-24 20:22:01 +07:00
await getDotnet ( '1000.0.0' ) ;
2022-09-27 19:47:12 +07:00
} ) . rejects . toThrow ( ) ;
2020-09-15 23:36:09 +07:00
} , 30000 ) ;
2019-06-21 19:21:08 +07:00
it ( 'Uses an up to date bash download script' , async ( ) = > {
2020-01-26 13:37:54 +07:00
const httpCallbackClient = new hc . HttpClient ( 'setup-dotnet-test' , [ ] , {
allowRetries : true ,
maxRetries : 3
} ) ;
const response : hc.HttpClientResponse = await httpCallbackClient . get (
2019-06-21 19:21:08 +07:00
'https://dot.net/v1/dotnet-install.sh'
) ;
2020-01-26 13:37:54 +07:00
expect ( response . message . statusCode ) . toBe ( 200 ) ;
2019-06-21 19:21:08 +07:00
const upToDateContents : string = await response . readBody ( ) ;
const currentContents : string = fs
. readFileSync (
path . join ( __dirname , '..' , 'externals' , 'install-dotnet.sh' )
)
. toString ( ) ;
expect ( normalizeFileContents ( currentContents ) ) . toBe (
normalizeFileContents ( upToDateContents )
) ;
2020-09-15 23:36:09 +07:00
} , 30000 ) ;
2019-06-21 19:21:08 +07:00
it ( 'Uses an up to date powershell download script' , async ( ) = > {
2023-03-09 19:43:05 +07:00
const httpCallbackClient = new hc . HttpClient ( 'setup-dotnet-test' , [ ] , {
2020-01-26 13:37:54 +07:00
allowRetries : true ,
maxRetries : 3
} ) ;
const response : hc.HttpClientResponse = await httpCallbackClient . get (
2019-06-21 19:21:08 +07:00
'https://dot.net/v1/dotnet-install.ps1'
) ;
2020-01-26 13:37:54 +07:00
expect ( response . message . statusCode ) . toBe ( 200 ) ;
2019-06-21 19:21:08 +07:00
const upToDateContents : string = await response . readBody ( ) ;
const currentContents : string = fs
. readFileSync (
path . join ( __dirname , '..' , 'externals' , 'install-dotnet.ps1' )
)
. toString ( ) ;
expect ( normalizeFileContents ( currentContents ) ) . toBe (
normalizeFileContents ( upToDateContents )
) ;
2020-09-15 23:36:09 +07:00
} , 30000 ) ;
2019-06-20 03:22:22 +07:00
} ) ;
2019-06-21 19:21:08 +07:00
2022-09-27 19:47:12 +07:00
describe ( 'DotnetVersionResolver tests' , ( ) = > {
each ( [
'3.1' ,
'3.x' ,
'3.1.x' ,
'3.1.*' ,
'3.1.X' ,
'3.1.2' ,
'3.1.0-preview1'
] ) . test (
"if valid version: '%s' is supplied, it should return version object with some value" ,
async version = > {
const dotnetVersionResolver = new installer . DotnetVersionResolver (
version
) ;
2023-05-12 21:28:16 +07:00
const versionObject = await dotnetVersionResolver . createDotnetVersion ( ) ;
2022-09-27 19:47:12 +07:00
2023-03-09 19:43:05 +07:00
expect ( ! ! versionObject . value ) . toBe ( true ) ;
2022-09-27 19:47:12 +07:00
}
) ;
each ( [
'.' ,
'..' ,
' . ' ,
'. ' ,
' .' ,
' . . ' ,
' .. ' ,
' . ' ,
'-1.-1' ,
'-1' ,
'-1.-1.-1' ,
'..3' ,
'1..3' ,
'1..' ,
'.2.3' ,
'.2.x' ,
'*.' ,
'1.2.' ,
'1.2.-abc' ,
'a.b' ,
'a.b.c' ,
'a.b.c-preview' ,
' 0 . 1 . 2 ' ,
'invalid'
] ) . test (
"if invalid version: '%s' is supplied, it should throw" ,
async version = > {
const dotnetVersionResolver = new installer . DotnetVersionResolver (
version
) ;
await expect (
2023-05-12 21:28:16 +07:00
async ( ) = > await dotnetVersionResolver . createDotnetVersion ( )
2022-09-27 19:47:12 +07:00
) . rejects . toThrow ( ) ;
}
) ;
each ( [ '3.1' , '3.1.x' , '3.1.*' , '3.1.X' ] ) . test (
2023-03-09 19:43:05 +07:00
"if version: '%s' that can be resolved to 'channel' option is supplied, it should set type to 'channel' in version object" ,
async version = > {
const dotnetVersionResolver = new installer . DotnetVersionResolver (
version
) ;
2023-05-12 21:28:16 +07:00
const versionObject = await dotnetVersionResolver . createDotnetVersion ( ) ;
2023-03-09 19:43:05 +07:00
expect ( versionObject . type . toLowerCase ( ) . includes ( 'channel' ) ) . toBe ( true ) ;
}
) ;
each ( [ '6.0' , '6.0.x' , '6.0.*' , '6.0.X' ] ) . test (
"if version: '%s' that can be resolved to 'channel' option is supplied and its major tag is >= 6, it should set type to 'channel' and qualityFlag to 'true' in version object" ,
2022-09-27 19:47:12 +07:00
async version = > {
const dotnetVersionResolver = new installer . DotnetVersionResolver (
version
) ;
2023-05-12 21:28:16 +07:00
const versionObject = await dotnetVersionResolver . createDotnetVersion ( ) ;
2022-09-27 19:47:12 +07:00
2023-03-09 19:43:05 +07:00
expect ( versionObject . type . toLowerCase ( ) . includes ( 'channel' ) ) . toBe ( true ) ;
expect ( versionObject . qualityFlag ) . toBe ( true ) ;
2022-09-27 19:47:12 +07:00
}
) ;
each ( [ '3.1.2' , '3.1.0-preview1' ] ) . test (
"if version: '%s' that can be resolved to 'version' option is supplied, it should set quality flag to 'false' and type to 'version' in version object" ,
async version = > {
const dotnetVersionResolver = new installer . DotnetVersionResolver (
version
) ;
2023-05-12 21:28:16 +07:00
const versionObject = await dotnetVersionResolver . createDotnetVersion ( ) ;
2022-09-27 19:47:12 +07:00
2023-03-09 19:43:05 +07:00
expect ( versionObject . type . toLowerCase ( ) . includes ( 'version' ) ) . toBe ( true ) ;
expect ( versionObject . qualityFlag ) . toBe ( false ) ;
2022-09-27 19:47:12 +07:00
}
) ;
each ( [ '3.1.2' , '3.1' ] ) . test (
'it should create proper line arguments for powershell/bash installation scripts' ,
async version = > {
const dotnetVersionResolver = new installer . DotnetVersionResolver (
version
) ;
2023-05-12 21:28:16 +07:00
const versionObject = await dotnetVersionResolver . createDotnetVersion ( ) ;
2022-09-27 19:47:12 +07:00
const windowsRegEx = new RegExp ( /^-[VC]/ ) ;
const nonWindowsRegEx = new RegExp ( /^--[vc]/ ) ;
if ( IS_WINDOWS ) {
2023-03-09 19:43:05 +07:00
expect ( windowsRegEx . test ( versionObject . type ) ) . toBe ( true ) ;
expect ( nonWindowsRegEx . test ( versionObject . type ) ) . toBe ( false ) ;
2022-09-27 19:47:12 +07:00
} else {
2023-03-09 19:43:05 +07:00
expect ( nonWindowsRegEx . test ( versionObject . type ) ) . toBe ( true ) ;
expect ( windowsRegEx . test ( versionObject . type ) ) . toBe ( false ) ;
2022-09-27 19:47:12 +07:00
}
}
) ;
} ) ;
2019-06-21 19:21:08 +07:00
function normalizeFileContents ( contents : string ) : string {
return contents
. trim ( )
. replace ( new RegExp ( '\r\n' , 'g' ) , '\n' )
. replace ( new RegExp ( '\r' , 'g' ) , '\n' ) ;
}
2023-03-09 19:43:05 +07:00
async function getDotnet ( version : string , quality = '' ) : Promise < string > {
2022-09-27 19:47:12 +07:00
const dotnetInstaller = new installer . DotnetCoreInstaller (
version ,
quality as QualityOptions
) ;
2022-09-29 22:45:25 +07:00
const installedVersion = await dotnetInstaller . installDotnet ( ) ;
2021-11-23 17:03:56 +07:00
installer . DotnetCoreInstaller . addToPath ( ) ;
2022-09-29 22:45:25 +07:00
return installedVersion ;
2019-06-21 19:21:08 +07:00
}