/
command.parse.test.js
166 lines (146 loc) · 5.69 KB
/
command.parse.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const commander = require('../');
// Testing some Electron conventions but not directly using Electron to avoid overheads.
// https://github.com/electron/electron/issues/4690#issuecomment-217435222
// https://www.electronjs.org/docs/api/process#processdefaultapp-readonly
describe('.parse() args from', () => {
test('when no args then use process.argv and app/script/args', () => {
const program = new commander.Command();
const hold = process.argv;
process.argv = 'node script.js user'.split(' ');
program.parse();
process.argv = hold;
expect(program.args).toEqual(['user']);
});
test('when no args and electron properties and not default app then use process.argv and app/args', () => {
const program = new commander.Command();
const holdArgv = process.argv;
process.versions.electron = '1.2.3';
process.argv = 'node user'.split(' ');
program.parse();
delete process.versions.electron;
process.argv = holdArgv;
expect(program.args).toEqual(['user']);
});
test('when args then app/script/args', () => {
const program = new commander.Command();
program.parse('node script.js user'.split(' '));
expect(program.args).toEqual(['user']);
});
test('when args from "node" then app/script/args', () => {
const program = new commander.Command();
program.parse('node script.js user'.split(' '), { from: 'node' });
expect(program.args).toEqual(['user']);
});
test('when args from "electron" and not default app then app/args', () => {
const program = new commander.Command();
const hold = process.defaultApp;
process.defaultApp = undefined;
program.parse('customApp user'.split(' '), { from: 'electron' });
process.defaultApp = hold;
expect(program.args).toEqual(['user']);
});
test('when args from "electron" and default app then app/script/args', () => {
const program = new commander.Command();
const hold = process.defaultApp;
process.defaultApp = true;
program.parse('electron script user'.split(' '), { from: 'electron' });
process.defaultApp = hold;
expect(program.args).toEqual(['user']);
});
test('when args from "user" then args', () => {
const program = new commander.Command();
program.parse('user'.split(' '), { from: 'user' });
expect(program.args).toEqual(['user']);
});
test('when args from "silly" then throw', () => {
const program = new commander.Command();
expect(() => {
program.parse(['node', 'script.js'], { from: 'silly' });
}).toThrow();
});
});
describe('return type', () => {
test('when call .parse then returns program', () => {
const program = new commander.Command();
program
.action(() => { });
const result = program.parse(['node', 'test']);
expect(result).toBe(program);
});
test('when await .parseAsync then returns program', async() => {
const program = new commander.Command();
program
.action(() => { });
const result = await program.parseAsync(['node', 'test']);
expect(result).toBe(program);
});
});
// Easy mistake to make when writing unit tests
test('when parse strings instead of array then throw', () => {
const program = new commander.Command();
expect(() => {
program.parse('node', 'test');
}).toThrow();
});
describe('parse parameter is treated as readonly, per TypeScript declaration', () => {
test('when parse called then parameter does not change', () => {
const program = new commander.Command();
program.option('--debug');
const original = ['node', '--debug', 'arg'];
const param = original.slice();
program.parse(param);
expect(param).toEqual(original);
});
test('when parse called and parsed args later changed then parameter does not change', () => {
const program = new commander.Command();
program.option('--debug');
const original = ['node', '--debug', 'arg'];
const param = original.slice();
program.parse(param);
program.args.length = 0;
program.rawArgs.length = 0;
expect(param).toEqual(original);
});
test('when parse called and param later changed then parsed args do not change', () => {
const program = new commander.Command();
program.option('--debug');
const param = ['node', '--debug', 'arg'];
program.parse(param);
const oldArgs = program.args.slice();
const oldRawArgs = program.rawArgs.slice();
param.length = 0;
expect(program.args).toEqual(oldArgs);
expect(program.rawArgs).toEqual(oldRawArgs);
});
});
describe('parseAsync parameter is treated as readonly, per TypeScript declaration', () => {
test('when parse called then parameter does not change', async() => {
const program = new commander.Command();
program.option('--debug');
const original = ['node', '--debug', 'arg'];
const param = original.slice();
await program.parseAsync(param);
expect(param).toEqual(original);
});
test('when parseAsync called and parsed args later changed then parameter does not change', async() => {
const program = new commander.Command();
program.option('--debug');
const original = ['node', '--debug', 'arg'];
const param = original.slice();
await program.parseAsync(param);
program.args.length = 0;
program.rawArgs.length = 0;
expect(param).toEqual(original);
});
test('when parseAsync called and param later changed then parsed args do not change', async() => {
const program = new commander.Command();
program.option('--debug');
const param = ['node', '--debug', 'arg'];
await program.parseAsync(param);
const oldArgs = program.args.slice();
const oldRawArgs = program.rawArgs.slice();
param.length = 0;
expect(program.args).toEqual(oldArgs);
expect(program.rawArgs).toEqual(oldRawArgs);
});
});