Skip to content

Commit

Permalink
Add possibility to log test erros if test fails for moudle
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Jahn committed Jan 9, 2016
1 parent 90de8fa commit 8f40749
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
6 changes: 4 additions & 2 deletions bin/updtr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ program
.option("-R, --reporter <reporter>", "choose reporter: " + reporters.join(", "), "default")
.option("-w, --wanted", "updates to wanted version specified in package.json instead of the modules latest version")
.option("-t, --test <test>", "change the command for the tests")
.option("-e, --exclude <exclude>", "exclude modules comma seperated, e.g. updtr --exclude module1,module2", "excludes");
.option("-e, --exclude <exclude>", "exclude modules comma seperated, e.g. updtr --exclude module1,module2", "excludes")
.option("--test-errors", "shows stdout if your test command fails");

program.parse(process.argv);

Expand All @@ -26,7 +27,8 @@ updtr.run({
reporter: updtr.reporters[program.reporter],
wanted: program.wanted,
testCmd: program.test,
exclude: program.exclude
exclude: program.exclude,
testErrors: program.testErrors
}, function (err) {
if (err) {
throw err;
Expand Down
3 changes: 3 additions & 0 deletions lib/reporters/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ function defaultReporter(emitter) {
logUpdateProgress(event, "error", info.updateTo + " failed".grey);
finishProgress();
});
emitter.on("testErrors", function (event) {
console.log(event.errors);
});
emitter.on("updatingDone", function (event) {
var info = event.info;

Expand Down
15 changes: 14 additions & 1 deletion lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function run(config, done) {
info: info,
testCmd: testCmd
};
var testStdout;

emitter.emit("updating", event);

Expand All @@ -57,7 +58,15 @@ function run(config, done) {
emitter.emit("testing", event);
setImmediate(done);
},
runTests: async.apply(exec, testCmd)
runTests: function runTests(callback) {
exec(testCmd, function (err, stdout, stderr) {
if (err) {
testStdout = stdout;
return callback(err);
}
callback(null, stdout, stderr);
});
}
}, function (err) {
if (err) {
emitter.emit("rollback", event);
Expand All @@ -67,6 +76,10 @@ function run(config, done) {
return;
}
emitter.emit("rollbackDone", event);
if (config.testErrors) {
event.errors = testStdout;
emitter.emit("testErrors", event);
}
done();
});
return;
Expand Down
39 changes: 37 additions & 2 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,23 @@ var expectedOptionsWithCurrentCountLatestAndCustomTestCmd = {
testCmd: "npm run test"
};

var expectedOptionsWithCurrentCountLatestAndTestErrors = {
current: 1,
total: 1,
info: {
current: "0.1.4",
wanted: "1.1.5",
latest: "2.0.0",
location: "unicons",
type: "dependencies",
name: "unicons",
saveCmd: "--save",
updateTo: "2.0.0"
},
testCmd: "npm test",
errors: "This is the test error stdout"
};

var expectedOptionsWithCurrentCountWanted = {
current: 1,
total: 1,
Expand All @@ -214,7 +231,8 @@ function execMock(object, testsExpectToPass) {
if (cmd === "npm outdated --json --long --depth=0") {
setImmediate(cb, null, JSON.stringify(object), null);
} else if (cmd === "npm test" && !testsExpectToPass) {
setImmediate(cb, new Error());
console.log("PASS");
setImmediate(cb, new Error("test failed"), "This is the test error stdout", "This is the test error stderr");
} else {
setImmediate(cb, null);
}
Expand Down Expand Up @@ -333,7 +351,7 @@ describe("run()", function () {
run({ cwd: process.cwd(), reporter: reporter, exclude: "servus.js" }, done);
});
});

describe("if outdated modules were found with excluded more modules", function () {
before(setupOutdatedModules(outdatedModulesExclude));
afterEach(tearDown);
Expand Down Expand Up @@ -505,6 +523,23 @@ describe("run()", function () {
});
});

describe.only("testErrors", function () {
describe("if --test-errors is set and update fails", function () {
before(setupOutdatedModules(outdatedModules, false));
afterEach(tearDown);

it("should be emitted", function (done) {
reporter = function (emitter) {
emitter.on("testErrors", function (options) {
expect(options).to.eql(expectedOptionsWithCurrentCountLatestAndTestErrors);
});
};

run({ cwd: process.cwd(), reporter: reporter, testErrors: true }, done);
});
});
});

describe("updatingDone", function () {
describe("if tests are passing", function () {
before(setupOutdatedModules(outdatedModules, true));
Expand Down

0 comments on commit 8f40749

Please sign in to comment.