Skip to content

Commit

Permalink
Merge pull request #196 from sanack/throw-errors-for-backwards-compat…
Browse files Browse the repository at this point in the history
…ibility

fix(stdin): throw errors on invalid input
  • Loading branch information
mackermans committed Aug 1, 2019
2 parents 7fc84c0 + f1abdfd commit 02fa26d
Show file tree
Hide file tree
Showing 4 changed files with 313 additions and 142 deletions.
29 changes: 29 additions & 0 deletions src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import { validateJSONPath } from './utils'

const JQ_PATH = process.env.JQ_PATH || path.join(__dirname, '..', 'bin', 'jq')

export const FILTER_UNDEFINED_ERROR =
'node-jq: invalid filter argument supplied: "undefined"'
export const INPUT_JSON_UNDEFINED_ERROR =
'node-jq: invalid json object argument supplied: "undefined"'
export const INPUT_STRING_ERROR =
'node-jq: invalid json string argument supplied'

const getFileArray = path => {
if (Array.isArray(path)) {
return path.reduce((array, file) => {
Expand All @@ -15,11 +22,33 @@ const getFileArray = path => {
return [path]
}

const validateArguments = (filter, json, options) => {
if (typeof filter === 'undefined') {
throw new Error(FILTER_UNDEFINED_ERROR)
}

switch (options.input) {
case 'json':
if (typeof json === 'undefined') {
throw new Error(INPUT_JSON_UNDEFINED_ERROR)
}
break
case 'string':
if (!json) {
throw new Error(`${INPUT_STRING_ERROR}: "${json === '' ? '' : json}"`)
}
break
}
}

export const commandFactory = (filter, json, options = {}) => {
const mergedOptions = {
...optionDefaults,
...options
}

validateArguments(filter, json, mergedOptions)

let args = [filter, ...parseOptions(mergedOptions)]
let stdin = ''

Expand Down
18 changes: 10 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import isPathValid from 'is-valid-path'

const INVALID_PATH_ERROR = 'Invalid path'
const INVALID_JSON_PATH_ERROR = 'Not a json file'
export const INVALID_PATH_ERROR =
'node-jq: invalid path argument supplied (not a valid path)'
export const INVALID_JSON_PATH_ERROR =
'node-jq: invalid path argument supplied (not a .json file)'

export const isJSONPath = (path) => {
export const isJSONPath = path => {
return /\.json$/.test(path)
}

export const validateJSONPath = (JSONFile) => {
if (!isPathValid(JSONFile)) {
throw (Error(INVALID_PATH_ERROR))
export const validateJSONPath = path => {
if (!isPathValid(path)) {
throw new Error(`${INVALID_PATH_ERROR}: "${path}"`)
}

if (!isJSONPath(JSONFile)) {
throw (Error(INVALID_JSON_PATH_ERROR))
if (!isJSONPath(path)) {
throw new Error(`${INVALID_JSON_PATH_ERROR}: "${path === '' ? '' : path}"`)
}
}
52 changes: 47 additions & 5 deletions test/jq.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { expect } from 'chai'
import path from 'path'

import { run } from '../src/jq'
import { FILTER_UNDEFINED_ERROR } from '../src/command'
import { INVALID_PATH_ERROR, INVALID_JSON_PATH_ERROR } from '../src/utils'

const PATH_ROOT = path.join(__dirname, '..')
const PATH_ASTERISK_FIXTURE = path.join(PATH_ROOT, 'src', '*.js')
Expand All @@ -12,20 +14,44 @@ const PATH_JS_FIXTURE = path.join(PATH_FIXTURES, '1.js')
const PATH_LARGE_JSON_FIXTURE = path.join(PATH_FIXTURES, 'large.json')
const PATH_VARIABLE_JSON_FIXTURE = path.join(PATH_FIXTURES, 'var.json')

const FIXTURE_JSON = require('./fixtures/1.json')
const FIXTURE_JSON_STRING = JSON.stringify(FIXTURE_JSON, null, 2)

const FILTER_VALID = '.repository.type'
const FILTER_INVALID = 'invalid'
const FILTER_WITH_VARIABLE =
'[ . as $x | .user[] | {"user": ., "site": $x.site} ]'

const ERROR_INVALID_FILTER = /invalid/
const ERROR_INVALID_PATH = 'Invalid path'
const ERROR_INVALID_JSON_PATH = 'Not a json file'

describe('jq core', () => {
it('should fulfill its promise', done => {
run(FILTER_VALID, PATH_JSON_FIXTURE)
.then(output => {
expect(output).to.be.a('string')
expect(output).to.equal('"git"')
done()
})
.catch(error => {
done(error)
})
})

it('should pass on an empty filter', done => {
run('', PATH_JSON_FIXTURE)
.then(output => {
const normalizedOutput = output.replace(/\r\n/g, '\n')
expect(normalizedOutput).to.equal(FIXTURE_JSON_STRING)
done()
})
.catch(error => {
done(error)
})
})

it('should pass on a null filter', done => {
run(null, PATH_JSON_FIXTURE)
.then(output => {
expect(output).to.equal('null')
done()
})
.catch(error => {
Expand All @@ -45,11 +71,25 @@ describe('jq core', () => {
})
})

it('should fail on an undefined filter', done => {
run(undefined, PATH_JSON_FIXTURE)
.catch(error => {
expect(error).to.be.an.instanceof(Error)
expect(error.message).to.equal(FILTER_UNDEFINED_ERROR)
done()
})
.catch(error => {
done(error)
})
})

it('should fail on an invalid path', done => {
run(FILTER_VALID, PATH_ASTERISK_FIXTURE)
.catch(error => {
expect(error).to.be.an.instanceof(Error)
expect(error.message).to.equal(ERROR_INVALID_PATH)
expect(error.message).to.equal(
`${INVALID_PATH_ERROR}: "${PATH_ASTERISK_FIXTURE}"`
)
done()
})
.catch(error => {
Expand All @@ -61,7 +101,9 @@ describe('jq core', () => {
run(FILTER_VALID, PATH_JS_FIXTURE)
.catch(error => {
expect(error).to.be.an.instanceof(Error)
expect(error.message).to.equal(ERROR_INVALID_JSON_PATH)
expect(error.message).to.equal(
`${INVALID_JSON_PATH_ERROR}: "${PATH_JS_FIXTURE}"`
)
done()
})
.catch(error => {
Expand Down
Loading

0 comments on commit 02fa26d

Please sign in to comment.