@@ -24,11 +24,9 @@ describe('main tests', () => {
24
24
let startGroupSpy : jest . SpyInstance ;
25
25
let endGroupSpy : jest . SpyInstance ;
26
26
27
- let existsSpy : jest . SpyInstance ;
28
-
29
27
let getExecOutputSpy : jest . SpyInstance ;
30
28
31
- let parseNodeVersionSpy : jest . SpyInstance ;
29
+ let getNodeVersionFromFileSpy : jest . SpyInstance ;
32
30
let cnSpy : jest . SpyInstance ;
33
31
let findSpy : jest . SpyInstance ;
34
32
let isCacheActionAvailable : jest . SpyInstance ;
@@ -41,6 +39,7 @@ describe('main tests', () => {
41
39
// node
42
40
os = { } ;
43
41
console . log ( '::stop-commands::stoptoken' ) ;
42
+ process . env [ 'GITHUB_WORKSPACE' ] = path . join ( __dirname , 'data' ) ;
44
43
process . env [ 'GITHUB_PATH' ] = '' ; // Stub out ENV file functionality so we can verify it writes to standard out
45
44
process . env [ 'GITHUB_OUTPUT' ] = '' ; // Stub out ENV file functionality so we can verify it writes to standard out
46
45
infoSpy = jest . spyOn ( core , 'info' ) ;
@@ -62,12 +61,10 @@ describe('main tests', () => {
62
61
63
62
isCacheActionAvailable = jest . spyOn ( cache , 'isFeatureAvailable' ) ;
64
63
65
- existsSpy = jest . spyOn ( fs , 'existsSync' ) ;
66
-
67
64
cnSpy = jest . spyOn ( process . stdout , 'write' ) ;
68
65
cnSpy . mockImplementation ( line => {
69
66
// uncomment to debug
70
- // process.stderr.write('write:' + line + '\n');
67
+ process . stderr . write ( 'write:' + line + '\n' ) ;
71
68
} ) ;
72
69
73
70
setupNodeJsSpy = jest . spyOn ( OfficialBuilds . prototype , 'setupNodeJs' ) ;
@@ -85,7 +82,7 @@ describe('main tests', () => {
85
82
jest . restoreAllMocks ( ) ;
86
83
} , 100000 ) ;
87
84
88
- describe ( 'parseNodeVersionFile ' , ( ) => {
85
+ describe ( 'getNodeVersionFromFile ' , ( ) => {
89
86
each `
90
87
contents | expected
91
88
${ '12' } | ${ '12' }
@@ -100,10 +97,27 @@ describe('main tests', () => {
100
97
${ 'unknown format' } | ${ 'unknown format' }
101
98
${ ' 14.1.0 ' } | ${ '14.1.0' }
102
99
${ '{"volta": {"node": ">=14.0.0 <=17.0.0"}}' } | ${ '>=14.0.0 <=17.0.0' }
100
+ ${ '{"volta": {"extends": "./package.json"}}' } | ${ '18.0.0' }
103
101
${ '{"engines": {"node": "17.0.0"}}' } | ${ '17.0.0' }
104
102
${ '{}' } | ${ null }
105
103
` . it ( 'parses "$contents"' , ( { contents, expected} ) => {
106
- expect ( util . parseNodeVersionFile ( contents ) ) . toBe ( expected ) ;
104
+ const existsSpy = jest . spyOn ( fs , 'existsSync' ) ;
105
+ existsSpy . mockImplementation ( ( ) => true ) ;
106
+
107
+ const readFileSpy = jest . spyOn ( fs , 'readFileSync' ) ;
108
+ readFileSpy . mockImplementation ( filePath => {
109
+ if (
110
+ typeof filePath === 'string' &&
111
+ path . basename ( filePath ) === 'package.json'
112
+ ) {
113
+ // Special case for volta.extends
114
+ return '{"volta": {"node": "18.0.0"}}' ;
115
+ }
116
+
117
+ return contents ;
118
+ } ) ;
119
+
120
+ expect ( util . getNodeVersionFromFile ( 'file' ) ) . toBe ( expected ) ;
107
121
} ) ;
108
122
} ) ;
109
123
@@ -142,118 +156,72 @@ describe('main tests', () => {
142
156
143
157
describe ( 'node-version-file flag' , ( ) => {
144
158
beforeEach ( ( ) => {
145
- parseNodeVersionSpy = jest . spyOn ( util , 'parseNodeVersionFile' ) ;
159
+ delete inputs [ 'node-version' ] ;
160
+ inputs [ 'node-version-file' ] = '.nvmrc' ;
161
+
162
+ getNodeVersionFromFileSpy = jest . spyOn ( util , 'getNodeVersionFromFile' ) ;
163
+ } ) ;
164
+
165
+ afterEach ( ( ) => {
166
+ getNodeVersionFromFileSpy . mockRestore ( ) ;
146
167
} ) ;
147
168
148
- it ( 'not used if node-version is provided' , async ( ) => {
169
+ it ( 'does not read node-version-file if node-version is provided' , async ( ) => {
149
170
// Arrange
150
171
inputs [ 'node-version' ] = '12' ;
151
172
152
173
// Act
153
174
await main . run ( ) ;
154
175
155
176
// Assert
156
- expect ( parseNodeVersionSpy ) . toHaveBeenCalledTimes ( 0 ) ;
157
- } , 10000 ) ;
158
-
159
- it ( 'not used if node-version-file not provided' , async ( ) => {
160
- // Act
161
- await main . run ( ) ;
162
-
163
- // Assert
164
- expect ( parseNodeVersionSpy ) . toHaveBeenCalledTimes ( 0 ) ;
177
+ expect ( inputs [ 'node-version' ] ) . toBeDefined ( ) ;
178
+ expect ( inputs [ 'node-version-file' ] ) . toBeDefined ( ) ;
179
+ expect ( getNodeVersionFromFileSpy ) . not . toHaveBeenCalled ( ) ;
180
+ expect ( warningSpy ) . toHaveBeenCalledWith (
181
+ 'Both node-version and node-version-file inputs are specified, only node-version will be used'
182
+ ) ;
165
183
} ) ;
166
184
167
- it ( 'reads node-version-file if provided' , async ( ) => {
185
+ it ( 'does not read node-version-file if node-version-file is not provided' , async ( ) => {
168
186
// Arrange
169
- const versionSpec = 'v14' ;
170
- const versionFile = '.nvmrc' ;
171
- const expectedVersionSpec = '14' ;
172
- process . env [ 'GITHUB_WORKSPACE' ] = path . join ( __dirname , 'data' ) ;
173
- inputs [ 'node-version-file' ] = versionFile ;
174
-
175
- parseNodeVersionSpy . mockImplementation ( ( ) => expectedVersionSpec ) ;
176
- existsSpy . mockImplementationOnce (
177
- input => input === path . join ( __dirname , 'data' , versionFile )
178
- ) ;
187
+ delete inputs [ 'node-version-file' ] ;
179
188
180
189
// Act
181
190
await main . run ( ) ;
182
191
183
192
// Assert
184
- expect ( existsSpy ) . toHaveBeenCalledTimes ( 1 ) ;
185
- expect ( existsSpy ) . toHaveReturnedWith ( true ) ;
186
- expect ( parseNodeVersionSpy ) . toHaveBeenCalledWith ( versionSpec ) ;
187
- expect ( infoSpy ) . toHaveBeenCalledWith (
188
- `Resolved ${ versionFile } as ${ expectedVersionSpec } `
189
- ) ;
190
- } , 10000 ) ;
193
+ expect ( getNodeVersionFromFileSpy ) . not . toHaveBeenCalled ( ) ;
194
+ } ) ;
191
195
192
- it ( 'reads package.json as node-version-file if provided ' , async ( ) => {
196
+ it ( 'reads node-version-file' , async ( ) => {
193
197
// Arrange
194
- const versionSpec = fs . readFileSync (
195
- path . join ( __dirname , 'data/package.json' ) ,
196
- 'utf-8'
197
- ) ;
198
- const versionFile = 'package.json' ;
199
198
const expectedVersionSpec = '14' ;
200
- process . env [ 'GITHUB_WORKSPACE' ] = path . join ( __dirname , 'data' ) ;
201
- inputs [ 'node-version-file' ] = versionFile ;
199
+ getNodeVersionFromFileSpy . mockImplementation ( ( ) => expectedVersionSpec ) ;
202
200
203
- parseNodeVersionSpy . mockImplementation ( ( ) => expectedVersionSpec ) ;
204
- existsSpy . mockImplementationOnce (
205
- input => input === path . join ( __dirname , 'data' , versionFile )
206
- ) ;
207
201
// Act
208
202
await main . run ( ) ;
209
203
210
204
// Assert
211
- expect ( existsSpy ) . toHaveBeenCalledTimes ( 1 ) ;
212
- expect ( existsSpy ) . toHaveReturnedWith ( true ) ;
213
- expect ( parseNodeVersionSpy ) . toHaveBeenCalledWith ( versionSpec ) ;
205
+ expect ( getNodeVersionFromFileSpy ) . toHaveBeenCalled ( ) ;
214
206
expect ( infoSpy ) . toHaveBeenCalledWith (
215
- `Resolved ${ versionFile } as ${ expectedVersionSpec } `
207
+ `Resolved ${ inputs [ 'node-version-file' ] } as ${ expectedVersionSpec } `
216
208
) ;
217
209
} , 10000 ) ;
218
210
219
- it ( 'both node-version-file and node-version are provided' , async ( ) => {
220
- inputs [ 'node-version' ] = '12' ;
221
- const versionSpec = 'v14' ;
222
- const versionFile = '.nvmrc' ;
223
- const expectedVersionSpec = '14' ;
224
- process . env [ 'GITHUB_WORKSPACE' ] = path . join ( __dirname , '..' ) ;
225
- inputs [ 'node-version-file' ] = versionFile ;
226
-
227
- parseNodeVersionSpy . mockImplementation ( ( ) => expectedVersionSpec ) ;
228
-
229
- // Act
230
- await main . run ( ) ;
231
-
232
- // Assert
233
- expect ( existsSpy ) . toHaveBeenCalledTimes ( 0 ) ;
234
- expect ( parseNodeVersionSpy ) . not . toHaveBeenCalled ( ) ;
235
- expect ( warningSpy ) . toHaveBeenCalledWith (
236
- 'Both node-version and node-version-file inputs are specified, only node-version will be used'
237
- ) ;
238
- } ) ;
239
-
240
- it ( 'should throw an error if node-version-file is not found' , async ( ) => {
241
- const versionFile = '.nvmrc' ;
242
- const versionFilePath = path . join ( __dirname , '..' , versionFile ) ;
243
- inputs [ 'node-version-file' ] = versionFile ;
244
-
245
- inSpy . mockImplementation ( name => inputs [ name ] ) ;
246
- existsSpy . mockImplementationOnce (
247
- input => input === path . join ( __dirname , 'data' , versionFile )
211
+ it ( 'should throw an error if node-version-file is not accessible' , async ( ) => {
212
+ // Arrange
213
+ inputs [ 'node-version-file' ] = 'non-existing-file' ;
214
+ const versionFilePath = path . join (
215
+ __dirname ,
216
+ 'data' ,
217
+ inputs [ 'node-version-file' ]
248
218
) ;
249
219
250
220
// Act
251
221
await main . run ( ) ;
252
222
253
223
// Assert
254
- expect ( existsSpy ) . toHaveBeenCalled ( ) ;
255
- expect ( existsSpy ) . toHaveReturnedWith ( false ) ;
256
- expect ( parseNodeVersionSpy ) . not . toHaveBeenCalled ( ) ;
224
+ expect ( getNodeVersionFromFileSpy ) . toHaveBeenCalled ( ) ;
257
225
expect ( cnSpy ) . toHaveBeenCalledWith (
258
226
`::error::The specified node version file at: ${ versionFilePath } does not exist${ osm . EOL } `
259
227
) ;
0 commit comments