'use strict';

var gulp = require('gulp');

var $ = require('gulp-load-plugins')();

var wiredep = require('wiredep');

var paths = gulp.paths;

function runTests (singleRun, done) {
  var bowerDeps = wiredep({
    directory: 'bower_components',
    exclude: ['bootstrap-sass-official'],
    dependencies: true,
    devDependencies: true
  });

  var testFiles = bowerDeps.js.concat([
<% if (props.jsPreprocessor.key === 'none') { %>
    paths.src + '/{app,components}/**/*.js'
<% } else if (props.jsPreprocessor.extension === 'js') { %>
    paths.tmp + '/serve/app/index.js',
    paths.src + '/{app,components}/**/*.spec.js',
    paths.src + '/{app,components}/**/*.mock.js'
<% } else if (props.jsPreprocessor.key === 'typescript') { %>
    paths.tmp + '/serve/{app,components}/**/!(index).js',
    paths.tmp + '/serve/{app,components}/**/index.js',
    paths.src + '/{app,components}/**/*.spec.js',
    paths.src + '/{app,components}/**/*.mock.js'
<% } else { %>
    paths.tmp + '/serve/{app,components}/**/*.js',
    paths.src + '/{app,components}/**/*.spec.js',
    paths.src + '/{app,components}/**/*.mock.js'
<% } %>
  ]);

  gulp.src(testFiles)
    .pipe($.karma({
      configFile: 'karma.conf.js',
      action: (singleRun)? 'run': 'watch'
    }))
    .on('error', function (err) {
      // Make sure failed tests cause gulp to exit non-zero
      throw err;
    });
}

<% if (props.jsPreprocessor.key === 'none') { %>
gulp.task('test', function (done) { runTests(true /* singleRun */, done) });
gulp.task('test:auto', function (done) { runTests(false /* singleRun */, done) });
<% } else if (props.jsPreprocessor.key === 'traceur') { %>
gulp.task('test', ['browserify'], function (done) { runTests(true /* singleRun */, done) });
gulp.task('test:auto', ['browserify'], function (done) { runTests(false /* singleRun */, done) });
<% } else { %>
gulp.task('test', ['scripts'], function (done) { runTests(true /* singleRun */, done) });
gulp.task('test:auto', ['scripts'], function (done) { runTests(false /* singleRun */, done) });
<% } %>

@@ -166,22 +166,16 @@
 ingleRun
-, done
 ) %7B%0A  va
@@ -1119,16 +1119,23 @@
  %5D);%0A%0A  
+return 
 gulp.src
@@ -1255,129 +1255,77 @@
 %7D))%0A
-    .on('error', function (err) %7B%0A      // Make sure failed tests cause gulp to exit non-zero%0A      throw err;%0A    %7D);%0A%7D%0A
+%7D%0A%0Agulp.task('test',%0A%3C%25 if (props.jsPreprocessor.key !== 'none') %7B %25%3E
 %0A%3C%25 
@@ -1353,198 +1353,199 @@
 sor.
-key === 'none
+extension === 'traceur
 ') %7B
- 
 %25%3E
-%0Agulp.task('test', function (done) %7B runTests(true /* singleRun */, done) %7D);%0Agulp.task('test:auto', function (done) %7B runTests(false /* singleRun */, d
+ %5B'browserify'%5D,%0A%3C%25%C2%A0%7D else %7B %25%3E %5B'scripts'%5D,%0A%3C%25 %7D %25%3E%0A%3C%25 %7D %25%3E runTests.bind(this, true));%0Agulp.task('test:auto',%0A%3C%25 if (props.jsPreprocessor.key !== 'n
 one
+'
 ) 
-%7D);%0A%3C%25 %7D else
+%7B %25%3E%0A%3C%25
  if 
@@ -1590,207 +1590,30 @@
 ') %7B
- 
 %25%3E
-%0Agulp.task('test', %5B'browserify'%5D, function (done) %7B runTests(true /* singleRun */, done) %7D);%0Agulp.task('test:auto', %5B'browserify'%5D, function (done) %7B runTests(false /* singleRun */, done) %7D);%0A%3C%25 
+ %5B'browserify'%5D,%0A%3C%25%C2%A0
 %7D el
@@ -1623,199 +1623,63 @@
 %7B %25%3E
-%0Agulp.task('test', %5B'scripts'%5D, function (done) %7B runTests(true /* singleRun */, done) %7D);%0Agulp.task('test:auto', %5B'scripts'%5D, function (done) %7B runTests(false /* singleRun */, done) %7D);%0A%3C%25 %7D %25%3E
+ %5B'scripts'%5D,%0A%3C%25 %7D %25%3E%0A%3C%25 %7D %25%3E runTests.bind(this, false));
 %0A

Refactor unit-tests.js gulpfile to terminate test task properly