From beaea9d11fead11b4cea831e3d0d2ed30832e380 Mon Sep 17 00:00:00 2001 From: Tim Oxley Date: Tue, 22 Oct 2019 13:16:44 +0800 Subject: [PATCH] Add tests around cli. Only show usage if on TTY & no argument, allow eaccess error if file not readable. --- cli.js | 10 +++++----- package.json | 2 +- test/test.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/cli.js b/cli.js index d11e3c1..e4f96b3 100755 --- a/cli.js +++ b/cli.js @@ -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 = [] diff --git a/package.json b/package.json index 58fb521..78c2a44 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/test.js b/test/test.js index 4be58c8..d86d396 100644 --- a/test/test.js +++ b/test/test.js @@ -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 @@ -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() + }) + }) +})