2017-01-18 17:08:00 +08:00
|
|
|
"use strict";
|
|
|
|
|
2016-12-23 14:17:57 +08:00
|
|
|
/* globals describe it */
|
2017-01-18 17:08: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");
|
2014-06-18 04:34:30 +08:00
|
|
|
|
2017-01-18 17:08:00 +08:00
|
|
|
const Stats = require("../lib/Stats");
|
|
|
|
const webpack = require("../lib/webpack");
|
2017-07-01 07:37:16 +08:00
|
|
|
const prepareOptions = require("../lib/prepareOptions");
|
2014-06-18 04:34:30 +08:00
|
|
|
|
2017-01-18 17:08:00 +08:00
|
|
|
describe("ConfigTestCases", () => {
|
|
|
|
const casesPath = path.join(__dirname, "configCases");
|
|
|
|
let categories = fs.readdirSync(casesPath);
|
2016-12-08 16:39:30 +08:00
|
|
|
|
2017-01-18 17:08:00 +08:00
|
|
|
categories = categories.map((cat) => {
|
2014-06-18 04:34:30 +08:00
|
|
|
return {
|
|
|
|
name: cat,
|
2017-01-18 17:08:00 +08:00
|
|
|
tests: fs.readdirSync(path.join(casesPath, cat)).filter((folder) => {
|
2014-06-18 04:34:30 +08:00
|
|
|
return folder.indexOf("_") < 0;
|
2017-01-18 17:08:00 +08:00
|
|
|
}).sort().filter((testName) => {
|
|
|
|
const testDirectory = path.join(casesPath, cat, testName);
|
|
|
|
const filterPath = path.join(testDirectory, "test.filter.js");
|
2016-12-23 14:17:57 +08:00
|
|
|
if(fs.existsSync(filterPath) && !require(filterPath)()) {
|
2017-01-18 17:08:00 +08:00
|
|
|
describe.skip(testName, () => it("filtered"));
|
2016-12-23 03:29:03 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2016-12-23 14:10:06 +08:00
|
|
|
})
|
|
|
|
};
|
|
|
|
});
|
2017-01-18 17:08:00 +08:00
|
|
|
categories.forEach((category) => {
|
|
|
|
describe(category.name, () => {
|
|
|
|
category.tests.forEach((testName) => {
|
|
|
|
const suite = describe(testName, () => {});
|
2014-06-18 04:34:30 +08:00
|
|
|
it(testName + " should compile", function(done) {
|
2017-01-18 17:08:00 +08:00
|
|
|
const testDirectory = path.join(casesPath, category.name, testName);
|
|
|
|
const outputDirectory = path.join(__dirname, "js", "config", category.name, testName);
|
2017-07-01 07:37:16 +08:00
|
|
|
const options = prepareOptions(require(path.join(testDirectory, "webpack.config.js")));
|
2017-01-18 17:08:00 +08:00
|
|
|
const optionsArr = [].concat(options);
|
|
|
|
optionsArr.forEach((options, idx) => {
|
2014-06-19 05:02:33 +08:00
|
|
|
if(!options.context) options.context = testDirectory;
|
|
|
|
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;
|
2016-09-07 15:46:27 +08:00
|
|
|
if(typeof options.output.pathinfo === "undefined") options.output.pathinfo = true;
|
2014-06-19 05:02:33 +08:00
|
|
|
if(!options.output.filename) options.output.filename = "bundle" + idx + ".js";
|
2015-05-16 22:10:25 +08:00
|
|
|
if(!options.output.chunkFilename) options.output.chunkFilename = "[id].bundle" + idx + ".js";
|
2014-06-19 05:02:33 +08:00
|
|
|
});
|
2017-10-09 21:28:59 +08:00
|
|
|
let testConfig = {
|
|
|
|
findBundle: function(i, options) {
|
|
|
|
if(fs.existsSync(path.join(options.output.path, "bundle" + i + ".js"))) {
|
|
|
|
return "./bundle" + i + ".js";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
timeout: 30000
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
// try to load a test file
|
|
|
|
testConfig = Object.assign(testConfig, require(path.join(testDirectory, "test.config.js")));
|
|
|
|
} catch(e) {}
|
|
|
|
|
|
|
|
this.timeout(testConfig.timeout);
|
|
|
|
|
2017-01-18 17:08:00 +08:00
|
|
|
webpack(options, (err, stats) => {
|
2014-06-18 04:34:30 +08:00
|
|
|
if(err) return done(err);
|
2017-01-18 17:08:00 +08:00
|
|
|
const statOptions = Stats.presetToOptions("verbose");
|
2016-09-07 15:46:27 +08:00
|
|
|
statOptions.colors = false;
|
|
|
|
fs.writeFileSync(path.join(outputDirectory, "stats.txt"), stats.toString(statOptions), "utf-8");
|
2017-01-18 17:08:00 +08:00
|
|
|
const jsonStats = stats.toJson({
|
2014-06-18 04:34:30 +08:00
|
|
|
errorDetails: true
|
|
|
|
});
|
|
|
|
if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return;
|
|
|
|
if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return;
|
2017-01-18 17:08:00 +08:00
|
|
|
let exportedTests = 0;
|
2015-08-09 18:42:43 +08:00
|
|
|
|
2014-06-18 04:34:30 +08:00
|
|
|
function _it(title, fn) {
|
2017-01-18 17:08:00 +08:00
|
|
|
const test = new Test(title, fn);
|
2014-06-18 04:34:30 +08:00
|
|
|
suite.addTest(test);
|
2014-08-05 00:08:52 +08:00
|
|
|
exportedTests++;
|
2014-06-18 04:34:30 +08:00
|
|
|
return test;
|
|
|
|
}
|
2015-08-09 18:42:43 +08:00
|
|
|
|
2017-01-18 17:08:00 +08:00
|
|
|
const globalContext = {
|
2016-06-16 05:25:40 +08:00
|
|
|
console: console
|
|
|
|
};
|
|
|
|
|
2016-09-21 23:26:25 +08:00
|
|
|
function _require(currentDirectory, module) {
|
2016-06-16 05:25:40 +08:00
|
|
|
if(Array.isArray(module) || /^\.\.?\//.test(module)) {
|
2017-01-18 17:08:00 +08:00
|
|
|
let fn;
|
|
|
|
let content;
|
|
|
|
let p;
|
2016-06-16 05:25:40 +08:00
|
|
|
if(Array.isArray(module)) {
|
2017-01-18 17:08:00 +08:00
|
|
|
p = path.join(currentDirectory, module[0]);
|
|
|
|
content = module.map((arg) => {
|
|
|
|
p = path.join(currentDirectory, arg);
|
2016-06-16 05:25:40 +08:00
|
|
|
return fs.readFileSync(p, "utf-8");
|
|
|
|
}).join("\n");
|
|
|
|
} else {
|
2017-01-18 17:08:00 +08:00
|
|
|
p = path.join(currentDirectory, module);
|
2016-06-16 05:25:40 +08:00
|
|
|
content = fs.readFileSync(p, "utf-8");
|
|
|
|
}
|
2016-12-28 21:49:26 +08:00
|
|
|
if(options.target === "web" || options.target === "webworker") {
|
2016-06-16 05:25:40 +08:00
|
|
|
fn = vm.runInNewContext("(function(require, module, exports, __dirname, __filename, it, window) {" + content + "\n})", globalContext, p);
|
2015-01-07 01:21:33 +08:00
|
|
|
} else {
|
2016-06-16 05:25:40 +08:00
|
|
|
fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it) {" + content + "\n})", p);
|
2015-01-07 01:21:33 +08:00
|
|
|
}
|
2017-01-18 17:08:00 +08:00
|
|
|
const m = {
|
2015-08-09 18:42:43 +08:00
|
|
|
exports: {}
|
|
|
|
};
|
2017-01-18 17:08:00 +08:00
|
|
|
fn.call(m.exports, _require.bind(null, path.dirname(p)), m, m.exports, path.dirname(p), p, _it, globalContext);
|
|
|
|
return m.exports;
|
2016-07-20 21:11:29 +08:00
|
|
|
} else if(testConfig.modules && module in testConfig.modules) {
|
|
|
|
return testConfig.modules[module];
|
2014-06-18 04:34:30 +08:00
|
|
|
} else return require(module);
|
|
|
|
}
|
2017-01-18 17:08:00 +08:00
|
|
|
let filesCount = 0;
|
2016-01-30 18:43:53 +08:00
|
|
|
|
|
|
|
if(testConfig.noTests) return process.nextTick(done);
|
2017-01-18 17:08:00 +08:00
|
|
|
for(let i = 0; i < optionsArr.length; i++) {
|
|
|
|
const bundlePath = testConfig.findBundle(i, optionsArr[i]);
|
2015-08-09 18:42:43 +08:00
|
|
|
if(bundlePath) {
|
2014-08-05 00:08:52 +08:00
|
|
|
filesCount++;
|
2016-09-21 23:26:25 +08:00
|
|
|
_require(outputDirectory, bundlePath);
|
2014-08-05 00:08:52 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-22 19:51:24 +08:00
|
|
|
// give a free pass to compilation that generated an error
|
|
|
|
if(!jsonStats.errors.length && filesCount !== optionsArr.length) return done(new Error("Should have found at least one bundle file per webpack config"));
|
2014-08-05 00:08:52 +08:00
|
|
|
if(exportedTests < filesCount) return done(new Error("No tests exported by test case"));
|
2015-03-04 03:24:46 +08:00
|
|
|
process.nextTick(done);
|
2014-06-18 04:34:30 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-08-22 19:51:24 +08:00
|
|
|
});
|