watch-test.js
3.59 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
/*
* watch-test.js: Tests for restarting forever processes when a file changes.
*
* (C) 2010 Charlie Robbins & the Contributors
* MIT LICENSE
*
*/
var assert = require('assert'),
path = require('path'),
fs = require('fs'),
vows = require('vows'),
fmonitor = require('../../lib');
var watchDir = fs.realpathSync(path.join(__dirname, '..', 'fixtures', 'watch')),
monitor;
vows.describe('forever-monitor/plugins/watch').addBatch({
'When using forever with watch enabled': {
'forever should': {
topic: fmonitor.start('daemon.js', {
silent: true,
args: ['-p', '8090'],
watch: true,
sourceDir: path.join(__dirname, '..', 'fixtures', 'watch')
}),
'have correct options set': function (child) {
monitor = child;
assert.isTrue(child.watchIgnoreDotFiles);
assert.equal(watchDir, fs.realpathSync(child.watchDirectory));
},
'read .foreverignore file and store ignore patterns': function (child) {
setTimeout(function () {
assert.deepEqual(
child.watchIgnorePatterns,
fs.readFileSync(
path.join(watchDir, '.foreverignore'),
'utf8'
).split("\n").filter(Boolean)
);
}, 100);
}
}
}
}).addBatch({
'When using forever with watch enabled': {
'when a file matching an ignore pattern is added': {
topic: function () {
var self = this;
this.filenames = [
path.join(watchDir, 'ignore_newFile'),
path.join(watchDir, 'ignoredDir', 'ignore_subfile')
];
//
// Setup a bad restart function
//
function badRestart() {
this.callback(new Error('Monitor restarted at incorrect time.'));
}
monitor.once('restart', badRestart);
this.filenames.forEach(function (filename) {
fs.writeFileSync(filename, '');
});
//
// `chokidar` does not emit anything when ignored
// files have changed so we need a setTimeout here
// to prove that nothing has happened.
//
setTimeout(function () {
monitor.removeListener('restart', badRestart);
self.callback();
}, 5000);
},
'do nothing': function (err) {
assert.isUndefined(err);
this.filenames.forEach(function (filename) {
fs.unlinkSync(filename);
});
}
}
}
}).addBatch({
'When using forever with watch enabled': {
'when file changes': {
topic: function (child) {
child.once('restart', this.callback);
fs.writeFileSync(path.join(watchDir, 'file'), '// hello, I know nodejitsu.');
},
'restart the script': function (child, _) {
fs.writeFileSync(path.join(watchDir, 'file'), '/* hello, I know nodejitsu. ');
}
}
}
}).addBatch({
'When using forever with watch enabled': {
'when file is added': {
topic: function () {
monitor.once('restart', this.callback);
fs.writeFileSync(path.join(watchDir, 'newFile'), '');
},
'restart the script': function (child, _) {
fs.unlinkSync(path.join(watchDir, 'newFile'));
}
}
}
}).addBatch({
'When using forever with watch enabled': {
'when file is removed': {
topic: function () {
monitor.once('restart', this.callback);
try { fs.unlinkSync(path.join(watchDir, 'removeMe')) }
catch (ex) { }
},
'restart the script': function (child, _) {
fs.writeFileSync(path.join(watchDir, 'removeMe'), '');
monitor.stop();
}
}
}
}).export(module);