Skip to content

Commit

Permalink
code coverage
Browse files Browse the repository at this point in the history
- Update power-assert (babel-plugin-espower)
- Add using istanbul.
- Add several tests.
  • Loading branch information
mysticatea committed Apr 18, 2015
1 parent 46d6b60 commit 87a8aeb
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules
/coverage
/lib
3 changes: 3 additions & 0 deletions mocha-babel-hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require("babel/register")({
plugins: ["babel-plugin-espower"]
});
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
"test": "OK"
},
"scripts": {
"clean": "rimraf lib",
"clean": "rimraf lib coverage",
"lint": "eslint src",
"build": "npm-run-all clean lint build:babel",
"build:babel": "babel src --out-dir lib",
"test": "npm-run-all build test:mocha",
"test:mocha": "mocha test/*.js --compilers js:espower-babel/guess --timeout 30000 --colors",
"test:mocha": "istanbul cover node_modules/mocha/bin/_mocha -- test/*.js --require mocha-babel-hook --timeout 30000 --colors",
"testing": "npm-run-all clean --parallel testing:*",
"testing:build": "npm run build:babel -- --watch --source-maps-inline",
"testing:mocha": "npm run test:mocha -- --watch --growl",
Expand All @@ -36,12 +36,14 @@
"minimatch": "^2.0.4"
},
"devDependencies": {
"babel": "^5.1.9",
"babel": "^5.1.10",
"babel-core": "^5.1.10",
"babel-plugin-espower": "^0.1.0",
"eslint": "^0.19.0",
"espower-babel": "^2.0.0",
"istanbul": "^0.3.13",
"mocha": "^2.2.4",
"npm-run-all": "^1.2.1",
"power-assert": "^0.10.2",
"power-assert": "^0.11.0",
"rimraf": "^2.3.2",
"shelljs": "^0.4.0"
},
Expand Down
67 changes: 40 additions & 27 deletions src/command.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#!/usr/bin/env node

import runAll from "./index";
import Promise from "./promise";

if (require.main === module) {
main(process.argv.slice(2));
}
const SUCCESS = Promise.resolve(null);

function printHelp() {
console.log(`
function printHelp(stdout) {
stdout.write(`
Usage: npm-run-all [OPTIONS] [...tasks]
Run specified tasks.
Expand All @@ -20,11 +19,12 @@ Usage: npm-run-all [OPTIONS] [...tasks]
See Also:
https://github.com/mysticatea/npm-run-all
`);
}

function printVersion() {
console.log("v" + require("../package.json").version);
function printVersion(stdout) {
stdout.write("v" + require("../package.json").version + "\n");
}

function createQueue(args) {
Expand All @@ -51,48 +51,61 @@ function createQueue(args) {
}, [{parallel: false, tasks: []}]);
}

/*eslint no-process-exit:0*/
function main(args) {
export default function main(
args,
stdout = process.stdout,
stderr = process.stderr
) {
if (args.length === 0) {
args.push("--help");
}
switch (args[0]) {
case "-h":
case "--help":
printHelp();
return;
printHelp(stdout);
return SUCCESS;

case "-v":
case "--version":
printVersion();
return;
printVersion(stdout);
return SUCCESS;
}

let queue;
try {
queue = createQueue(args);
}
catch (err) {
console.error(err.message);
process.exit(1);
return Promise.reject(err);
}

(function next() {
return (function next() {
const group = queue.shift();
if (group == null) {
return;
return SUCCESS;
}
if (group.tasks.length === 0) {
next();
return;
return next();
}
runAll(
group.tasks,
{
stdout: process.stdout,
stderr: process.stderr,
parallel: group.parallel
})
.then(next, () => process.exit(1));

const options = {
stdout: stdout,
stderr: stderr,
parallel: group.parallel
};
return runAll(group.tasks, options).then(next);
})();
}

/*eslint no-process-exit:0*/
if (require.main === module) {
main(process.argv.slice(2)).then(
() => {
process.exit(0);
},
err => {
console.log("ERROR:", err.message);
process.exit(1);
}
);
}
58 changes: 40 additions & 18 deletions test/common.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,68 @@
import {exec} from "shelljs";
import {PassThrough} from "stream";
import assert from "power-assert";
import {result, removeResult} from "./lib/util";
import {result, removeResult, BufferStream} from "./lib/util";

// Test targets.
import runAll from "../lib/index";
import "../lib/command";
import command from "../lib/command";

describe("npm-run-all", () => {
beforeEach(removeResult);
after(removeResult);

describe("should run a task by npm:", () => {
it("should print a help text if arguments are nothing.", () => {
const buf = new BufferStream();
return command([], /*stdout*/buf)
.then(() => assert(/Usage:/.test(buf.value)));
});

it("should print a help text if the first argument is -h", () => {
const buf = new BufferStream();
return command(["-h"], /*stdout*/buf)
.then(() => assert(/Usage:/.test(buf.value)));
});

it("should print a version number if the first argument is -v", () => {
const buf = new BufferStream();
return command(["-v"], /*stdout*/buf)
.then(() => assert(/v[0-9]+\.[0-9]+\.[0-9]+/.test(buf.value)));
});

it("should fail if an invalid option exists.", () => {
return command(["--invalid"])
.then(
() => assert(false, "should fail"),
() => null //< OK!
);
});

describe("should run a task by npm (check an environment variable):", () => {
it("lib version", () => {
return runAll("test-task:env-check")
.then(() => {
assert(result() === "OK");
});
.then(() => assert(result() === "OK"));
});

it("command version", () => {
exec("node lib/command.js test-task:env-check");
assert(result() === "OK");
return command(["test-task:env-check"])
.then(() => assert(result() === "OK"));
});
});

describe("should fail to run when tasks exited with non-zero code:", () => {
it("lib version", () => {
return runAll("test-task:error")
.then(
() => {
assert(false, "should fail");
},
() => {
// OK.
return null;
});
() => assert(false, "should fail"),
() => null //< OK!
);
});

it("command version", () => {
var res = exec("node lib/command.js test-task:error");
assert.ok(res.code > 0);
return command(["test-task:error"])
.then(
() => assert(false, "should fail"),
() => null //< OK!
);
});
});

Expand Down
14 changes: 14 additions & 0 deletions test/lib/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var fs = require("fs");
var Writable = require("stream").Writable;
var inherits = require("util").inherits;

var FILE_NAME = "test.txt";

Expand All @@ -22,3 +24,15 @@ exports.removeResult = function removeResult() {
}
catch (err) {} //eslint-disable-line no-empty
};


var BufferStream = exports.BufferStream = function BufferStream() {
Writable.call(this);
this.value = "";
};
inherits(BufferStream, Writable);

BufferStream.prototype._write = function(chunk, encoding, callback) { //eslint-disable-line no-underscore-dangle
this.value += chunk.toString();
callback();
};
41 changes: 26 additions & 15 deletions test/mixed.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
import {exec} from "shelljs";
import assert from "power-assert";
import {result, removeResult} from "./lib/util";

// Test targets.
import runAll from "../lib/index";
import "../lib/command";
import command from "../lib/command";

describe("npm-run-all", () => {
beforeEach(removeResult);
after(removeResult);

it("should run tasks, mixed sequential and parallel 1:", () => {
exec("node lib/command.js \"test-task:append a\" -p \"test-task:append b\" \"test-task:append c\" -s \"test-task:append d\" \"test-task:append e\"");
assert(result() === "aabcbcddee" ||
result() === "aabccbddee" ||
result() === "aacbbcddee" ||
result() === "aacbcbddee");
it("should run a mix of sequential and parallel tasks (has the default group):", () => {
return command([
"test-task:append a",
"-p", "test-task:append b", "test-task:append c",
"-s", "test-task:append d", "test-task:append e"
])
.then(() => {
assert(
result() === "aabcbcddee" ||
result() === "aabccbddee" ||
result() === "aacbbcddee" ||
result() === "aacbcbddee");
});
});

it("should run tasks, mixed sequential and parallel 2:", () => {
exec("node lib/command.js -p \"test-task:append b\" \"test-task:append c\" -s \"test-task:append d\" \"test-task:append e\"");
assert(result() === "bcbcddee" ||
result() === "bccbddee" ||
result() === "cbbcddee" ||
result() === "cbcbddee");
it("should run a mix of sequential and parallel tasks (doesn't have the default group):", () => {
return command([
"-p", "test-task:append b", "test-task:append c",
"-s", "test-task:append d", "test-task:append e"
])
.then(() => {
assert(
result() === "bcbcddee" ||
result() === "bccbddee" ||
result() === "cbbcddee" ||
result() === "cbcbddee");
});
});

});
25 changes: 14 additions & 11 deletions test/parallel.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {exec} from "shelljs";
import assert from "power-assert";
import {result, removeResult} from "./lib/util";

// Test targets.
import runAll from "../lib/index";
import "../lib/command";
import command from "../lib/command";

describe("npm-run-all", () => {
beforeEach(removeResult);
Expand All @@ -14,19 +13,23 @@ describe("npm-run-all", () => {
it("lib version", () => {
return runAll(["test-task:append a", "test-task:append b"], {parallel: true})
.then(() => {
assert(result() === "abab" ||
result() === "baba" ||
result() === "abba" ||
result() === "baab");
assert(
result() === "abab" ||
result() === "baba" ||
result() === "abba" ||
result() === "baab");
});
});

it("command version", () => {
exec("node lib/command.js --parallel \"test-task:append a\" \"test-task:append b\"");
assert(result() === "abab" ||
result() === "baba" ||
result() === "abba" ||
result() === "baab");
return command(["--parallel", "test-task:append a", "test-task:append b"])
.then(() => {
assert(
result() === "abab" ||
result() === "baba" ||
result() === "abba" ||
result() === "baab");
});
});
});

Expand Down

0 comments on commit 87a8aeb

Please sign in to comment.