Skip to content

Commit 472558c

Browse files
committed
added config test infrastructure
1 parent 3efb715 commit 472558c

5 files changed

Lines changed: 99 additions & 22 deletions

File tree

test/ConfigTestCases.test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
var should = require("should");
2+
var path = require("path");
3+
var fs = require("fs");
4+
var vm = require("vm");
5+
var Test = require("mocha/lib/test");
6+
var checkArrayExpectation = require("./checkArrayExpectation");
7+
8+
var webpack = require("../lib/webpack");
9+
10+
describe("ConfigTestCases", function() {
11+
var casesPath = path.join(__dirname, "configCases");
12+
var categories = fs.readdirSync(casesPath);
13+
categories = categories.map(function(cat) {
14+
return {
15+
name: cat,
16+
tests: fs.readdirSync(path.join(casesPath, cat)).filter(function(folder) {
17+
return folder.indexOf("_") < 0;
18+
})
19+
};
20+
});
21+
categories.forEach(function(category) {
22+
describe(category.name, function() {
23+
category.tests.forEach(function(testName) {
24+
var suite = describe(testName, function() {});
25+
it(testName + " should compile", function(done) {
26+
this.timeout(10000);
27+
var testDirectory = path.join(casesPath, category.name, testName);
28+
var outputDirectory = path.join(__dirname, "js", "config", category.name, testName);
29+
var options = require(path.join(testDirectory, "webpack.config.js"));
30+
if(!options.context) options.context = testDirectory;
31+
if(!options.entry) options.entry = "./index.js";
32+
if(!options.target) options.target = "async-node";
33+
if(!options.output) options.output = {};
34+
if(!options.output.path) options.output.path = outputDirectory;
35+
if(!options.output.filename) options.output.filename = "bundle.js";
36+
webpack(options, function(err, stats) {
37+
if(err) return done(err);
38+
var jsonStats = stats.toJson({
39+
errorDetails: true
40+
});
41+
if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return;
42+
if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return;
43+
var exportedTest = 0;
44+
function _it(title, fn) {
45+
var test = new Test(title, fn);
46+
suite.addTest(test);
47+
exportedTest++;
48+
return test;
49+
}
50+
function _require(module) {
51+
if(module.substr(0, 2) === "./") {
52+
var p = path.join(outputDirectory, module);
53+
var fn = vm.runInThisContext("(function(require, module, exports, __dirname, it) {" + fs.readFileSync(p, "utf-8") + "\n})", p);
54+
var module = { exports: {} };
55+
fn.call(module.exports, _require, module, module.exports, outputDirectory, _it);
56+
return module.exports;
57+
} else return require(module);
58+
}
59+
_require("./bundle.js");
60+
if(exportedTest === 0) return done(new Error("No tests exported by test case"));
61+
done();
62+
});
63+
});
64+
});
65+
});
66+
});
67+
});

test/TestCases.test.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var path = require("path");
33
var fs = require("fs");
44
var vm = require("vm");
55
var Test = require("mocha/lib/test");
6+
var checkArrayExpectation = require("./checkArrayExpectation");
67

78
var webpack = require("../lib/webpack");
89

@@ -121,26 +122,4 @@ describe("TestCases", function() {
121122
});
122123
});
123124
});
124-
function checkArrayExpectation(testDirectory, object, kind, upperCaseKind, done) {
125-
var array = object[kind+"s"].slice().sort();
126-
if(kind === "warning") array = array.filter(function(item) { return !/from UglifyJs/.test(item); });
127-
if(fs.existsSync(path.join(testDirectory, kind+ "s.js"))) {
128-
var expected = require(path.join(testDirectory, kind + "s.js"));
129-
if(expected.length < array.length)
130-
return done(new Error("More " + kind + "s while compiling than expected:\n\n" + array.join("\n\n"))), true;
131-
else if(expected.length > array.length)
132-
return done(new Error("Less " + kind + "s while compiling than expected:\n\n" + array.join("\n\n"))), true;
133-
for(var i = 0; i < array.length; i++) {
134-
if(Array.isArray(expected[i])) {
135-
for(var j = 0; j < expected[i].length; j++) {
136-
if(!expected[i][j].test(array[i]))
137-
return done(new Error(upperCaseKind + " " + i + ": " + array[i] + " doesn't match " + expected[i][j].toString())), true;
138-
}
139-
} else if(!expected[i].test(array[i]))
140-
return done(new Error(upperCaseKind + " " + i + ": " + array[i] + " doesn't match " + expected[i].toString())), true;
141-
}
142-
} else if(array.length > 0) {
143-
return done(new Error(upperCaseKind + "s while compiling:\n\n" + array.join("\n\n"))), true;
144-
}
145-
}
146125
});

test/checkArrayExpectation.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var fs = require("fs");
2+
var path = require("path");
3+
4+
module.exports = function checkArrayExpectation(testDirectory, object, kind, upperCaseKind, done) {
5+
var array = object[kind+"s"].slice().sort();
6+
if(kind === "warning") array = array.filter(function(item) { return !/from UglifyJs/.test(item); });
7+
if(fs.existsSync(path.join(testDirectory, kind+ "s.js"))) {
8+
var expected = require(path.join(testDirectory, kind + "s.js"));
9+
if(expected.length < array.length)
10+
return done(new Error("More " + kind + "s while compiling than expected:\n\n" + array.join("\n\n"))), true;
11+
else if(expected.length > array.length)
12+
return done(new Error("Less " + kind + "s while compiling than expected:\n\n" + array.join("\n\n"))), true;
13+
for(var i = 0; i < array.length; i++) {
14+
if(Array.isArray(expected[i])) {
15+
for(var j = 0; j < expected[i].length; j++) {
16+
if(!expected[i][j].test(array[i]))
17+
return done(new Error(upperCaseKind + " " + i + ": " + array[i] + " doesn't match " + expected[i][j].toString())), true;
18+
}
19+
} else if(!expected[i].test(array[i]))
20+
return done(new Error(upperCaseKind + " " + i + ": " + array[i] + " doesn't match " + expected[i].toString())), true;
21+
}
22+
} else if(array.length > 0) {
23+
return done(new Error(upperCaseKind + "s while compiling:\n\n" + array.join("\n\n"))), true;
24+
}
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should compile and run the test", function() {
2+
3+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
3+
};

0 commit comments

Comments
 (0)