Skip to content

Commit 3180224

Browse files
aorinevobcoe
authored andcommittedFeb 18, 2019
fix: add zsh script to files array
1 parent 0a96394 commit 3180224

File tree

2 files changed

+229
-217
lines changed

2 files changed

+229
-217
lines changed
 

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"lib",
1616
"locales",
1717
"completion.sh.hbs",
18+
"completion.zsh.hbs",
1819
"LICENSE"
1920
],
2021
"dependencies": {

‎test/completion.js

+228-217
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict'
2-
/* global describe, it, beforeEach */
2+
/* global describe, it, beforeEach, after */
33
const checkUsage = require('./helpers/utils').checkOutput
44
const yargs = require('../')
55

@@ -13,99 +13,11 @@ describe('Completion', () => {
1313
yargs.reset()
1414
})
1515

16-
describe('default completion behavior', () => {
17-
it('it returns a list of commands as completion suggestions (bash)', () => {
18-
process.env.SHELL = '/bin/bash'
19-
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', ''])
20-
.command('foo', 'bar')
21-
.command('apple', 'banana')
22-
.completion()
23-
.argv
24-
)
25-
26-
r.logs.should.include('apple')
27-
r.logs.should.include('foo')
28-
})
29-
30-
it('it returns a list of commands as completion suggestions (zsh)', () => {
31-
process.env.SHELL = '/bin/zsh'
32-
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', ''])
33-
.command('foo', 'bar')
34-
.command('apple', 'banana')
35-
.completion()
36-
.argv
37-
)
38-
39-
r.logs.should.include('apple:banana')
40-
r.logs.should.include('foo:bar')
41-
})
42-
43-
it('avoids interruption from command recommendations (bash)', () => {
44-
process.env.SHELL = '/bin/bash'
45-
const r = checkUsage(() =>
46-
yargs(['./completion', '--get-yargs-completions', './completion', 'a'])
47-
.command('apple', 'fruit')
48-
.command('aardvark', 'animal')
49-
.recommendCommands()
50-
.completion()
51-
.argv
52-
)
53-
54-
r.errors.should.deep.equal([])
55-
r.logs.should.include('apple')
56-
r.logs.should.include('aardvark')
57-
})
58-
59-
it('avoids interruption from command recommendations (zsh)', () => {
60-
process.env.SHELL = '/bin/zsh'
61-
const r = checkUsage(() =>
62-
yargs(['./completion', '--get-yargs-completions', './completion', 'a'])
63-
.command('apple', 'fruit')
64-
.command('aardvark', 'animal')
65-
.recommendCommands()
66-
.completion()
67-
.argv
68-
)
69-
70-
r.errors.should.deep.equal([])
71-
r.logs.should.include('apple:fruit')
72-
r.logs.should.include('aardvark:animal')
73-
})
74-
75-
it('avoids interruption from default command (bash)', () => {
76-
process.env.SHELL = '/bin/bash'
77-
const r = checkUsage(() =>
78-
yargs(['./usage', '--get-yargs-completions', './usage', ''])
79-
.usage('$0 [thing]', 'skipped', subYargs => {
80-
subYargs.command('aardwolf', 'is a thing according to google')
81-
})
82-
.command('aardvark', 'animal')
83-
.completion()
84-
.argv
85-
)
86-
87-
r.errors.should.deep.equal([])
88-
r.logs.should.not.include('aardwolf')
89-
r.logs.should.include('aardvark')
90-
})
91-
92-
it('avoids interruption from default command (zsh)', () => {
93-
process.env.SHELL = '/bin/zsh'
94-
const r = checkUsage(() =>
95-
yargs(['./usage', '--get-yargs-completions', './usage', ''])
96-
.usage('$0 [thing]', 'skipped', subYargs => {
97-
subYargs.command('aardwolf', 'is a thing according to google')
98-
})
99-
.command('aardvark', 'animal')
100-
.completion()
101-
.argv
102-
)
103-
104-
r.errors.should.deep.equal([])
105-
r.logs.should.not.include('aardwolf')
106-
r.logs.should.include('aardvark:animal')
107-
})
16+
after(() => {
17+
yargs.reset()
18+
})
10819

20+
describe('default completion behavior', () => {
10921
it('avoids repeating already included commands', () => {
11022
process.env.SHELL = '/bin/bash'
11123
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', 'apple'])
@@ -146,44 +58,6 @@ describe('Completion', () => {
14658
r.logs.should.not.include('--foo')
14759
})
14860

149-
it('completes options for a command (bash)', () => {
150-
process.env.SHELL = '/bin/bash'
151-
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', 'foo', '--b'])
152-
.command('foo', 'foo command', subYargs => subYargs.options({
153-
bar: {
154-
describe: 'bar option'
155-
}
156-
})
157-
.help(true)
158-
.version(false))
159-
.completion()
160-
.argv
161-
)
162-
163-
r.logs.should.have.length(2)
164-
r.logs.should.include('--bar')
165-
r.logs.should.include('--help')
166-
})
167-
168-
it('completes options for a command (zsh)', () => {
169-
process.env.SHELL = '/bin/zsh'
170-
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', 'foo', '--b'])
171-
.command('foo', 'foo command', subYargs => subYargs.options({
172-
bar: {
173-
describe: 'bar option'
174-
}
175-
})
176-
.help(true)
177-
.version(false))
178-
.completion()
179-
.argv
180-
)
181-
182-
r.logs.should.have.length(2)
183-
r.logs.should.include('--bar:bar option')
184-
r.logs.should.include('--help:Show help')
185-
})
186-
18761
it('completes options for the correct command', () => {
18862
process.env.SHELL = '/bin/bash'
18963
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', 'cmd2', '--o'])
@@ -271,24 +145,6 @@ describe('Completion', () => {
271145
})
272146

273147
describe('generateCompletionScript()', () => {
274-
it('replaces application variable with $0 in script (bash)', () => {
275-
process.env.SHELL = '/bin/bash'
276-
const r = checkUsage(() => yargs([])
277-
.showCompletionScript(), ['ndm'])
278-
279-
r.logs[0].should.match(/bashrc/)
280-
r.logs[0].should.match(/ndm --get-yargs-completions/)
281-
})
282-
283-
it('replaces application variable with $0 in script (zsh)', () => {
284-
process.env.SHELL = '/bin/zsh'
285-
const r = checkUsage(() => yargs([])
286-
.showCompletionScript(), ['ndm'])
287-
288-
r.logs[0].should.match(/zshrc/)
289-
r.logs[0].should.match(/ndm --get-yargs-completions/)
290-
})
291-
292148
it('replaces completion command variable with custom completion command in script', () => {
293149
const r = checkUsage(() => yargs([]).completion('flintlock')
294150
.showCompletionScript(), ['ndm'])
@@ -385,96 +241,251 @@ describe('Completion', () => {
385241
})
386242
})
387243

388-
describe('getCompletion() - bash', () => {
389-
it('returns default completion to callback', () => {
244+
describe('bash', () => {
245+
it('returns a list of commands as completion suggestions', () => {
390246
process.env.SHELL = '/bin/bash'
391-
const r = checkUsage(() => {
392-
yargs()
393-
.command('foo', 'bar')
394-
.command('apple', 'banana')
395-
.completion()
396-
.getCompletion([''], (completions) => {
397-
;(completions || []).forEach((completion) => {
398-
console.log(completion)
399-
})
400-
})
401-
})
247+
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', ''])
248+
.command('foo', 'bar')
249+
.command('apple', 'banana')
250+
.completion()
251+
.argv
252+
)
402253

403254
r.logs.should.include('apple')
404255
r.logs.should.include('foo')
405256
})
406-
})
407257

408-
describe('getCompletion() - zsh', () => {
409-
it('returns default completion to callback', () => {
410-
process.env.SHELL = '/bin/zsh'
411-
const r = checkUsage(() => {
412-
yargs()
413-
.command('foo', 'bar')
414-
.command('apple', 'banana')
258+
it('avoids interruption from command recommendations', () => {
259+
process.env.SHELL = '/bin/bash'
260+
const r = checkUsage(() =>
261+
yargs(['./completion', '--get-yargs-completions', './completion', 'a'])
262+
.command('apple', 'fruit')
263+
.command('aardvark', 'animal')
264+
.recommendCommands()
415265
.completion()
416-
.getCompletion([''], (completions) => {
417-
;(completions || []).forEach((completion) => {
418-
console.log(completion)
419-
})
266+
.argv
267+
)
268+
269+
r.errors.should.deep.equal([])
270+
r.logs.should.include('apple')
271+
r.logs.should.include('aardvark')
272+
})
273+
274+
it('avoids interruption from default command', () => {
275+
process.env.SHELL = '/bin/bash'
276+
const r = checkUsage(() =>
277+
yargs(['./usage', '--get-yargs-completions', './usage', ''])
278+
.usage('$0 [thing]', 'skipped', subYargs => {
279+
subYargs.command('aardwolf', 'is a thing according to google')
420280
})
281+
.command('aardvark', 'animal')
282+
.completion()
283+
.argv
284+
)
285+
286+
r.errors.should.deep.equal([])
287+
r.logs.should.not.include('aardwolf')
288+
r.logs.should.include('aardvark')
289+
})
290+
291+
it('completes options for a command', () => {
292+
process.env.SHELL = '/bin/bash'
293+
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', 'foo', '--b'])
294+
.command('foo', 'foo command', subYargs => subYargs.options({
295+
bar: {
296+
describe: 'bar option'
297+
}
298+
})
299+
.help(true)
300+
.version(false))
301+
.completion()
302+
.argv
303+
)
304+
305+
r.logs.should.have.length(2)
306+
r.logs.should.include('--bar')
307+
r.logs.should.include('--help')
308+
})
309+
310+
describe('generateCompletionScript()', () => {
311+
it('replaces application variable with $0 in script', () => {
312+
process.env.SHELL = '/bin/bash'
313+
const r = checkUsage(() => yargs([])
314+
.showCompletionScript(), ['ndm'])
315+
316+
r.logs[0].should.match(/bashrc/)
317+
r.logs[0].should.match(/ndm --get-yargs-completions/)
421318
})
319+
})
320+
321+
// fixes for #177.
322+
it('does not apply validation when --get-yargs-completions is passed in', () => {
323+
process.env.SHELL = '/bin/bash'
324+
const r = checkUsage(() => {
325+
try {
326+
return yargs(['./completion', '--get-yargs-completions', '--'])
327+
.option('foo', {})
328+
.completion()
329+
.strict()
330+
.argv
331+
} catch (e) {
332+
console.log(e.message)
333+
}
334+
})
335+
336+
r.errors.length.should.equal(0)
337+
r.logs.should.include('--foo')
338+
})
339+
340+
describe('getCompletion()', () => {
341+
it('returns default completion to callback', () => {
342+
process.env.SHELL = '/bin/bash'
343+
const r = checkUsage(() => {
344+
yargs()
345+
.command('foo', 'bar')
346+
.command('apple', 'banana')
347+
.completion()
348+
.getCompletion([''], (completions) => {
349+
;(completions || []).forEach((completion) => {
350+
console.log(completion)
351+
})
352+
})
353+
})
354+
355+
r.logs.should.include('apple')
356+
r.logs.should.include('foo')
357+
})
358+
})
359+
})
360+
361+
describe('zsh', () => {
362+
it('it returns a list of commands as completion suggestions', () => {
363+
process.env.SHELL = '/bin/zsh'
364+
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', ''])
365+
.command('foo', 'bar')
366+
.command('apple', 'banana')
367+
.completion()
368+
.argv
369+
)
422370

423371
r.logs.should.include('apple:banana')
424372
r.logs.should.include('foo:bar')
425373
})
426-
})
427374

428-
// fixes for #177.
429-
it('does not apply validation when --get-yargs-completions is passed in (bash)', () => {
430-
process.env.SHELL = '/bin/bash'
431-
const r = checkUsage(() => {
432-
try {
433-
return yargs(['./completion', '--get-yargs-completions', '--'])
434-
.option('foo', {})
375+
it('avoids interruption from command recommendations', () => {
376+
process.env.SHELL = '/bin/zsh'
377+
const r = checkUsage(() =>
378+
yargs(['./completion', '--get-yargs-completions', './completion', 'a'])
379+
.command('apple', 'fruit')
380+
.command('aardvark', 'animal')
381+
.recommendCommands()
435382
.completion()
436-
.strict()
437383
.argv
438-
} catch (e) {
439-
console.log(e.message)
440-
}
384+
)
385+
386+
r.errors.should.deep.equal([])
387+
r.logs.should.include('apple:fruit')
388+
r.logs.should.include('aardvark:animal')
441389
})
442390

443-
r.errors.length.should.equal(0)
444-
r.logs.should.include('--foo')
445-
})
446-
it('does not apply validation when --get-yargs-completions is passed in (zsh)', () => {
447-
process.env.SHELL = '/bin/zsh'
448-
const r = checkUsage(() => {
449-
try {
450-
return yargs(['./completion', '--get-yargs-completions', '--'])
451-
.option('foo', { 'describe': 'bar' })
391+
it('avoids interruption from default command', () => {
392+
process.env.SHELL = '/bin/zsh'
393+
const r = checkUsage(() =>
394+
yargs(['./usage', '--get-yargs-completions', './usage', ''])
395+
.usage('$0 [thing]', 'skipped', subYargs => {
396+
subYargs.command('aardwolf', 'is a thing according to google')
397+
})
398+
.command('aardvark', 'animal')
452399
.completion()
453-
.strict()
454400
.argv
455-
} catch (e) {
456-
console.log(e.message)
457-
}
401+
)
402+
403+
r.errors.should.deep.equal([])
404+
r.logs.should.not.include('aardwolf')
405+
r.logs.should.include('aardvark:animal')
458406
})
459407

460-
r.errors.length.should.equal(0)
461-
r.logs.should.include('--foo:bar')
462-
})
463-
it('when using subcommands ensure early bailout if full command is typed (zsh)', () => {
464-
process.env.SHELL = '/bin/zsh'
465-
const r = checkUsage(() => {
466-
try {
467-
return yargs(['./completion', '--get-yargs-completions', 'dream'])
468-
.commandDir('./fixtures/cmddir', { 'recurse': true })
469-
.demand(1)
470-
.strict()
471-
.completion()
472-
.argv
473-
} catch (e) {
474-
console.log(e.message)
475-
}
408+
it('completes options for a command', () => {
409+
process.env.SHELL = '/bin/zsh'
410+
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', 'foo', '--b'])
411+
.command('foo', 'foo command', subYargs => subYargs.options({
412+
bar: {
413+
describe: 'bar option'
414+
}
415+
})
416+
.help(true)
417+
.version(false))
418+
.completion()
419+
.argv
420+
)
421+
422+
r.logs.should.have.length(2)
423+
r.logs.should.include('--bar:bar option')
424+
r.logs.should.include('--help:Show help')
425+
})
426+
427+
it('replaces application variable with $0 in script', () => {
428+
process.env.SHELL = '/bin/zsh'
429+
const r = checkUsage(() => yargs([])
430+
.showCompletionScript(), ['ndm'])
431+
432+
r.logs[0].should.match(/zshrc/)
433+
r.logs[0].should.match(/ndm --get-yargs-completions/)
434+
})
435+
436+
describe('getCompletion()', () => {
437+
it('returns default completion to callback', () => {
438+
process.env.SHELL = '/bin/zsh'
439+
const r = checkUsage(() => {
440+
yargs()
441+
.command('foo', 'bar')
442+
.command('apple', 'banana')
443+
.completion()
444+
.getCompletion([''], (completions) => {
445+
;(completions || []).forEach((completion) => {
446+
console.log(completion)
447+
})
448+
})
449+
})
450+
451+
r.logs.should.include('apple:banana')
452+
r.logs.should.include('foo:bar')
453+
})
454+
})
455+
456+
it('does not apply validation when --get-yargs-completions is passed in', () => {
457+
process.env.SHELL = '/bin/zsh'
458+
const r = checkUsage(() => {
459+
try {
460+
return yargs(['./completion', '--get-yargs-completions', '--'])
461+
.option('foo', { 'describe': 'bar' })
462+
.completion()
463+
.strict()
464+
.argv
465+
} catch (e) {
466+
console.log(e.message)
467+
}
468+
})
469+
470+
r.errors.length.should.equal(0)
471+
r.logs.should.include('--foo:bar')
472+
})
473+
it('when using subcommands ensure early bailout if full command is typed', () => {
474+
process.env.SHELL = '/bin/zsh'
475+
const r = checkUsage(() => {
476+
try {
477+
return yargs(['./completion', '--get-yargs-completions', 'dream'])
478+
.commandDir('./fixtures/cmddir', { 'recurse': true })
479+
.demand(1)
480+
.strict()
481+
.completion()
482+
.argv
483+
} catch (e) {
484+
console.log(e.message)
485+
}
486+
})
487+
r.errors.length.should.equal(0)
488+
r.logs.should.include('of-memory:Dream about a specific memory')
476489
})
477-
r.errors.length.should.equal(0)
478-
r.logs.should.include('of-memory:Dream about a specific memory')
479490
})
480491
})

0 commit comments

Comments
 (0)
Please sign in to comment.