Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50665 views
1
var Hash = require('hashish');
2
var optimist = require('../index');
3
var assert = require('assert');
4
5
exports.usageFail = function () {
6
var r = checkUsage(function () {
7
return optimist('-x 10 -z 20'.split(' '))
8
.usage('Usage: $0 -x NUM -y NUM')
9
.demand(['x','y'])
10
.argv;
11
});
12
assert.deepEqual(
13
r.result,
14
{ x : 10, z : 20, _ : [], $0 : './usage' }
15
);
16
17
assert.deepEqual(
18
r.errors.join('\n').split(/\n+/),
19
[
20
'Usage: ./usage -x NUM -y NUM',
21
'Options:',
22
' -x [required]',
23
' -y [required]',
24
'Missing required arguments: y',
25
]
26
);
27
assert.deepEqual(r.logs, []);
28
assert.ok(r.exit);
29
};
30
31
exports.usagePass = function () {
32
var r = checkUsage(function () {
33
return optimist('-x 10 -y 20'.split(' '))
34
.usage('Usage: $0 -x NUM -y NUM')
35
.demand(['x','y'])
36
.argv;
37
});
38
assert.deepEqual(r, {
39
result : { x : 10, y : 20, _ : [], $0 : './usage' },
40
errors : [],
41
logs : [],
42
exit : false,
43
});
44
};
45
46
exports.checkPass = function () {
47
var r = checkUsage(function () {
48
return optimist('-x 10 -y 20'.split(' '))
49
.usage('Usage: $0 -x NUM -y NUM')
50
.check(function (argv) {
51
if (!('x' in argv)) throw 'You forgot about -x';
52
if (!('y' in argv)) throw 'You forgot about -y';
53
})
54
.argv;
55
});
56
assert.deepEqual(r, {
57
result : { x : 10, y : 20, _ : [], $0 : './usage' },
58
errors : [],
59
logs : [],
60
exit : false,
61
});
62
};
63
64
exports.checkFail = function () {
65
var r = checkUsage(function () {
66
return optimist('-x 10 -z 20'.split(' '))
67
.usage('Usage: $0 -x NUM -y NUM')
68
.check(function (argv) {
69
if (!('x' in argv)) throw 'You forgot about -x';
70
if (!('y' in argv)) throw 'You forgot about -y';
71
})
72
.argv;
73
});
74
75
assert.deepEqual(
76
r.result,
77
{ x : 10, z : 20, _ : [], $0 : './usage' }
78
);
79
80
assert.deepEqual(
81
r.errors.join('\n').split(/\n+/),
82
[
83
'Usage: ./usage -x NUM -y NUM',
84
'You forgot about -y'
85
]
86
);
87
88
assert.deepEqual(r.logs, []);
89
assert.ok(r.exit);
90
};
91
92
exports.checkCondPass = function () {
93
function checker (argv) {
94
return 'x' in argv && 'y' in argv;
95
}
96
97
var r = checkUsage(function () {
98
return optimist('-x 10 -y 20'.split(' '))
99
.usage('Usage: $0 -x NUM -y NUM')
100
.check(checker)
101
.argv;
102
});
103
assert.deepEqual(r, {
104
result : { x : 10, y : 20, _ : [], $0 : './usage' },
105
errors : [],
106
logs : [],
107
exit : false,
108
});
109
};
110
111
exports.checkCondFail = function () {
112
function checker (argv) {
113
return 'x' in argv && 'y' in argv;
114
}
115
116
var r = checkUsage(function () {
117
return optimist('-x 10 -z 20'.split(' '))
118
.usage('Usage: $0 -x NUM -y NUM')
119
.check(checker)
120
.argv;
121
});
122
123
assert.deepEqual(
124
r.result,
125
{ x : 10, z : 20, _ : [], $0 : './usage' }
126
);
127
128
assert.deepEqual(
129
r.errors.join('\n').split(/\n+/).join('\n'),
130
'Usage: ./usage -x NUM -y NUM\n'
131
+ 'Argument check failed: ' + checker.toString()
132
);
133
134
assert.deepEqual(r.logs, []);
135
assert.ok(r.exit);
136
};
137
138
exports.countPass = function () {
139
var r = checkUsage(function () {
140
return optimist('1 2 3 --moo'.split(' '))
141
.usage('Usage: $0 [x] [y] [z] {OPTIONS}')
142
.demand(3)
143
.argv;
144
});
145
assert.deepEqual(r, {
146
result : { _ : [ '1', '2', '3' ], moo : true, $0 : './usage' },
147
errors : [],
148
logs : [],
149
exit : false,
150
});
151
};
152
153
exports.countFail = function () {
154
var r = checkUsage(function () {
155
return optimist('1 2 --moo'.split(' '))
156
.usage('Usage: $0 [x] [y] [z] {OPTIONS}')
157
.demand(3)
158
.argv;
159
});
160
assert.deepEqual(
161
r.result,
162
{ _ : [ '1', '2' ], moo : true, $0 : './usage' }
163
);
164
165
assert.deepEqual(
166
r.errors.join('\n').split(/\n+/),
167
[
168
'Usage: ./usage [x] [y] [z] {OPTIONS}',
169
'Not enough non-option arguments: got 2, need at least 3',
170
]
171
);
172
173
assert.deepEqual(r.logs, []);
174
assert.ok(r.exit);
175
};
176
177
exports.defaultSingles = function () {
178
var r = checkUsage(function () {
179
return optimist('--foo 50 --baz 70 --powsy'.split(' '))
180
.default('foo', 5)
181
.default('bar', 6)
182
.default('baz', 7)
183
.argv
184
;
185
});
186
assert.eql(r.result, {
187
foo : '50',
188
bar : 6,
189
baz : '70',
190
powsy : true,
191
_ : [],
192
$0 : './usage',
193
});
194
};
195
196
exports.defaultHash = function () {
197
var r = checkUsage(function () {
198
return optimist('--foo 50 --baz 70'.split(' '))
199
.default({ foo : 10, bar : 20, quux : 30 })
200
.argv
201
;
202
});
203
assert.eql(r.result, {
204
foo : '50',
205
bar : 20,
206
baz : 70,
207
quux : 30,
208
_ : [],
209
$0 : './usage',
210
});
211
};
212
213
exports.rebase = function () {
214
assert.equal(
215
optimist.rebase('/home/substack', '/home/substack/foo/bar/baz'),
216
'./foo/bar/baz'
217
);
218
assert.equal(
219
optimist.rebase('/home/substack/foo/bar/baz', '/home/substack'),
220
'../../..'
221
);
222
assert.equal(
223
optimist.rebase('/home/substack/foo', '/home/substack/pow/zoom.txt'),
224
'../pow/zoom.txt'
225
);
226
};
227
228
function checkUsage (f) {
229
var _process = process;
230
process = Hash.copy(process);
231
var exit = false;
232
process.exit = function () { exit = true };
233
process.env = Hash.merge(process.env, { _ : 'node' });
234
process.argv = [ './usage' ];
235
236
var errors = [];
237
var logs = [];
238
239
console._error = console.error;
240
console.error = function (msg) { errors.push(msg) };
241
console._log = console.log;
242
console.log = function (msg) { logs.push(msg) };
243
244
var result = f();
245
246
process = _process;
247
console.error = console._error;
248
console.log = console._log;
249
250
return {
251
errors : errors,
252
logs : logs,
253
exit : exit,
254
result : result,
255
};
256
};
257
258