/**
 * Test dependencies
 */

var assert = require('assert');
var fs = require('fs-extra');
var request = require('request');
var exec = require('child_process').exec;
var path = require('path');
var spawn = require('child_process').spawn;

// Make existsSync not crash on older versions of Node
fs.existsSync = fs.existsSync || require('path').existsSync;

describe('Running sails www', function() {
  var sailsBin = path.resolve('./bin/sails.js');
  var appName = 'testApp';

  before(function() {
    if (fs.existsSync(appName)) {
      fs.removeSync(appName);
    }
  });

  describe('in an empty directory', function() {

    before(function() {
      // Make empty folder and move into it
      fs.mkdirSync('empty');
      process.chdir('empty');
      sailsBin = path.resolve('..', sailsBin);
    });

    // TODO: run tests in here

    after(function() {
      // Delete empty folder and move out of it
      process.chdir('../');
      fs.rmdirSync('empty');
      sailsBin = path.resolve(sailsBin);
    });

  });

  describe('in a sails app directory', function() {

    var sailsChildProc;


    it('should start server without error', function(done) {

      exec('node ' + sailsBin + ' new ' + appName + ' --fast --traditional --without=lodash,async', function(err) {
        if (err) { done(new Error(err)); }

        // Move into app directory
        process.chdir(appName);
        sailsBin = path.resolve('..', sailsBin);

        sailsChildProc = spawn('node', [sailsBin, 'www']);

        // Any output from stderr is considered an error by this test.
        sailsChildProc.stderr.on('data', function(data) {
          return done(data);
        });

        sailsChildProc.stdout.on('data', function(data) {
          var dataString = data + '';
          assert(dataString.indexOf('error') === -1);
          sailsChildProc.stdout.removeAllListeners('data');
          // Move out of app directory
          process.chdir('../');
          sailsChildProc.kill();
          return done();
        });
      });
    });

  });

  describe('with command line arguments', function() {
    afterEach(function(done) {
      sailsChildProc.stderr.removeAllListeners('data');
      process.chdir('../');
      sailsChildProc.kill();
      return done();
    });

    it('--dev should execute grunt build', function(done) {

      // Move into app directory
      process.chdir(appName);

      // Change environment to production in config file
      fs.writeFileSync('config/application.js', 'module.exports = ' + JSON.stringify({
        appName: 'Sails Application',
        port: 1342,
        environment: 'production',
        log: {
          level: 'info'
        }
      }));

      sailsChildProc = spawn('node', [sailsBin, 'www', '--dev']);

      sailsChildProc.stdout.on('data', function(data) {
        var dataString = data + '';
        if (dataString.indexOf('`grunt build`') !== -1) {
          done();
        }
      });
    });


    it('--prod should execute grunt buildProd', function(done) {

      // Move into app directory
      process.chdir(appName);

      // Overrwrite session config file
      // to set session adapter:null ( to prevent warning message from appearing on command line )
      fs.writeFileSync('config/session.js', 'module.exports.session = { adapter: null }');

      sailsChildProc = spawn('node', [sailsBin, 'www', '--prod']);

      sailsChildProc.stdout.on('data', function(data) {
        var dataString = data + '';
        if (dataString.indexOf('`grunt buildProd`') !== -1) {

          done();
        }
      });
    });

  });

  after(function() {
    if (fs.existsSync(appName)) {
      fs.removeSync(appName);
    }
  });
});

@@ -89,42 +89,8 @@
 ');%0A
-var request = require('request');%0A
 var 
@@ -127,16 +127,16 @@
 ).exec;%0A
+
 var path
@@ -1261,16 +1261,23 @@
  (err) %7B
+ return
  done(ne
@@ -2070,24 +2070,50 @@
 unction() %7B%0A
+%0A    var sailsChildProc;%0A%0A
     afterEac
@@ -2916,32 +2916,39 @@
 -1) %7B%0A          
+return 
 done();%0A        
@@ -3549,24 +3549,24 @@
  !== -1) %7B%0A%0A
-
           do
@@ -3563,16 +3563,23 @@
         
+return 
 done();%0A

Fix lint and global var issues in www test