-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
146 lines (127 loc) · 4.16 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
134
135
136
137
138
139
140
141
142
143
144
145
146
/* globals describe, it, beforeEach, afterEach */
var pluginSleep = require('..');
/* eslint-disable import/order */
var shell = require('shelljs');
require('should');
function assertApproxEqual(val1, val2, opts) {
opts = opts || {};
var epsilon = opts.epsilon || 20;
if (process.env.CI) epsilon *= 20; // CI tends to run slower
var diff = Math.abs(val1 - val2);
try {
(diff <= epsilon).should.be.ok();
} catch (e) {
var msg = 'took ' + val1 + ' milliseconds but expected ' + val2 + '\u00B1'
+ epsilon + ' milliseconds';
throw new Error(msg);
}
}
describe('plugin-sleep', function () {
var oldConsoleError;
beforeEach(function () {
// override console.error() to cover up common.error() calls
oldConsoleError = console.error;
console.error = function () { };
});
afterEach(function () {
console.error = oldConsoleError;
});
it('gets added to the shelljs instance', function () {
shell.sleep.should.be.type('function');
});
it('does not override other commands or methods', function () {
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
*/
pluginSleep.should.be.type('object');
pluginSleep.should.have.property('sleep');
pluginSleep.sleep.should.be.type('function');
});
it('does not accept options/flags', function () {
var ret = shell.sleep('-f', 1);
ret.code.should.equal(1);
ret.stdout.should.equal('');
var errorMsg = 'sleep: option not recognized: f';
ret.stderr.should.equal(errorMsg);
shell.error().should.equal(errorMsg);
});
it('works for 0 seconds', function () {
var start = new Date();
var ret = shell.sleep(0);
var end = new Date();
assertApproxEqual(end - start, 0);
ret.code.should.equal(0);
ret.stdout.should.equal('');
});
it('works for 1 second', function () {
var start = new Date();
var ret = shell.sleep(1);
var end = new Date();
assertApproxEqual(end - start, 1000);
ret.code.should.equal(0);
ret.stdout.should.equal('');
});
it('works for 2 seconds', function () {
this.timeout(3000);
var start = new Date();
var ret = shell.sleep(2);
var end = new Date();
assertApproxEqual(end - start, 2000);
ret.code.should.equal(0);
ret.stdout.should.equal('');
});
it('rejects negative numbers', function () {
var ret = shell.sleep(-1);
ret.code.should.equal(1);
var errorMsg = 'sleep: negative numbers are not supported';
ret.stdout.should.equal('');
ret.stderr.should.equal(errorMsg);
shell.error().should.equal(errorMsg);
});
it('rejects non-integer arguments', function () {
var ret = shell.sleep('hi there');
ret.code.should.equal(1);
var errorMsg = 'sleep: sleep time must be an integer';
ret.stdout.should.equal('');
ret.stderr.should.equal(errorMsg);
shell.error().should.equal(errorMsg);
ret = shell.sleep(2.3);
ret.code.should.equal(1);
ret.stdout.should.equal('');
ret.stderr.should.equal(errorMsg);
shell.error().should.equal(errorMsg);
});
it('allows string arguments', function () {
var start = new Date();
var ret = shell.sleep('1');
var end = new Date();
assertApproxEqual(end - start, 1000);
ret.code.should.equal(0);
ret.stdout.should.equal('');
});
describe('fallback methods', function () {
it('can rely on execSync + sleep command', function () {
if (shell.which('sleep')) {
var start = new Date();
pluginSleep.sleepWithCommand(1);
var end = new Date();
assertApproxEqual(end - start, 1000, { epsilon: 300 });
} else {
console.warn('Cannot find sleep command so cannot verify');
}
});
it('can rely on execSync + helper script', function () {
var start = new Date();
pluginSleep.sleepWithNode(1);
var end = new Date();
assertApproxEqual(end - start, 1000, { epsilon: 300 });
});
});
});