Initial check in of testing scripts.

This commit is contained in:
Arthur Britto
2012-09-15 14:37:21 -07:00
parent cc467bf1d3
commit b8f2b6c0dc
5 changed files with 192 additions and 1 deletions

3
.gitignore vendored
View File

@@ -16,6 +16,7 @@
*.o *.o
obj/* obj/*
bin/newcoind bin/newcoind
newcoind newcoind
# Ignore locally installed node_modules
node_modules

9
test/buster.js Normal file
View File

@@ -0,0 +1,9 @@
var config = module.exports;
config["Newcoin tests"] = {
rootPath: "../",
environment: "node",
tests: [
"test/*-test.js"
]
}

35
test/server.js Normal file
View File

@@ -0,0 +1,35 @@
// Manage test servers
// Provide servers
//
// Servers are created in tmp/server/$server
//
console.log("server.js>");
var utils = require("./utils.js");
// var child = require("child");
var serverPath = function(name) {
return "tmp/server/" + name;
};
var makeBase = function(name, done) {
var path = serverPath(name);
console.log("start> %s: %s", name, path);
// Remove the existing dir.
utils.resetPath(path, parseInt('0777', 8), done);
console.log("start< %s", name);
};
var start = function(name, done) {
makeBase(name, done);
};
exports.start = start;
console.log("server.js<");

17
test/standalone-test.js Normal file
View File

@@ -0,0 +1,17 @@
// console.log("standalone-test.js>");
var fs = require("fs");
var buster = require("buster");
var server = require("./server.js");
buster.testCase("Check standalone server startup", {
"Start": function (done) {
server.start("alpha", function(e) {
buster.refute(e);
done();
});
}
});
// console.log("standalone-test.js<");

129
test/utils.js Normal file
View File

@@ -0,0 +1,129 @@
var fs = require("fs");
var path = require("path");
var filterErr = function(code, done) {
return function (e) {
done(e.code !== code ? e : undefined);
}
};
var throwErr = function(done) {
return function (e) {
if (e)
throw e;
done();
};
};
// apply function to elements of array. Return first true value to done or undefined.
var mapOr = function(func, array, done) {
if (array.length) {
func(array[array.length-1], function (v) {
if (v) {
done(v);
}
else {
array.length -= 1;
mapOr(func, array, done);
}
});
}
else {
done();
}
};
// Make a directory and sub-directories.
var mkPath = function(dirPath, mode, done) {
fs.mkdir(dirPath, mode, function (e) {
if (e && e.code === "EEXIST") {
// Already exists, done.
done();
}
else if (e.code === "ENOENT") {
// Missing sub dir.
mkPath(path.dirname(dirPath), mode, function(e) {
if (e) {
throw e;
}
else {
mkPath(dirPath, mode, done);
}
});
}
else {
throw e;
}
});
};
// Empty a directory.
var emptyPath = function(dirPath, done) {
fs.readdir(dirPath, function (err, files) {
if (err) {
done(err);
}
else {
mapOr(rmPath, files.map(function(f) { return path.join(dirPath, f); }), done);
}
});
};
// Remove path recursively.
var rmPath = function(dirPath, done) {
// console.log("rmPath: %s", dirPath);
fs.lstat(dirPath, function (err, stats) {
if (err && err.code == "ENOENT") {
done();
}
if (err) {
done(err);
}
else if (stats.isDirectory()) {
emptyPath(dirPath, function (e) {
if (e) {
done(e);
}
else {
// console.log("rmdir: %s", dirPath); done();
fs.rmdir(dirPath, done);
}
});
}
else {
// console.log("unlink: %s", dirPath); done();
fs.unlink(dirPath, done);
}
});
};
// Create directory if needed and empty if needed.
var resetPath = function(dirPath, mode, done) {
mkPath(dirPath, mode, function (e) {
if (e) {
done(e);
}
else {
emptyPath(dirPath, done);
}
});
};
var trace = function(comment, func) {
return function() {
console.log("%s: %s", trace, arguments.toString);
func(arguments);
};
};
exports.emptyPath = emptyPath;
exports.mapOr = mapOr;
exports.mkPath = mkPath;
exports.resetPath = resetPath;
exports.rmPath = rmPath;
exports.trace = trace;