Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: raise permission error for Deno if config load fails #298

Merged
merged 2 commits into from Aug 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,3 +6,4 @@ package-lock.json
./test/fixtures/package.json
coverage
build
example.*
5 changes: 4 additions & 1 deletion lib/yargs-parser.ts
Expand Up @@ -660,7 +660,10 @@ export class YargsParser {

setConfigObject(config)
} catch (ex) {
if (argv[configKey]) error = Error(__('Invalid JSON config file: %s', configPath))
// Deno will receive a PermissionDenied error if an attempt is
// made to load config without the --allow-read flag:
if (ex.name === 'PermissionDenied') error = ex
else if (argv[configKey]) error = Error(__('Invalid JSON config file: %s', configPath))
}
}
})
Expand Down
85 changes: 42 additions & 43 deletions test/typescript/tokenize-arg-string.ts
@@ -1,7 +1,6 @@
/* global describe, it */
import { strictEqual } from 'assert'
import { tokenizeArgString } from '../../lib/tokenize-arg-string.js'
import { expect } from 'chai'

describe('TokenizeArgString', function () {
it('handles unquoted string', function () {
Expand All @@ -24,109 +23,109 @@ describe('TokenizeArgString', function () {

it('handles single quoted string with spaces', function () {
const args = tokenizeArgString("--foo 'hello world' --bar='foo bar'")
args[0].should.equal('--foo')
args[1].should.equal("'hello world'")
args[2].should.equal("--bar='foo bar'")
strictEqual(args[0], '--foo')
strictEqual(args[1], "'hello world'")
strictEqual(args[2], "--bar='foo bar'")
})

it('handles double quoted string with spaces', function () {
const args = tokenizeArgString('--foo "hello world" --bar="foo bar"')
args[0].should.equal('--foo')
args[1].should.equal('"hello world"')
args[2].should.equal('--bar="foo bar"')
strictEqual(args[0], '--foo')
strictEqual(args[1], '"hello world"')
strictEqual(args[2], '--bar="foo bar"')
})

it('handles single quoted empty string', function () {
const args = tokenizeArgString('--foo \'\' --bar=\'\'')
args[0].should.equal('--foo')
args[1].should.equal("''")
args[2].should.equal("--bar=''")
strictEqual(args[0], '--foo')
strictEqual(args[1], "''")
strictEqual(args[2], "--bar=''")
})

it('handles double quoted empty string', function () {
const args = tokenizeArgString('--foo "" --bar=""')
args[0].should.equal('--foo')
args[1].should.equal('""')
args[2].should.equal('--bar=""')
strictEqual(args[0], '--foo')
strictEqual(args[1], '""')
strictEqual(args[2], '--bar=""')
})

it('handles quoted string with embedded quotes', function () {
var args = tokenizeArgString('--foo "hello \'world\'" --bar=\'foo "bar"\'')
args[0].should.equal('--foo')
args[1].should.equal('"hello \'world\'"')
args[2].should.equal('--bar=\'foo "bar"\'')
strictEqual(args[0], '--foo')
strictEqual(args[1], '"hello \'world\'"')
strictEqual(args[2], '--bar=\'foo "bar"\'')
})

// https://github.com/yargs/yargs-parser/pull/100
// https://github.com/yargs/yargs-parser/pull/106
it('ignores unneeded spaces', function () {
const args = tokenizeArgString(' foo bar "foo bar" ')
args[0].should.equal('foo')
expect(args[1]).equal('bar')
expect(args[2]).equal('"foo bar"')
strictEqual(args[0], 'foo')
strictEqual(args[1], 'bar')
strictEqual(args[2], '"foo bar"')
})

it('handles boolean options', function () {
const args = tokenizeArgString('--foo -bar')
expect(args[0]).to.equal(('--foo'))
expect(args[1]).to.equal(('-bar'))
strictEqual(args[0], '--foo')
strictEqual(args[1], '-bar')
})

it('handles empty string', function () {
const args = tokenizeArgString('')
expect(args.length).to.equal(0)
strictEqual(args.length, 0)
})

it('handles array with unquoted string', function () {
const args = tokenizeArgString(['--foo', '99'])
args[0].should.equal('--foo')
args[1].should.equal('99')
strictEqual(args[0], '--foo')
strictEqual(args[1], '99')
})

it('handles array with quoted string with no spaces', function () {
const args = tokenizeArgString(['--foo', "'hello'"])
args[0].should.equal('--foo')
args[1].should.equal("'hello'")
strictEqual(args[0], '--foo')
strictEqual(args[1], "'hello'")
})

it('handles array with single quoted string with spaces', function () {
const args = tokenizeArgString(['--foo', "'hello world'", "--bar='foo bar'"])
args[0].should.equal('--foo')
args[1].should.equal("'hello world'")
args[2].should.equal("--bar='foo bar'")
strictEqual(args[0], '--foo')
strictEqual(args[1], "'hello world'")
strictEqual(args[2], "--bar='foo bar'")
})

it('handles array with double quoted string with spaces', function () {
const args = tokenizeArgString(['--foo', '"hello world"', '--bar="foo bar"'])
args[0].should.equal('--foo')
args[1].should.equal('"hello world"')
args[2].should.equal('--bar="foo bar"')
strictEqual(args[0], '--foo')
strictEqual(args[1], '"hello world"')
strictEqual(args[2], '--bar="foo bar"')
})

it('handles array with single quoted empty string', function () {
const args = tokenizeArgString(['--foo', "''", "--bar=''"])
args[0].should.equal('--foo')
args[1].should.equal("''")
args[2].should.equal("--bar=''")
strictEqual(args[0], '--foo')
strictEqual(args[1], "''")
strictEqual(args[2], "--bar=''")
})

it('handles array with double quoted empty string', function () {
const args = tokenizeArgString(['--foo', '""', '--bar=""'])
args[0].should.equal('--foo')
args[1].should.equal('""')
args[2].should.equal('--bar=""')
strictEqual(args[0], '--foo')
strictEqual(args[1], '""')
strictEqual(args[2], '--bar=""')
})

it('handles array with quoted string with embedded quotes', function () {
var args = tokenizeArgString(['--foo', '"hello \'world\'"', '--bar=\'foo "bar"\''])
args[0].should.equal('--foo')
args[1].should.equal('"hello \'world\'"')
args[2].should.equal('--bar=\'foo "bar"\'')
strictEqual(args[0], '--foo')
strictEqual(args[1], '"hello \'world\'"')
strictEqual(args[2], '--bar=\'foo "bar"\'')
})

it('handles array with boolean options', function () {
const args = tokenizeArgString(['--foo', '-bar'])
expect(args[0]).to.equal('--foo')
expect(args[1]).to.equal('-bar')
strictEqual(args[0], '--foo')
strictEqual(args[1], '-bar')
})
})