webpack/test/WatchTestCases.test.js

344 lines
9.6 KiB
JavaScript
Raw Normal View History

2018-01-27 23:34:11 +08:00
/* global beforeAll expect fit */
2017-01-18 23:47:00 +08:00
"use strict";
2016-12-14 19:03:24 +08:00
2017-01-18 23:47:00 +08:00
const path = require("path");
const fs = require("fs");
const vm = require("vm");
const mkdirp = require("mkdirp");
2017-01-18 23:47:00 +08:00
const checkArrayExpectation = require("./checkArrayExpectation");
2018-01-27 23:34:11 +08:00
const async = require("async");
2017-01-18 23:47:00 +08:00
const Stats = require("../lib/Stats");
const webpack = require("../lib/webpack");
2016-12-14 19:03:24 +08:00
function copyDiff(src, dest) {
2018-02-25 18:46:17 +08:00
if (!fs.existsSync(dest)) fs.mkdirSync(dest);
2017-01-18 23:47:00 +08:00
const files = fs.readdirSync(src);
2018-02-25 18:46:17 +08:00
files.forEach(filename => {
2017-01-18 23:47:00 +08:00
const srcFile = path.join(src, filename);
const destFile = path.join(dest, filename);
const directory = fs.statSync(srcFile).isDirectory();
2018-02-25 18:46:17 +08:00
if (directory) {
2016-12-14 19:03:24 +08:00
copyDiff(srcFile, destFile);
} else {
var content = fs.readFileSync(srcFile);
2018-02-25 18:46:17 +08:00
if (/^DELETE\s*$/.test(content.toString("utf-8")))
fs.unlinkSync(destFile);
2018-02-25 18:46:17 +08:00
else fs.writeFileSync(destFile, content);
2016-12-14 19:03:24 +08:00
}
});
}
function remove(src) {
2018-02-25 18:46:17 +08:00
if (!fs.existsSync(src)) return;
2017-01-18 23:47:00 +08:00
const files = fs.readdirSync(src);
2018-02-25 18:46:17 +08:00
files.forEach(filename => {
2017-01-18 23:47:00 +08:00
const srcFile = path.join(src, filename);
const directory = fs.statSync(srcFile).isDirectory();
2018-02-25 18:46:17 +08:00
if (directory) {
2016-12-14 19:03:24 +08:00
remove(srcFile);
} else {
fs.unlinkSync(srcFile);
}
});
}
2017-01-18 23:47:00 +08:00
describe("WatchTestCases", () => {
2018-02-25 18:46:17 +08:00
if (process.env.NO_WATCH_TESTS) {
2017-12-20 23:53:56 +08:00
it("long running tests excluded");
return;
}
2017-01-18 23:47:00 +08:00
const casesPath = path.join(__dirname, "watchCases");
let categories = fs.readdirSync(casesPath);
2016-12-14 19:03:24 +08:00
2018-02-25 18:46:17 +08:00
categories = categories.map(cat => {
2016-12-14 19:03:24 +08:00
return {
name: cat,
2018-02-25 18:46:17 +08:00
tests: fs
.readdirSync(path.join(casesPath, cat))
.filter(folder => folder.indexOf("_") < 0)
.sort()
2016-12-14 19:03:24 +08:00
};
});
2018-01-27 23:34:11 +08:00
beforeAll(() => {
2017-01-18 23:47:00 +08:00
let dest = path.join(__dirname, "js");
2018-02-25 18:46:17 +08:00
if (!fs.existsSync(dest)) fs.mkdirSync(dest);
2016-12-14 19:03:24 +08:00
dest = path.join(__dirname, "js", "watch-src");
2018-02-25 18:46:17 +08:00
if (!fs.existsSync(dest)) fs.mkdirSync(dest);
2016-12-14 19:03:24 +08:00
});
2018-02-25 18:46:17 +08:00
categories.forEach(category => {
2018-01-31 04:03:23 +08:00
beforeAll(() => {
2017-01-18 23:47:00 +08:00
const dest = path.join(__dirname, "js", "watch-src", category.name);
2018-02-25 18:46:17 +08:00
if (!fs.existsSync(dest)) fs.mkdirSync(dest);
2017-11-15 21:08:11 +08:00
});
2017-01-18 23:47:00 +08:00
describe(category.name, () => {
2018-02-25 18:46:17 +08:00
category.tests.forEach(testName => {
2017-01-18 23:47:00 +08:00
describe(testName, () => {
2018-02-25 18:46:17 +08:00
const tempDirectory = path.join(
__dirname,
"js",
"watch-src",
category.name,
`${testName}-${Math.random()
.toPrecision(21)
.slice(2)}`
);
2017-01-18 23:47:00 +08:00
const testDirectory = path.join(casesPath, category.name, testName);
2018-02-25 18:46:17 +08:00
const runs = fs
.readdirSync(testDirectory)
.sort()
.filter(name => {
return fs.statSync(path.join(testDirectory, name)).isDirectory();
})
.map(name => ({ name }));
let exportedTests = [];
2018-02-25 18:46:17 +08:00
beforeAll(
() =>
new Promise((resolve, reject) => {
const done = err => {
if (err) return reject(err);
resolve();
};
const outputDirectory = path.join(
__dirname,
"js",
"watch",
category.name,
testName
);
2016-12-14 19:03:24 +08:00
2018-02-25 18:46:17 +08:00
let options = {};
const configPath = path.join(
testDirectory,
"webpack.config.js"
);
if (fs.existsSync(configPath)) options = require(configPath);
const applyConfig = options => {
if (!options.mode) options.mode = "development";
if (!options.context) options.context = tempDirectory;
if (!options.entry) options.entry = "./index.js";
if (!options.target) options.target = "async-node";
if (!options.output) options.output = {};
if (!options.output.path)
options.output.path = outputDirectory;
if (typeof options.output.pathinfo === "undefined")
options.output.pathinfo = true;
if (!options.output.filename)
options.output.filename = "bundle.js";
};
2018-02-25 18:46:17 +08:00
if (Array.isArray(options)) {
options.forEach(applyConfig);
} else {
applyConfig(options);
}
2018-02-25 18:46:17 +08:00
const state = {};
let runIdx = 0;
let waitMode = false;
let run = runs[runIdx];
let triggeringFilename;
let lastHash = "";
const currentWatchStepModule = require("./helpers/currentWatchStep");
currentWatchStepModule.step = run.name;
copyDiff(path.join(testDirectory, run.name), tempDirectory);
setTimeout(() => {
const compiler = webpack(options);
compiler.hooks.invalid.tap(
"WatchTestCasesTest",
(filename, mtime) => {
triggeringFilename = filename;
}
2018-02-25 18:46:17 +08:00
);
const watching = compiler.watch(
{
aggregateTimeout: 1000
},
(err, stats) => {
if (err) return done(err);
if (!stats)
return done(
new Error("No stats reported from Compiler")
);
if (stats.hash === lastHash) return;
lastHash = stats.hash;
if (run.done && lastHash !== stats.hash) {
return done(
new Error(
"Compilation changed but no change was issued " +
lastHash +
" != " +
stats.hash +
" (run " +
runIdx +
")\n" +
"Triggering change: " +
triggeringFilename
)
);
}
if (waitMode) return;
run.done = true;
if (err) return done(err);
const statOptions = Stats.presetToOptions("verbose");
statOptions.colors = false;
mkdirp.sync(outputDirectory);
fs.writeFileSync(
path.join(outputDirectory, "stats.txt"),
stats.toString(statOptions),
"utf-8"
);
const jsonStats = stats.toJson({
errorDetails: true
});
if (
checkArrayExpectation(
path.join(testDirectory, run.name),
jsonStats,
"error",
"Error",
done
)
)
return;
if (
checkArrayExpectation(
path.join(testDirectory, run.name),
jsonStats,
"warning",
"Warning",
done
)
)
return;
2016-12-14 19:03:24 +08:00
2018-02-25 18:46:17 +08:00
function _it(title, fn) {
exportedTests.push({ title, fn, timeout: 45000 });
}
const globalContext = {
console: console,
expect: expect
};
function _require(currentDirectory, module) {
if (Array.isArray(module) || /^\.\.?\//.test(module)) {
let fn;
let content;
let p;
if (Array.isArray(module)) {
p = path.join(currentDirectory, module[0]);
content = module
.map(arg => {
p = path.join(currentDirectory, arg);
return fs.readFileSync(p, "utf-8");
})
.join("\n");
} else {
p = path.join(currentDirectory, module);
content = fs.readFileSync(p, "utf-8");
}
if (
options.target === "web" ||
options.target === "webworker"
) {
fn = vm.runInNewContext(
"(function(require, module, exports, __dirname, __filename, it, WATCH_STEP, STATS_JSON, STATE, expect, window) {" +
content +
"\n})",
globalContext,
p
);
} else {
fn = vm.runInThisContext(
"(function(require, module, exports, __dirname, __filename, it, WATCH_STEP, STATS_JSON, STATE, expect) {" +
content +
"\n})",
p
);
}
const m = {
exports: {}
};
fn.call(
m.exports,
_require.bind(null, path.dirname(p)),
m,
m.exports,
path.dirname(p),
p,
_it,
run.name,
jsonStats,
state,
expect,
globalContext
);
return module.exports;
} else if (
testConfig.modules &&
module in testConfig.modules
) {
return testConfig.modules[module];
} else return require.requireActual(module);
}
let testConfig = {};
try {
// try to load a test file
testConfig = require(path.join(
testDirectory,
"test.config.js"
));
} catch (e) {}
if (testConfig.noTests) return process.nextTick(done);
_require(
outputDirectory,
testConfig.bundlePath || "./bundle.js"
);
if (exportedTests.length < 1)
return done(
new Error("No tests exported by test case")
);
runIdx++;
if (runIdx < runs.length) {
run = runs[runIdx];
waitMode = true;
setTimeout(() => {
waitMode = false;
currentWatchStepModule.step = run.name;
copyDiff(
path.join(testDirectory, run.name),
tempDirectory
);
}, 1500);
} else {
watching.close();
process.nextTick(done);
}
}
);
}, 300);
}),
45000
);
it(testName + " should compile", () => {});
2018-02-25 18:46:17 +08:00
exportedTests.forEach(({ title, fn, timeout }) =>
it(title, fn, timeout)
);
afterAll(() => {
remove(tempDirectory);
});
2016-12-14 19:03:24 +08:00
});
});
});
});
});