@@ -5,6 +5,7 @@ var test = require('tap').test;
55global . Promise = require ( 'bluebird' ) ;
66var getStream = require ( 'get-stream' ) ;
77var arrify = require ( 'arrify' ) ;
8+ var touch = require ( 'touch' ) ;
89var cliPath = path . join ( __dirname , '../cli.js' ) ;
910
1011function execCli ( args , dirname , cb ) {
@@ -21,11 +22,12 @@ function execCli(args, dirname, cb) {
2122 env . AVA_APPVEYOR = 1 ;
2223 }
2324
25+ var child ;
2426 var stdout ;
2527 var stderr ;
2628
2729 var processPromise = new Promise ( function ( resolve ) {
28- var child = childProcess . spawn ( process . execPath , [ path . relative ( dirname , cliPath ) ] . concat ( arrify ( args ) ) , {
30+ child = childProcess . spawn ( process . execPath , [ path . relative ( dirname , cliPath ) ] . concat ( arrify ( args ) ) , {
2931 cwd : dirname ,
3032 env : env ,
3133 stdio : [ null , 'pipe' , 'pipe' ]
@@ -49,6 +51,8 @@ function execCli(args, dirname, cb) {
4951 Promise . all ( [ processPromise , stdout , stderr ] ) . then ( function ( args ) {
5052 cb . apply ( null , args ) ;
5153 } ) ;
54+
55+ return child ;
5256}
5357
5458test ( 'throwing a named function will report the to the console' , function ( t ) {
@@ -110,3 +114,44 @@ test('pkg-conf: cli takes precedence', function (t) {
110114 t . end ( ) ;
111115 } ) ;
112116} ) ;
117+
118+ test ( 'watcher works' , function ( t ) {
119+ var killed = false ;
120+
121+ var hasChokidar = false ;
122+ try {
123+ require ( 'chokidar' ) ;
124+ hasChokidar = true ;
125+ } catch ( err ) { }
126+
127+ var child = execCli ( [ '--verbose' , '--watch' , 'test.js' ] , 'fixture/watcher' , function ( err , stdout ) {
128+ if ( err && err . code === 1 && ! hasChokidar ) {
129+ t . comment ( 'chokidar dependency is missing, cannot test watcher' ) ;
130+ t . match ( stdout , 'The optional dependency chokidar failed to install and is required for --watch. Chokidar is likely not supported on your platform.' ) ;
131+ t . end ( ) ;
132+ } else {
133+ t . ok ( killed ) ;
134+ t . ifError ( err ) ;
135+ t . end ( ) ;
136+ }
137+ } ) ;
138+
139+ var buffer = '' ;
140+ var passedFirst = false ;
141+ // Pause the stream before attaching the 'data' listener. execCli() uses
142+ // get-stream which read()s from the stream. The test just needs to piggyback
143+ // on that without switching the stream to flowing mode.
144+ child . stderr . pause ( ) . on ( 'data' , function ( str ) {
145+ buffer += str ;
146+ if ( / 1 t e s t p a s s e d / . test ( str ) ) {
147+ if ( ! passedFirst ) {
148+ touch . sync ( path . join ( __dirname , 'fixture/watcher/test.js' ) ) ;
149+ buffer = '' ;
150+ passedFirst = true ;
151+ } else if ( ! killed ) {
152+ child . kill ( ) ;
153+ killed = true ;
154+ }
155+ }
156+ } ) ;
157+ } ) ;
0 commit comments