 # lint clean and build everything
 gulp.task 'build', ['lint', 'clean', 'build-lib', 'build-test']
 
+testFiles = ['test/helpers/**/*.js', 'test/spec/**/*.js']
 # helper to run coverage generation with given options
 runCoverage = (opts) ->
-  gulp.src(['test/helpers/**/*.js', 'test/spec/**/*.js'], read: false)
+  gulp.src(testFiles, read: false)
     .pipe($.coverage.instrument
       pattern: ['lib/**/*.js', '!node_modules']
       debugDirectory: 'debug')
+    .pipe($.plumber())
     .pipe($.mocha reporter: 'dot').on('error', (e) -> @emit 'end') # test errors dropped
+    .pipe($.plumber.stop())
     .pipe($.coverage.gather())
     .pipe($.coverage.format opts)
 
-runTest = (opts) ->
-  gulp.src(['test/helpers/**/*.js', 'test/spec/**/*.js'], read: false)
-    .pipe($.mocha opts).on('error', (e) -> @emit 'end') # test errors dropped
+runTest = (stream, opts) ->
+  stream
+    .pipe($.plumber())
+    .pipe($.mocha opts)
+    .pipe($.plumber.stop())
 
 # test and create coverage
 gulp.task 'coverage', ['build'], ->

  runCoverage(reporter: 'lcov')
    .pipe($.coveralls())

gulp.task 'test-quiet', ['build'], ->
  runTest reporter: 'dot'

gulp.task 'test', ['build'], ->
  runTest reporter: 'spec'

gulp.task 'karma', ['test-quiet'], (done) ->
  karma.start
    configFile: require('path').join __dirname, './karma.conf.js'
    singleRun: true
  , done

gulp.task 'karma-gui', ['test-quiet'], (done) ->
  karma.start
    configFile: require('path').join __dirname, './karma.conf.js'

    singleRun: true
  , done

# doesn't work... yet
gulp.task 'watch-coverage', ['coverage'], (done) ->
  $.livereload.listen port: 35729
  require('opn') './coverage.html', null, util.log
  gulp.watch('src/**/*.coffee', ['coverage'])
  gulp.watch('./coverage.html').on 'change', $.livereload
  return

# watch all coffee files and run tests for changes
gulp.task 'watch', (done) ->
  gulp.watch 'src/**/*.coffee', ['test']
  return

# default, test
gulp.task 'default', ['test']


