webpack/test/WatchTestCases.test.js

190 lines
6.2 KiB
JavaScript
Raw Normal View History

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 should = require("should");
const path = require("path");
const fs = require("fs");
const vm = require("vm");
const Test = require("mocha/lib/test");
const checkArrayExpectation = require("./checkArrayExpectation");
const Stats = require("../lib/Stats");
const webpack = require("../lib/webpack");
2016-12-14 19:03:24 +08:00
function copyDiff(src, dest) {
if(!fs.existsSync(dest))
fs.mkdirSync(dest);
2017-01-18 23:47:00 +08:00
const files = fs.readdirSync(src);
files.forEach((filename) => {
const srcFile = path.join(src, filename);
const destFile = path.join(dest, filename);
const directory = fs.statSync(srcFile).isDirectory();
2016-12-14 19:03:24 +08:00
if(directory) {
copyDiff(srcFile, destFile);
} else {
fs.writeFileSync(destFile, fs.readFileSync(srcFile));
}
});
}
function remove(src) {
if(!fs.existsSync(src))
return;
2017-01-18 23:47:00 +08:00
const files = fs.readdirSync(src);
files.forEach((filename) => {
const srcFile = path.join(src, filename);
const directory = fs.statSync(srcFile).isDirectory();
2016-12-14 19:03:24 +08:00
if(directory) {
remove(srcFile);
} else {
fs.unlinkSync(srcFile);
}
});
}
2017-01-18 23:47:00 +08:00
describe("WatchTestCases", () => {
const casesPath = path.join(__dirname, "watchCases");
let categories = fs.readdirSync(casesPath);
2016-12-14 19:03:24 +08:00
2017-01-18 23:47:00 +08:00
categories = categories.map((cat) => {
2016-12-14 19:03:24 +08:00
return {
name: cat,
2017-01-18 23:47:00 +08:00
tests: fs.readdirSync(path.join(casesPath, cat)).filter((folder) => folder.indexOf("_") < 0).sort()
2016-12-14 19:03:24 +08:00
};
});
2017-01-18 23:47:00 +08:00
before(() => {
let dest = path.join(__dirname, "js");
2016-12-14 19:03:24 +08:00
if(!fs.existsSync(dest))
fs.mkdirSync(dest);
dest = path.join(__dirname, "js", "watch-src");
if(!fs.existsSync(dest))
fs.mkdirSync(dest);
});
2017-01-18 23:47:00 +08:00
categories.forEach((category) => {
before(() => {
const dest = path.join(__dirname, "js", "watch-src", category.name);
2016-12-14 19:03:24 +08:00
if(!fs.existsSync(dest))
fs.mkdirSync(dest);
})
2017-01-18 23:47:00 +08:00
describe(category.name, () => {
category.tests.forEach((testName) => {
describe(testName, () => {
const tempDirectory = path.join(__dirname, "js", "watch-src", category.name, testName);
const testDirectory = path.join(casesPath, category.name, testName);
const runs = fs.readdirSync(testDirectory).sort().filter((name) => {
2016-12-14 19:03:24 +08:00
return fs.statSync(path.join(testDirectory, name)).isDirectory();
2017-01-18 23:47:00 +08:00
}).map((name) => {
2016-12-14 19:03:24 +08:00
return {
name: name,
2017-01-18 23:47:00 +08:00
suite: describe(name, () => {})
};
2016-12-14 19:03:24 +08:00
});
2017-01-18 23:47:00 +08:00
before(() => remove(tempDirectory));
2016-12-14 19:03:24 +08:00
it("should compile", function(done) {
this.timeout(45000);
2017-01-18 23:47:00 +08:00
const outputDirectory = path.join(__dirname, "js", "watch", category.name, testName);
2016-12-14 19:03:24 +08:00
2017-01-18 23:47:00 +08:00
let options = {};
const configPath = path.join(testDirectory, "webpack.config.js");
2016-12-14 19:03:24 +08:00
if(fs.existsSync(configPath))
options = require(configPath);
2017-01-18 23:47:00 +08:00
const applyConfig = (options) => {
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";
2017-01-18 23:47:00 +08:00
};
if(Array.isArray(options)) {
2017-01-18 23:47:00 +08:00
options.forEach(applyConfig);
} else {
2017-01-18 23:47:00 +08:00
applyConfig(options);
}
2017-01-18 23:47:00 +08:00
const state = {};
let runIdx = 0;
let run = runs[runIdx];
let lastHash = "";
2016-12-14 19:03:24 +08:00
copyDiff(path.join(testDirectory, run.name), tempDirectory);
2017-01-18 23:47:00 +08:00
const compiler = webpack(options);
const watching = compiler.watch({}, (err, stats) => {
if(stats.hash === lastHash)
return;
lastHash = stats.hash;
if(run.done)
return done(new Error("Compilation changed but no change was issued " + lastHash + " != " + stats.hash + " (run " + runIdx + ")"));
2016-12-14 19:03:24 +08:00
run.done = true;
if(err) return done(err);
2017-01-18 23:47:00 +08:00
const statOptions = Stats.presetToOptions("verbose");
2016-12-14 19:03:24 +08:00
statOptions.colors = false;
fs.writeFileSync(path.join(outputDirectory, "stats.txt"), stats.toString(statOptions), "utf-8");
2017-01-18 23:47:00 +08:00
const jsonStats = stats.toJson({
2016-12-14 19:03:24 +08:00
errorDetails: true
});
if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return;
if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return;
2017-01-18 23:47:00 +08:00
let exportedTests = 0;
2016-12-14 19:03:24 +08:00
function _it(title, fn) {
2017-01-18 23:47:00 +08:00
const test = new Test(title, fn);
2016-12-14 19:03:24 +08:00
run.suite.addTest(test);
exportedTests++;
return test;
}
function _require(currentDirectory, module) {
if(Array.isArray(module) || /^\.\.?\//.test(module)) {
2017-01-18 23:47:00 +08:00
let fn;
let content;
let p;
2016-12-14 19:03:24 +08:00
if(Array.isArray(module)) {
2017-01-18 23:47:00 +08:00
p = path.join(currentDirectory, module[0]);
content = module.map((arg) => {
p = path.join(currentDirectory, arg);
2016-12-14 19:03:24 +08:00
return fs.readFileSync(p, "utf-8");
}).join("\n");
} else {
2017-01-18 23:47:00 +08:00
p = path.join(currentDirectory, module);
2016-12-14 19:03:24 +08:00
content = fs.readFileSync(p, "utf-8");
}
fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, WATCH_STEP, STATS_JSON, STATE) {" + content + "\n})", p);
2017-01-18 23:47:00 +08:00
const m = {
2016-12-14 19:03:24 +08:00
exports: {}
};
2017-01-18 23:47:00 +08:00
fn.call(m.exports, _require.bind(null, path.dirname(p)), m, m.exports, path.dirname(p), p, _it, run.name, jsonStats, state);
2016-12-14 19:03:24 +08:00
return module.exports;
} else if(testConfig.modules && module in testConfig.modules) {
return testConfig.modules[module];
} else return require(module);
}
2017-01-18 23:47:00 +08:00
let testConfig = {};
2016-12-14 19:03:24 +08:00
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, "./bundle.js");
if(exportedTests < 1) return done(new Error("No tests exported by test case"));
runIdx++;
if(runIdx < runs.length) {
run = runs[runIdx];
2017-01-18 23:47:00 +08:00
setTimeout(() => {
copyDiff(path.join(testDirectory, run.name), tempDirectory);
}, 50);
2016-12-14 19:03:24 +08:00
} else {
watching.close();
2016-12-14 19:03:24 +08:00
process.nextTick(done);
}
});
});
});
});
});
});
});