/**
 * @license
 * Copyright 2014 The Lovefield Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var gulp = /** @type {{
    task: function(string, (!Array|!Function), !Function=),
    src: !Function}} */ (require('gulp'));
var gjslint = /** @type {!Function} */ (require('gulp-gjslint'));
var pathMod = require('path');
var chalk = /** @type {{green: !Function, red: !Function}} */ (
    require('chalk'));
var nopt = /** @type {!Function} */ (require('nopt'));

var builder = /** @type {{
    buildLib: !Function,
    buildTest: !Function,
    buildAllTests: !Function}} */ (
        require(pathMod.resolve(
            pathMod.join(__dirname, 'tools/builder.js'))));
var runner = /** @type {{
    runJsUnitTests: function(?string, string):!IThenable,
    runSpacTests: function():!IThenable,
    runJsPerfTests: function(string=):!IThenable}} */ (
        require(pathMod.resolve(
            pathMod.join(__dirname, 'tools/run_test.js'))));
var testServer = /** @type {{
    runUnitTestServer: function(number):!IThenable,
    runPerfTestServer: function(number):!IThenable,
    stopServer: !Function }} */ (
        require(pathMod.resolve(
            pathMod.join(__dirname, 'tools/run_test_server.js'))));


var log = console['log'];


gulp.task('default', function() {
  log('Usage: ');
  log('  gulp build --target=<all|lib|tests> --mode=<opt|debug>:');
  log('      Compile source files using Closure compiler');
  log('  gulp debug [--target=<tests|perf>] [--port=<number>]:');
  log('      Start a debug server (default is test at port 8000)');
  log('  gulp lint: Lint against source files');
  log('  gulp test --target=spac: Run SPAC tests');
  log('  gulp test --target=perf [--browser=<chrome|firefox>]:');
  log('      Run perf tests using webdriver (need separate install).');
  log('  gulp test --target=tests [--filter=<test name> ' +
      '--browser=<chrome|firefox>]:');
  log('      Run unit tests using webdriver (need separate install).');
});


gulp.task('lint', function() {
  return gulp.src([
    'src/**/*.js',
    'tests/**/*.js',
    'testing/**/*.js',
  ]).pipe(gjslint()).
      pipe(gjslint['reporter']('console'), {fail: true});
});


gulp.task('build', function() {
  var knownOpts = {
    'mode': [String, null],
    'target': [String, null]
  };
  var options = nopt(knownOpts);

  if (options.target == 'all' || options.target == null) {
    // TODO(dpapad): Build also the lib here.
    return builder.buildAllTests();
  } else if (options.target == 'lib') {
    return builder.buildLib(options);
  } else {
    return builder.buildTest(options);
  }
});


gulp.task('debug', function() {
  var knownOpts = {
    'target': [String, null],
    'port': [Number, null]
  };

  var options = nopt(knownOpts);
  options.target = options.target || 'tests';
  var port = options.port || 8000;

  // The test server cannot callback. It is terminated by Ctrl-C.
  if (options.target == 'perf') {
    testServer.runPerfTestServer(port);
  } else if (options.target != 'spac') {
    testServer.runUnitTestServer(port);
  }
});


/**
 * TODO(dpapad):
 *  1) Accept command line param to run a specific browser, run all browsers by
 *     default.
 *  2) Gather the output of the tests and display something useful (summary).
 */
gulp.task('test', ['debug'], function() {
  var knownOpts = {
    'browser': [String, null],
    'filter': [String, null],
    'target': [String]
  };
  var options = nopt(knownOpts);
  options.browser = options.browser || 'chrome';

  var whenTestsDone = null;
  if (options.target == 'perf') {
    // Run only perf regression tests and dump output in the console.
    whenTestsDone = runner.runJsPerfTests(options.browser).then(
        function(perfData) {
          log(JSON.stringify(perfData, null, 2));
          testServer.stopServer();
          process.exit();
        });
  } else if (options.target == 'spac') {
    // Run only SPAC.
    whenTestsDone = runner.runSpacTests();
  } else {
    // Run only JSUnit tests.
    whenTestsDone =
        runner.runJsUnitTests(options.filter, options.browser).then(
            function(results) {
              var failedCount = results.reduce(function(prev, item) {
                return prev + item['pass'] ? 0 : 1;
              }, 0);
              log(results.length + ' tests, ' + failedCount + ' failure(s).');
              log('JSUnit tests: ', failedCount > 0 ? chalk.red('FAILED') :
                  chalk.green('PASSED'));
              testServer.stopServer();
              process.exit();
            });
  }
  return whenTestsDone;
});

@@ -3611,207 +3611,8 @@
 ;%0A%0A%0A
-/**%0A * TODO(dpapad):%0A *  1) Accept command line param to run a specific browser, run all browsers by%0A *     default.%0A *  2) Gather the output of the tests and display something useful (summary).%0A */%0A
 gulp
@@ -4558,16 +4558,17 @@
  prev + 
+(
 item%5B'pa
@@ -4579,16 +4579,17 @@
  ? 0 : 1
+)
 ;%0A      

Fix a bug calculating test summary. Remove obsolete comments in gulp file. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=100433931