-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathparse-options.test.js
More file actions
248 lines (209 loc) · 7.09 KB
/
parse-options.test.js
File metadata and controls
248 lines (209 loc) · 7.09 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* @overview Contains unit tests for the functionality to parse options.
* @license MIT
*/
import { testProp } from "@fast-check/ava";
import test from "ava";
import * as fc from "fast-check";
import * as sinon from "sinon";
import { resolveExecutable } from "../../../src/internal/executables.js";
import { noShell, parseOptions } from "../../../src/internal/options.js";
import { arbitrary } from "./_.js";
const arbitraryInput = () =>
fc
.tuple(arbitrary.env(), arbitrary.shescapeOptions(), arbitrary.semver())
.map(([env, options, version]) => {
options ||= {};
version = `v${version}`;
return { env, options, version };
});
test.beforeEach((t) => {
const getDefaultShell = sinon.stub();
const getShellName = sinon.stub();
const isShellSupported = sinon.stub();
isShellSupported.returns(true);
t.context.deps = { getDefaultShell, getShellName, isShellSupported };
});
testProp("flag protection not specified", [arbitraryInput()], (t, args) => {
delete args.options.flagProtection;
const result = parseOptions(args, t.context.deps);
t.is(result.flagProtection, true);
});
testProp("flag protection set to true", [arbitraryInput()], (t, args) => {
args.options.flagProtection = true;
const result = parseOptions(args, t.context.deps);
t.is(result.flagProtection, true);
});
testProp("flag protection set to false", [arbitraryInput()], (t, args) => {
args.options.flagProtection = false;
const result = parseOptions(args, t.context.deps);
t.is(result.flagProtection, false);
});
testProp(
"shell is false",
[arbitraryInput(), fc.constantFrom(false)],
(t, args, providedShell) => {
args.options.shell = providedShell;
const result = parseOptions(args, t.context.deps);
t.is(t.context.deps.getDefaultShell.callCount, 0);
t.is(t.context.deps.getShellName.callCount, 0);
t.is(result.shellName, noShell);
},
);
testProp(
"shell is omitted",
[arbitraryInput(), fc.string(), fc.string()],
(t, args, defaultShell, shellName) => {
t.context.deps.getDefaultShell.resetHistory();
t.context.deps.getDefaultShell.returns(defaultShell);
t.context.deps.getShellName.resetHistory();
t.context.deps.getShellName.returns(shellName);
const env = args.env;
delete args.options.shell;
const result = parseOptions(args, t.context.deps);
t.is(t.context.deps.getDefaultShell.callCount, 1);
t.true(t.context.deps.getDefaultShell.calledWithExactly({ env }));
t.is(t.context.deps.getShellName.callCount, 1);
t.true(
t.context.deps.getShellName.calledWithExactly(
{ env, shell: defaultShell },
{ resolveExecutable },
),
);
t.is(result.shellName, shellName);
},
);
testProp(
"shell is nil",
[
arbitraryInput(),
fc.constantFrom(null, undefined),
fc.string(),
fc.string(),
],
(t, args, providedShell, defaultShell, shellName) => {
t.context.deps.getDefaultShell.resetHistory();
t.context.deps.getDefaultShell.returns(defaultShell);
t.context.deps.getShellName.resetHistory();
t.context.deps.getShellName.returns(shellName);
const env = args.env;
args.options.shell = providedShell;
const result = parseOptions(args, t.context.deps);
t.is(t.context.deps.getDefaultShell.callCount, 1);
t.true(t.context.deps.getDefaultShell.calledWithExactly({ env }));
t.is(t.context.deps.getShellName.callCount, 1);
t.true(
t.context.deps.getShellName.calledWithExactly(
{ env, shell: defaultShell },
{ resolveExecutable },
),
);
t.is(result.shellName, shellName);
},
);
testProp(
"shell is truthy",
[arbitraryInput(), fc.constantFrom(true), fc.string(), fc.string()],
(t, args, providedShell, defaultShell, shellName) => {
t.context.deps.getDefaultShell.resetHistory();
t.context.deps.getDefaultShell.returns(defaultShell);
t.context.deps.getShellName.resetHistory();
t.context.deps.getShellName.returns(shellName);
const env = args.env;
args.options.shell = providedShell;
const result = parseOptions(args, t.context.deps);
t.is(t.context.deps.getDefaultShell.callCount, 1);
t.true(t.context.deps.getDefaultShell.calledWithExactly({ env }));
t.is(t.context.deps.getShellName.callCount, 1);
t.true(
t.context.deps.getShellName.calledWithExactly(
{ env, shell: defaultShell },
{ resolveExecutable },
),
);
t.is(result.shellName, shellName);
},
);
testProp(
"shell is specified",
[arbitraryInput(), fc.string(), fc.string()],
(t, args, providedShell, shellName) => {
t.context.deps.getShellName.resetHistory();
t.context.deps.getShellName.returns(shellName);
const env = args.env;
args.options.shell = providedShell;
const result = parseOptions(args, t.context.deps);
t.is(t.context.deps.getDefaultShell.callCount, 0);
t.is(t.context.deps.getShellName.callCount, 1);
t.true(
t.context.deps.getShellName.calledWithExactly(
{ env, shell: providedShell },
{ resolveExecutable },
),
);
t.is(result.shellName, shellName);
},
);
testProp(
"shell is inherited (before Node.js v22)",
[arbitraryInput(), fc.string(), arbitrary.semver({ maxMajor: 21 })],
(t, args, inheritedShell, version) => {
t.context.deps.getShellName.resetHistory();
delete args.options.shell;
args.version = `v${version}`;
args.options = Object.assign(
Object.create({ shell: inheritedShell }),
args.options,
);
parseOptions(args, t.context.deps);
t.is(t.context.deps.getShellName.callCount, 1);
t.true(
t.context.deps.getShellName.calledWith(
sinon.match({ shell: inheritedShell }),
),
);
},
);
testProp(
"shell is not inherited (Node.js v22 or later)",
[arbitraryInput(), fc.string(), arbitrary.semver({ minMajor: 22 })],
(t, args, inheritedShell, version) => {
t.context.deps.getShellName.resetHistory();
delete args.options.shell;
args.version = `v${version}`;
args.options = Object.assign(
Object.create({ shell: inheritedShell }),
args.options,
);
parseOptions(args, t.context.deps);
t.is(t.context.deps.getShellName.callCount, 1);
t.true(
t.context.deps.getShellName.calledWith(sinon.match({ shell: undefined })),
);
},
);
testProp("shell is supported", [arbitraryInput()], (t, args) => {
t.context.deps.isShellSupported.resetHistory();
t.context.deps.isShellSupported.returns(true);
t.notThrows(() => parseOptions(args, t.context.deps));
t.is(t.context.deps.isShellSupported.callCount, 1);
});
testProp(
"shell is unsupported",
[
arbitraryInput(),
fc.oneof(fc.string(), fc.constantFrom(true, null, undefined)),
fc.string(),
],
(t, args, providedShell, shellName) => {
t.context.deps.isShellSupported.resetHistory();
t.context.deps.isShellSupported.returns(false);
t.context.deps.getShellName.returns(shellName);
args.options.shell = providedShell;
t.throws(() => parseOptions(args, t.context.deps), {
instanceOf: Error,
message: `Shescape does not support the shell ${shellName}`,
});
t.is(t.context.deps.isShellSupported.callCount, 1);
},
);