Skip to content

Commit

Permalink
run examples in the test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoshwolfe committed Feb 19, 2024
1 parent 39a1dc1 commit 140626b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/coverage
/node_modules
/test/.tmp
2 changes: 1 addition & 1 deletion examples/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

let yauzl = require("../"); // replace with: let yauzl = require("yauzl");

let simpleZipBuffer = new Buffer([
let simpleZipBuffer = Buffer.from([
80,75,3,4,20,0,8,8,0,0,134,96,146,74,0,0,
0,0,0,0,0,0,0,0,0,0,5,0,0,0,97,46,116,120,
116,104,101,108,108,111,10,80,75,7,8,32,
Expand Down
74 changes: 65 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var fs = require("fs");
var path = require("path");
var Pend = require("pend");
var util = require("util");
var child_process = require("child_process");
var Readable = require("stream").Readable;
var Writable = require("stream").Writable;

Expand Down Expand Up @@ -93,15 +94,6 @@ listZipFiles([path.join(__dirname, "success"), path.join(__dirname, "wrong-entry
if (timestamp < earliestTimestamp) throw new Error(messagePrefix + "timestamp too early: " + timestamp);
if (timestamp > new Date()) throw new Error(messagePrefix + "timestamp in the future: " + timestamp);

// Do this asynchronously because it's not critical,
// and this way it might find race condition bugs with autoclose.
zipfile.readLocalFileHeader(entry, function(err, localFileHeader) {
// Just check one of non-minumal fields fields.
if (localFileHeader.versionNeededToExtract == null) throw new Error(messagePrefix + "local file header missing versionNeededToExtract field");
// This field is the most mechnically important field.
if (localFileHeader.fileDataStart == null) throw new Error(messagePrefix + "local file header missing fileDataStart field");
});

var fileNameKey = fileName.replace(/\/$/, "");
var expectedContents = expectedArchiveContents[fileNameKey];
if (expectedContents == null) {
Expand Down Expand Up @@ -357,6 +349,70 @@ pend.go(zip64.runTest);
// openReadStream with range
pend.go(rangeTest.runTest);

// Make sure the examples run with crashing.
pend.go(function(cb) {
var examplesDir = path.join(__dirname, "../examples");
var zipfiles = listZipFiles([path.join(__dirname, "success")]);
var tmpDir = path.join(__dirname, ".tmp");
if (typeof fs.rmSync === "function") fs.rmSync(tmpDir, {recursive: true, force: true});

var parametersToTest = {
"compareCentralAndLocalHeaders.js": zipfiles,
"dump.js": zipfiles,
"promises.js": [null],
"unzip.js": zipfiles,
};
if (JSON.stringify(fs.readdirSync(examplesDir).sort()) !== JSON.stringify(Object.keys(parametersToTest).sort())) throw new Error("unexpected examples/ directory listing");
for (var f in parametersToTest) {
var args = parametersToTest[f];
var script = path.join(examplesDir, f);

if (f === "unzip.js" && typeof fs.rmSync !== "function") {
console.log("WARNING: skipping examples/unzip.js tests for node <14");
continue;
}

args.forEach(function(arg) {
var args = [path.resolve(script)];
var options = {
stdio: ["ignore", "ignore", "inherit"],
timeout: 10_000,
};
var testId;
if (arg != null) {
args.push(path.resolve(arg));
testId = `examples/${f} ${path.basename(arg)}: `;
} else {
testId = `examples/${f}: `;
}

// Handle special cases.
if (f === "dump.js" && /traditional-encryption/.exec(path.basename(arg))) {
args.push("--no-contents");
}
if (f === "unzip.js") {
if (/traditional-encryption/.exec(path.basename(arg))) return; // Can't do these.
// Quaranetine this in a temp directory.
fs.mkdirSync(tmpDir);
options.cwd = tmpDir;
}

process.stdout.write(testId);
var {status, error} = child_process.spawnSync("node", args, options);
if (status) error = new Error("child process return exit code " + status);
if (error) throw error;

if (f === "unzip.js") {
// Quaranetine this in a temp directory.
fs.rmSync(tmpDir, {recursive: true, force: true});
}

process.stdout.write("pass\n");
});
}
cb();
});

var done = false;
pend.wait(function() {
console.log("all done");
Expand Down

0 comments on commit 140626b

Please sign in to comment.