Skip to content

Commit beaea9d

Browse files
committed
Add tests around cli. Only show usage if on TTY & no argument, allow eaccess error if file not readable.
1 parent 533ac93 commit beaea9d

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

cli.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ const readline = require('readline')
66

77
const flat = require('./index')
88

9-
if (process.stdin.isTTY) {
10-
const filepath = process.argv.slice(2)[0]
11-
if (!filepath) return usage()
9+
const filepath = process.argv.slice(2)[0]
10+
if (filepath) {
1211
// Read from file
1312
const file = path.resolve(process.cwd(), filepath)
14-
if (!file) return usage(1)
15-
if (!fs.existsSync(file)) return usage(1)
13+
fs.accessSync(file, fs.constants.R_OK) // allow to throw if not readable
1614
out(require(file))
15+
} else if (process.stdin.isTTY) {
16+
usage(0)
1717
} else {
1818
// Read from newline-delimited STDIN
1919
const lines = []

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"main": "index.js",
55
"bin": "cli.js",
66
"scripts": {
7-
"test": "mocha -u tdd --reporter spec && standard index.js test/index.js"
7+
"test": "mocha -u tdd --reporter spec && standard cli.js index.js test/index.js"
88
},
99
"license": "BSD-3-Clause",
1010
"description": "Take a nested Javascript object and flatten it, or unflatten an object with delimited keys",

test/test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const assert = require('assert')
44
const path = require('path')
5+
const { exec } = require('child_process')
6+
const pkg = require('../package.json')
57
const flat = require('../index')
68

79
const flatten = flat.flatten
@@ -593,3 +595,35 @@ suite('Order of Keys', function () {
593595
assert.deepStrictEqual(Object.keys(obj.abc.c[0]), Object.keys(result.abc.c[0]))
594596
})
595597
})
598+
599+
suite('CLI', function () {
600+
test('can take filename', function (done) {
601+
const cli = path.resolve(__dirname, '..', pkg.bin)
602+
const pkgJSON = path.resolve(__dirname, '..', 'package.json')
603+
exec(`${cli} ${pkgJSON}`, (err, stdout, stderr) => {
604+
assert.ifError(err)
605+
assert.strictEqual(stdout.trim(), JSON.stringify(flatten(pkg), null, 2))
606+
done()
607+
})
608+
})
609+
610+
test('exits with usage if no file', function (done) {
611+
const cli = path.resolve(__dirname, '..', pkg.bin)
612+
const pkgJSON = path.resolve(__dirname, '..', 'package.json')
613+
exec(`${cli} ${pkgJSON}`, (err, stdout, stderr) => {
614+
assert.ifError(err)
615+
assert.strictEqual(stdout.trim(), JSON.stringify(flatten(pkg), null, 2))
616+
done()
617+
})
618+
})
619+
620+
test('can take piped file', function (done) {
621+
const cli = path.resolve(__dirname, '..', pkg.bin)
622+
const pkgJSON = path.resolve(__dirname, '..', 'package.json')
623+
exec(`cat ${pkgJSON} | ${cli}`, (err, stdout, stderr) => {
624+
assert.ifError(err)
625+
assert.strictEqual(stdout.trim(), JSON.stringify(flatten(pkg), null, 2))
626+
done()
627+
})
628+
})
629+
})

0 commit comments

Comments
 (0)