1+ import child_process = require( 'child_process' ) ;
12import io = require( '@actions/io' ) ;
23import fs = require( 'fs' ) ;
34import nock = require( 'nock' ) ;
45import os = require( 'os' ) ;
56import path = require( 'path' ) ;
67
7- const toolDir = path . join (
8- __dirname ,
9- 'runner' ,
10- path . join (
11- Math . random ( )
12- . toString ( 36 )
13- . substring ( 7 )
14- ) ,
15- 'tools'
16- ) ;
17- const tempDir = path . join (
18- __dirname ,
19- 'runner' ,
20- path . join (
21- Math . random ( )
22- . toString ( 36 )
23- . substring ( 7 )
24- ) ,
25- 'temp'
26- ) ;
8+ function makeTestDir ( pathName : string ) {
9+ return path . join (
10+ __dirname ,
11+ 'runner' ,
12+ path . join (
13+ Math . random ( )
14+ . toString ( 36 )
15+ . substring ( 7 )
16+ ) ,
17+ pathName
18+ ) ;
19+ }
20+
21+ const toolDir = makeTestDir ( 'tools' ) ;
22+ const tempDir = makeTestDir ( 'temp' ) ;
23+ const fixtureDir = makeTestDir ( 'generatedFixtures' ) ;
2724
2825process . env [ 'RUNNER_TOOL_CACHE' ] = toolDir ;
2926process . env [ 'RUNNER_TEMP' ] = tempDir ;
3027import * as installer from '../src/installer' ;
3128
3229const IS_WINDOWS = process . platform === 'win32' ;
3330
31+ function makeDirSyncSafe ( directory : string ) {
32+ try {
33+ fs . mkdirSync ( directory , { recursive : true } ) ;
34+ } catch ( e ) {
35+ if ( e . code !== 'EEXIST' ) {
36+ throw e ;
37+ }
38+ }
39+ }
40+
41+ function buildFakeNodeFixture ( version : string , osArch : string ) {
42+ makeDirSyncSafe ( fixtureDir ) ;
43+ if ( IS_WINDOWS ) {
44+ const nodeFolderName = `node-v${ version } -win-${ osArch } ` ;
45+ const nodeFolderPath = path . join ( fixtureDir , nodeFolderName ) ;
46+ const nodeBinaryPath = path . join ( nodeFolderPath , 'node.exe' ) ;
47+ makeDirSyncSafe ( nodeFolderPath ) ;
48+ fs . writeFileSync ( nodeBinaryPath , 'nonsense binary content' , 'utf8' ) ;
49+
50+ const fixtureName = `mock-${ nodeFolderName } .7z` ;
51+ const fixturePath = path . join ( fixtureDir , fixtureName ) ;
52+ if ( fs . existsSync ( fixturePath ) ) {
53+ return fixturePath ;
54+ }
55+
56+ const binary7zip = path . join ( __dirname , '..' , 'externals' , '7zr.exe' ) ;
57+ child_process . execSync (
58+ `${ binary7zip } a mock-${ nodeFolderName } .7z ${ nodeFolderName } ` ,
59+ { cwd : fixtureDir }
60+ ) ;
61+
62+ return fixturePath ;
63+ } else {
64+ const nodeFolderName = `node-v${ version } -${ process . platform } -${ osArch } ` ;
65+ const nodeFolderPath = path . join ( fixtureDir , nodeFolderName ) ;
66+ const nodeBinPath = path . join ( nodeFolderPath , 'bin' ) ;
67+ const nodeBinaryPath = path . join ( nodeBinPath , 'node' ) ;
68+ makeDirSyncSafe ( nodeFolderPath ) ;
69+ makeDirSyncSafe ( nodeBinPath ) ;
70+ fs . writeFileSync ( nodeBinaryPath , 'nonsense binary content' , 'utf8' ) ;
71+
72+ const fixtureName = `mock-${ nodeFolderName } .tar.gz` ;
73+ const fixturePath = path . join ( fixtureDir , fixtureName ) ;
74+ if ( fs . existsSync ( fixturePath ) ) {
75+ return fixturePath ;
76+ }
77+
78+ child_process . execSync (
79+ `tar -czvf mock-${ nodeFolderName } .tar.gz ${ nodeFolderName } ` ,
80+ { cwd : fixtureDir }
81+ ) ;
82+
83+ return fixturePath ;
84+ }
85+ }
86+
3487describe ( 'installer tests' , ( ) => {
3588 beforeAll ( async ( ) => {
3689 await io . rmRF ( toolDir ) ;
@@ -129,20 +182,14 @@ describe('installer tests', () => {
129182 it ( 'Acquires specified x86 version of node if no matching version is installed' , async ( ) => {
130183 const arch = 'x86' ;
131184 const version = '8.8.0' ;
185+ const fixturePath = buildFakeNodeFixture ( version , arch ) ;
132186 const fileExtension = IS_WINDOWS ? '7z' : 'tar.gz' ;
133- const platform = {
134- linux : 'linux' ,
135- darwin : 'darwin' ,
136- win32 : 'win'
137- } [ process . platform ] ;
187+ const platform = IS_WINDOWS ? 'win' : process . platform ;
138188 const fileName = `node-v${ version } -${ platform } -${ arch } .${ fileExtension } ` ;
139189 const pathOnNodeJs = `/dist/v${ version } /${ fileName } ` ;
140190 const scope = nock ( 'https://nodejs.org' )
141191 . get ( pathOnNodeJs )
142- . replyWithFile (
143- 200 ,
144- path . join ( __dirname , '__fixtures__' , `mock-${ fileName } ` )
145- ) ;
192+ . replyWithFile ( 200 , fixturePath ) ;
146193 await installer . getNode ( version , arch ) ;
147194 const nodeDir = path . join ( toolDir , 'node' , version , arch ) ;
148195
@@ -158,20 +205,14 @@ describe('installer tests', () => {
158205 it ( 'Acquires specified x64 version of node if no matching version is installed' , async ( ) => {
159206 const arch = 'x64' ;
160207 const version = '8.9.1' ;
208+ const fixturePath = buildFakeNodeFixture ( version , arch ) ;
161209 const fileExtension = IS_WINDOWS ? '7z' : 'tar.gz' ;
162- const platform = {
163- linux : 'linux' ,
164- darwin : 'darwin' ,
165- win32 : 'win'
166- } [ process . platform ] ;
210+ const platform = IS_WINDOWS ? 'win' : process . platform ;
167211 const fileName = `node-v${ version } -${ platform } -${ arch } .${ fileExtension } ` ;
168212 const pathOnNodeJs = `/dist/v${ version } /${ fileName } ` ;
169213 const scope = nock ( 'https://nodejs.org' )
170214 . get ( pathOnNodeJs )
171- . replyWithFile (
172- 200 ,
173- path . join ( __dirname , '__fixtures__' , `mock-${ fileName } ` )
174- ) ;
215+ . replyWithFile ( 200 , fixturePath ) ;
175216 await installer . getNode ( version , arch ) ;
176217 const nodeDir = path . join ( toolDir , 'node' , version , arch ) ;
177218
0 commit comments