Skip to content

Commit 1174e2b

Browse files
authored
fix: raise permission error for Deno if config load fails (yargs#298)
1 parent 1a925d8 commit 1174e2b

File tree

3 files changed

+47
-44
lines changed

3 files changed

+47
-44
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ package-lock.json
66
./test/fixtures/package.json
77
coverage
88
build
9+
example.*

lib/yargs-parser.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,10 @@ export class YargsParser {
660660

661661
setConfigObject(config)
662662
} catch (ex) {
663-
if (argv[configKey]) error = Error(__('Invalid JSON config file: %s', configPath))
663+
// Deno will receive a PermissionDenied error if an attempt is
664+
// made to load config without the --allow-read flag:
665+
if (ex.name === 'PermissionDenied') error = ex
666+
else if (argv[configKey]) error = Error(__('Invalid JSON config file: %s', configPath))
664667
}
665668
}
666669
})
+42-43
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* global describe, it */
22
import { strictEqual } from 'assert'
33
import { tokenizeArgString } from '../../lib/tokenize-arg-string.js'
4-
import { expect } from 'chai'
54

65
describe('TokenizeArgString', function () {
76
it('handles unquoted string', function () {
@@ -24,109 +23,109 @@ describe('TokenizeArgString', function () {
2423

2524
it('handles single quoted string with spaces', function () {
2625
const args = tokenizeArgString("--foo 'hello world' --bar='foo bar'")
27-
args[0].should.equal('--foo')
28-
args[1].should.equal("'hello world'")
29-
args[2].should.equal("--bar='foo bar'")
26+
strictEqual(args[0], '--foo')
27+
strictEqual(args[1], "'hello world'")
28+
strictEqual(args[2], "--bar='foo bar'")
3029
})
3130

3231
it('handles double quoted string with spaces', function () {
3332
const args = tokenizeArgString('--foo "hello world" --bar="foo bar"')
34-
args[0].should.equal('--foo')
35-
args[1].should.equal('"hello world"')
36-
args[2].should.equal('--bar="foo bar"')
33+
strictEqual(args[0], '--foo')
34+
strictEqual(args[1], '"hello world"')
35+
strictEqual(args[2], '--bar="foo bar"')
3736
})
3837

3938
it('handles single quoted empty string', function () {
4039
const args = tokenizeArgString('--foo \'\' --bar=\'\'')
41-
args[0].should.equal('--foo')
42-
args[1].should.equal("''")
43-
args[2].should.equal("--bar=''")
40+
strictEqual(args[0], '--foo')
41+
strictEqual(args[1], "''")
42+
strictEqual(args[2], "--bar=''")
4443
})
4544

4645
it('handles double quoted empty string', function () {
4746
const args = tokenizeArgString('--foo "" --bar=""')
48-
args[0].should.equal('--foo')
49-
args[1].should.equal('""')
50-
args[2].should.equal('--bar=""')
47+
strictEqual(args[0], '--foo')
48+
strictEqual(args[1], '""')
49+
strictEqual(args[2], '--bar=""')
5150
})
5251

5352
it('handles quoted string with embedded quotes', function () {
5453
var args = tokenizeArgString('--foo "hello \'world\'" --bar=\'foo "bar"\'')
55-
args[0].should.equal('--foo')
56-
args[1].should.equal('"hello \'world\'"')
57-
args[2].should.equal('--bar=\'foo "bar"\'')
54+
strictEqual(args[0], '--foo')
55+
strictEqual(args[1], '"hello \'world\'"')
56+
strictEqual(args[2], '--bar=\'foo "bar"\'')
5857
})
5958

6059
// https://github.com/yargs/yargs-parser/pull/100
6160
// https://github.com/yargs/yargs-parser/pull/106
6261
it('ignores unneeded spaces', function () {
6362
const args = tokenizeArgString(' foo bar "foo bar" ')
64-
args[0].should.equal('foo')
65-
expect(args[1]).equal('bar')
66-
expect(args[2]).equal('"foo bar"')
63+
strictEqual(args[0], 'foo')
64+
strictEqual(args[1], 'bar')
65+
strictEqual(args[2], '"foo bar"')
6766
})
6867

6968
it('handles boolean options', function () {
7069
const args = tokenizeArgString('--foo -bar')
71-
expect(args[0]).to.equal(('--foo'))
72-
expect(args[1]).to.equal(('-bar'))
70+
strictEqual(args[0], '--foo')
71+
strictEqual(args[1], '-bar')
7372
})
7473

7574
it('handles empty string', function () {
7675
const args = tokenizeArgString('')
77-
expect(args.length).to.equal(0)
76+
strictEqual(args.length, 0)
7877
})
7978

8079
it('handles array with unquoted string', function () {
8180
const args = tokenizeArgString(['--foo', '99'])
82-
args[0].should.equal('--foo')
83-
args[1].should.equal('99')
81+
strictEqual(args[0], '--foo')
82+
strictEqual(args[1], '99')
8483
})
8584

8685
it('handles array with quoted string with no spaces', function () {
8786
const args = tokenizeArgString(['--foo', "'hello'"])
88-
args[0].should.equal('--foo')
89-
args[1].should.equal("'hello'")
87+
strictEqual(args[0], '--foo')
88+
strictEqual(args[1], "'hello'")
9089
})
9190

9291
it('handles array with single quoted string with spaces', function () {
9392
const args = tokenizeArgString(['--foo', "'hello world'", "--bar='foo bar'"])
94-
args[0].should.equal('--foo')
95-
args[1].should.equal("'hello world'")
96-
args[2].should.equal("--bar='foo bar'")
93+
strictEqual(args[0], '--foo')
94+
strictEqual(args[1], "'hello world'")
95+
strictEqual(args[2], "--bar='foo bar'")
9796
})
9897

9998
it('handles array with double quoted string with spaces', function () {
10099
const args = tokenizeArgString(['--foo', '"hello world"', '--bar="foo bar"'])
101-
args[0].should.equal('--foo')
102-
args[1].should.equal('"hello world"')
103-
args[2].should.equal('--bar="foo bar"')
100+
strictEqual(args[0], '--foo')
101+
strictEqual(args[1], '"hello world"')
102+
strictEqual(args[2], '--bar="foo bar"')
104103
})
105104

106105
it('handles array with single quoted empty string', function () {
107106
const args = tokenizeArgString(['--foo', "''", "--bar=''"])
108-
args[0].should.equal('--foo')
109-
args[1].should.equal("''")
110-
args[2].should.equal("--bar=''")
107+
strictEqual(args[0], '--foo')
108+
strictEqual(args[1], "''")
109+
strictEqual(args[2], "--bar=''")
111110
})
112111

113112
it('handles array with double quoted empty string', function () {
114113
const args = tokenizeArgString(['--foo', '""', '--bar=""'])
115-
args[0].should.equal('--foo')
116-
args[1].should.equal('""')
117-
args[2].should.equal('--bar=""')
114+
strictEqual(args[0], '--foo')
115+
strictEqual(args[1], '""')
116+
strictEqual(args[2], '--bar=""')
118117
})
119118

120119
it('handles array with quoted string with embedded quotes', function () {
121120
var args = tokenizeArgString(['--foo', '"hello \'world\'"', '--bar=\'foo "bar"\''])
122-
args[0].should.equal('--foo')
123-
args[1].should.equal('"hello \'world\'"')
124-
args[2].should.equal('--bar=\'foo "bar"\'')
121+
strictEqual(args[0], '--foo')
122+
strictEqual(args[1], '"hello \'world\'"')
123+
strictEqual(args[2], '--bar=\'foo "bar"\'')
125124
})
126125

127126
it('handles array with boolean options', function () {
128127
const args = tokenizeArgString(['--foo', '-bar'])
129-
expect(args[0]).to.equal('--foo')
130-
expect(args[1]).to.equal('-bar')
128+
strictEqual(args[0], '--foo')
129+
strictEqual(args[1], '-bar')
131130
})
132131
})

0 commit comments

Comments
 (0)