11var fs = require ( 'fs' )
22 , child = require ( 'child_process' )
33 , test = require ( 'tap' ) . test
4+ , touch = require ( 'touch' )
45
56var dir = __dirname + '/fixture'
67 , bin = __dirname + '/../bin/node-dev'
78 , msgFile = dir + '/message.js'
8- , msg = 'module.exports = "Please touch message .js now"\n '
9+ , ignoredFile = dir + '/ignoredModule .js'
910
10- function touch ( ) {
11- fs . writeFileSync ( msgFile , msg )
11+
12+ // Constants
13+ var MESSAGE = fs . readFileSync ( msgFile ) . toString ( )
14+
15+ // Helpers
16+ function touchFile ( file ) {
17+ return function ( ) {
18+ touch . sync ( file ? file : msgFile )
19+ }
1220}
1321
1422function spawn ( cmd , cb ) {
@@ -31,7 +39,7 @@ function spawn(cmd, cb) {
3139function run ( cmd , done ) {
3240 spawn ( cmd , function ( out ) {
3341 if ( out . match ( / t o u c h m e s s a g e .j s / ) ) {
34- setTimeout ( touch , 500 )
42+ setTimeout ( touchFile ( ) , 500 )
3543 return function ( out ) {
3644 if ( out . match ( / R e s t a r t i n g / ) ) {
3745 return { exit : done }
@@ -41,18 +49,18 @@ function run(cmd, done) {
4149 } )
4250}
4351
44-
52+ // Tests
4553test ( 'should restart the server' , function ( t ) {
4654 run ( 'server.js' , t . end . bind ( t ) )
4755} )
4856
4957test ( 'should restart the server twice' , function ( t ) {
5058 spawn ( 'server.js' , function ( out ) {
5159 if ( out . match ( / t o u c h m e s s a g e .j s / ) ) {
52- setTimeout ( touch , 500 )
60+ setTimeout ( touchFile ( ) , 500 )
5361 return function ( out ) {
5462 if ( out . match ( / R e s t a r t i n g / ) ) {
55- setTimeout ( touch , 500 )
63+ setTimeout ( touchFile ( ) , 500 )
5664 return function ( out ) {
5765 if ( out . match ( / R e s t a r t i n g / ) ) {
5866 return { exit : t . end . bind ( t ) }
@@ -64,6 +72,28 @@ test('should restart the server twice', function(t) {
6472 } )
6573} )
6674
75+ test ( 'should not restart the server for ignored modules' , function ( t ) {
76+ ps = spawn ( 'server.js' , function ( out ) {
77+ if ( out . match ( / t o u c h m e s s a g e .j s / ) ) {
78+ setTimeout ( touchFile ( ignoredFile ) , 500 )
79+
80+ var successTimeoutId = setTimeout ( function ( ) {
81+ ps . removeAllListeners ( 'exit' )
82+ ps . kill ( )
83+
84+ t . end ( )
85+ } , 1000 ) ;
86+
87+ return function ( out ) {
88+ clearTimeout ( successTimeoutId )
89+ t . fail ( 'server restarted unexpectedly' )
90+
91+ return { exit : t . end . bind ( t ) }
92+ }
93+ }
94+ } )
95+ } )
96+
6797test ( 'should restart the cluster' , function ( t ) {
6898 run ( 'cluster.js' , t . end . bind ( t ) )
6999} )
@@ -78,7 +108,7 @@ test('should support coffee-script', function(t) {
78108
79109test ( 'should restart when a file is renamed' , function ( t ) {
80110 var tmp = dir + '/message.tmp'
81- fs . writeFileSync ( tmp , msg )
111+ fs . writeFileSync ( tmp , MESSAGE )
82112 spawn ( 'log.js' , function ( out ) {
83113 if ( out . match ( / t o u c h m e s s a g e .j s / ) ) {
84114 fs . renameSync ( tmp , msgFile )
@@ -94,7 +124,7 @@ test('should restart when a file is renamed', function(t) {
94124test ( 'should handle errors' , function ( t ) {
95125 spawn ( 'error.js' , function ( out ) {
96126 if ( out . match ( / E R R O R / ) ) {
97- setTimeout ( touch , 500 )
127+ setTimeout ( touchFile ( ) , 500 )
98128 return function ( out ) {
99129 if ( out . match ( / R e s t a r t i n g / ) ) {
100130 return { exit : t . end . bind ( t ) }
@@ -135,7 +165,7 @@ test('should pass through the exit code', function(t) {
135165 } )
136166} )
137167
138- test ( 'should conceil the wrapper' , function ( t ) {
168+ test ( 'should conceal the wrapper' , function ( t ) {
139169 // require.main should be main.js not wrap.js!
140170 spawn ( 'main.js' ) . on ( 'exit' , function ( code ) {
141171 t . is ( code , 0 )
@@ -177,7 +207,7 @@ test('should set NODE_ENV', function(t) {
177207test ( 'should allow graceful shutdowns' , function ( t ) {
178208 spawn ( 'server.js' , function ( out ) {
179209 if ( out . match ( / t o u c h m e s s a g e .j s / ) ) {
180- setTimeout ( touch , 500 )
210+ setTimeout ( touchFile ( ) , 500 )
181211 return function ( out ) {
182212 if ( out . match ( / e x i t / ) ) {
183213 return { exit : t . end . bind ( t ) }
0 commit comments