From 7d174a2d9117404550849709424c91986c995ce8 Mon Sep 17 00:00:00 2001 From: Josh Wolfe Date: Sun, 18 Feb 2024 19:59:38 -0500 Subject: [PATCH] run examples in the test suite --- .gitignore | 1 + examples/promises.js | 2 +- test/test.js | 69 ++++++++++++++++++++++++++++++++++++++------ 3 files changed, 62 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index ccc2930..c25e08d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /coverage /node_modules +/test/.tmp diff --git a/examples/promises.js b/examples/promises.js index 5451424..34d370f 100644 --- a/examples/promises.js +++ b/examples/promises.js @@ -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, diff --git a/test/test.js b/test/test.js index d756afc..bd2c0e9 100644 --- a/test/test.js +++ b/test/test.js @@ -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; @@ -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) { @@ -357,6 +349,65 @@ 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"); + 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); + + 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}: `; + } + process.stdout.write(testId); + + // 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; + } + + 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");