2016-12-30 14:38:43 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/* globals describe it */
|
|
|
|
const should = require("should");
|
|
|
|
const path = require("path");
|
|
|
|
const fs = require("fs");
|
2017-01-08 09:36:11 +08:00
|
|
|
const spawn = require("child_process").spawn;
|
|
|
|
|
|
|
|
function loadOptsFile(optsPath) {
|
|
|
|
// Options file parser from Mocha
|
|
|
|
// https://github.com/mochajs/mocha/blob/2bb2b9fa35818db7a02e5068364b0c417436b1af/bin/options.js#L25-L31
|
|
|
|
return fs.readFileSync(optsPath, 'utf8')
|
|
|
|
.replace(/\\\s/g, '%20')
|
|
|
|
.split(/\s/)
|
|
|
|
.filter(Boolean)
|
|
|
|
.map(function(value) {
|
|
|
|
return value.replace(/%20/g, ' ');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTestSpecificArguments(testDirectory) {
|
|
|
|
try {
|
|
|
|
return loadOptsFile(path.join(testDirectory, "test.opts"));
|
|
|
|
} catch(e) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 03:53:14 +08:00
|
|
|
function convertToArrayOfLines(outputArray) {
|
|
|
|
if(outputArray.length === 0) return outputArray;
|
|
|
|
return outputArray.join('').split('\n');
|
|
|
|
}
|
|
|
|
|
2017-01-08 09:36:11 +08:00
|
|
|
const casesPath = path.join(__dirname, "binCases");
|
|
|
|
const defaultArgs = loadOptsFile(path.join(casesPath, "test.opts"));
|
2016-12-30 14:38:43 +08:00
|
|
|
|
2017-01-03 05:02:49 +08:00
|
|
|
describe("BinTestCases", function() {
|
2017-01-08 09:36:11 +08:00
|
|
|
const categoryDirectories = fs.readdirSync(casesPath).filter((folder) => {
|
2017-01-03 05:02:49 +08:00
|
|
|
return fs.statSync(path.join(casesPath, folder)).isDirectory()
|
|
|
|
});
|
2017-01-08 09:36:11 +08:00
|
|
|
|
|
|
|
const categories = categoryDirectories.map(function(categoryDirectory) {
|
2016-12-30 14:38:43 +08:00
|
|
|
return {
|
2017-01-08 09:36:11 +08:00
|
|
|
name: categoryDirectory,
|
|
|
|
tests: fs.readdirSync(path.join(casesPath, categoryDirectory))
|
2016-12-30 14:38:43 +08:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2017-01-08 09:36:11 +08:00
|
|
|
categories.forEach(function(category) {
|
2017-01-03 05:02:49 +08:00
|
|
|
describe(category.name, function() {
|
|
|
|
|
2017-01-08 09:36:11 +08:00
|
|
|
category.tests.forEach(function(testName) {
|
2017-01-03 05:02:49 +08:00
|
|
|
const testDirectory = path.join(casesPath, category.name, testName);
|
2017-01-08 09:36:11 +08:00
|
|
|
const testArgs = defaultArgs.concat(getTestSpecificArguments(testDirectory));
|
|
|
|
const testAssertions = require(path.join(testDirectory, "test.js"));
|
|
|
|
const outputPath = path.join(path.resolve(casesPath, "../js/bin"), category.name, testName);
|
|
|
|
|
|
|
|
const cmd = `${path.resolve(process.cwd(), "bin/webpack.js")}`;
|
|
|
|
const args = testArgs.concat(["--output-path", `${outputPath}`]);
|
|
|
|
const opts = {
|
|
|
|
cwd: path.resolve("./", testDirectory)
|
|
|
|
};
|
|
|
|
|
2017-01-16 21:24:31 +08:00
|
|
|
const async = fs.existsSync(path.join(testDirectory, "async"));
|
|
|
|
|
2017-01-08 09:36:11 +08:00
|
|
|
const env = {
|
|
|
|
stdout: [],
|
|
|
|
stderr: [],
|
|
|
|
error: []
|
2017-01-03 05:02:49 +08:00
|
|
|
};
|
|
|
|
|
2017-01-16 21:35:27 +08:00
|
|
|
if(async) {
|
2017-01-16 21:24:31 +08:00
|
|
|
describe(testName, function() {
|
|
|
|
it("should run successfully", function(done) {
|
|
|
|
this.timeout(10000);
|
|
|
|
const child = spawn(process.execPath, [cmd].concat(args), opts);
|
|
|
|
|
|
|
|
child.on("close", function(code) {
|
|
|
|
env.code = code;
|
|
|
|
});
|
|
|
|
|
|
|
|
child.on("error", function(error) {
|
|
|
|
env.error.push(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
child.stdout.on("data", (data) => {
|
|
|
|
env.stdout.push(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
child.stderr.on("data", (data) => {
|
|
|
|
env.stderr.push(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2017-01-16 21:35:27 +08:00
|
|
|
if(env.code) {
|
2017-01-17 20:18:08 +08:00
|
|
|
done(`Watch didn't run ${env.error}`)
|
2017-01-16 21:24:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const stdout = convertToArrayOfLines(env.stdout);
|
|
|
|
const stderr = convertToArrayOfLines(env.stderr);
|
|
|
|
testAssertions(stdout, stderr, done);
|
|
|
|
child.kill()
|
|
|
|
}, 3000); // wait a little to get an output
|
2017-01-08 09:36:11 +08:00
|
|
|
});
|
2017-01-16 21:24:31 +08:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
describe(testName, function() {
|
|
|
|
before(function(done) {
|
|
|
|
this.timeout(20000);
|
|
|
|
|
|
|
|
const child = spawn(process.execPath, [cmd].concat(args), opts);
|
|
|
|
|
|
|
|
child.on("close", function(code) {
|
|
|
|
env.code = code;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
child.on("error", function(error) {
|
|
|
|
env.error.push(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
child.stdout.on("data", (data) => {
|
|
|
|
env.stdout.push(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
child.stderr.on("data", (data) => {
|
|
|
|
env.stderr.push(data);
|
|
|
|
});
|
2017-01-08 09:36:11 +08:00
|
|
|
});
|
|
|
|
|
2017-01-16 21:24:31 +08:00
|
|
|
it("should not cause any errors", function() {
|
|
|
|
should(env.error).be.empty();
|
2017-01-08 09:36:11 +08:00
|
|
|
});
|
|
|
|
|
2017-01-16 21:24:31 +08:00
|
|
|
it("should run successfully", function() {
|
|
|
|
const stdout = convertToArrayOfLines(env.stdout);
|
|
|
|
const stderr = convertToArrayOfLines(env.stderr);
|
|
|
|
testAssertions(env.code, stdout, stderr);
|
2017-01-08 09:36:11 +08:00
|
|
|
});
|
|
|
|
});
|
2017-01-16 21:24:31 +08:00
|
|
|
}
|
2016-12-30 14:38:43 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|