'use strict';

var assert = require('assert');
var vm = require('vm');
var fs = require('fs');
var path = require('path');

var spawnFile = require.resolve('../');
var spawnSource = fs.readFileSync(spawnFile, 'utf8');

function testSpawn(spawn) {
  var result = spawn("node", [__dirname + '/test-spawn.js'], {input: 'my-output'});
  if (result.exitCode !== 0) {
    console.error(result.stderr.toString());
    process.exit(result.exitCode);
  }
  assert(result.exitCode === 0);
  assert(Buffer.isBuffer(result.stdout));
  assert(Buffer.isBuffer(result.stderr));
  assert(result.stdout.toString() === 'output written');
  assert(result.stderr.toString() === 'error log exists');
  assert(fs.readFileSync(__dirname + '/output.txt', 'utf8') === 'my-output');
  fs.unlinkSync(__dirname + '/output.txt');
  console.log('test pass');
}

var resolve = require.resolve;
function getSpawn(require) {
  require.resolve = resolve;
  var exports = {};
  var context = {
    module: exports,
    exports: exports,
    require: require,
    __dirname: path.dirname(spawnFile),
    console: console,
    process: process
  };
  vm.runInNewContext(spawnSource, context, spawnFile);
  return context.module.exports
}

console.log('# Test native operation');
testSpawn(getSpawn(function (path) {
  if (path === 'child_process') {
    require('execSync');
    throw new Error('child_process shouldn\'t be needed when ffi is available');
  }
  return require(path);
}));

console.log('# Test fallback operation');
testSpawn(getSpawn(function (path) {
  if (path === 'execSync') {
    throw new Error('execSync isn\'t always available');
  }
  return require(path);
}));

@@ -1195,16 +1195,169 @@
 orts%0A%7D%0A%0A
+var execSyncAvailable;%0Atry %7B%0A  require('execSync');%0A  execSyncAvailable = true;%0A%7D catch (ex) %7B%0A  execSyncAvailable = false;%0A%7D%0Aif (execSyncAvailable) %7B%0A  
 console.
@@ -1380,32 +1380,34 @@
 ve operation');%0A
+  
 testSpawn(getSpa
@@ -1419,32 +1419,34 @@
 nction (path) %7B%0A
+  
   if (path === '
@@ -1463,24 +1463,26 @@
 ess') %7B%0A    
+  
 require('exe
@@ -1490,16 +1490,18 @@
 Sync');%0A
+  
     thro
@@ -1567,34 +1567,38 @@
  available');%0A  
+ 
+ 
 %7D%0A
+  
   return require
@@ -1605,20 +1605,80 @@
 (path);%0A
+  
 %7D));
+%0A%7D else %7B%0A  console.log('# SKIP Test native operation');%0A%7D
 %0A%0Aconsol

Update tests for 0.11