-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathtest-all.js
More file actions
169 lines (141 loc) · 5.46 KB
/
test-all.js
File metadata and controls
169 lines (141 loc) · 5.46 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
// Test basic usage of cli. Contains confusing setTimeouts
/* eslint-env mocha */
'use strict';
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const {run} = require('../utils');
// If true, output of commands are shown
const DEBUG_TESTS = false;
// Arbitrary file which is created on detected changes
// Used to determine that file changes were actually detected.
const CHANGE_FILE = 'dir/change';
// Time to wait for different tasks
const TIMEOUT_WATCH_READY = 1000;
const TIMEOUT_CHANGE_DETECTED = 700;
const TIMEOUT_KILL = TIMEOUT_WATCH_READY + TIMEOUT_CHANGE_DETECTED + 1000;
// Abs path to test directory
const 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(() => {
done();
});
});
it('help should be succesful', done => {
run('node index.js --help', {pipe: DEBUG_TESTS})
.then(exitCode => {
// exit code 0 means success
assert.strictEqual(exitCode, 0);
done();
});
});
it('version should be successful', done => {
run('node index.js -v', {pipe: DEBUG_TESTS})
.then(exitCode => {
// exit code 0 means success
assert.strictEqual(exitCode, 0);
done();
});
});
it('**/*.less should detect all less files in dir tree', done => {
let killed = false;
// Use a file to detect that trigger command is actually run
const 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(child) {
setTimeout(function killChild() {
// Kill child after test case
child.kill();
killed = true;
}, TIMEOUT_KILL);
}
})
.then(function childProcessExited() {
// 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(() => {
assert(changeFileExists(), 'change file should exist');
}, TIMEOUT_CHANGE_DETECTED);
}, TIMEOUT_WATCH_READY);
});
it('should run command for each simultaneous change, if not debounced or throttled', done => {
let killed = false;
// I hope it's safe to use the pipe and tee here. It should work
// on any unix-y system.
const logChanges = `echo {path} | tee -a ${CHANGE_FILE}`;
// Run watcher
run(`node ../index.js "dir/**/*.js" -d 0 -c "${logChanges}"`, {
pipe: DEBUG_TESTS,
cwd: './test',
// Called after process is spawned
callback(child) {
setTimeout(function killChild() {
// Kill child after test case
child.kill();
killed = true;
}, TIMEOUT_KILL);
}
})
.then(function childProcessExited() {
// Process should be killed after a timeout,
// test if the process died unexpectedly before it
assert(killed, 'process exited too quickly');
done();
});
setTimeout(() => {
run('touch dir/a.js dir/b.js', { cwd: './test' })
.then(
setTimeout(() => {
assert(changeFileContains('a.js'), 'change file should include a.js');
assert(changeFileContains('b.js'), 'change file should include b.js');
}, TIMEOUT_CHANGE_DETECTED)
);
}, TIMEOUT_WATCH_READY);
});
it('should replace {path} and {event} in command', done => {
const command = `echo '{event}:{path}' > ${CHANGE_FILE}`;
setTimeout(() => {
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(child) {
setTimeout(child.kill.bind(child), TIMEOUT_KILL);
}
})
.then(() => {
const res = fs.readFileSync(resolve(CHANGE_FILE)).toString().trim();
assert.strictEqual(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));
}
function changeFileContains(pattern) {
return fs.readFileSync(resolve(CHANGE_FILE)).toString().match(pattern);
}