1 | 'use strict';
|
2 |
|
3 | var gaze = require('../lib/gaze.js');
|
4 | var grunt = require('grunt');
|
5 | var path = require('path');
|
6 | var fs = require('fs');
|
7 |
|
8 |
|
9 | fs.existsSync = fs.existsSync || path.existsSync;
|
10 |
|
11 |
|
12 | function cleanUp(done) {
|
13 | [
|
14 | 'sub/tmp.js',
|
15 | 'sub/tmp',
|
16 | 'sub/renamed.js',
|
17 | 'added.js',
|
18 | 'nested/added.js',
|
19 | 'nested/sub/added.js'
|
20 | ].forEach(function(d) {
|
21 | var p = path.resolve(__dirname, 'fixtures', d);
|
22 | if (fs.existsSync(p)) { fs.unlinkSync(p); }
|
23 | });
|
24 | done();
|
25 | }
|
26 |
|
27 | exports.watch = {
|
28 | setUp: function(done) {
|
29 | process.chdir(path.resolve(__dirname, 'fixtures'));
|
30 | cleanUp(done);
|
31 | },
|
32 | tearDown: cleanUp,
|
33 | remove: function(test) {
|
34 | test.expect(2);
|
35 | gaze('**/*', function() {
|
36 | this.remove(path.resolve(__dirname, 'fixtures', 'sub', 'two.js'));
|
37 | this.remove(path.resolve(__dirname, 'fixtures'));
|
38 | var result = this.relative(null, true);
|
39 | test.deepEqual(result['sub/'], ['one.js']);
|
40 | test.notDeepEqual(result['.'], ['one.js']);
|
41 | this.close();
|
42 | test.done();
|
43 | });
|
44 | },
|
45 | changed: function(test) {
|
46 | test.expect(1);
|
47 | gaze('**/*', function(err, watcher) {
|
48 | watcher.on('changed', function(filepath) {
|
49 | var expected = path.relative(process.cwd(), filepath);
|
50 | test.equal(path.join('sub', 'one.js'), expected);
|
51 | watcher.close();
|
52 | });
|
53 | this.on('added', function() { test.ok(false, 'added event should not have emitted.'); });
|
54 | this.on('deleted', function() { test.ok(false, 'deleted event should not have emitted.'); });
|
55 | fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;');
|
56 | watcher.on('end', test.done);
|
57 | });
|
58 | },
|
59 | added: function(test) {
|
60 | test.expect(1);
|
61 | gaze('**/*', function(err, watcher) {
|
62 | watcher.on('added', function(filepath) {
|
63 | var expected = path.relative(process.cwd(), filepath);
|
64 | test.equal(path.join('sub', 'tmp.js'), expected);
|
65 | watcher.close();
|
66 | });
|
67 | this.on('changed', function() { test.ok(false, 'changed event should not have emitted.'); });
|
68 | this.on('deleted', function() { test.ok(false, 'deleted event should not have emitted.'); });
|
69 | fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp.js'), 'var tmp = true;');
|
70 | watcher.on('end', test.done);
|
71 | });
|
72 | },
|
73 | dontAddUnmatchedFiles: function(test) {
|
74 | test.expect(2);
|
75 | gaze('**/*.js', function(err, watcher) {
|
76 | setTimeout(function() {
|
77 | test.ok(true, 'Ended without adding a file.');
|
78 | watcher.close();
|
79 | }, 1000);
|
80 | this.on('added', function(filepath) {
|
81 | test.equal(path.relative(process.cwd(), filepath), path.join('sub', 'tmp.js'));
|
82 | });
|
83 | fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp'), 'Dont add me!');
|
84 | fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'tmp.js'), 'add me!');
|
85 | watcher.on('end', test.done);
|
86 | });
|
87 | },
|
88 | deleted: function(test) {
|
89 | test.expect(1);
|
90 | var tmpfile = path.resolve(__dirname, 'fixtures', 'sub', 'deleted.js');
|
91 | fs.writeFileSync(tmpfile, 'var tmp = true;');
|
92 | gaze('**/*', function(err, watcher) {
|
93 | watcher.on('deleted', function(filepath) {
|
94 | test.equal(path.join('sub', 'deleted.js'), path.relative(process.cwd(), filepath));
|
95 | watcher.close();
|
96 | });
|
97 | this.on('changed', function() { test.ok(false, 'changed event should not have emitted.'); });
|
98 | this.on('added', function() { test.ok(false, 'added event should not have emitted.'); });
|
99 | fs.unlinkSync(tmpfile);
|
100 | watcher.on('end', test.done);
|
101 | });
|
102 | },
|
103 | nomark: function(test) {
|
104 | test.expect(1);
|
105 | gaze('**/*', {mark:false}, function(err, watcher) {
|
106 | watcher.on('changed', function(filepath) {
|
107 | var expected = path.relative(process.cwd(), filepath);
|
108 | test.equal(path.join('sub', 'one.js'), expected);
|
109 | watcher.close();
|
110 | });
|
111 | fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;');
|
112 | watcher.on('end', test.done);
|
113 | });
|
114 | },
|
115 | dontEmitTwice: function(test) {
|
116 | test.expect(2);
|
117 | gaze('**/*', function(err, watcher) {
|
118 | watcher.on('all', function(status, filepath) {
|
119 | var expected = path.relative(process.cwd(), filepath);
|
120 | test.equal(path.join('sub', 'one.js'), expected);
|
121 | test.equal(status, 'changed');
|
122 | fs.readFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'));
|
123 | setTimeout(function() {
|
124 | fs.readFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'));
|
125 | }, 1000);
|
126 |
|
127 | setTimeout(function() { watcher.close(); }, 5000);
|
128 | });
|
129 | setTimeout(function() {
|
130 | fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;');
|
131 | }, 1000);
|
132 | watcher.on('end', test.done);
|
133 | });
|
134 | },
|
135 | emitTwice: function(test) {
|
136 | test.expect(2);
|
137 | var times = 0;
|
138 | gaze('**/*', function(err, watcher) {
|
139 | watcher.on('all', function(status, filepath) {
|
140 | test.equal(status, 'changed');
|
141 | times++;
|
142 | setTimeout(function() {
|
143 | if (times < 2) {
|
144 | fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;');
|
145 | } else {
|
146 | watcher.close();
|
147 | }
|
148 | }, 1000);
|
149 | });
|
150 | setTimeout(function() {
|
151 | fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'one.js'), 'var one = true;');
|
152 | }, 1000);
|
153 | watcher.on('end', test.done);
|
154 | });
|
155 | },
|
156 | nonExistent: function(test) {
|
157 | test.expect(1);
|
158 | gaze('non/existent/**/*', function(err, watcher) {
|
159 | test.ok(true);
|
160 | test.done();
|
161 | });
|
162 | },
|
163 | differentCWD: function(test) {
|
164 | test.expect(1);
|
165 | var cwd = path.resolve(__dirname, 'fixtures', 'sub');
|
166 | gaze('two.js', {
|
167 | cwd: cwd
|
168 | }, function(err, watcher) {
|
169 | watcher.on('changed', function(filepath) {
|
170 | test.deepEqual(this.relative(), {'.':['two.js']});
|
171 | watcher.close();
|
172 | });
|
173 | fs.writeFileSync(path.resolve(cwd, 'two.js'), 'var two = true;');
|
174 | watcher.on('end', test.done);
|
175 | });
|
176 | },
|
177 | addedEmitInSubFolders: function(test) {
|
178 | test.expect(4);
|
179 | var adds = [
|
180 | { pattern: '**/*', file: path.resolve(__dirname, 'fixtures', 'nested', 'sub', 'added.js') },
|
181 | { pattern: '**/*', file: path.resolve(__dirname, 'fixtures', 'added.js') },
|
182 | { pattern: 'nested/**/*', file: path.resolve(__dirname, 'fixtures', 'nested', 'added.js') },
|
183 | { pattern: 'nested/sub/*.js', file: path.resolve(__dirname, 'fixtures', 'nested', 'sub', 'added.js') },
|
184 | ];
|
185 | grunt.util.async.forEachSeries(adds, function(add, next) {
|
186 | new gaze.Gaze(add.pattern, function(err, watcher) {
|
187 | watcher.on('added', function(filepath) {
|
188 | test.equal('added.js', path.basename(filepath));
|
189 | fs.unlinkSync(filepath);
|
190 | watcher.close();
|
191 | next();
|
192 | });
|
193 | watcher.on('changed', function() { test.ok(false, 'changed event should not have emitted.'); });
|
194 | watcher.on('deleted', function() { test.ok(false, 'deleted event should not have emitted.'); });
|
195 | fs.writeFileSync(add.file, 'var added = true;');
|
196 | });
|
197 | }, function() {
|
198 | test.done();
|
199 | });
|
200 | },
|
201 | };
|