var Q = require('q');
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var rimraf = require('rimraf');
var mockery = require('mockery');
var utils = require('../lib/utils');

describe('runBinary', function() {
  var runBinary;
  var spawnCalls;
  var root = 'foobar';
  var runBinaryPath = '../lib/cli/run-binary';

  beforeEach(function() {
    spawnCalls = [];
    deleteFolder();

    mockery.enable({
      useCleanCache: true,
      warnOnUnregistered: false
    });

    mockery.registerAllowable(runBinaryPath);

    mockery.registerMock('../utils', {
      projectRoot: function() {
        return Q(root);
      },
      spawn: function() {
        spawnCalls.push({
          binary: arguments[0],
          args: arguments[1],
          options: arguments[2]
        });
        return Q(true);
      }
    });

    runBinary = require(runBinaryPath);
  });

  afterEach(function() {
    deleteFolder();
    mockery.disable();
    mockery.deregisterAll();
  });

  it('rejects if binary does not exist and adds additional info if the command is `donejs add`', function(done) {
    runBinary(['add'])
      .then(function() {
        assert(false, 'should have failed');
        done();
      })
      .catch(function(err) {
        assert(
          /Could not find local DoneJS/.test(err.message),
          'it should reject with error'
        );

        assert(
          /Allowed types for a new project are/.test(err.message),
          'it should reject with error'
        );
        
        done();
      });
  });

  it('spawns process with provided arguments', function(done) {
    var binaryPath;

    createDoneJSBinary()
      .then(function(binary) {
        binaryPath = binary;
        return runBinary(['--help']);
      })
      .then(function() {
        assert.equal(spawnCalls.length, 1, 'should spawn process');
        var call = spawnCalls[0];

        assert.equal(call.binary, binaryPath);
        assert.deepEqual(call.args, ['--help']);
        assert.deepEqual(call.options, { cwd: root });

        done();
      })
      .catch(done);
  });

  function deleteFolder() {
    if (fs.existsSync(root)) {
      rimraf.sync(root);
    }
  }

  function createDoneJSBinary() {
    var binary = path.join(root, 'node_modules', '.bin');
    var doneScript = process.platform === 'win32' ? 'donejs.cmd' : 'donejs';
    var binaryPath = path.join(binary, doneScript);

    return utils.mkdirp(binary)
      .then(function() {
        var writeFile = Q.denodeify(fs.writeFile);
        return writeFile(binaryPath);
      })
      .then(function() {
        return binaryPath;
      });
  }
});

@@ -1524,24 +1524,16 @@
      );%0A
-        
 %0A       
@@ -2427,62 +2427,276 @@
 t);%0A
-%0A    return utils.mkdirp(binary)%0A      .then(function(
+    var actualBinaryPath = path.join(__dirname, %22/../bin%22, %22donejs%22);%0A%0A    var readBinaryPromise = Q.denodeify(fs.readFile)(actualBinaryPath);%0A%0A    return utils.mkdirp(binary)%0A      .then(function()%7B%0A        return readBinaryPromise;%0A      %7D)%0A      .then(function(code
 ) %7B%0A
@@ -2773,32 +2773,38 @@
 eFile(binaryPath
+, code
 );%0A      %7D)%0A    

Fix runBinary test