-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathtest-all.js
More file actions
192 lines (165 loc) · 6.79 KB
/
test-all.js
File metadata and controls
192 lines (165 loc) · 6.79 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
// Test basic usage of cli. Contains confusing setTimeouts
var fs = require('fs');
var path = require('path');
var exec = require('child_process').exec;
var assert = require('assert');
var utils = require('../utils');
var run = utils.run;
// If true, output of commands are shown
var DEBUG_TESTS = false;
// Arbitrary file which is created on detected changes
// Used to determine that file changes were actually detected.
var CHANGE_FILE = 'dir/change';
// Time to wait for different tasks
var TIMEOUT_WATCH_READY = 1000;
var TIMEOUT_CHANGE_DETECTED = 700;
var TIMEOUT_KILL = TIMEOUT_WATCH_READY + TIMEOUT_CHANGE_DETECTED + 1000;
// Abs path to test directory
var testDir = path.resolve(__dirname);
process.chdir(path.join(testDir, '..'));
describe('chokidar-cli', function() {
this.timeout(5000);
afterEach(function clean(done) {
if (changeFileExists()) {
fs.unlinkSync(resolve(CHANGE_FILE));
}
// Clear all changes in the test directory
run('git checkout HEAD dir', {cwd: testDir})
.then(function() {
done();
});
});
it('help should be succesful', function(done) {
run('node index.js --help', {pipe: DEBUG_TESTS})
.then(function(exitCode) {
// exit code 0 means success
assert.strictEqual(exitCode, 0);
done();
});
});
it('version should be successful', function(done) {
run('node index.js -v', {pipe: DEBUG_TESTS})
.then(function(exitCode) {
// exit code 0 means success
assert.strictEqual(exitCode, 0);
done();
});
});
it('**/*.less should detect all less files in dir tree', function(done) {
var killed = false;
// Use a file to detect that trigger command is actually run
var touch = 'touch ' + CHANGE_FILE;
// No quotes needed in glob pattern because node process spawn
// does no globbing
// TODO: touch command does not always create file before assertion
run('node ../index.js "dir/**/*.less" -c "' + touch + '"', {
pipe: DEBUG_TESTS,
cwd: './test',
// Called after process is spawned
callback: function(child) {
setTimeout(function killChild() {
// Kill child after test case
child.kill();
killed = true;
}, TIMEOUT_KILL);
}
})
.then(function childProcessExited(exitCode) {
// Process should be killed after a timeout,
// test if the process died unexpectedly before it
assert(killed, 'process exited too quickly');
done();
});
setTimeout(function afterWatchIsReady() {
fs.writeFileSync(resolve('dir/subdir/c.less'), 'content');
setTimeout(function() {
assert(changeFileExists(), 'change file should exist');
}, TIMEOUT_CHANGE_DETECTED);
}, TIMEOUT_WATCH_READY);
});
it('should throttle invocations of command', function(done) {
var touch = 'touch ' + CHANGE_FILE;
var changedDetectedTime = 100;
var throttleTime = (2 * changedDetectedTime) + 100;
run('node ../index.js "dir/**/*.less" --debounce 0 --throttle ' + throttleTime + ' -c "' + touch + '"', {
pipe: DEBUG_TESTS,
cwd: './test',
callback: function(child) {
setTimeout(function killChild() {
// Kill child after test case
child.kill();
}, TIMEOUT_KILL);
}
})
.then(function childProcessExited(exitCode) {
done();
});
setTimeout(function afterWatchIsReady() {
fs.writeFileSync(resolve('dir/subdir/c.less'), 'content');
setTimeout(function() {
assert(changeFileExists(), 'change file should exist after first change');
fs.unlinkSync(resolve(CHANGE_FILE));
fs.writeFileSync(resolve('dir/subdir/c.less'), 'more content');
setTimeout(function() {
assert.equal(changeFileExists(), false, 'change file should not exist after second change');
}, changedDetectedTime);
}, changedDetectedTime);
}, TIMEOUT_WATCH_READY);
});
it('should debounce invocations of command', function(done) {
var touch = 'touch ' + CHANGE_FILE;
var changedDetectedTime = 100;
var debounceTime = (2 * changedDetectedTime) + 100;
var killTime = TIMEOUT_WATCH_READY + (2 * changedDetectedTime) + debounceTime + 1000;
run('node ../index.js "dir/**/*.less" --debounce ' + debounceTime + ' -c "' + touch + '"', {
pipe: DEBUG_TESTS,
cwd: './test',
callback: function(child) {
setTimeout(function killChild() {
// Kill child after test case
child.kill();
}, killTime);
}
})
.then(function childProcessExited(exitCode) {
done();
});
setTimeout(function afterWatchIsReady() {
fs.writeFileSync(resolve('dir/subdir/c.less'), 'content');
setTimeout(function() {
assert.equal(changeFileExists(), false, 'change file should not exist earlier than debounce time (first)');
fs.writeFileSync(resolve('dir/subdir/c.less'), 'more content');
setTimeout(function() {
assert.equal(changeFileExists(), false, 'change file should not exist earlier than debounce time (second)');
}, changedDetectedTime);
setTimeout(function() {
assert(changeFileExists(), 'change file should exist after debounce time');
}, debounceTime + changedDetectedTime);
}, changedDetectedTime);
}, TIMEOUT_WATCH_READY);
});
it('should replace {path} and {event} in command', function(done) {
var command = "echo '{event}:{path}' > " + CHANGE_FILE;
setTimeout(function() {
fs.writeFileSync(resolve('dir/a.js'), 'content');
}, TIMEOUT_WATCH_READY);
run('node ../index.js "dir/a.js" -c "' + command + '"', {
pipe: DEBUG_TESTS,
cwd: './test',
callback: function(child) {
setTimeout(child.kill.bind(child), TIMEOUT_KILL);
}
})
.then(function() {
var res = fs.readFileSync(resolve(CHANGE_FILE)).toString().trim();
assert.equal(res, 'change:dir/a.js', 'need event/path detail');
done();
});
});
});
function resolve(relativePath) {
return path.join(testDir, relativePath);
}
function changeFileExists() {
return fs.existsSync(resolve(CHANGE_FILE));
}