-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.test.js
214 lines (182 loc) · 5.37 KB
/
index.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**!
* wt - test/index.test.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
/**
* Module dependencies.
*/
var path = require('path');
var fs = require('fs');
var pedding = require('pedding');
var wt = require('../');
describe('index.test.js', function () {
var fixtures = path.join(__dirname, 'fixtures');
beforeEach(function (done) {
this.watcher = wt.watch(fixtures, done);
});
afterEach(function (done) {
try {
fs.rmdirSync(path.join(fixtures, '.createdir'));
} catch (err) {}
this.watcher.close();
setTimeout(done, 100);
});
it('should watch file change', function (done) {
done = pedding(2, done);
var filepath = path.join(fixtures, 'foo.txt');
fs.writeFile(filepath, 'bar', done);
var lastpath = null;
this.watcher.once('file', function (info) {
lastpath = info.path;
info.path.should.equal(filepath);
info.isFile.should.equal(true);
done();
});
});
it('should ignore hidden file change', function (done) {
done = pedding(2, done);
var filepath = path.join(fixtures, '.file.txt.swp');
fs.writeFile(filepath, 'vim tmp file\n', done);
this.watcher.on('file', function () {
throw new Error('should not run this');
});
setTimeout(done, 200);
});
it('should ignore hidden dir change', function (done) {
done = pedding(2, done);
var dirpath = path.join(fixtures, '.createdir');
fs.mkdir(dirpath, done);
this.watcher.on('dir', function () {
throw new Error('should not run this');
});
setTimeout(done, 200);
});
it('should not ignore hidden dir change with ignoreHidden option', function (done) {
done = pedding(2, done);
this.watcher.close();
this.watcher = wt.watch(fixtures, {ignoreHidden: false}, function() {
var dirpath = path.join(fixtures, '.createdir');
fs.mkdir(dirpath, done);
this.watcher.on('dir', function () {
done();
});
}.bind(this));
});
it('should watch subdir file change', function (done) {
done = pedding(2, done);
var filepath = path.join(fixtures, 'subdir', 'subfoo.txt');
fs.writeFile(filepath, 'subbar', done);
var lastpath = null;
this.watcher.on('file', function (info) {
if (lastpath === info.path) {
// watch will repeat
return;
}
lastpath = info.path;
info.path.should.equal(filepath);
info.isFile.should.equal(true);
done();
});
});
it('should watch file remove', function (done) {
done = pedding(2, done);
var filepath = path.join(fixtures, 'subdir', 'subsubdir', 'subsubdel.txt');
fs.writeFileSync(filepath, 'need to be delete');
fs.unlink(filepath, done);
var lastpath = null;
this.watcher.on('remove', function (info) {
if (filepath !== info.path) {
return;
}
if (lastpath === info.path) {
// watch will repeat
return;
}
lastpath = info.path;
info.path.should.equal(filepath);
info.isFile.should.equal(false);
info.remove.should.equal(true);
done();
});
});
it('should watch dir remove', function (done) {
done = pedding(3, done);
var filepath = path.join(fixtures, 'subdir', 'subsubdir', 'subsubdeldir');
!fs.existsSync(filepath) && fs.mkdirSync(filepath);
fs.rmdir(filepath, done);
var lastpath = null;
var allpath = null;
this.watcher.on('remove', function (info) {
if (info.path !== filepath) {
return;
}
if (lastpath === info.path) {
// watch will repeat
return;
}
lastpath = info.path;
info.path.should.equal(filepath);
info.isFile.should.equal(false);
info.remove.should.equal(true);
done();
}).on('all', function (info) {
if (info.path !== filepath) {
// watch will repeat and cause other change on linux
return;
}
if (allpath === info.path) {
// watch will repeat
return;
}
allpath = info.path;
info.path.should.equal(filepath);
info.isFile.should.equal(false);
info.remove.should.equal(true);
done();
});
});
it('should watch dir create', function (done) {
done = pedding(2, done);
var filepath = path.join(fixtures, 'subdir', 'subsubdir', 'subsubdeldir');
fs.existsSync(filepath) && fs.rmdirSync(filepath);
fs.mkdir(filepath, done);
var lastpath = null;
this.watcher.on('dir', function (info) {
if (filepath !== info.path) {
return;
}
if (lastpath === info.path) {
return;
}
lastpath = info.path;
info.path.should.equal(filepath);
info.isFile.should.equal(false);
info.isDirectory.should.equal(true);
info.remove.should.equal(false);
done();
});
});
it('should emit watch event twice', function(done) {
done = pedding(2, done);
this.watcher.close();
this.watcher = wt.watch([
path.join(fixtures, 'subdir'),
path.join(fixtures, 'subdir2')
]).on('watch', done.bind(null, null));
});
it('should emit watch-error event', function(done) {
var filepath = path.join(fixtures, 'not-exist');
this.watcher.close();
this.watcher = wt.watch(filepath)
.on('watch-error', function(err) {
err.dir.should.eql(filepath);
done();
});
});
});