Skip to content

Commit 42b95d9

Browse files
authored
Merge pull request webpack#3844 from alistairjcbrown/feature/bin_cases_infra
feat(test): add very simple binCases test infra to build off of [Fork]
2 parents 0607fc1 + 4dcbf96 commit 42b95d9

7 files changed

Lines changed: 158 additions & 0 deletions

File tree

test/BinTestCases.test.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"use strict";
2+
3+
/* globals describe it */
4+
const should = require("should");
5+
const path = require("path");
6+
const fs = require("fs");
7+
const spawn = require("child_process").spawn;
8+
9+
function loadOptsFile(optsPath) {
10+
// Options file parser from Mocha
11+
// https://github.com/mochajs/mocha/blob/2bb2b9fa35818db7a02e5068364b0c417436b1af/bin/options.js#L25-L31
12+
return fs.readFileSync(optsPath, 'utf8')
13+
.replace(/\\\s/g, '%20')
14+
.split(/\s/)
15+
.filter(Boolean)
16+
.map(function(value) {
17+
return value.replace(/%20/g, ' ');
18+
});
19+
}
20+
21+
function getTestSpecificArguments(testDirectory) {
22+
try {
23+
return loadOptsFile(path.join(testDirectory, "test.opts"));
24+
} catch(e) {
25+
return [];
26+
}
27+
}
28+
29+
function convertToArrayOfLines(outputArray) {
30+
if(outputArray.length === 0) return outputArray;
31+
return outputArray.join('').split('\n');
32+
}
33+
34+
const casesPath = path.join(__dirname, "binCases");
35+
const defaultArgs = loadOptsFile(path.join(casesPath, "test.opts"));
36+
37+
describe("BinTestCases", function() {
38+
const categoryDirectories = fs.readdirSync(casesPath).filter((folder) => {
39+
return fs.statSync(path.join(casesPath, folder)).isDirectory()
40+
});
41+
42+
const categories = categoryDirectories.map(function(categoryDirectory) {
43+
return {
44+
name: categoryDirectory,
45+
tests: fs.readdirSync(path.join(casesPath, categoryDirectory))
46+
};
47+
});
48+
49+
categories.forEach(function(category) {
50+
describe(category.name, function() {
51+
52+
category.tests.forEach(function(testName) {
53+
const testDirectory = path.join(casesPath, category.name, testName);
54+
const testArgs = defaultArgs.concat(getTestSpecificArguments(testDirectory));
55+
const testAssertions = require(path.join(testDirectory, "test.js"));
56+
const outputPath = path.join(path.resolve(casesPath, "../js/bin"), category.name, testName);
57+
58+
const cmd = `${path.resolve(process.cwd(), "bin/webpack.js")}`;
59+
const args = testArgs.concat(["--output-path", `${outputPath}`]);
60+
const opts = {
61+
cwd: path.resolve("./", testDirectory)
62+
};
63+
64+
const env = {
65+
stdout: [],
66+
stderr: [],
67+
error: []
68+
};
69+
70+
describe(testName, function() {
71+
before(function(done) {
72+
this.timeout(20000);
73+
74+
const child = spawn(process.execPath, [cmd].concat(args), opts);
75+
76+
child.on("close", function(code) {
77+
env.code = code;
78+
done();
79+
});
80+
81+
child.on("error", function(error) {
82+
env.error.push(error);
83+
});
84+
85+
child.stdout.on("data", (data) => {
86+
env.stdout.push(data);
87+
});
88+
89+
child.stderr.on("data", (data) => {
90+
env.stderr.push(data);
91+
});
92+
});
93+
94+
it("should not cause any errors", function() {
95+
should(env.error).be.empty();
96+
});
97+
98+
it("should run successfully", function() {
99+
const stdout = convertToArrayOfLines(env.stdout);
100+
const stderr = convertToArrayOfLines(env.stderr);
101+
testAssertions(env.code, stdout, stderr);
102+
});
103+
});
104+
});
105+
});
106+
});
107+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
3+
module.exports = function testAssertions(code, stdout, stderr) {
4+
code.should.be.exactly(0);
5+
6+
stdout.should.be.ok();
7+
stdout[0].should.startWith("webpack");
8+
stdout.should.containEql("Config options:");
9+
stdout.should.containEql("Basic options:");
10+
stdout.should.containEql("Module options:");
11+
stdout.should.containEql("Output options:");
12+
stdout.should.containEql("Advanced options:");
13+
stdout.should.containEql("Resolving options:");
14+
stdout.should.containEql("Optimizing options:");
15+
stdout.should.containEql("Stats options:");
16+
stdout.should.containEql("Options:");
17+
18+
stderr.should.be.empty();
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--help
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "foo";
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
3+
module.exports = function testAssertions(code, stdout, stderr) {
4+
code.should.be.oneOf(0, 1);
5+
6+
stdout.should.be.ok();
7+
stdout[3].should.containEql("Hash: ");
8+
stdout[4].should.containEql("Version: ");
9+
stdout[5].should.containEql("Time: ");
10+
stdout[7].should.containEql("null.js");
11+
stdout[8].should.containEql("./index.js");
12+
stdout[8].should.containEql("[built]");
13+
14+
stderr.should.be.empty();
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var path = require("path");
2+
3+
module.exports = {
4+
entry: path.resolve(__dirname, "./index"),
5+
stats: {
6+
assets: true,
7+
colors: true,
8+
chunks: true
9+
}
10+
}

test/binCases/test.opts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--entry ./index.js
2+
--config ./webpack.config.js
3+
--output-filename [name].js
4+
--output-chunk-filename [id].chunk.js
5+
--target async-node

0 commit comments

Comments
 (0)