1
- var assert = require ( "assert" )
2
- var fs = require ( 'fs-extra' ) ;
3
- var path = require ( 'path' ) ;
4
- var mkdirp = require ( 'mkdirp' ) ;
5
- var rimraf = require ( 'rimraf' ) ;
6
- var webpack = require ( 'webpack' ) ;
1
+ const assert = require ( "assert" )
2
+ const fs = require ( 'fs-extra' ) ;
3
+ const path = require ( 'path' ) ;
4
+ const mkdirp = require ( 'mkdirp' ) ;
5
+ const rimraf = require ( 'rimraf' ) ;
6
+ const webpack = require ( 'webpack' ) ;
7
7
// @ts -ignore
8
- var webpackVersion = require ( 'webpack/package.json' ) . version ;
9
- var regexEscape = require ( 'escape-string-regexp' ) ;
10
- var typescript = require ( 'typescript' ) ;
11
- var semver = require ( 'semver' ) ;
12
- var glob = require ( 'glob' ) ;
13
- var pathExists = require ( '../pathExists' ) ;
14
- var aliasLoader = require ( '../aliasLoader' ) ;
8
+ const webpackVersion = require ( 'webpack/package.json' ) . version ;
9
+ const regexEscape = require ( 'escape-string-regexp' ) ;
10
+ const typescript = require ( 'typescript' ) ;
11
+ const semver = require ( 'semver' ) ;
12
+ const glob = require ( 'glob' ) ;
13
+ const pathExists = require ( '../pathExists' ) ;
14
+ const aliasLoader = require ( '../aliasLoader' ) ;
15
15
16
- var saveOutputMode = process . argv . indexOf ( '--save-output' ) !== - 1 ;
16
+ const saveOutputMode = process . argv . indexOf ( '--save-output' ) !== - 1 ;
17
17
18
- var indexOfTestToRun = process . argv . indexOf ( '--test-to-run' ) ;
19
- var testToRun = process . argv [ indexOfTestToRun + 1 ] ;
18
+ const indexOfTestToRun = process . argv . indexOf ( '--test-to-run' ) ;
19
+ const testToRun = process . argv [ indexOfTestToRun + 1 ] ;
20
20
21
- var savedOutputs = { } ;
21
+ const indexOfExtraOption = process . argv . indexOf ( '--extra-option' ) ;
22
+ const extraOption = indexOfExtraOption === - 1 ? undefined : process . argv [ indexOfExtraOption + 1 ] ;
23
+
24
+ /** @type {{ [testName: string]: { regular?: {}; transpiled?: {}; } } } */
25
+ const savedOutputs = { } ;
22
26
23
27
if ( saveOutputMode ) {
24
28
console . log ( 'Will save output as --save-output was supplied...' ) ;
25
29
}
26
30
27
- var typescriptVersion = semver . major ( typescript . version ) + '.' + semver . minor ( typescript . version ) ;
28
- var FLAKY = '_FLAKY_' ;
29
- var IGNORE = '_IGNORE_' ;
31
+ const typescriptVersion = semver . major ( typescript . version ) + '.' + semver . minor ( typescript . version ) ;
32
+ const FLAKY = '_FLAKY_' ;
33
+ const IGNORE = '_IGNORE_' ;
30
34
31
35
// set up new paths
32
- var rootPath = path . resolve ( __dirname , '../../' ) ;
33
- var rootPathWithIncorrectWindowsSeparator = rootPath . replace ( / \\ / g, '/' ) ;
34
- var stagingPath = path . resolve ( rootPath , '.test' ) ;
36
+ const rootPath = path . resolve ( __dirname , '../../' ) ;
37
+ const rootPathWithIncorrectWindowsSeparator = rootPath . replace ( / \\ / g, '/' ) ;
38
+ const stagingPath = path . resolve ( rootPath , '.test' ) ;
35
39
36
- var testPath = path . join ( __dirname , testToRun ) ;
37
- var testIsFlaky = pathExists ( path . join ( testPath , FLAKY ) ) ;
38
- var testIsIgnored = pathExists ( path . join ( testPath , IGNORE ) ) ;
40
+ const testPath = path . join ( __dirname , testToRun ) ;
41
+ const testIsFlaky = pathExists ( path . join ( testPath , FLAKY ) ) ;
42
+ const testIsIgnored = pathExists ( path . join ( testPath , IGNORE ) ) ;
39
43
40
44
if ( testIsIgnored ) {
41
45
console . log ( testPath + ' is ignored... Not running test.' ) ;
@@ -46,7 +50,7 @@ if (fs.statSync(testPath).isDirectory() &&
46
50
! testIsIgnored ) {
47
51
48
52
// @ts -ignore
49
- describe ( testToRun , function ( ) {
53
+ describe ( ` ${ testToRun } ${ extraOption ? ` - ${ extraOption } : true` : '' } ` , function ( ) {
50
54
// @ts -ignore
51
55
it ( 'should have the correct output' , createTest ( testToRun , testPath , { } ) ) ;
52
56
@@ -59,9 +63,6 @@ if (fs.statSync(testPath).isDirectory() &&
59
63
60
64
// @ts -ignore
61
65
it ( 'should work with transpileOnly' , createTest ( testToRun , testPath , { transpileOnly : true } ) ) ;
62
-
63
- // The future....
64
- // it('should work with experimentalFileCaching', createTest(testToRun, testPath, { experimentalFileCaching: true }));
65
66
} ) ;
66
67
}
67
68
@@ -76,9 +77,13 @@ function createTest(test, testPath, options) {
76
77
return function ( done ) {
77
78
this . timeout ( 60000 ) ; // sometimes it just takes awhile
78
79
79
- var testState = createTestState ( ) ;
80
- var paths = createPaths ( stagingPath , test , options ) ;
81
- var outputs = createOutputs ( ) ;
80
+ const testState = createTestState ( ) ;
81
+ const paths = createPaths ( stagingPath , test , options ) ;
82
+ const outputs = {
83
+ regularSavedOutput : undefined ,
84
+ transpiledSavedOutput : undefined ,
85
+ currentSavedOutput : undefined
86
+ } ;
82
87
83
88
storeSavedOutputs ( saveOutputMode , outputs , test , options , paths ) ;
84
89
@@ -107,7 +112,9 @@ function createTestState() {
107
112
}
108
113
109
114
function createPaths ( stagingPath , test , options ) {
110
- var testStagingPath = path . join ( stagingPath , test + ( options . transpileOnly ? '.transpile' : '' ) ) ;
115
+ const testStagingPath = path . join ( stagingPath , test + ( options . transpileOnly ? '.transpile' : '' ) ) ;
116
+ rimraf . sync ( testStagingPath ) ; // Make sure it's clean
117
+
111
118
return {
112
119
testStagingPath : testStagingPath ,
113
120
actualOutput : path . join ( testStagingPath , 'actualOutput' ) ,
@@ -117,21 +124,21 @@ function createPaths(stagingPath, test, options) {
117
124
} ;
118
125
}
119
126
120
- function createOutputs ( ) {
121
- return {
122
- regularSavedOutput : undefined ,
123
- transpiledSavedOutput : undefined ,
124
- currentSavedOutput : undefined
125
- } ;
126
- }
127
-
127
+ /**
128
+ *
129
+ * @param { boolean } saveOutputMode
130
+ * @param { { regularSavedOutput: {}; transpiledSavedOutput: {}; currentSavedOutput: {};} } outputs
131
+ * @param { * } test
132
+ * @param { * } options
133
+ * @param { { testStagingPath: string; actualOutput: string; expectedOutput: string; webpackOutput: string; originalExpectedOutput: string;} } paths
134
+ */
128
135
function storeSavedOutputs ( saveOutputMode , outputs , test , options , paths ) {
129
136
if ( saveOutputMode ) {
130
137
savedOutputs [ test ] = savedOutputs [ test ] || { } ;
131
138
132
- outputs . regularSavedOutput = savedOutputs [ test ] . regular = savedOutputs [ test ] . regular || { } ;
139
+ outputs . regularSavedOutput = savedOutputs [ test ] . regular = savedOutputs [ test ] . regular || { } ;
133
140
outputs . transpiledSavedOutput = savedOutputs [ test ] . transpiled = savedOutputs [ test ] . transpiled || { } ;
134
- outputs . currentSavedOutput = options . transpileOnly ? outputs . transpiledSavedOutput : outputs . regularSavedOutput ;
141
+ outputs . currentSavedOutput = options . transpileOnly ? outputs . transpiledSavedOutput : outputs . regularSavedOutput ;
135
142
136
143
mkdirp . sync ( paths . originalExpectedOutput ) ;
137
144
} else {
@@ -140,37 +147,36 @@ function storeSavedOutputs(saveOutputMode, outputs, test, options, paths) {
140
147
}
141
148
142
149
function createWebpackConfig ( paths , optionsOriginal ) {
143
- var config = require ( path . join ( paths . testStagingPath , 'webpack.config' ) ) ;
150
+ const config = require ( path . join ( paths . testStagingPath , 'webpack.config' ) ) ;
144
151
145
- var options = Object . assign ( {
152
+ const extraOptionMaybe = extraOption ? { [ extraOption ] : true } : { } ;
153
+ const options = Object . assign ( {
146
154
// colors: false,
147
155
silent : true ,
148
156
compilerOptions : {
149
157
newLine : 'LF'
150
158
}
151
- } , optionsOriginal ) ;
159
+ } , optionsOriginal , extraOptionMaybe ) ;
152
160
153
- var tsLoaderPath = require ( 'path' ) . join ( __dirname , "../../index.js" ) ;
161
+ const tsLoaderPath = require ( 'path' ) . join ( __dirname , "../../index.js" ) ;
154
162
155
163
aliasLoader ( config , tsLoaderPath , options ) ;
156
164
157
- delete config . ts ;
158
-
159
165
config . output . path = paths . webpackOutput ;
160
166
config . context = paths . testStagingPath ;
161
167
config . resolveLoader = config . resolveLoader || { } ;
162
168
config . resolveLoader . alias = config . resolveLoader . alias || { } ;
163
169
config . resolveLoader . alias . newLine = path . join ( __dirname , 'newline.loader.js' ) ;
164
170
165
- var rules = config . module . rules || config . module . loaders ;
171
+ const rules = config . module . rules || config . module . loaders ;
166
172
167
173
rules . push ( { test : / \. j s $ / , loader : 'newLine' } ) ;
168
174
return config ;
169
175
}
170
176
171
177
function createWebpackWatchHandler ( done , paths , testState , outputs , options , test ) {
172
178
return function ( err , stats ) {
173
- var patch = setPathsAndGetPatch ( paths , testState ) ;
179
+ const patch = setPathsAndGetPatch ( paths , testState ) ;
174
180
175
181
cleanHashFromOutput ( stats , paths . webpackOutput ) ;
176
182
@@ -190,7 +196,7 @@ function createWebpackWatchHandler(done, paths, testState, outputs, options, tes
190
196
}
191
197
192
198
function setPathsAndGetPatch ( paths , testState ) {
193
- var patch = '' ;
199
+ let patch = '' ;
194
200
if ( testState . iteration > 0 ) {
195
201
patch = 'patch' + ( testState . iteration - 1 ) ;
196
202
paths . actualOutput = path . join ( paths . testStagingPath , 'actualOutput' , patch ) ;
@@ -208,12 +214,12 @@ function saveOutputIfRequired(saveOutputMode, paths, outputs, options, patch) {
208
214
if ( saveOutputMode ) {
209
215
// loop through webpackOutput and rename to .transpiled if needed
210
216
glob . sync ( '**/*' , { cwd : paths . webpackOutput , nodir : true } ) . forEach ( function ( file ) {
211
- var patchedFileName = patch + '/' + file ;
217
+ const patchedFileName = patch + '/' + file ;
212
218
outputs . currentSavedOutput [ patchedFileName ] = fs . readFileSync ( path . join ( paths . webpackOutput , file ) , 'utf-8' ) ;
213
219
214
220
if ( options . transpileOnly ) {
215
221
if ( outputs . regularSavedOutput [ patchedFileName ] !== outputs . transpiledSavedOutput [ patchedFileName ] ) {
216
- var extension = path . extname ( file ) ;
222
+ const extension = path . extname ( file ) ;
217
223
fs . renameSync (
218
224
path . join ( paths . webpackOutput , file ) ,
219
225
path . join ( paths . webpackOutput , path . basename ( file , extension ) + '.transpiled' + extension )
@@ -228,17 +234,17 @@ function saveOutputIfRequired(saveOutputMode, paths, outputs, options, patch) {
228
234
229
235
function handleErrors ( err , paths , outputs , patch , options ) {
230
236
if ( err ) {
231
- var errFileName = 'err.txt' ;
237
+ const errFileName = 'err.txt' ;
232
238
233
- var errString = err . toString ( )
239
+ const errString = err . toString ( )
234
240
. replace ( new RegExp ( regexEscape ( paths . testStagingPath + path . sep ) , 'g' ) , '' )
235
241
. replace ( new RegExp ( regexEscape ( rootPath + path . sep ) , 'g' ) , '' )
236
242
. replace ( new RegExp ( regexEscape ( rootPath ) , 'g' ) , '' )
237
243
. replace ( / \. t r a n s p i l e / g, '' ) ;
238
244
239
245
fs . writeFileSync ( path . join ( paths . actualOutput , errFileName ) , errString ) ;
240
246
if ( saveOutputMode ) {
241
- var patchedErrFileName = patch + '/' + errFileName ;
247
+ const patchedErrFileName = patch + '/' + errFileName ;
242
248
outputs . currentSavedOutput [ patchedErrFileName ] = errString ;
243
249
244
250
if ( options . transpileOnly ) {
@@ -257,16 +263,16 @@ function storeStats(stats, testState, paths, outputs, patch, options) {
257
263
if ( stats && stats . hash !== testState . lastHash ) {
258
264
testState . lastHash = stats . hash ;
259
265
260
- var statsFileName = 'output.txt' ;
266
+ const statsFileName = 'output.txt' ;
261
267
262
268
// do a little magic to normalize `\` to `/` for asset output
263
- var newAssets = { } ;
269
+ const newAssets = { } ;
264
270
Object . keys ( stats . compilation . assets ) . forEach ( function ( asset ) {
265
271
newAssets [ asset . replace ( / \\ / g, "/" ) ] = stats . compilation . assets [ asset ] ;
266
272
} ) ;
267
273
stats . compilation . assets = newAssets ;
268
274
269
- var statsString = stats . toString ( { timings : false , version : false , hash : false , builtAt : false } )
275
+ const statsString = stats . toString ( { timings : false , version : false , hash : false , builtAt : false } )
270
276
. replace ( / ^ B u i l t a t : .+ $ / gm, '' )
271
277
. replace ( new RegExp ( regexEscape ( paths . testStagingPath + path . sep ) , 'g' ) , '' )
272
278
. replace ( new RegExp ( regexEscape ( rootPath + path . sep ) , 'g' ) , '' )
@@ -276,7 +282,7 @@ function storeStats(stats, testState, paths, outputs, patch, options) {
276
282
277
283
fs . writeFileSync ( path . join ( paths . actualOutput , statsFileName ) , statsString ) ;
278
284
if ( saveOutputMode ) {
279
- var patchedStatsFileName = patch + '/' + statsFileName ;
285
+ const patchedStatsFileName = patch + '/' + statsFileName ;
280
286
outputs . currentSavedOutput [ patchedStatsFileName ] = statsString ;
281
287
282
288
if ( options . transpileOnly ) {
@@ -297,7 +303,7 @@ function compareFiles(paths, options, test, patch) {
297
303
glob . sync ( '**/*' , { cwd : paths . expectedOutput , nodir : true } ) . forEach ( function ( file ) {
298
304
if ( / \. t r a n s p i l e d / . test ( file ) ) {
299
305
if ( options . transpileOnly ) { // rename if we're in transpile mode
300
- var extension = path . extname ( file ) ;
306
+ const extension = path . extname ( file ) ;
301
307
fs . renameSync (
302
308
path . join ( paths . expectedOutput , file ) ,
303
309
path . join ( paths . expectedOutput , path . dirname ( file ) , path . basename ( file , '.transpiled' + extension ) + extension )
@@ -310,7 +316,7 @@ function compareFiles(paths, options, test, patch) {
310
316
} ) ;
311
317
312
318
// compare actual to expected
313
- var actualFiles = glob . sync ( '**/*' , { cwd : paths . actualOutput , nodir : true } ) ,
319
+ const actualFiles = glob . sync ( '**/*' , { cwd : paths . actualOutput , nodir : true } ) ,
314
320
expectedFiles = glob . sync ( '**/*' , { cwd : paths . expectedOutput , nodir : true } )
315
321
. filter ( function ( file ) { return ! / ^ p a t c h / . test ( file ) ; } ) ,
316
322
allFiles = { } ;
@@ -319,8 +325,8 @@ function compareFiles(paths, options, test, patch) {
319
325
expectedFiles . forEach ( function ( file ) { allFiles [ file ] = true } ) ;
320
326
321
327
Object . keys ( allFiles ) . forEach ( function ( file ) {
322
- var actual = getNormalisedFileContent ( file , paths . actualOutput , test ) ;
323
- var expected = getNormalisedFileContent ( file , paths . expectedOutput , test ) ;
328
+ const actual = getNormalisedFileContent ( file , paths . actualOutput ) ;
329
+ const expected = getNormalisedFileContent ( file , paths . expectedOutput ) ;
324
330
325
331
compareActualAndExpected ( test , actual , expected , patch , file ) ;
326
332
} ) ;
@@ -329,7 +335,7 @@ function compareFiles(paths, options, test, patch) {
329
335
330
336
function copyPatchOrEndTest ( testStagingPath , watcher , testState , done ) {
331
337
// check for new files to copy in
332
- var patchPath = path . join ( testStagingPath , 'patch' + testState . iteration ) ;
338
+ const patchPath = path . join ( testStagingPath , 'patch' + testState . iteration ) ;
333
339
if ( fs . existsSync ( patchPath ) ) {
334
340
testState . iteration ++ ;
335
341
@@ -356,10 +362,10 @@ function copyPatchOrEndTest(testStagingPath, watcher, testState, done) {
356
362
* independent as possible
357
363
**/
358
364
function cleanHashFromOutput ( stats , webpackOutput ) {
359
- var escapedStagingPath = stagingPath . replace ( new RegExp ( regexEscape ( '\\' ) , 'g' ) , '\\\\' ) ;
365
+ const escapedStagingPath = stagingPath . replace ( new RegExp ( regexEscape ( '\\' ) , 'g' ) , '\\\\' ) ;
360
366
if ( stats ) {
361
367
glob . sync ( '**/*' , { cwd : webpackOutput , nodir : true } ) . forEach ( function ( file ) {
362
- var content = fs . readFileSync ( path . join ( webpackOutput , file ) , 'utf-8' )
368
+ const content = fs . readFileSync ( path . join ( webpackOutput , file ) , 'utf-8' )
363
369
. split ( stats . hash ) . join ( '[hash]' )
364
370
. replace ( / \r \n / g, '\n' )
365
371
// Ignore complete paths
@@ -372,12 +378,13 @@ function cleanHashFromOutput(stats, webpackOutput) {
372
378
}
373
379
}
374
380
375
- function getNormalisedFileContent ( file , location , test ) {
376
- var fileContent ;
377
- var filePath = path . join ( location , file ) ;
381
+ function getNormalisedFileContent ( file , location ) {
382
+ /** @type {string } */
383
+ let fileContent ;
384
+ const filePath = path . join ( location , file ) ;
378
385
379
386
try {
380
- var originalContent = fs . readFileSync ( filePath ) . toString ( ) ;
387
+ const originalContent = fs . readFileSync ( filePath ) . toString ( ) ;
381
388
fileContent = ( file . indexOf ( 'output.' ) === 0
382
389
? normaliseString ( originalContent )
383
390
// Built at: 2/15/2018 8:33:18 PM
0 commit comments