'use strict';

var MockUI        = require('./mock-ui');
var MockAnalytics = require('./mock-analytics');
var cli           = require('../../lib/cli');
var path          = require('path');
var Promise       = require('../../lib/ext/promise');

/*
  Accepts a single array argument, that contains the
  `process.argv` style arguments.

  Example:

  ```
  ember test --no-build --test-port=4567
  ```

  In this example, `process.argv` would be something similar to:

  ```
  ['path/to/node', 'path/to/bin/ember', 'test', '--no-build', '--test-port=4567']
  ```


  And this could be emulated by calling:

  ```
  ember(['test', '--no-build', '--test-port=4567']);
  ```

  ---

  The return value of this helper is a promise that resolves
  with an object that contains the following properties:

  * `exitCode` is the normal exit code in standard command invocation
  * `ui` is the `ui` object that was used for the invocation (which is
    a `MockUI` instance), this can be used to inspect the commands output.

*/
module.exports = function ember(args, options) {
  var cliInstance;
  var ui = options && options.UI || MockUI;
  var pkg = options && options.package || path.resolve(__dirname, '..', '..');
  var disableDependencyChecker = options && options.disableDependencyChecker || true;
  var inputStream  = [];
  var outputStream = [];
  var errorLog     = [];

  args.push('--disable-analytics');
  args.push('--watcher=node');
  args.push('--skipGit');
  cliInstance = cli({
    inputStream:  inputStream,
    outputStream: outputStream,
    errorLog:     errorLog,
    cliArgs:      args,
    Leek: MockAnalytics,
    UI: ui,
    testing: true,
    disableDependencyChecker: disableDependencyChecker,
    cli: {
      // This prevents ember-cli from detecting any other package.json files
      // forcing ember-cli to act as the globally installed package
      npmPackage: 'ember-cli',
      root: pkg
    }
  });
  function returnTestState(statusCode) {
     return {
        exitCode: statusCode,
        statusCode: statusCode,
        inputStream: inputStream,
        outputStream: outputStream,
        errorLog: errorLog
     };
   }

  return cliInstance.then(returnTestState, function(statusCode) {
     return Promise.reject(returnTestState(statusCode));
  });
};

@@ -1920,16 +1920,17 @@
 %7D%0A  %7D);%0A
+%0A
   functi
@@ -1966,23 +1966,28 @@
 ) %7B%0A    
- return
+var result =
  %7B%0A     
@@ -1987,18 +1987,16 @@
 %7B%0A      
-  
 exitCode
@@ -2005,26 +2005,24 @@
 statusCode,%0A
-  
       status
@@ -2035,26 +2035,24 @@
 statusCode,%0A
-  
       inputS
@@ -2077,18 +2077,16 @@
 ,%0A      
-  
 outputSt
@@ -2111,18 +2111,16 @@
 ,%0A      
-  
 errorLog
@@ -2134,148 +2134,144 @@
 Log%0A
- 
     %7D;%0A
+%0A
    
-%7D%0A%0A  return cliInstance.then(returnTestState, function(statusCode) %7B%0A     return Promise.reject(returnTestState(statusCode));%0A  %7D
+ if (statusCode) %7B%0A      throw result;%0A    %7D else %7B%0A      return result;%0A    %7D%0A  %7D%0A%0A  return cliInstance.then(returnTestState
 );%0A%7D

Determine Promise result based on statusCode