This repository has been archived by the owner on May 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 517
/
TestCases.test.js
59 lines (56 loc) · 2.05 KB
/
TestCases.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
var fs = require("fs");
var vm = require("vm");
var path = require("path");
var webpack = require("webpack");
var should = require("should");
var rimraf = require('rimraf');
var ExtractTextPlugin = require("../");
var cases = process.env.CASES ? process.env.CASES.split(",") : fs.readdirSync(path.join(__dirname, "cases"));
describe("TestCases", function() {
cases.forEach(function(testCase) {
it(testCase, function(done) {
var testDirectory = path.join(__dirname, "cases", testCase);
var outputDirectory = path.join(__dirname, "js", testCase);
function test() {
var options = { entry: { test: "./index.js" } };
var configFile = path.join(testDirectory, "webpack.config.js");
if (fs.existsSync(configFile))
options = require(configFile);
options.context = testDirectory;
if (!options.module) options.module = {};
if (!options.module.loaders) options.module.loaders = [
{ test: /\.txt$/, loader: ExtractTextPlugin.extract("raw-loader") }
];
if (!options.output) options.output = { filename: "[name].js" };
if (!options.output.path) options.output.path = outputDirectory;
if (process.env.CASES) {
console.log("\nwebpack." + testCase + ".config.js " + JSON.stringify(options, null, 2));
}
webpack(options, function (err, stats) {
if (err) return done(err);
if (stats.hasErrors()) return done(new Error(stats.toString()));
var testFile = path.join(outputDirectory, "test.js");
if (fs.existsSync(testFile))
require(testFile)(suite);
var expectedDirectory = path.join(testDirectory, "expected");
fs.readdirSync(expectedDirectory).forEach(function (file) {
var filePath = path.join(expectedDirectory, file);
var actualPath = path.join(outputDirectory, file);
readFileOrEmpty(actualPath).should.be.eql(
readFileOrEmpty(filePath),
file + " should be correct");
});
done();
});
}
rimraf(outputDirectory, test);
});
});
});
function readFileOrEmpty(path) {
try {
return fs.readFileSync(path, "utf-8");
} catch(e) {
return "";
}
}