ar
Version:
ar - Read Unix archive files.
119 lines (104 loc) • 3.82 kB
JavaScript
var ar = require('../ar');
var fs = require('fs');
var path = require('path');
var util = require('util');
var glob = require('glob'), cwd = process.cwd();
process.chdir(__dirname);
function padNumber(num, length) {
var numStr = "" + num;
while (numStr.length < length) {
numStr = "0" + numStr;
}
return numStr;
}
function appendDots(str, length) {
while (str.length < length) {
str = str + ".";
}
return str;
}
var Test = (function () {
function Test(filename) {
this.filename = filename;
}
Test.prototype.testFile = function (arFile, filePath) {
var realFileContents = fs.readFileSync(filePath), arFileContents = arFile.fileData(), i;
if (realFileContents.length !== arFileContents.length) {
throw new Error("Archive file's length does not match file: " + arFileContents.length + " bytes vs " + realFileContents.length + " bytes.");
}
for (i = 0; i < arFileContents.length; i++) {
if (arFileContents.readUInt8(i) !== realFileContents.readUInt8(i)) {
throw new Error("Archive file and file differ at byte " + i + ".");
}
}
};
Test.prototype.testFiles = function () {
var files = fs.readdirSync(this.dirPath), arFiles = this.archive.getFiles(), i, file, name, nameIdx;
for (i = 0; i < arFiles.length; i++) {
file = arFiles[i];
name = file.name();
nameIdx = files.indexOf(name);
if (nameIdx === -1) {
throw new Error("Archive file contains extraneous file \"" + name + "\".");
}
this.testFile(file, path.resolve(this.dirPath, name));
files.splice(nameIdx, 1);
}
if (files.length > 0) {
throw new Error("Archive file is missing the following files: " + files.toString());
}
};
Test.prototype.run = function (testNumber, totalTests) {
var dirName;
util.print(appendDots([
"[", padNumber(testNumber, 2), "/",
padNumber(totalTests, 2), "] ",
this.filename, " "].join(''), 76));
try {
this.archive = new ar.Archive(fs.readFileSync(this.filename));
dirName = path.basename(this.filename);
dirName = dirName.substr(0, dirName.length - 2);
if (dirName.indexOf('-') !== -1) {
dirName = dirName.substr(0, dirName.indexOf('-'));
}
this.dirPath = path.resolve(path.dirname(this.filename), dirName);
if (!fs.existsSync(this.dirPath)) {
throw new Error("Cannot find test data in folder \"" + this.dirPath + "\"");
}
this.testFiles();
util.print("..OK\n");
return true;
} catch (e) {
util.print("FAIL\n");
console.log("\t" + e);
return false;
}
};
return Test;
})();
var TestSuite = (function () {
function TestSuite(filenames) {
this.tests = [];
var i;
for (i = 0; i < filenames.length; i++) {
this.tests.push(new Test(filenames[i]));
}
}
TestSuite.prototype.run = function () {
var i, length = this.tests.length, passed = 0;
console.log("Running " + length + " tests...");
for (i = 0; i < length; i++) {
if (this.tests[i].run(i + 1, length))
passed++;
}
console.log("Tests complete! " + passed + "/" + this.tests.length + " (" + ((passed / this.tests.length) * 100) + "%) passed.");
};
return TestSuite;
})();
glob("*.a", function (er, files) {
if (er != null)
throw er;
(new TestSuite(files)).run();
process.chdir(cwd);
});
//# sourceMappingURL=runner.js.map