Skip to content

Commit 3588bb0

Browse files
committed
run experimentalFileCaching as a second pass in test pack
1 parent 16f6f39 commit 3588bb0

File tree

3 files changed

+177
-120
lines changed

3 files changed

+177
-120
lines changed

test/.prettierrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"overrides": [
3+
{
4+
"files": "*.js",
5+
"options": {
6+
"singleQuote": true
7+
}
8+
}
9+
]
10+
}

test/comparison-tests/create-and-execute-test.js

Lines changed: 80 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
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');
77
// @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');
1515

16-
var saveOutputMode = process.argv.indexOf('--save-output') !== -1;
16+
const saveOutputMode = process.argv.indexOf('--save-output') !== -1;
1717

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];
2020

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 = {};
2226

2327
if (saveOutputMode) {
2428
console.log('Will save output as --save-output was supplied...');
2529
}
2630

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_';
3034

3135
// 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');
3539

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));
3943

4044
if (testIsIgnored) {
4145
console.log(testPath + ' is ignored... Not running test.');
@@ -46,7 +50,7 @@ if (fs.statSync(testPath).isDirectory() &&
4650
!testIsIgnored) {
4751

4852
// @ts-ignore
49-
describe(testToRun, function () {
53+
describe(`${testToRun}${extraOption ? ` - ${extraOption}: true` : ''}`, function () {
5054
// @ts-ignore
5155
it('should have the correct output', createTest(testToRun, testPath, {}));
5256

@@ -59,9 +63,6 @@ if (fs.statSync(testPath).isDirectory() &&
5963

6064
// @ts-ignore
6165
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 }));
6566
});
6667
}
6768

@@ -76,9 +77,13 @@ function createTest(test, testPath, options) {
7677
return function (done) {
7778
this.timeout(60000); // sometimes it just takes awhile
7879

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+
};
8287

8388
storeSavedOutputs(saveOutputMode, outputs, test, options, paths);
8489

@@ -107,7 +112,9 @@ function createTestState() {
107112
}
108113

109114
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+
111118
return {
112119
testStagingPath: testStagingPath,
113120
actualOutput: path.join(testStagingPath, 'actualOutput'),
@@ -117,21 +124,21 @@ function createPaths(stagingPath, test, options) {
117124
};
118125
}
119126

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+
*/
128135
function storeSavedOutputs(saveOutputMode, outputs, test, options, paths) {
129136
if (saveOutputMode) {
130137
savedOutputs[test] = savedOutputs[test] || {};
131138

132-
outputs.regularSavedOutput = savedOutputs[test].regular = savedOutputs[test].regular || {};
139+
outputs.regularSavedOutput = savedOutputs[test].regular = savedOutputs[test].regular || {};
133140
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;
135142

136143
mkdirp.sync(paths.originalExpectedOutput);
137144
} else {
@@ -140,37 +147,36 @@ function storeSavedOutputs(saveOutputMode, outputs, test, options, paths) {
140147
}
141148

142149
function createWebpackConfig(paths, optionsOriginal) {
143-
var config = require(path.join(paths.testStagingPath, 'webpack.config'));
150+
const config = require(path.join(paths.testStagingPath, 'webpack.config'));
144151

145-
var options = Object.assign({
152+
const extraOptionMaybe = extraOption ? { [extraOption]: true } : {};
153+
const options = Object.assign({
146154
// colors: false,
147155
silent: true,
148156
compilerOptions: {
149157
newLine: 'LF'
150158
}
151-
}, optionsOriginal);
159+
}, optionsOriginal, extraOptionMaybe);
152160

153-
var tsLoaderPath = require('path').join(__dirname, "../../index.js");
161+
const tsLoaderPath = require('path').join(__dirname, "../../index.js");
154162

155163
aliasLoader(config, tsLoaderPath, options);
156164

157-
delete config.ts;
158-
159165
config.output.path = paths.webpackOutput;
160166
config.context = paths.testStagingPath;
161167
config.resolveLoader = config.resolveLoader || {};
162168
config.resolveLoader.alias = config.resolveLoader.alias || {};
163169
config.resolveLoader.alias.newLine = path.join(__dirname, 'newline.loader.js');
164170

165-
var rules = config.module.rules || config.module.loaders;
171+
const rules = config.module.rules || config.module.loaders;
166172

167173
rules.push({ test: /\.js$/, loader: 'newLine' });
168174
return config;
169175
}
170176

171177
function createWebpackWatchHandler(done, paths, testState, outputs, options, test) {
172178
return function (err, stats) {
173-
var patch = setPathsAndGetPatch(paths, testState);
179+
const patch = setPathsAndGetPatch(paths, testState);
174180

175181
cleanHashFromOutput(stats, paths.webpackOutput);
176182

@@ -190,7 +196,7 @@ function createWebpackWatchHandler(done, paths, testState, outputs, options, tes
190196
}
191197

192198
function setPathsAndGetPatch(paths, testState) {
193-
var patch = '';
199+
let patch = '';
194200
if (testState.iteration > 0) {
195201
patch = 'patch' + (testState.iteration - 1);
196202
paths.actualOutput = path.join(paths.testStagingPath, 'actualOutput', patch);
@@ -208,12 +214,12 @@ function saveOutputIfRequired(saveOutputMode, paths, outputs, options, patch) {
208214
if (saveOutputMode) {
209215
// loop through webpackOutput and rename to .transpiled if needed
210216
glob.sync('**/*', { cwd: paths.webpackOutput, nodir: true }).forEach(function (file) {
211-
var patchedFileName = patch + '/' + file;
217+
const patchedFileName = patch + '/' + file;
212218
outputs.currentSavedOutput[patchedFileName] = fs.readFileSync(path.join(paths.webpackOutput, file), 'utf-8');
213219

214220
if (options.transpileOnly) {
215221
if (outputs.regularSavedOutput[patchedFileName] !== outputs.transpiledSavedOutput[patchedFileName]) {
216-
var extension = path.extname(file);
222+
const extension = path.extname(file);
217223
fs.renameSync(
218224
path.join(paths.webpackOutput, file),
219225
path.join(paths.webpackOutput, path.basename(file, extension) + '.transpiled' + extension)
@@ -228,17 +234,17 @@ function saveOutputIfRequired(saveOutputMode, paths, outputs, options, patch) {
228234

229235
function handleErrors(err, paths, outputs, patch, options) {
230236
if (err) {
231-
var errFileName = 'err.txt';
237+
const errFileName = 'err.txt';
232238

233-
var errString = err.toString()
239+
const errString = err.toString()
234240
.replace(new RegExp(regexEscape(paths.testStagingPath + path.sep), 'g'), '')
235241
.replace(new RegExp(regexEscape(rootPath + path.sep), 'g'), '')
236242
.replace(new RegExp(regexEscape(rootPath), 'g'), '')
237243
.replace(/\.transpile/g, '');
238244

239245
fs.writeFileSync(path.join(paths.actualOutput, errFileName), errString);
240246
if (saveOutputMode) {
241-
var patchedErrFileName = patch + '/' + errFileName;
247+
const patchedErrFileName = patch + '/' + errFileName;
242248
outputs.currentSavedOutput[patchedErrFileName] = errString;
243249

244250
if (options.transpileOnly) {
@@ -257,16 +263,16 @@ function storeStats(stats, testState, paths, outputs, patch, options) {
257263
if (stats && stats.hash !== testState.lastHash) {
258264
testState.lastHash = stats.hash;
259265

260-
var statsFileName = 'output.txt';
266+
const statsFileName = 'output.txt';
261267

262268
// do a little magic to normalize `\` to `/` for asset output
263-
var newAssets = {};
269+
const newAssets = {};
264270
Object.keys(stats.compilation.assets).forEach(function (asset) {
265271
newAssets[asset.replace(/\\/g, "/")] = stats.compilation.assets[asset];
266272
});
267273
stats.compilation.assets = newAssets;
268274

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 })
270276
.replace(/^Built at: .+$/gm, '')
271277
.replace(new RegExp(regexEscape(paths.testStagingPath + path.sep), 'g'), '')
272278
.replace(new RegExp(regexEscape(rootPath + path.sep), 'g'), '')
@@ -276,7 +282,7 @@ function storeStats(stats, testState, paths, outputs, patch, options) {
276282

277283
fs.writeFileSync(path.join(paths.actualOutput, statsFileName), statsString);
278284
if (saveOutputMode) {
279-
var patchedStatsFileName = patch + '/' + statsFileName;
285+
const patchedStatsFileName = patch + '/' + statsFileName;
280286
outputs.currentSavedOutput[patchedStatsFileName] = statsString;
281287

282288
if (options.transpileOnly) {
@@ -297,7 +303,7 @@ function compareFiles(paths, options, test, patch) {
297303
glob.sync('**/*', { cwd: paths.expectedOutput, nodir: true }).forEach(function (file) {
298304
if (/\.transpiled/.test(file)) {
299305
if (options.transpileOnly) { // rename if we're in transpile mode
300-
var extension = path.extname(file);
306+
const extension = path.extname(file);
301307
fs.renameSync(
302308
path.join(paths.expectedOutput, file),
303309
path.join(paths.expectedOutput, path.dirname(file), path.basename(file, '.transpiled' + extension) + extension)
@@ -310,7 +316,7 @@ function compareFiles(paths, options, test, patch) {
310316
});
311317

312318
// compare actual to expected
313-
var actualFiles = glob.sync('**/*', { cwd: paths.actualOutput, nodir: true }),
319+
const actualFiles = glob.sync('**/*', { cwd: paths.actualOutput, nodir: true }),
314320
expectedFiles = glob.sync('**/*', { cwd: paths.expectedOutput, nodir: true })
315321
.filter(function (file) { return !/^patch/.test(file); }),
316322
allFiles = {};
@@ -319,8 +325,8 @@ function compareFiles(paths, options, test, patch) {
319325
expectedFiles.forEach(function (file) { allFiles[file] = true });
320326

321327
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);
324330

325331
compareActualAndExpected(test, actual, expected, patch, file);
326332
});
@@ -329,7 +335,7 @@ function compareFiles(paths, options, test, patch) {
329335

330336
function copyPatchOrEndTest(testStagingPath, watcher, testState, done) {
331337
// 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);
333339
if (fs.existsSync(patchPath)) {
334340
testState.iteration++;
335341

@@ -356,10 +362,10 @@ function copyPatchOrEndTest(testStagingPath, watcher, testState, done) {
356362
* independent as possible
357363
**/
358364
function cleanHashFromOutput(stats, webpackOutput) {
359-
var escapedStagingPath = stagingPath.replace(new RegExp(regexEscape('\\'), 'g'), '\\\\');
365+
const escapedStagingPath = stagingPath.replace(new RegExp(regexEscape('\\'), 'g'), '\\\\');
360366
if (stats) {
361367
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')
363369
.split(stats.hash).join('[hash]')
364370
.replace(/\r\n/g, '\n')
365371
// Ignore complete paths
@@ -372,12 +378,13 @@ function cleanHashFromOutput(stats, webpackOutput) {
372378
}
373379
}
374380

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);
378385

379386
try {
380-
var originalContent = fs.readFileSync(filePath).toString();
387+
const originalContent = fs.readFileSync(filePath).toString();
381388
fileContent = (file.indexOf('output.') === 0
382389
? normaliseString(originalContent)
383390
// Built at: 2/15/2018 8:33:18 PM

0 commit comments

Comments
 (0)