'use strict';

var fs = require('fs');

var execSeries = require('./');
var isAppveyor = require('is-appveyor');
var rimraf = require('rimraf');
var test = require('tape');

test('execSeries()', function(t) {
  t.plan(15);

  var waitTime = 120;
  /* istanbul ignore if */
  if (isAppveyor) {
    waitTime = 3000;
  }

  execSeries(['mkdir tmp']);
  setTimeout(function() {
    fs.exists('tmp', function(result) {
      t.ok(result, 'should run the command even if the callback is not specified.');
      rimraf.sync('tmp');
    });
  }, waitTime);

  execSeries(['node -e "console.log(1)"'], function(err, stdout, stderr) {
    t.strictEqual(err, null, 'should not fail when a command doesn\'t fail.');
    t.deepEqual(stdout, ['1\n'], 'should create an array of the stdout string.');
    t.deepEqual(stderr, [''], 'should create an array of the stderr string.');
  });

  execSeries([
    'node -e "console.log(1)"',
    'node -e "console.log(2); console.warn(1);"'
  ], function(err, stdout, stderr) {
    t.strictEqual(err, null, 'should not fail when every command doesn\'t fail.');
    t.deepEqual(stdout, ['1\n', '2\n'], 'should create an array of the multiple stdout strings.');
    t.deepEqual(stderr, ['', '1\n'], 'should create an array of the multiple stderr strings.');
  });

  execSeries([
    'node -e "console.log(1)"',
    'unknown-command',
    'node -e "console.log(3)"'
  ], function(err, stdout, stderr) {
    t.notEqual(
      err.code, 0,
      'should pass an error to the callback when one of the commands fails.'
    );
    t.deepEqual(stdout, ['1\n', ''], 'should not include stdout strings after error');
    t.equal(stderr.length, 2, 'should not include stderr strings after error');
    t.ok(stderr[1].length > 0, 'should reflect stderr of the failed command.');
  });

  execSeries(['node -e "console.log(1)"'], {encoding: 'base64'}, function(err, stdout) {
    t.strictEqual(err, null, 'should accept `exec` options.');
    t.deepEqual(stdout, ['MQo='], 'should reflect `exec` options to the output.');
  });

  t.throws(
    execSeries.bind(null, 'node -v'), /TypeError/,
    'should throw a type error when its first argument is not an array.'
  );

  t.throws(
    execSeries.bind(null, ['node -v'], {}, 'function'), /TypeError/,
    'should throw a type error when its third argument is specified but not a function.'
  );
});

'use strict';

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

var execSeries = require('./');
var isAppveyor = require('is-appveyor');
var rimraf = require('rimraf');
var test = require('tape');

test('execSeries()', function(t) {
  t.plan(15);

  var waitTime = 120;
  /* istanbul ignore if */
  if (isAppveyor) {
    waitTime = 5000;
  }

  execSeries([
    'mkdir ' + path.resolve('tmp'),
    'mkdir ' + path.resolve('tmp/tmp')
  ]);
  setTimeout(function() {
    fs.exists('tmp/tmp', function(result) {
      t.ok(result, 'should run the command even if the callback is not specified.');
      rimraf.sync('tmp');
    });
  }, waitTime);

  execSeries(['node -e "console.log(1)"'], function(err, stdout, stderr) {
    t.strictEqual(err, null, 'should not fail when a command doesn\'t fail.');
    t.deepEqual(stdout, ['1\n'], 'should create an array of the stdout string.');
    t.deepEqual(stderr, [''], 'should create an array of the stderr string.');
  });

  execSeries([
    'node -e "console.log(1)"',
    'node -e "console.log(2); console.warn(1);"'
  ], function(err, stdout, stderr) {
    t.strictEqual(err, null, 'should not fail when every command doesn\'t fail.');
    t.deepEqual(stdout, ['1\n', '2\n'], 'should create an array of the multiple stdout strings.');
    t.deepEqual(stderr, ['', '1\n'], 'should create an array of the multiple stderr strings.');
  });

  execSeries([
    'node -e "console.log(1)"',
    'unknown-command',
    'node -e "console.log(3)"'
  ], function(err, stdout, stderr) {
    t.notEqual(
      err.code, 0,
      'should pass an error to the callback when one of the commands fails.'
    );
    t.deepEqual(stdout, ['1\n', ''], 'should not include stdout strings after error');
    t.equal(stderr.length, 2, 'should not include stderr strings after error');
    t.ok(stderr[1].length > 0, 'should reflect stderr of the failed command.');
  });

  execSeries(['node -e "console.log(1)"'], {encoding: 'base64'}, function(err, stdout) {
    t.strictEqual(err, null, 'should accept `exec` options.');
    t.deepEqual(stdout, ['MQo='], 'should reflect `exec` options to the output.');
  });

  t.throws(
    execSeries.bind(null, 'node -v'), /TypeError/,
    'should throw a type error when its first argument is not an array.'
  );

  t.throws(
    execSeries.bind(null, ['node -v'], {}, 'function'), /TypeError/,
    'should throw a type error when its third argument is specified but not a function.'
  );
});

check if the commands run in order