safewrite_test.js
1.3 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
'use strict';
var gaze = require('../lib/gaze.js');
var path = require('path');
var fs = require('fs');
// Clean up helper to call in setUp and tearDown
function cleanUp(done) {
[
'safewrite.js'
].forEach(function(d) {
var p = path.resolve(__dirname, 'fixtures', d);
if (fs.existsSync(p)) { fs.unlinkSync(p); }
});
done();
}
exports.safewrite = {
setUp: function(done) {
process.chdir(path.resolve(__dirname, 'fixtures'));
cleanUp(done);
},
tearDown: cleanUp,
safewrite: function(test) {
test.expect(4);
var times = 0;
var file = path.resolve(__dirname, 'fixtures', 'safewrite.js');
var backup = path.resolve(__dirname, 'fixtures', 'safewrite.ext~');
fs.writeFileSync(file, 'var safe = true;');
function simSafewrite() {
fs.writeFileSync(backup, fs.readFileSync(file));
fs.unlinkSync(file);
fs.renameSync(backup, file);
times++;
}
gaze('**/*', function() {
this.on('all', function(action, filepath) {
test.equal(action, 'changed');
test.equal(path.basename(filepath), 'safewrite.js');
if (times < 2) {
setTimeout(simSafewrite, 1000);
} else {
this.close();
test.done();
}
});
setTimeout(function() {
simSafewrite();
}, 1000);
});
}
};