-
Notifications
You must be signed in to change notification settings - Fork 722
/
common.js
115 lines (94 loc) · 3.32 KB
/
common.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
var shell = require('..');
var common = require('../src/common');
var assert = require('assert');
shell.config.silent = true;
shell.rm('-rf', 'tmp');
shell.mkdir('tmp');
//
// Invalids
//
// too few args
assert.throws(function () {
common.expand();
}, TypeError);
// should be a list
assert.throws(function () {
common.expand('resources');
}, TypeError);
//
// Valids
//
var result;
// single file, array syntax
result = common.expand(['resources/file1.txt']);
assert.equal(shell.error(), null);
assert.deepEqual(result, ['resources/file1.txt']);
// multiple file, glob syntax, * for file name
result = common.expand(['resources/file*.txt']);
assert.equal(shell.error(), null);
assert.deepEqual(result.sort(), ['resources/file1.txt', 'resources/file2.txt'].sort());
// multiple file, glob syntax, * for directory name
result = common.expand(['*/file*.txt']);
assert.equal(shell.error(), null);
assert.deepEqual(result.sort(), ['resources/file1.txt', 'resources/file2.txt'].sort());
// multiple file, glob syntax, ** for directory name
result = common.expand(['**/file*.js']);
assert.equal(shell.error(), null);
assert.deepEqual(result.sort(), ['resources/file1.js', 'resources/file2.js', 'resources/ls/file1.js', 'resources/ls/file2.js'].sort());
// broken links still expand
result = common.expand(['resources/b*dlink']);
assert.equal(shell.error(), null);
assert.deepEqual(result, ['resources/badlink']);
// common.parseOptions (normal case)
result = common.parseOptions('-Rf', {
'R': 'recursive',
'f': 'force',
'r': 'reverse'
});
assert.ok(result.recursive === true);
assert.ok(result.force === true);
assert.ok(result.reverse === false);
// common.parseOptions (with mutually-negating options)
result = common.parseOptions('-f', {
'n': 'no_force',
'f': '!no_force',
'R': 'recursive'
});
assert.ok(result.recursive === false);
assert.ok(result.no_force === false);
assert.ok(result.force === undefined); // this key shouldn't exist
// common.parseOptions (the last of the conflicting options should hold)
var options = {
'n': 'no_force',
'f': '!no_force',
'R': 'recursive'
};
result = common.parseOptions('-fn', options);
assert.ok(result.recursive === false);
assert.ok(result.no_force === true);
assert.ok(result.force === undefined); // this key shouldn't exist
result = common.parseOptions('-nf', options);
assert.ok(result.recursive === false);
assert.ok(result.no_force === false);
assert.ok(result.force === undefined); // this key shouldn't exist
// common.parseOptions using an object to hold options
result = common.parseOptions({ '-v': 'some text here' }, {
'v': 'value',
'f': 'force',
'r': 'reverse'
});
assert.ok(result.value === 'some text here');
assert.ok(result.force === false);
assert.ok(result.reverse === false);
// Some basic tests on the ShellString type
result = shell.ShellString('foo');
assert.strictEqual(result.toString(), 'foo');
assert.equal(result.stdout, 'foo');
assert.ok(typeof result.stderr === 'undefined');
assert.ok(result.to);
assert.ok(result.toEnd);
// Commands that fail will still output error messages to stderr
result = shell.exec(JSON.stringify(common.nodeBinPath) + ' -e "require(\'../global\'); ls(\'noexist\'); cd(\'noexist\');"');
assert.equal(result.stdout, '');
assert.equal(result.stderr, 'ls: no such file or directory: noexist\ncd: no such file or directory: noexist\n');
shell.exit(123);