Skip to content

Commit

Permalink
fix #137 refactor tests for mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
silkentrance committed Jul 16, 2017
1 parent 8f73d89 commit b9ce374
Show file tree
Hide file tree
Showing 54 changed files with 1,143 additions and 1,482 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
"os-tmpdir": "~1.0.2"
},
"devDependencies": {
"vows": "~0.7.0"
"vows": "~0.7.0",
"mocha": "~3.4.2"
},
"main": "lib/tmp.js",
"files": [
"lib/"
],
"scripts": {
"test": "vows test/*-test.js",
"test": "mocha test/*-test.js",
"doc": "jsdoc -c .jsdoc.json"
}
}
77 changes: 77 additions & 0 deletions test/assertions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* eslint-disable no-octal */

var
assert = require('assert'),
fs = require('fs'),
path = require('path'),
existsSync = fs.existsSync || path.existsSync;


module.exports.assertName = function assertName(name, expected) {
assert.ok(typeof name == 'string');
assert.ok(name.length > 0, 'an empty string is not a valid name');
if (expected) {
assert.equal(path.basename(name), expected, 'should be the expected name');
}
};


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

/*
// geteuid() and getegid() do not exist on Windows.
// must use the effective gid and effective uid for testing
if (process.geteuid) {
assert.equal(stat.uid, process.geteuid(), 'should have the same UID');
}
if (process.getegid) {
// FIXME does not always work as expected (setgid bit on parent directory)
console.log('stat.gid ' + stat.gid);
console.log('egid ' + process.getegid());
assert.equal(stat.gid, process.getegid(), 'should have the same GUID');
}
*/

// mode values do not work properly on Windows. Ignore “group” and
// “other” bits then. Ignore execute bit on that platform because it
// doesn’t exist—even for directories.
if (process.platform == 'win32') {
assert.equal(stat.mode & 000600, mode & 000600);
} else {
assert.equal(stat.mode & 000777, mode);
}
};


module.exports.assertPrefix = function assertPrefix(name, prefix) {
assert.equal(path.basename(name).slice(0, prefix.length), prefix, 'should have the provided prefix');
};


module.exports.assertPostfix = function assertPostfix(name, postfix) {
assert.equal(name.slice(name.length - postfix.length, name.length), postfix, 'should have the provided postfix');
};


module.exports.assertProperResult = function assertProperResult(result, withfd) {
assert.ok(result);
assert.ok(result.name, 'should have a name');
if (withfd) assert.ok(result.fd, 'should have an fd');
else assert.strictEqual(result.fd, undefined, 'should not have an fd');
assert.ok(typeof result.removeCallback == 'function', 'should have a removeCallback');
};


module.exports.assertExists = function assertExists(name, isfile) {
assert.ok(existsSync(name), name + ' should exist');
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');
};


module.exports.assertDoesNotExist = function assertDoesNotExist(name) {
assert.ok(!existsSync(name), name + ' should not exist');
};

222 changes: 0 additions & 222 deletions test/base.js

This file was deleted.

72 changes: 72 additions & 0 deletions test/child-process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// vim: expandtab:ts=2:sw=2

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


module.exports = function spawnChildProcess(configFile, cb) {
var
node_path = process.argv[0],
command_args = [ path.join(__dirname, 'spawn.js') ].concat(configFile),
stdoutBufs = [],
stderrBufs = [],
child,
done = false,
stderrDone = false,
stdoutDone = false;

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

// spawn doesn’t have the quoting problems that exec does,
// especially when going for Windows portability.
child = spawn(node_path, command_args);
child.stdin.end();
// Cannot use 'close' event because not on node-0.6.
function _close() {
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() {
stderrDone = true;
_close();
});
}


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;
}, []));
}
}

Loading

0 comments on commit b9ce374

Please sign in to comment.