Skip to content

Commit 21a7c41

Browse files
committed
Update: add tests and fix bugs
1 parent 34cb764 commit 21a7c41

File tree

15 files changed

+605
-15
lines changed

15 files changed

+605
-15
lines changed

.babelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["power-assert"],
3+
"only": "test/*.js"
4+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
/.nyc_output
2+
/coverage
13
/node_modules
24
/npm-debug.log

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- "0.10"
5+
- "0.12"
6+
- "4"
7+
- "6"
8+
before_script:
9+
- "node bin/index.js '^0.10.0' npm install spawn-sync"
10+
after_success:
11+
- npm run coveralls

bin/help.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ var help = fs.readFileSync(path.join(__dirname, "help.txt"), "utf8")
2222
// Public Interface
2323
//------------------------------------------------------------------------------
2424

25-
exports.printHelp = function printHelp() {
26-
console.log(help)
25+
exports.printHelp = function printHelp(output) {
26+
output.write(help)
2727
}

bin/index.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,30 @@ var versionRange = argv[2]
2222
var command = argv[3]
2323
var args = argv.slice(4)
2424

25-
if (argv.length < 4) {
26-
require("./help").printHelp()
27-
process.exit(1)
28-
}
29-
3025
if (versionRange === "--help" || versionRange === "-h") {
31-
require("./help").printHelp()
26+
require("./help").printHelp(process.stdout)
3227
return
3328
}
3429

3530
if (versionRange === "--version" || versionRange === "-v") {
36-
require("./version").printVersion()
31+
require("./version").printVersion(process.stdout)
3732
return
3833
}
3934

40-
spawnIfNodeVersionSatisfies(versionRange, command, args, {stdio: "inherit"})
35+
if (argv.length < 4) {
36+
require("./help").printHelp(process.stderr)
37+
process.exit(1)
38+
}
39+
40+
var cp = spawnIfNodeVersionSatisfies(
41+
versionRange,
42+
command,
43+
args,
44+
{stdio: "inherit"}
45+
)
46+
47+
if (cp != null) {
48+
cp.on("close", function(exitCode) {
49+
process.exit(exitCode)
50+
})
51+
}

bin/version.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ var version = require(path.resolve(__dirname, "../package.json")).version
1616
// Public Interface
1717
//------------------------------------------------------------------------------
1818

19-
exports.printVersion = function printVersion() {
20-
console.log("v" + version)
19+
exports.printVersion = function printVersion(output) {
20+
output.write("v" + version)
2121
}

package.json

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,41 @@
77
},
88
"main": "lib/index.js",
99
"bin": "bin/index.js",
10-
"files": ["bin", "lib"],
10+
"files": [
11+
"bin",
12+
"lib"
13+
],
1114
"scripts": {
12-
"test": "echo \"Error: no test specified\" && exit 1"
15+
"clean": "rimraf .nyc_output coverage",
16+
"lint": "eslint bin lib test",
17+
"coverage": "wait-on coverage/lcov-report/index.html && opener coverage/lcov-report/index.html",
18+
"coveralls": "cat coverage/lcov.info | coveralls",
19+
"preversion": "run-s clean test",
20+
"postversion": "git push && git push --tags",
21+
"test": "run-s clean lint test:main",
22+
"test:main": "nyc --require babel-register --reporter=lcov mocha \"test/*.js\" --timeout 10000",
23+
"watch": "npm run clean && run-p watch:main coverage",
24+
"watch:main": "chokidar \"{bin,lib,test}/**/*.js\" --initial --command \"npm run test:main\""
1325
},
1426
"dependencies": {
1527
"cross-spawn": "^4.0.0",
1628
"semver": "^5.2.0"
1729
},
1830
"devDependencies": {
31+
"babel-preset-power-assert": "^1.0.0",
32+
"babel-register": "^6.9.0",
33+
"chokidar-cli": "^1.2.0",
34+
"coveralls": "^2.11.9",
1935
"eslint": "^2.13.1",
20-
"eslint-config-mysticatea": "^4.0.0"
36+
"eslint-config-mysticatea": "^4.0.0",
37+
"mocha": "^2.5.3",
38+
"npm-run-all": "^2.3.0",
39+
"nyc": "^6.6.1",
40+
"opener": "^1.4.1",
41+
"pinkie-promise": "^2.0.1",
42+
"power-assert": "^1.4.1",
43+
"rimraf": "^2.5.2",
44+
"wait-on": "^1.5.2"
2145
},
2246
"repository": {
2347
"type": "git",

test/.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "mysticatea/mocha"
3+
}

test/api.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/**
2+
* @author Toru Nagashima
3+
* @copyright 2016 Toru Nagashima. All rights reserved.
4+
* See LICENSE file in root directory for full license.
5+
*/
6+
"use strict"
7+
8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
12+
var assert = require("assert")
13+
var semver = require("semver")
14+
var api = require("./lib/api")
15+
16+
//------------------------------------------------------------------------------
17+
// Helpers
18+
//------------------------------------------------------------------------------
19+
20+
var version = process.versions.node
21+
var run = api.run
22+
var runSync = api.runSync
23+
24+
//------------------------------------------------------------------------------
25+
// Test
26+
//------------------------------------------------------------------------------
27+
28+
describe("Under Node.js v" + version + ",", function() {
29+
var ranges = [
30+
">=0.10",
31+
">=0.12",
32+
">=4",
33+
">=6",
34+
"<0.10",
35+
"<0.12",
36+
"<4",
37+
"<6",
38+
"^0.10",
39+
"^0.12",
40+
"^4",
41+
"^6",
42+
"^0.10 || ^0.12 || ^4 || ^6",
43+
]
44+
45+
ranges.forEach(function(range) {
46+
describe("if '" + range + "' is given", function() {
47+
describe("with good command,", function() {
48+
describe("async version", function() {
49+
var result = null
50+
51+
before(function() {
52+
return run(range).then(function(ret) {
53+
result = ret
54+
})
55+
})
56+
57+
if (semver.satisfies(version, range)) {
58+
it("should exit with zero", function() {
59+
assert(result.exitCode === 0)
60+
})
61+
it("should run the specified command", function() {
62+
assert(result.stdout === "OK")
63+
})
64+
}
65+
else {
66+
it("should NOT run the specified command", function() {
67+
assert(result == null)
68+
})
69+
}
70+
})
71+
72+
describe("sync version", function() {
73+
var result = null
74+
75+
before(function() {
76+
result = runSync(range)
77+
})
78+
79+
if (semver.satisfies(version, range)) {
80+
it("should exit with zero", function() {
81+
assert(result.exitCode === 0)
82+
})
83+
it("should run the specified command", function() {
84+
assert(result.stdout === "OK")
85+
})
86+
}
87+
else {
88+
it("should NOT run the specified command", function() {
89+
assert(result == null)
90+
})
91+
}
92+
})
93+
})
94+
95+
describe("with bad command,", function() {
96+
describe("async version", function() {
97+
var result = null
98+
99+
before(function() {
100+
return run(range, {fail: true}).then(function(ret) {
101+
result = ret
102+
})
103+
})
104+
105+
if (semver.satisfies(version, range)) {
106+
it("should exit with a non-zero code", function() {
107+
assert(result.exitCode === 1)
108+
})
109+
it("should run the specified command", function() {
110+
assert(result.stdout === "NG")
111+
})
112+
}
113+
else {
114+
it("should NOT run the specified command", function() {
115+
assert(result == null)
116+
})
117+
}
118+
})
119+
120+
describe("sync version", function() {
121+
var result = null
122+
123+
before(function() {
124+
result = runSync(range, {fail: true})
125+
})
126+
127+
if (semver.satisfies(version, range)) {
128+
it("should exit with a non-zero code", function() {
129+
assert(result.exitCode === 1)
130+
})
131+
it("should run the specified command", function() {
132+
assert(result.stdout === "NG")
133+
})
134+
}
135+
else {
136+
it("should NOT run the specified command", function() {
137+
assert(result == null)
138+
})
139+
}
140+
})
141+
})
142+
})
143+
})
144+
})

0 commit comments

Comments
 (0)