-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
133 lines (118 loc) · 4.05 KB
/
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
/* globals describe, it, open */
/*
* Using ShellJS Plugins:
*
* To learn how to use a ShellJS plugin, look for the comments following the
* slash-star/star-slash syntax.
*/
/*
* Load all your plugins first:
* You can load a plugin with either of the following syntaxes:
* `require('shelljs-plugin-open');` or
* `var pluginOpen = require('shelljs-plugin-open');`
*
* Here, we've opted to save it in a variable, allowing us the option to use the
* bare function in addition to shell.open() (which includes helpful ShellJS
* features)
*/
var pluginOpen = require('..');
// If we were using additional plugins, we could load them here
/*
* After that, you must load in ShellJS itself, either locally (recommended) or
* globally (which still supported). For the purposes of testing this plugin,
* we're actually testing both ways of importing ShellJS (but you would never
* want to use ShellJS like this normally):
*/
var shell = require('shelljs'); // recommended
require('shelljs/global'); // not recommended
/*
* Now, require whichever other modules you want to require:
*/
require('should');
var assert = require('assert');
// override console.error() to cover up common.error() calls
console.error = function () { };
describe('plugin-open', function () {
it('gets added to the shelljs instance', function () {
/*
* Plugins automatically add new commands to the ShellJS instance, such as
* shell.open()
*/
shell.open.should.be.type('function');
});
it('gets added to the global namespace for shelljs/global', function () {
/*
* Plugins are also compatible with using require('shelljs/global');
*/
global.open.should.be.type('function');
global.open.should.equal(shell.open);
});
it('does not override other commands or methods', function () {
/*
* Plugins shouldn't interfere with existing commands
*/
shell.cp.should.be.type('function');
shell.mv.should.be.type('function');
shell.ls().should.have.property('toEnd');
shell.ls().should.have.property('grep');
shell.ls().should.have.property('sed');
});
it('exports the plugin implementation', function () {
/*
* A plugin author can also export the implementation of their commands
*/
pluginOpen.should.be.type('object');
pluginOpen.should.have.property('open');
pluginOpen.open.should.be.type('function');
});
it('does not accept options/flags', function () {
/*
* Plugins are an easy way of specifying what options/flags your command
* supports
*/
var ret = shell.open('-f', 'test');
ret.code.should.equal(1);
ret.stdout.should.equal('');
var errorMsg = 'open: option not recognized: f';
ret.stderr.should.equal(errorMsg);
shell.error().should.equal(errorMsg);
});
it('cannot open files that are missing', function () {
var ret = shell.open('missingFile.txt');
ret.code.should.equal(1);
ret.stdout.should.equal('');
var errorMsg = 'open: Unable to locate file: missingFile.txt';
ret.stderr.should.equal(errorMsg);
shell.error().should.equal(errorMsg);
ret = open('missingFile.txt');
ret.code.should.equal(1);
ret.stdout.should.equal('');
ret.stderr.should.equal(errorMsg);
shell.error().should.equal(errorMsg);
});
it('opens URLs', function () {
var ret = shell.open('https://www.google.com');
ret.code.should.equal(0);
assert.ok(!ret.stderr);
assert.ok(!shell.error());
});
it('opens files asynchronously', function () {
var ret = shell.open('test/');
// if this gets to this point, it must have been asynchronous
ret.code.should.equal(0);
ret.stdout.should.equal('');
assert.ok(!ret.stderr);
assert.ok(!shell.error());
});
it('handles glob characters', function () {
/*
* Plugins can easily take advantage of ShellJS's built-in glob expansion.
* This is indicated by the globStart option
*/
var ret = shell.open('te?t/*st.js');
ret.code.should.equal(0);
ret.stdout.should.equal('');
assert.ok(!ret.stderr);
assert.ok(!shell.error());
});
});