2015-04-27 03:19:39 +08:00
|
|
|
/*globals describe it */
|
2015-08-03 17:01:29 +08:00
|
|
|
var should = require("should");
|
2015-04-27 03:19:39 +08:00
|
|
|
var path = require("path");
|
|
|
|
|
var fs = require("fs");
|
2016-01-07 06:21:33 +08:00
|
|
|
var mkdirp = require("mkdirp");
|
2015-04-27 03:19:39 +08:00
|
|
|
|
|
|
|
|
var webpack = require("../lib/webpack");
|
|
|
|
|
|
2015-04-29 05:07:24 +08:00
|
|
|
var base = path.join(__dirname, "statsCases");
|
2016-01-07 06:21:33 +08:00
|
|
|
var outputBase = path.join(__dirname, "js", "stats");
|
2016-06-04 20:47:20 +08:00
|
|
|
var tests = fs.readdirSync(base).filter(function(testName) {
|
|
|
|
|
return fs.existsSync(path.join(base, testName, "index.js"))
|
|
|
|
|
});
|
2015-06-06 02:17:08 +08:00
|
|
|
var Stats = require("../lib/Stats");
|
2015-04-27 03:19:39 +08:00
|
|
|
|
|
|
|
|
describe("Stats", function() {
|
|
|
|
|
tests.forEach(function(testName) {
|
|
|
|
|
it("should print correct stats for " + testName, function(done) {
|
|
|
|
|
var options = {
|
2015-05-16 22:10:25 +08:00
|
|
|
entry: "./index",
|
|
|
|
|
output: {
|
|
|
|
|
filename: "bundle.js"
|
|
|
|
|
}
|
2015-04-27 03:19:39 +08:00
|
|
|
};
|
|
|
|
|
if(fs.existsSync(path.join(base, testName, "webpack.config.js"))) {
|
|
|
|
|
options = require(path.join(base, testName, "webpack.config.js"));
|
|
|
|
|
}
|
2015-09-03 20:17:11 +08:00
|
|
|
(Array.isArray(options) ? options : [options]).forEach(function(options) {
|
|
|
|
|
options.context = path.join(base, testName);
|
2016-01-07 06:21:33 +08:00
|
|
|
options.output = options.output || {};
|
|
|
|
|
options.output.path = path.join(outputBase, testName);
|
2015-09-03 20:17:11 +08:00
|
|
|
});
|
2015-04-27 03:19:39 +08:00
|
|
|
var c = webpack(options);
|
2015-09-03 20:17:11 +08:00
|
|
|
var compilers = c.compilers ? c.compilers : [c];
|
|
|
|
|
compilers.forEach(function(c) {
|
|
|
|
|
var ifs = c.inputFileSystem;
|
|
|
|
|
c.inputFileSystem = Object.create(ifs);
|
|
|
|
|
c.inputFileSystem.readFile = function() {
|
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
|
var callback = args.pop();
|
|
|
|
|
ifs.readFile.apply(ifs, args.concat([function(err, result) {
|
|
|
|
|
if(err) return callback(err);
|
|
|
|
|
callback(null, result.toString("utf-8").replace(/\r/g, ""));
|
|
|
|
|
}]));
|
|
|
|
|
};
|
|
|
|
|
c.apply(new webpack.optimize.OccurrenceOrderPlugin());
|
|
|
|
|
});
|
2015-04-27 03:19:39 +08:00
|
|
|
c.run(function(err, stats) {
|
|
|
|
|
if(err) return done(err);
|
2015-08-09 18:42:43 +08:00
|
|
|
|
2015-08-03 17:01:29 +08:00
|
|
|
if(/error$/.test(testName)) {
|
2015-09-03 20:17:11 +08:00
|
|
|
stats.hasErrors().should.be.equal(true);
|
|
|
|
|
} else if(stats.hasErrors()) {
|
|
|
|
|
done(new Error(stats.toJson().errors.join("\n\n")));
|
2015-08-03 17:01:29 +08:00
|
|
|
}
|
2015-08-09 18:42:43 +08:00
|
|
|
|
|
|
|
|
var toStringOptions = {
|
|
|
|
|
colors: false
|
|
|
|
|
};
|
2015-11-14 21:08:04 +08:00
|
|
|
var hasColorSetting = false;
|
|
|
|
|
|
2015-08-03 17:01:29 +08:00
|
|
|
if(typeof options.stats !== "undefined") {
|
|
|
|
|
toStringOptions = options.stats;
|
2015-11-14 21:08:04 +08:00
|
|
|
|
|
|
|
|
hasColorSetting = typeof options.stats.colors !== "undefined";
|
2015-08-03 17:01:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var actual = stats.toString(toStringOptions);
|
2015-04-27 03:19:39 +08:00
|
|
|
(typeof actual).should.be.eql("string");
|
2015-11-14 21:08:04 +08:00
|
|
|
if(!hasColorSetting) {
|
|
|
|
|
actual = actual
|
|
|
|
|
.replace(/\u001b\[[0-9;]*m/g, "")
|
|
|
|
|
.replace(/[0-9]+(\s?ms)/g, "X$1");
|
|
|
|
|
} else {
|
|
|
|
|
actual = actual
|
|
|
|
|
.replace(/\u001b\[1m\u001b\[([0-9;]*)m/g, "<CLR=$1,BOLD>")
|
|
|
|
|
.replace(/\u001b\[1m/g, "<CLR=BOLD>")
|
|
|
|
|
.replace(/\u001b\[39m\u001b\[22m/g, "</CLR>")
|
|
|
|
|
.replace(/\u001b\[([0-9;]*)m/g, "<CLR=$1>")
|
|
|
|
|
.replace(/[0-9]+(<\/CLR>)?(\s?ms)/g, "X$1$2");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
actual = actual
|
2015-09-03 20:42:00 +08:00
|
|
|
.replace(/\r\n?/g, "\n")
|
|
|
|
|
.replace(/[\t ]*Version:.+\n/g, "")
|
2015-08-09 18:42:43 +08:00
|
|
|
.replace(path.join(base, testName), "Xdir/" + testName);
|
2015-04-27 03:43:51 +08:00
|
|
|
var expected = fs.readFileSync(path.join(base, testName, "expected.txt"), "utf-8").replace(/\r/g, "");
|
2015-04-27 03:19:39 +08:00
|
|
|
if(actual !== expected) {
|
|
|
|
|
fs.writeFileSync(path.join(base, testName, "actual.txt"), actual, "utf-8");
|
|
|
|
|
} else if(fs.existsSync(path.join(base, testName, "actual.txt"))) {
|
|
|
|
|
fs.unlinkSync(path.join(base, testName, "actual.txt"));
|
|
|
|
|
}
|
|
|
|
|
actual.should.be.eql(expected);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-08-09 18:42:43 +08:00
|
|
|
describe("Error Handling", function() {
|
|
|
|
|
describe("does have", function() {
|
2015-06-06 02:17:08 +08:00
|
|
|
it("hasErrors", function() {
|
2015-08-09 18:42:43 +08:00
|
|
|
var mockStats = new Stats({
|
|
|
|
|
errors: ['firstError'],
|
|
|
|
|
hash: '1234'
|
|
|
|
|
});
|
2015-06-06 02:17:08 +08:00
|
|
|
mockStats.hasErrors().should.be.ok;
|
|
|
|
|
});
|
|
|
|
|
it("hasWarnings", function() {
|
2015-08-09 18:42:43 +08:00
|
|
|
var mockStats = new Stats({
|
|
|
|
|
warnings: ['firstError'],
|
|
|
|
|
hash: '1234'
|
|
|
|
|
});
|
2015-06-06 02:17:08 +08:00
|
|
|
mockStats.hasWarnings().should.be.ok;
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-08-09 18:42:43 +08:00
|
|
|
describe("does not have", function() {
|
2015-06-06 02:17:08 +08:00
|
|
|
it("hasErrors", function() {
|
2015-08-09 18:42:43 +08:00
|
|
|
var mockStats = new Stats({
|
|
|
|
|
errors: [],
|
|
|
|
|
hash: '1234'
|
|
|
|
|
});
|
2015-06-06 02:17:08 +08:00
|
|
|
mockStats.hasErrors().should.not.be.ok;
|
|
|
|
|
});
|
|
|
|
|
it("hasWarnings", function() {
|
2015-08-09 18:42:43 +08:00
|
|
|
var mockStats = new Stats({
|
|
|
|
|
warnings: [],
|
|
|
|
|
hash: '1234'
|
|
|
|
|
});
|
2015-06-06 02:17:08 +08:00
|
|
|
mockStats.hasWarnings().should.not.be.ok;
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-08-09 18:42:43 +08:00
|
|
|
it("formatError handles string errors", function() {
|
2015-07-17 15:32:21 +08:00
|
|
|
var mockStats = new Stats({
|
2015-08-09 18:42:43 +08:00
|
|
|
errors: ['firstError'],
|
|
|
|
|
warnings: [],
|
|
|
|
|
assets: [],
|
2016-07-13 17:03:14 +08:00
|
|
|
entrypoints: {},
|
2015-08-09 18:42:43 +08:00
|
|
|
chunks: [],
|
|
|
|
|
modules: [],
|
|
|
|
|
children: [],
|
|
|
|
|
hash: '1234',
|
|
|
|
|
mainTemplate: {
|
|
|
|
|
getPublicPath: function() {
|
|
|
|
|
return 'path';
|
|
|
|
|
}
|
2015-06-06 02:17:08 +08:00
|
|
|
}
|
|
|
|
|
});
|
2015-07-17 15:32:21 +08:00
|
|
|
var obj = mockStats.toJson();
|
2015-06-06 02:17:08 +08:00
|
|
|
obj.errors[0].should.be.equal('firstError');
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-08-09 18:42:43 +08:00
|
|
|
describe("Presets", function() {
|
2015-08-03 17:01:29 +08:00
|
|
|
describe("presetToOptions", function() {
|
|
|
|
|
it("returns correct object with 'Normal'", function() {
|
|
|
|
|
Stats.presetToOptions("Normal").should.eql({
|
|
|
|
|
assets: false,
|
|
|
|
|
version: false,
|
|
|
|
|
timings: true,
|
|
|
|
|
hash: true,
|
2016-07-13 17:03:14 +08:00
|
|
|
entrypoints: false,
|
2015-08-03 17:01:29 +08:00
|
|
|
chunks: true,
|
|
|
|
|
chunkModules: false,
|
|
|
|
|
errorDetails: true,
|
|
|
|
|
reasons: false,
|
2016-06-29 07:17:15 +08:00
|
|
|
usedExports: false,
|
2016-09-07 15:48:32 +08:00
|
|
|
providedExports: false,
|
2015-08-03 17:01:29 +08:00
|
|
|
colors: true
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
it("truthy values behave as 'normal'", function() {
|
|
|
|
|
var normalOpts = Stats.presetToOptions('normal');
|
|
|
|
|
Stats.presetToOptions("pizza").should.eql(normalOpts);
|
|
|
|
|
Stats.presetToOptions(true).should.eql(normalOpts);
|
|
|
|
|
Stats.presetToOptions(1).should.eql(normalOpts);
|
2015-08-09 18:42:43 +08:00
|
|
|
|
2015-08-03 17:01:29 +08:00
|
|
|
Stats.presetToOptions("verbose").should.not.eql(normalOpts);
|
|
|
|
|
Stats.presetToOptions(false).should.not.eql(normalOpts);
|
|
|
|
|
});
|
|
|
|
|
it("returns correct object with 'none'", function() {
|
|
|
|
|
Stats.presetToOptions("none").should.eql({
|
|
|
|
|
hash: false,
|
|
|
|
|
version: false,
|
|
|
|
|
timings: false,
|
|
|
|
|
assets: false,
|
2016-07-13 17:03:14 +08:00
|
|
|
entrypoints: false,
|
2015-08-03 17:01:29 +08:00
|
|
|
chunks: false,
|
|
|
|
|
modules: false,
|
|
|
|
|
reasons: false,
|
2016-06-29 07:17:15 +08:00
|
|
|
usedExports: false,
|
2016-09-07 15:48:32 +08:00
|
|
|
providedExports: false,
|
2015-08-03 17:01:29 +08:00
|
|
|
children: false,
|
|
|
|
|
source: false,
|
|
|
|
|
errors: false,
|
|
|
|
|
errorDetails: false,
|
|
|
|
|
warnings: false,
|
|
|
|
|
publicPath: false
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
it("falsy values behave as 'none'", function() {
|
|
|
|
|
var noneOpts = Stats.presetToOptions('none');
|
|
|
|
|
Stats.presetToOptions("").should.eql(noneOpts);
|
|
|
|
|
Stats.presetToOptions(null).should.eql(noneOpts);
|
|
|
|
|
Stats.presetToOptions().should.eql(noneOpts);
|
|
|
|
|
Stats.presetToOptions(0).should.eql(noneOpts);
|
|
|
|
|
Stats.presetToOptions(false).should.eql(noneOpts);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-04-27 03:19:39 +08:00
|
|
|
});
|