forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinTestCases.test.js
145 lines (119 loc) · 3.86 KB
/
BinTestCases.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* globals describe it before */
"use strict";
const should = require("should");
const path = require("path");
const fs = require("fs");
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 null;
}
}
function convertToArrayOfLines(outputArray) {
if(outputArray.length === 0) return outputArray;
return outputArray.join("").split("\n");
}
const casesPath = path.join(__dirname, "binCases");
const defaultArgs = loadOptsFile(path.join(casesPath, "test.opts"));
describe("BinTestCases", function() {
const categoryDirectories = fs.readdirSync(casesPath).filter((folder) => {
return fs.statSync(path.join(casesPath, folder)).isDirectory();
});
const categories = categoryDirectories.map(function(categoryDirectory) {
return {
name: categoryDirectory,
tests: fs.readdirSync(path.join(casesPath, categoryDirectory))
};
});
categories.forEach(function(category) {
describe(category.name, function() {
category.tests.forEach(function(testName) {
const testDirectory = path.join(casesPath, category.name, testName);
const testArgs = getTestSpecificArguments(testDirectory) || defaultArgs;
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)
};
const asyncExists = fs.existsSync(path.join(testDirectory, "async"));
const env = {
stdout: [],
stderr: [],
error: []
};
if(asyncExists) {
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(() => {
if(env.code) {
done(`Watch didn't run ${env.error}`);
}
const stdout = convertToArrayOfLines(env.stdout);
const stderr = convertToArrayOfLines(env.stderr);
testAssertions(stdout, stderr, done);
child.kill();
}, 3000); // wait a little to get an output
});
});
} 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);
});
});
it("should not cause any errors", function() {
should(env.error).be.empty();
});
it("should run successfully", function() {
const stdout = convertToArrayOfLines(env.stdout);
const stderr = convertToArrayOfLines(env.stderr);
testAssertions(env.code, stdout, stderr);
});
});
}
});
});
});
});