Skip to content

Commit

Permalink
Improve coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Nov 11, 2015
1 parent bc749bc commit a549297
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 88 deletions.
22 changes: 2 additions & 20 deletions src/bin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,13 @@ function parse(args) {
}

export default function npmRunAll(args, stdout, stderr) {
let currentPromise = null;
let aborted = false;

const resultPromise = parse(args).then(async (groups) => {
return parse(args).then(async (groups) => {
for (const group of groups) {
if (aborted) {
return;
}
if (group.tasks.length === 0) {
continue;
}

currentPromise = runAll(
await runAll(
group.tasks,
{
stdout,
Expand All @@ -53,18 +47,6 @@ export default function npmRunAll(args, stdout, stderr) {
parallel: group.parallel
}
);
await currentPromise;
currentPromise = null;
}
});

// Define abort method.
resultPromise.abort = function abort() {
aborted = true;
if (currentPromise != null) {
currentPromise.abort();
}
};

return resultPromise;
}
40 changes: 18 additions & 22 deletions src/lib/npm-run-all.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {join} from "path";
import matchTasks from "./match-tasks";
import readTasks from "./read-tasks";
import runTasksInParallel from "./run-tasks-in-parallel";
Expand All @@ -21,29 +20,26 @@ export default function runAll(
taskList = null
} = {}
) {
const patterns = toArray(patternOrPatterns);
if (patterns.length === 0) {
return Promise.resolve(null);
}
try {
const patterns = toArray(patternOrPatterns);
if (patterns.length === 0) {
return Promise.resolve(null);
}
if (taskList != null && Array.isArray(taskList) === false) {
throw new Error("Invalid options.taskList");
}

if (taskList != null && Array.isArray(taskList) === false) {
return Promise.reject(new Error("Invalid options.taskList"));
}
const tasks = matchTasks(taskList || readTasks(), patterns);
if (tasks.length === 0) {
throw new Error(`Matched tasks not found: ${patterns.join(", ")}`);
}

const actualTaskList = taskList || readTasks();
if (Array.isArray(actualTaskList) === false) {
const message = `Not Found: ${join(process.cwd(), "package.json")}`;
return Promise.reject(new Error(message));
return (
parallel ? runTasksInParallel(tasks, stdin, stdout, stderr) :
/* else */ runTasksInSequencial(tasks, stdin, stdout, stderr)
);
}

const tasks = matchTasks(actualTaskList, patterns);
if (tasks.length === 0) {
const message = `Matched tasks not found: ${patterns.join(", ")}`;
return Promise.reject(new Error(message));
catch (err) {
return Promise.reject(new Error(err.message));
}

return (
parallel ? runTasksInParallel(tasks, stdin, stdout, stderr) :
/* else */ runTasksInSequencial(tasks, stdin, stdout, stderr)
);
}
16 changes: 5 additions & 11 deletions src/lib/read-tasks.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import {join} from "path";

export default function readTasks() {
try {
const packageJsonPath = join(process.cwd(), "package.json");
const packageJson = require(packageJsonPath);
const scripts = packageJson && packageJson.scripts;
if (typeof scripts === "object" && !Array.isArray(scripts)) {
return Object.keys(scripts);
}
const packageJsonPath = join(process.cwd(), "package.json");
const packageJson = require(packageJsonPath);
const scripts = packageJson && packageJson.scripts;
if (typeof scripts === "object" && !Array.isArray(scripts)) {
return Object.keys(scripts);
}
catch (err) {
// do nothing.
}

return [];
}
9 changes: 1 addition & 8 deletions src/lib/run-tasks-in-parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,10 @@ export default function runTasksInParallel(tasks, stdin, stdout, stderr) {
});

// Make fail if there are tasks that exited non-zero.
const resultPromise = parallelPromise.then(() => {
return parallelPromise.then(() => {
if (nonZeroExited != null) {
throw new Error(
`${nonZeroExited.task}: None-Zero Exit(${nonZeroExited.code});`);
}
});

// Define abort method.
resultPromise.abort = function abort() {
taskPromises.forEach(t => { t.abort(); });
};

return resultPromise;
}
33 changes: 6 additions & 27 deletions src/lib/run-tasks-in-sequencial.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
import runTask from "./run-task";

export default function runTasksInSequencial(tasks, stdin, stdout, stderr) {
let currentPromise = null;
let aborted = false;

const resultPromise = (async () => {
for (const task of tasks) {
currentPromise = runTask(task, stdin, stdout, stderr);
const result = await currentPromise;
currentPromise = null;

if (aborted) {
break;
}
if (result.code) {
throw new Error(`${result.task}: None-Zero Exit(${result.code});`);
}
export default async function runTasksInSequencial(tasks, stdin, stdout, stderr) {
for (const task of tasks) {
const result = await runTask(task, stdin, stdout, stderr);
if (result.code) {
throw new Error(`${result.task}: None-Zero Exit(${result.code});`);
}
})();

// Define abort method.
resultPromise.abort = function abort() {
aborted = true;
if (currentPromise != null) {
currentPromise.abort();
}
};

return resultPromise;
}
}
1 change: 1 addition & 0 deletions test-workspace/no-package-json/dummy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
to add to git.
15 changes: 15 additions & 0 deletions test-workspace/no-scripts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "npm-run-all-test",
"version": "0.0.0",
"private": true,
"description": "",
"config": {
"test": "OK"
},
"repository": {
"type": "git",
"url": "https://github.com/mysticatea/npm-run-all.git"
},
"author": "Toru Nagashima",
"license": "MIT"
}
72 changes: 72 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ describe("[common] npm-run-all", () => {
)
);

it("should fail if invalid `options.taskList` is given.", () =>
runAll("test-task:append a", {taskList: {invalid: 0}})
.then(
() => assert(false, "should fail"),
() => null // OK!
)
);

it("should do nothing if a task list is empty.", () =>
runAll(null).then(() => assert(result() == null))
);

describe("should run a task by npm (check an environment variable):", () => {
it("lib version", () =>
runAll("test-task:env-check")
Expand All @@ -50,6 +62,66 @@ describe("[common] npm-run-all", () => {
);
});

describe("should fail if unknown tasks are given:", () => {
it("lib version", () =>
runAll("unknown-task")
.then(
() => assert(false, "should fail"),
() => null // OK!
)
);

it("command version", () =>
command(["unknown-task"])
.then(
() => assert(false, "should fail"),
() => null // OK!
)
);
});

describe("should fail if package.json is not found:", () => {
before(() => { process.chdir("no-package-json"); });
after(() => { process.chdir(".."); });

it("lib version", () =>
runAll(["test-task:append:a"])
.then(
() => assert(false, "should fail"),
() => null // OK!
)
);

it("command version", () =>
command(["test-task:append:a"])
.then(
() => assert(false, "should fail"),
() => null // OK!
)
);
});

describe("should fail if package.json does not have scripts field:", () => {
before(() => { process.chdir("no-scripts"); });
after(() => { process.chdir(".."); });

it("lib version", () =>
runAll(["test-task:append:a"])
.then(
() => assert(false, "should fail"),
() => null // OK!
)
);

it("command version", () =>
command(["test-task:append:a"])
.then(
() => assert(false, "should fail"),
() => null // OK!
)
);
});

describe("should fail to run when tasks exited with non-zero code:", () => {
it("lib version", () =>
runAll("test-task:error")
Expand Down

0 comments on commit a549297

Please sign in to comment.