2017-01-18 23:47:00 +08:00
|
|
|
"use strict";
|
2016-12-14 19:03:24 +08:00
|
|
|
|
2017-11-15 21:08:11 +08:00
|
|
|
require("should");
|
2017-01-18 23:47:00 +08:00
|
|
|
const path = require("path");
|
|
|
|
const fs = require("fs");
|
|
|
|
const vm = require("vm");
|
|
|
|
const Test = require("mocha/lib/test");
|
2017-11-21 17:41:01 +08:00
|
|
|
const mkdirp = require("mkdirp");
|
2017-01-18 23:47:00 +08:00
|
|
|
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 {
|
2017-01-18 05:26:38 +08:00
|
|
|
var content = fs.readFileSync(srcFile);
|
|
|
|
if(/^DELETE\s*$/.test(content.toString("utf-8")))
|
|
|
|
fs.unlinkSync(destFile);
|
|
|
|
else
|
|
|
|
fs.writeFileSync(destFile, content);
|
2016-12-14 19:03:24 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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", () => {
|
2017-12-20 23:53:56 +08:00
|
|
|
if(process.env.NO_WATCH_TESTS) {
|
|
|
|
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
|
|
|
|
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-11-15 21:08:11 +08:00
|
|
|
});
|
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) {
|
2017-03-06 11:52:17 +08:00
|
|
|
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) => {
|
2017-11-21 17:41:01 +08:00
|
|
|
if(!options.mode) options.mode = "development";
|
2017-01-05 13:46:13 +08:00
|
|
|
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
|
|
|
};
|
2017-01-05 13:46:13 +08:00
|
|
|
if(Array.isArray(options)) {
|
2017-01-18 23:47:00 +08:00
|
|
|
options.forEach(applyConfig);
|
2017-01-05 13:46:13 +08:00
|
|
|
} else {
|
2017-01-18 23:47:00 +08:00
|
|
|
applyConfig(options);
|
2017-01-05 13:46:13 +08:00
|
|
|
}
|
2017-01-06 13:03:54 +08:00
|
|
|
|
2017-01-18 23:47:00 +08:00
|
|
|
const state = {};
|
|
|
|
let runIdx = 0;
|
2017-09-13 22:29:37 +08:00
|
|
|
let waitMode = false;
|
2017-01-18 23:47:00 +08:00
|
|
|
let run = runs[runIdx];
|
2017-09-13 22:29:37 +08:00
|
|
|
let triggeringFilename;
|
2017-01-18 23:47:00 +08:00
|
|
|
let lastHash = "";
|
2017-09-13 22:29:37 +08:00
|
|
|
const currentWatchStepModule = require("./helpers/currentWatchStep");
|
|
|
|
currentWatchStepModule.step = run.name;
|
2016-12-14 19:03:24 +08:00
|
|
|
copyDiff(path.join(testDirectory, run.name), tempDirectory);
|
|
|
|
|
2017-09-13 22:29:37 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
const compiler = webpack(options);
|
2017-12-20 23:51:24 +08:00
|
|
|
compiler.hooks.invalid.tap("WatchTestCasesTest", (filename, mtime) => {
|
2017-09-13 22:29:37 +08:00
|
|
|
triggeringFilename = filename;
|
2016-12-14 19:03:24 +08:00
|
|
|
});
|
2017-09-13 22:29:37 +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;
|
2017-11-21 17:41:01 +08:00
|
|
|
mkdirp.sync(outputDirectory);
|
2017-09-13 22:29:37 +08:00
|
|
|
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;
|
|
|
|
let exportedTests = 0;
|
2016-12-14 19:03:24 +08:00
|
|
|
|
2017-09-13 22:29:37 +08:00
|
|
|
function _it(title, fn) {
|
|
|
|
const test = new Test(title, fn);
|
|
|
|
run.suite.addTest(test);
|
|
|
|
exportedTests++;
|
|
|
|
return test;
|
|
|
|
}
|
2016-12-14 19:03:24 +08:00
|
|
|
|
2018-01-13 06:50:47 +08:00
|
|
|
const globalContext = {
|
|
|
|
console: console
|
|
|
|
};
|
|
|
|
|
2017-09-13 22:29:37 +08:00
|
|
|
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");
|
|
|
|
}
|
2018-01-13 06:50:47 +08:00
|
|
|
if(options.target === "web" || options.target === "webworker") {
|
|
|
|
fn = vm.runInNewContext("(function(require, module, exports, __dirname, __filename, it, WATCH_STEP, STATS_JSON, STATE, window) {" + content + "\n})", globalContext, p);
|
|
|
|
} else {
|
|
|
|
fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, WATCH_STEP, STATS_JSON, STATE) {" + content + "\n})", p);
|
|
|
|
}
|
2017-09-13 22:29:37 +08:00
|
|
|
const m = {
|
|
|
|
exports: {}
|
|
|
|
};
|
2018-01-13 06:50:47 +08:00
|
|
|
fn.call(m.exports, _require.bind(null, path.dirname(p)), m, m.exports, path.dirname(p), p, _it, run.name, jsonStats, state, globalContext);
|
2017-09-13 22:29:37 +08:00
|
|
|
return module.exports;
|
|
|
|
} else if(testConfig.modules && module in testConfig.modules) {
|
|
|
|
return testConfig.modules[module];
|
|
|
|
} else return require(module);
|
|
|
|
}
|
2016-12-14 19:03:24 +08:00
|
|
|
|
2017-09-13 22:29:37 +08:00
|
|
|
let testConfig = {};
|
|
|
|
try {
|
|
|
|
// try to load a test file
|
|
|
|
testConfig = require(path.join(testDirectory, "test.config.js"));
|
|
|
|
} catch(e) {}
|
2016-12-14 19:03:24 +08:00
|
|
|
|
2017-09-13 22:29:37 +08:00
|
|
|
if(testConfig.noTests) return process.nextTick(done);
|
2018-01-13 06:50:47 +08:00
|
|
|
_require(outputDirectory, testConfig.bundlePath || "./bundle.js");
|
2016-12-14 19:03:24 +08:00
|
|
|
|
2017-09-13 22:29:37 +08:00
|
|
|
if(exportedTests < 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);
|
2016-12-14 19:03:24 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|