Skip to content

Commit 48f3d05

Browse files
author
Chuck Dumont
authored
Merge pull request #59 from chuckdumont/work2
Add error test cases
2 parents 55ba0e9 + 629df6f commit 48f3d05

File tree

19 files changed

+151
-10
lines changed

19 files changed

+151
-10
lines changed

lib/DojoAMDPlugin.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,18 @@ Refer to https://github.com/OpenNTF/dojo-webpack-plugin/blob/master/README.md#bu
186186
getDojoLoader(options__, loaderConfig, callback) {
187187
var dojoLoader;
188188
if (this.options.loader) {
189-
dojoLoader = require(this.options.loader);
189+
try {
190+
dojoLoader = require(this.options.loader);
191+
} catch (error) {
192+
callback(error);
193+
}
190194
callback(null, dojoLoader);
191195
} else {
192196
console.log("Dojo loader not specified in options. Building the loader...");
193197
const execFile = require("child_process").execFile;
194198
const tmp = require("tmp");
195199
let dojoPath;
196-
if (!loaderConfig.packages.some((pkg) => {
200+
if (!loaderConfig.packages || !loaderConfig.packages.some((pkg) => {
197201
if (pkg.name === "dojo") return (dojoPath = pkg.location);
198202
})) {
199203
return callback(new Error("dojo package not defined in loader config"));

package-lock.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"eslint": "^4.7.2",
3434
"eslint-plugin-json": "^1.2.0",
3535
"eslint-plugin-markdown": "^1.0.0-beta.6",
36+
"fs-extra": "^4.0.2",
3637
"istanbul": "^0.4.5",
3738
"mocha": "^3.5.3",
3839
"mocha-lcov-reporter": "^1.3.0",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
test: function(error) {
3+
return /Dojo loader version does not match the version of Dojo/.test(error.message);
4+
}
5+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
define([], function() {});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var DojoWebpackPlugin = require("../../../../index");
2+
var path = require("path");
3+
var fs = require("fs-extra");
4+
var loader = fs.readFileSync(path.join(__dirname, "../../../js/dojo/dojo.js"), "utf-8"); // eslint-disable-line
5+
var loaderPath = path.join(__dirname.replace(/[\/\\]test[\/\\]ErrorTestCases[\/\\]/, "/test/js/ErrorTestCases/"), "dojoLoader.js");
6+
loader = loader.replace(/(this\.loaderVersion)\s*=\s*"[^"]+"/, '$1 = "1.0.0"');
7+
fs.ensureDirSync(path.join(loaderPath, ".."));
8+
fs.writeFileSync(loaderPath, loader, "utf-8");
9+
module.exports = {
10+
entry: "test/index",
11+
plugins: [
12+
new DojoWebpackPlugin({
13+
loaderConfig: {
14+
paths:{test: "."}
15+
},
16+
loader: loaderPath
17+
})
18+
]
19+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
test: function(error) {
3+
return /Command failed:/.test(error.message);
4+
}
5+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
define([], function() {});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var DojoWebpackPlugin = require("../../../../index");
2+
module.exports = {
3+
entry: "test/index",
4+
plugins: [
5+
new DojoWebpackPlugin({
6+
loaderConfig: {
7+
paths:{test: "."},
8+
packages:[{name: "dojo", location: "./dojo"}]
9+
}
10+
})
11+
]
12+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
test: function(error) {
3+
return /dojo package not defined in loader config/.test(error.message);
4+
}
5+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
define([], function() {});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var DojoWebpackPlugin = require("../../../../index");
2+
module.exports = {
3+
entry: "test/index",
4+
plugins: [
5+
new DojoWebpackPlugin({
6+
loaderConfig: {
7+
paths:{test: "."}
8+
}
9+
})
10+
]
11+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
test: function(error) {
3+
return /Cannot find module \'.*[\/\\]dojo.js'$/.test(error);
4+
}
5+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
define([], function() {});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var path = require("path");
2+
var DojoWebpackPlugin = require("../../../../index");
3+
module.exports = {
4+
entry: "test/index",
5+
plugins: [
6+
new DojoWebpackPlugin({
7+
loaderConfig: {
8+
paths:{test: "."}
9+
},
10+
loader: path.join(__dirname, "dojo.js")
11+
})
12+
]
13+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = function() {
2+
return {
3+
paths:{test: "."}
4+
};
5+
};

test/TestCases/dependencies/constNamedModules/webpack.config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ module.exports = {
44
entry: "test/index",
55
plugins: [
66
new DojoWebpackPlugin({
7-
loaderConfig: {
8-
paths:{test: "."}
9-
},
7+
loaderConfig: require.resolve("./loaderConfig"),
108
loader: path.join(__dirname, "../../../js/dojo/dojo.js")
119
})
1210
],
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
define(["dojo/query!css2"], function(engine) {
2-
it("should load text files" , function() {
2+
it("should load the specified selector engine" , function() {
33
"css2".should.be.eql(engine);
44
});
55
});

test/test.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ var NodeRequireEnsurePatchPlugin = require("./plugins/NodeRequireEnsurePatchPlug
3131

3232

3333
describe("TestCases", () => {
34-
var casesPath = path.join(__dirname, "TestCases");
34+
runTestCases(path.join(__dirname, "TestCases"));
35+
});
36+
37+
describe("ErrorTestCases", () => {
38+
runTestCases(path.join(__dirname, "ErrorTestCases"), true);
39+
});
40+
41+
function runTestCases(casesPath, isErrorTest) {
3542
var categories = fs.readdirSync(casesPath);
3643
categories = categories.map(function(cat) {
3744
return {
@@ -45,7 +52,7 @@ describe("TestCases", () => {
4552
describe(category.name, function() {
4653
category.tests.forEach(function(testName) {
4754
var suite = describe(testName, function() {});
48-
it(testName + " should compile", function(done) {
55+
it(testName + isErrorTest ? " should fail" : " should compile", function(done) {
4956
this.timeout(60000);
5057
var testDirectory = path.join(casesPath, category.name, testName);
5158
var outputDirectory = path.join(__dirname, "js", "TestCases", category.name, testName);
@@ -64,7 +71,9 @@ describe("TestCases", () => {
6471
options.plugins.push(new NodeRequireEnsurePatchPlugin());
6572
});
6673
webpack(options, function(err, stats) {
67-
if(err) return done(err);
74+
if (checkExpectedError(isErrorTest, testDirectory, err, done)) {
75+
return;
76+
}
6877
var statOptions = Stats.presetToOptions("verbose");
6978
statOptions.colors = false;
7079
fs.writeFileSync(path.join(outputDirectory, "stats.txt"), stats.toString(statOptions), "utf-8");
@@ -129,7 +138,26 @@ describe("TestCases", () => {
129138
});
130139
});
131140
});
132-
});
141+
}
142+
143+
function checkExpectedError(isErrorTest, testDirectory, error, done) {
144+
if (isErrorTest) {
145+
if (error) {
146+
const expectedError = require(path.join(testDirectory, "expectedError"));
147+
if (expectedError.test(error)) {
148+
done();
149+
} else {
150+
done(new Error("Unexpected error message: " + error));
151+
}
152+
} else {
153+
done(new Error("Expected error but test passed."));
154+
}
155+
return true;
156+
} else if (error) {
157+
done(error);
158+
return true;
159+
}
160+
}
133161

134162
function checkArrayExpectation(testDirectory, object, kind, filename, upperCaseKind, done) {
135163
if(!done) {

0 commit comments

Comments
 (0)