forked from tj/commander.js
/
options.optsWithGlobals.test.js
115 lines (98 loc) · 3.85 KB
/
options.optsWithGlobals.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const commander = require('../');
// Testing optsWithGlobals and getOptionValueSourceWithGlobals with focus on globals.
describe('optsWithGlobals', () => {
test('when variety of options used with program then opts is same as optsWithGlobals', () => {
const program = new commander.Command();
program
.option('-b, --boolean')
.option('-r, --require-value <value)')
.option('-f, --float <value>', 'description', parseFloat)
.option('-d, --default-value <value)', 'description', 'default value')
.option('-n, --no-something');
program.parse(['-b', '-r', 'req', '-f', '1e2'], { from: 'user' });
expect(program.opts()).toEqual(program.optsWithGlobals());
});
test('when options in sub and program then optsWithGlobals includes both', () => {
const program = new commander.Command();
let mergedOptions;
program
.option('-g, --global <value>');
program
.command('sub')
.option('-l, --local <value)')
.action((options, cmd) => {
mergedOptions = cmd.optsWithGlobals();
});
program.parse(['-g', 'GGG', 'sub', '-l', 'LLL'], { from: 'user' });
expect(mergedOptions).toEqual({ global: 'GGG', local: 'LLL' });
});
test('when options in sub and subsub then optsWithGlobals includes both', () => {
const program = new commander.Command();
let mergedOptions;
program
.command('sub')
.option('-g, --global <value)')
.command('subsub')
.option('-l, --local <value)')
.action((options, cmd) => {
mergedOptions = cmd.optsWithGlobals();
});
program.parse(['sub', '-g', 'GGG', 'subsub', '-l', 'LLL'], { from: 'user' });
expect(mergedOptions).toEqual({ global: 'GGG', local: 'LLL' });
});
test('when same named option in sub and program then optsWithGlobals includes global', () => {
const program = new commander.Command();
let mergedOptions;
program
.option('-c, --common <value>')
.enablePositionalOptions();
program
.command('sub')
.option('-c, --common <value)')
.action((options, cmd) => {
mergedOptions = cmd.optsWithGlobals();
});
program.parse(['-c', 'GGG', 'sub', '-c', 'LLL'], { from: 'user' });
expect(mergedOptions).toEqual({ common: 'GGG' });
});
});
describe('getOptionValueSourceWithGlobals', () => {
test('when option used with simple command then source is defined', () => {
const program = new commander.Command();
program
.option('-g, --global');
program.parse(['-g'], { from: 'user' });
expect(program.getOptionValueSourceWithGlobals('global')).toEqual('cli');
});
test('when option used with program then source is defined', () => {
const program = new commander.Command();
program
.option('-g, --global');
const sub = program.command('sub')
.option('-l, --local')
.action(() => {});
program.parse(['sub', '-g'], { from: 'user' });
expect(sub.getOptionValueSourceWithGlobals('global')).toEqual('cli');
});
test('when option used with subcommand then source is defined', () => {
const program = new commander.Command();
program
.option('-g, --global');
const sub = program.command('sub')
.option('-l, --local')
.action(() => {});
program.parse(['sub', '-l'], { from: 'user' });
expect(sub.getOptionValueSourceWithGlobals('local')).toEqual('cli');
});
test('when same named option in sub and program then source is defined by global', () => {
const program = new commander.Command();
program
.enablePositionalOptions()
.option('-c, --common <value>', 'description', 'default value');
const sub = program.command('sub')
.option('-c, --common <value>')
.action(() => {});
program.parse(['sub', '--common', 'value'], { from: 'user' });
expect(sub.getOptionValueSourceWithGlobals('common')).toEqual('default');
});
});