Skip to content

Commit

Permalink
Add tests around cli. Only show usage if on TTY & no argument, allow …
Browse files Browse the repository at this point in the history
…eaccess error if file not readable.
  • Loading branch information
timoxley committed Oct 22, 2019
1 parent 533ac93 commit beaea9d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
10 changes: 5 additions & 5 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ const readline = require('readline')

const flat = require('./index')

if (process.stdin.isTTY) {
const filepath = process.argv.slice(2)[0]
if (!filepath) return usage()
const filepath = process.argv.slice(2)[0]
if (filepath) {
// Read from file
const file = path.resolve(process.cwd(), filepath)
if (!file) return usage(1)
if (!fs.existsSync(file)) return usage(1)
fs.accessSync(file, fs.constants.R_OK) // allow to throw if not readable
out(require(file))
} else if (process.stdin.isTTY) {
usage(0)
} else {
// Read from newline-delimited STDIN
const lines = []
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"main": "index.js",
"bin": "cli.js",
"scripts": {
"test": "mocha -u tdd --reporter spec && standard index.js test/index.js"
"test": "mocha -u tdd --reporter spec && standard cli.js index.js test/index.js"
},
"license": "BSD-3-Clause",
"description": "Take a nested Javascript object and flatten it, or unflatten an object with delimited keys",
Expand Down
34 changes: 34 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const assert = require('assert')
const path = require('path')
const { exec } = require('child_process')
const pkg = require('../package.json')
const flat = require('../index')

const flatten = flat.flatten
Expand Down Expand Up @@ -593,3 +595,35 @@ suite('Order of Keys', function () {
assert.deepStrictEqual(Object.keys(obj.abc.c[0]), Object.keys(result.abc.c[0]))
})
})

suite('CLI', function () {
test('can take filename', function (done) {
const cli = path.resolve(__dirname, '..', pkg.bin)
const pkgJSON = path.resolve(__dirname, '..', 'package.json')
exec(`${cli} ${pkgJSON}`, (err, stdout, stderr) => {
assert.ifError(err)
assert.strictEqual(stdout.trim(), JSON.stringify(flatten(pkg), null, 2))
done()
})
})

test('exits with usage if no file', function (done) {
const cli = path.resolve(__dirname, '..', pkg.bin)
const pkgJSON = path.resolve(__dirname, '..', 'package.json')
exec(`${cli} ${pkgJSON}`, (err, stdout, stderr) => {
assert.ifError(err)
assert.strictEqual(stdout.trim(), JSON.stringify(flatten(pkg), null, 2))
done()
})
})

test('can take piped file', function (done) {
const cli = path.resolve(__dirname, '..', pkg.bin)
const pkgJSON = path.resolve(__dirname, '..', 'package.json')
exec(`cat ${pkgJSON} | ${cli}`, (err, stdout, stderr) => {
assert.ifError(err)
assert.strictEqual(stdout.trim(), JSON.stringify(flatten(pkg), null, 2))
done()
})
})
})

0 comments on commit beaea9d

Please sign in to comment.