"use strict";


/*global nodeca, _*/


// stdlib
var fs        = require('fs');
var path      = require('path');
var resolve   = path.resolve;
var exists    = fs.existsSync || path.existsSync;
var join      = path.join;
var basename  = path.basename;


// 3rd-party
var NLib      = require('nlib');
var Async     = require('nlib').Vendor.Async;
var Mocha     = require('mocha');


////////////////////////////////////////////////////////////////////////////////


module.exports.parserParameters = {
  version: nodeca.runtime.version,
  addHelp: true,
  help: 'run test suites',
  description: 'Run all tests of enabled apps'
};


////////////////////////////////////////////////////////////////////////////////


require('should');


if (!process.env.NODECA_ENV) {
  nodeca.runtime.env = process.env.NODECA_ENV = 'test';
}


// Lookup file names at the given `path`.
//
function lookupFiles(path, recursive) {
  var files = [], stat;

  if (!exists(path)) {
    path += '.js';
  }

  try {
    stat = fs.statSync(path);
  } catch (err) {
    return files;
  }

  if (stat.isFile()) {
    return path;
  }

  fs.readdirSync(path).forEach(function(file){
    file = join(path, file);

    var stat = fs.statSync(file);

    if (stat.isDirectory()) {
      if (recursive) {
        files = files.concat(lookupFiles(file, recursive));
      }

      return;
    }

    if (!stat.isFile() || !(/\.js$/).test(file) || '.' === basename(file)[0]) {
      return;
    }

    files.push(file);
  });

  return files;
}


////////////////////////////////////////////////////////////////////////////////


module.exports.run = function (args, callback) {
  Async.series([
    require('../lib/init/redis'),
    require('../lib/init/mongoose'),

    NLib.InitStages.loadModels,

    require('../lib/init/migrations_check'),

    NLib.InitStages.loadServerApiSubtree,
    NLib.InitStages.loadSharedApiSubtree,
    NLib.InitStages.loadClientApiSubtree,
    NLib.InitStages.loadSettings,
    NLib.InitStages.initRouter,

    NLib.InitStages.initTranslations,

    require('../lib/init/assets'),

    function (next) {
      var mocha = new Mocha(), files = [];


      mocha.reporter('spec');
      mocha.ui('bdd');

      _.each(nodeca.runtime.apps, function (app) {
        files = files.concat(lookupFiles(app.root + '/test', true));
      });

      mocha.files = files.map(function (path) {
        return resolve(path);
      });

      mocha.run(next);
    }
  ], function (err) {
    if (err) {
      callback(err);
      return;
    }

    process.exit(0);
  });
};

@@ -732,41 +732,8 @@
 ;%0A%0A%0A
-if (!process.env.NODECA_ENV) %7B%0A  
 node
@@ -772,17 +772,18 @@
 ECA_ENV 
-=
+%7C%7C
  'test';
@@ -783,18 +783,16 @@
 'test';%0A
-%7D%0A
 %0A%0A// Loo

Fix test env