Skip to content

Commit

Permalink
merge rebase to master
Browse files Browse the repository at this point in the history
  • Loading branch information
silkentrance committed Apr 30, 2018
1 parent 29a2244 commit 0c339fb
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 154 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -37,7 +37,7 @@
"scripts": {
"lint": "eslint lib --env mocha test",
"clean": "rm -Rf ./coverage",
"test": "istanbul cover ./node_modules/mocha/bin/_mocha --report none --print none --dir ./coverage/json -- -u exports test/*-test.js && istanbul report --root ./coverage/json html && istanbul report text-summary",
"test": "npm run clean && istanbul cover ./node_modules/mocha/bin/_mocha --report none --print none --dir ./coverage/json -u exports -R test/*-test.js && istanbul report --root ./coverage/json html && istanbul report text-summary",
"doc": "jsdoc -c .jsdoc.json"
}
}
6 changes: 3 additions & 3 deletions test/assertions.js
@@ -1,6 +1,6 @@
/* eslint-disable no-octal */

const
var
assert = require('assert'),
fs = require('fs'),
path = require('path'),
Expand All @@ -18,7 +18,7 @@ module.exports.assertName = function assertName(name, expected) {


module.exports.assertMode = function assertMode(name, mode) {
const stat = fs.statSync(name);
var stat = fs.statSync(name);

// mode values do not work properly on Windows. Ignore “group” and
// “other” bits then. Ignore execute bit on that platform because it
Expand Down Expand Up @@ -57,7 +57,7 @@ module.exports.assertProperResult = function assertProperResult(result, withfd)

module.exports.assertExists = function assertExists(name, isfile) {
assert.ok(existsSync(name), name + ' should exist');
const stat = fs.statSync(name);
var stat = fs.statSync(name);
if (isfile) assert.ok(stat.isFile(), name + ' should be a file');
else assert.ok(stat.isDirectory(), name + ' should be a directory');
};
Expand Down
83 changes: 41 additions & 42 deletions test/child-process.js
@@ -1,81 +1,80 @@
// vim: expandtab:ts=2:sw=2

const
var
fs = require('fs'),
path = require('path'),
existsSync = fs.existsSync || path.existsSync,
exists = fs.exists || path.exists,
spawn = require('child_process').spawn;

const ISTANBUL_PATH = path.join(__dirname, '..', 'node_modules', 'istanbul', 'lib', 'cli.js');

module.exports.genericChildProcess = function spawnGenericChildProcess(configFile, cb) {
const
configFilePath = path.join(__dirname, 'outband', configFile),
command_args = [path.join(__dirname, 'spawn-generic.js'), configFilePath];
module.exports.genericChildProcess = _spawnProcess('spawn-generic.js');
module.exports.childProcess = _spawnProcess('spawn-custom.js');

// make sure that the config file exists
if (!existsSync(configFilePath))
return cb(new Error('ENOENT: configFile ' + configFilePath + ' does not exist'));
function _spawnProcess(spawnFile) {
return function (testCase, configFile, cb) {
var
configFilePath = path.join(__dirname, 'outband', configFile),
commandArgs = [path.join(__dirname, spawnFile), configFilePath];

_do_spawn(command_args, cb);
};
exists(configFilePath, function (configExists) {
if (configExists) return _doSpawn(commandArgs, cb);

module.exports.childProcess = function spawnChildProcess(configFile, cb, detach) {
var
configFilePath = path.join(__dirname, 'outband', configFile),
command_args = [path.join(__dirname, 'spawn-custom.js'), configFilePath];

// make sure that the config file exists
if (!existsSync(configFilePath))
return cb(new Error('ENOENT: configFile ' + configFilePath + ' does not exist'));

if (arguments.length > 2) {
for (var i=2; i < arguments.length; i++) {
command_args.push(arguments[i]);
}
}

_do_spawn(command_args, cb, detach);
cb(new Error('ENOENT: configFile ' + configFilePath + ' does not exist'));
});
};
}

function _do_spawn(command_args, cb, detach) {
const
function _doSpawn(commandArgs, cb) {
var
node_path = process.argv[0],
stdoutBufs = [],
stderrBufs = [];

var
stderrBufs = [],
child,
done = false,
stderrDone = false,
stdoutDone = false;

if (process.env.running_under_istanbul) {
commandArgs = [
ISTANBUL_PATH, 'cover', '--report' , 'none', '--print', 'none',
'--dir', path.join('coverage', 'json'), '--include-pid',
commandArgs[0], '--', commandArgs[1]
];
}

// spawn doesn’t have the quoting problems that exec does,
// especially when going for Windows portability.
child = spawn(node_path, command_args, detach ? { detached: true } : undefined);
child = spawn(node_path, commandArgs);
child.stdin.end();
// TODO:we no longer support node <0.10.0

// TODO we no longer support node 0.6
// Cannot use 'close' event because not on node-0.6.
function _close() {
const
var
stderr = _bufferConcat(stderrBufs).toString(),
stdout = _bufferConcat(stdoutBufs).toString();

if (stderrDone && stdoutDone && !done) {
done = true;
cb(null, stderr, stdout);
}
}

child.on('error', function _spawnError(err) {
if (!done) {
done = true;
cb(err);
}
});

child.stdout.on('data', function _stdoutData(data) {
stdoutBufs.push(data);
}).on('close', function _stdoutEnd() {
stdoutDone = true;
_close();
});

child.stderr.on('data', function _stderrData(data) {
stderrBufs.push(data);
}).on('close', function _stderrEnd() {
Expand All @@ -87,13 +86,13 @@ function _do_spawn(command_args, cb, detach) {
function _bufferConcat(buffers) {
if (Buffer.concat) {
return Buffer.concat.apply(this, arguments);
} else {
return new Buffer(buffers.reduce(function (acc, buf) {
for (var i = 0; i < buf.length; i++) {
acc.push(buf[i]);
}
return acc;
}, []));
}

return new Buffer(buffers.reduce(function (acc, buf) {
for (var i = 0; i < buf.length; i++) {
acc.push(buf[i]);
}
return acc;
}, []));
}

66 changes: 43 additions & 23 deletions test/dir-sync-test.js
@@ -1,13 +1,14 @@
/* eslint-disable no-octal */
// vim: expandtab:ts=2:sw=2

const
var
assert = require('assert'),
fs = require('fs'),
path = require('path'),
inbandStandardTests = require('./inband-standard'),
childProcess = require('./child-process').genericChildProcess,
assertions = require('./assertions'),
rimraf = require('rimraf'),
tmp = require('../lib/tmp');


Expand Down Expand Up @@ -50,19 +51,26 @@ describe('tmp', function () {
it('on graceful cleanup', function (done) {
childProcess(this, 'graceful-dir-sync.json', function (err, stderr, stdout) {
if (err) return done(err);
else if (!stderr) assert.fail('stderr expected');
else assertions.assertDoesNotExist(stdout);
if (!stderr) return done(new Error('stderr expected'));
try {
assertions.assertDoesNotExist(stdout);
} catch (err) {
rimraf.sync(stdout);
return done(err);
}
done();
});
});

it('on non graceful cleanup', function (done) {
childProcess(this, 'non-graceful-dir-sync.json', function (err, stderr, stdout) {
if (err) return done(err);
else if (!stderr) assert.fail('stderr expected');
else {
if (!stderr) return done(new Error('stderr expected'));
try {
assertions.assertExists(stdout);
fs.rmdirSync(stdout);
rimraf.sync(stdout);
} catch (err) {
return done(err);
}
done();
});
Expand All @@ -71,10 +79,12 @@ describe('tmp', function () {
it('on keep', function (done) {
childProcess(this, 'keep-dir-sync.json', function (err, stderr, stdout) {
if (err) return done(err);
else if (stderr) assert.fail(stderr);
else {
if (stderr) return done(new Error(stderr));
try {
assertions.assertExists(stdout);
fs.rmdirSync(stdout);
rimraf.sync(stdout);
} catch (err) {
return done(err);
}
done();
});
Expand All @@ -83,21 +93,26 @@ describe('tmp', function () {
it('on unlink (keep == false)', function (done) {
childProcess(this, 'unlink-dir-sync.json', function (err, stderr, stdout) {
if (err) return done(err);
else if (stderr) assert.fail(stderr);
else assertions.assertDoesNotExist(stdout);
if (stderr) return done(new Error(stderr));
try {
assertions.assertDoesNotExist(stdout);
} catch (err) {
rimraf.sync(stdout);
return done(err);
}
done();
});
});

it('on unsafe cleanup', function (done) {
childProcess(this, 'unsafe-sync.json', function (err, stderr, stdout) {
if (err) return done(err);
else if (stderr) assert.fail(stderr);
else {
if (stderr) return done(new Error(stderr));
try {
assertions.assertDoesNotExist(stdout);
const basepath = path.join(__dirname, 'outband', 'fixtures', 'symlinkme');
assertions.assertExists(basepath);
assertions.assertExists(path.join(basepath, 'file.js'), true);
} catch (err) {
rimraf.sync(stdout);
return done(err);
}
done();
});
Expand All @@ -106,17 +121,17 @@ describe('tmp', function () {
it('on non unsafe cleanup', function (done) {
childProcess(this, 'non-unsafe-sync.json', function (err, stderr, stdout) {
if (err) return done(err);
else if (stderr) assert.fail(stderr);
else {
if (stderr) return done(new Error(stderr));
try {
assertions.assertExists(stdout);
assertions.assertExists(path.join(stdout, 'should-be-removed.file'), true);
if (process.platform == 'win32')
assertions.assertExists(path.join(stdout, 'symlinkme-target'), true);
else
assertions.assertExists(path.join(stdout, 'symlinkme-target'));
fs.unlinkSync(path.join(stdout, 'should-be-removed.file'));
fs.unlinkSync(path.join(stdout, 'symlinkme-target'));
fs.rmdirSync(stdout);
rimraf.sync(stdout);
} catch (err) {
return done(err);
}
done();
});
Expand All @@ -127,8 +142,13 @@ describe('tmp', function () {
it('on issue #62', function (done) {
childProcess(this, 'issue62-sync.json', function (err, stderr, stdout) {
if (err) return done(err);
else if (stderr) assert.fail(stderr);
else assertions.assertDoesNotExist(stdout);
if (stderr) return done(new Error(stderr));
try {
assertions.assertDoesNotExist(stdout);
} catch (err) {
rimraf.sync(stdout);
return done(err);
}
done();
});
});
Expand Down

0 comments on commit 0c339fb

Please sign in to comment.