Skip to content

Commit bcc0227

Browse files
committed
add infrastructure for watch tests
1 parent ba0b4c1 commit bcc0227

18 files changed

Lines changed: 224 additions & 0 deletions

File tree

‎test/WatchTestCases.test.js‎

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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 Stats = require("../lib/Stats");
9+
var webpack = require("../lib/webpack");
10+
11+
function copyDiff(src, dest) {
12+
if(!fs.existsSync(dest))
13+
fs.mkdirSync(dest);
14+
var files = fs.readdirSync(src);
15+
files.forEach(function(filename) {
16+
var srcFile = path.join(src, filename);
17+
var destFile = path.join(dest, filename);
18+
var directory = fs.statSync(srcFile).isDirectory();
19+
if(directory) {
20+
copyDiff(srcFile, destFile);
21+
} else {
22+
fs.writeFileSync(destFile, fs.readFileSync(srcFile));
23+
}
24+
});
25+
}
26+
27+
function remove(src) {
28+
if(!fs.existsSync(src))
29+
return;
30+
var files = fs.readdirSync(src);
31+
files.forEach(function(filename) {
32+
var srcFile = path.join(src, filename);
33+
var directory = fs.statSync(srcFile).isDirectory();
34+
if(directory) {
35+
remove(srcFile);
36+
} else {
37+
fs.unlinkSync(srcFile);
38+
}
39+
});
40+
}
41+
42+
describe("WatchTestCases", function() {
43+
var casesPath = path.join(__dirname, "watchCases");
44+
var categories = fs.readdirSync(casesPath);
45+
46+
categories = categories.map(function(cat) {
47+
return {
48+
name: cat,
49+
tests: fs.readdirSync(path.join(casesPath, cat)).filter(function(folder) {
50+
return folder.indexOf("_") < 0;
51+
}).sort()
52+
};
53+
});
54+
before(function() {
55+
var dest = path.join(__dirname, "js");
56+
if(!fs.existsSync(dest))
57+
fs.mkdirSync(dest);
58+
dest = path.join(__dirname, "js", "watch-src");
59+
if(!fs.existsSync(dest))
60+
fs.mkdirSync(dest);
61+
});
62+
categories.forEach(function(category) {
63+
before(function() {
64+
var dest = path.join(__dirname, "js", "watch-src", category.name);
65+
if(!fs.existsSync(dest))
66+
fs.mkdirSync(dest);
67+
})
68+
describe(category.name, function() {
69+
category.tests.forEach(function(testName) {
70+
describe(testName, function() {
71+
var tempDirectory = path.join(__dirname, "js", "watch-src", category.name, testName);
72+
var testDirectory = path.join(casesPath, category.name, testName);
73+
var runs = fs.readdirSync(testDirectory).sort().map(function(name) {
74+
return {
75+
name: name,
76+
suite: describe(name, function() {})
77+
}
78+
});
79+
before(function() {
80+
remove(tempDirectory);
81+
});
82+
it("should compile", function(done) {
83+
this.timeout(30000);
84+
var outputDirectory = path.join(__dirname, "js", "watch", category.name, testName);
85+
86+
var options = {
87+
context: tempDirectory,
88+
entry: "./index.js",
89+
output: {
90+
path: outputDirectory,
91+
pathinfo: true,
92+
filename: "bundle.js"
93+
}
94+
};
95+
96+
var runIdx = 0;
97+
var run = runs[runIdx];
98+
copyDiff(path.join(testDirectory, run.name), tempDirectory);
99+
100+
var compiler = webpack(options);
101+
compiler.watch({}, function(err, stats) {
102+
if(run.done) return;
103+
run.done = true;
104+
if(err) return done(err);
105+
var statOptions = Stats.presetToOptions("verbose");
106+
statOptions.colors = false;
107+
fs.writeFileSync(path.join(outputDirectory, "stats.txt"), stats.toString(statOptions), "utf-8");
108+
var jsonStats = stats.toJson({
109+
errorDetails: true
110+
});
111+
if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return;
112+
if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return;
113+
var exportedTests = 0;
114+
115+
function _it(title, fn) {
116+
var test = new Test(title, fn);
117+
run.suite.addTest(test);
118+
exportedTests++;
119+
return test;
120+
}
121+
122+
function _require(currentDirectory, module) {
123+
if(Array.isArray(module) || /^\.\.?\//.test(module)) {
124+
var fn;
125+
var content;
126+
if(Array.isArray(module)) {
127+
var p = path.join(currentDirectory, module[0]);
128+
content = module.map(function(p) {
129+
var p = path.join(currentDirectory, p);
130+
return fs.readFileSync(p, "utf-8");
131+
}).join("\n");
132+
} else {
133+
var p = path.join(currentDirectory, module);
134+
content = fs.readFileSync(p, "utf-8");
135+
}
136+
fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, WATCH_STEP) {" + content + "\n})", p);
137+
var module = {
138+
exports: {}
139+
};
140+
fn.call(module.exports, _require.bind(null, path.dirname(p)), module, module.exports, path.dirname(p), p, _it, run.name);
141+
return module.exports;
142+
} else if(testConfig.modules && module in testConfig.modules) {
143+
return testConfig.modules[module];
144+
} else return require(module);
145+
}
146+
147+
var testConfig = {};
148+
try {
149+
// try to load a test file
150+
testConfig = require(path.join(testDirectory, "test.config.js"));
151+
} catch(e) {}
152+
153+
if(testConfig.noTests) return process.nextTick(done);
154+
_require(outputDirectory, "./bundle.js");
155+
156+
if(exportedTests < 1) return done(new Error("No tests exported by test case"));
157+
runIdx++;
158+
if(runIdx < runs.length) {
159+
run = runs[runIdx];
160+
copyDiff(path.join(testDirectory, run.name), tempDirectory);
161+
} else {
162+
process.nextTick(done);
163+
}
164+
});
165+
});
166+
});
167+
});
168+
});
169+
});
170+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "original";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import m from "./module";
2+
import cm from "./changing-module";
3+
4+
it("should flag harmony modules correctly", function() {
5+
m.should.be.eql("module" + WATCH_STEP);
6+
switch(WATCH_STEP) {
7+
case 0:
8+
cm.should.be.eql("original");
9+
break;
10+
case 1:
11+
cm.should.be.eql("change");
12+
break;
13+
}
14+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "module" + WATCH_STEP;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "change";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "cc0";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "ch0";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "hc0";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "hh0";
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import hh from "./hh";
2+
import hc from "./hc";
3+
import ch from "./ch";
4+
import cc from "./cc";
5+
6+
it("should flag modules correctly", function() {
7+
hh.should.be.eql("hh" + WATCH_STEP);
8+
cc.should.be.eql("cc" + WATCH_STEP);
9+
hc.should.be.eql("hc" + WATCH_STEP);
10+
ch.should.be.eql("ch" + WATCH_STEP);
11+
require("./hh").default.should.be.eql("hh" + WATCH_STEP);
12+
require("./cc").should.be.eql("cc" + WATCH_STEP);
13+
switch(WATCH_STEP) {
14+
case 0:
15+
require("./hc").default.should.be.eql("hc0");
16+
require("./ch").should.be.eql("ch0");
17+
break;
18+
case 1:
19+
require("./hc").should.be.eql("hc1");
20+
require("./ch").default.should.be.eql("ch1");
21+
break;
22+
}
23+
});

0 commit comments

Comments
 (0)