Skip to content

Commit

Permalink
small updates (#513)
Browse files Browse the repository at this point in the history
* test asset seems to be faulty
* more JSDoc like comments
* update helpers
* allow "./" in get entry
* test for zipnamefix

---------

Co-authored-by: 5saviahv <[email protected]>
  • Loading branch information
5saviahv and 5saviahv authored Jun 24, 2024
1 parent 76b1dd8 commit 336fd51
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 199 deletions.
179 changes: 93 additions & 86 deletions adm-zip.js

Large diffs are not rendered by default.

Binary file modified test/assets/ultra.zip
Binary file not shown.
10 changes: 5 additions & 5 deletions test/mbcs/mbcs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe("Multibyte Character Sets in Filename", () => {
const zip2 = new Zip(pth.join(destination, "06-jp_name.zip"));
const text1 = zip2.readAsText(file);
assert(text1 === content, text1);
const entry2 = zip2.getEntry("test/" + file);
const entry2 = zip2.getEntry("./test/" + file);
const text2 = zip2.readAsText(entry2);
assert(text2 === content, text2);
assert(entry2.header.flags_efs);
Expand All @@ -192,7 +192,7 @@ describe("Multibyte Character Sets in Filename", () => {
let entry1 = zip2.getEntry(file);
let bufdata1 = zip2.readFile(entry1);
assert(bufdata1.equals(content));
let entry2 = zip2.getEntry("test/" + file);
let entry2 = zip2.getEntry("./test/" + file);
let bufdata2 = zip2.readFile(entry2);
assert(bufdata2.equals(content));
assert(entry1.header.flags_efs === false);
Expand Down Expand Up @@ -237,7 +237,7 @@ describe("Multibyte Character Sets in Filename", () => {
const zip2 = new Zip(pth.join(destination, "09-heb_name.zip"));
const text1 = zip2.readAsText(file);
assert(text1 === content, text1);
const entry2 = zip2.getEntry("test/" + file);
const entry2 = zip2.getEntry("./test/" + file);
const text2 = zip2.readAsText(entry2);
assert(text2 === content, text2);
assert(entry2.header.flags_efs);
Expand Down Expand Up @@ -289,7 +289,7 @@ describe("Multibyte Character Sets in Filename", () => {
let entry1 = zip2.getEntry(file);
let text1 = zip2.readAsText(entry1, "utf16le");
assert(text1 === content, text1);
let entry2 = zip2.getEntry("test/" + file);
let entry2 = zip2.getEntry("./test/" + file);
let text2 = zip2.readAsText(entry2, "utf16le");
assert(text2 === content, text2);
assert(entry1.header.flags_efs === false);
Expand All @@ -312,7 +312,7 @@ describe("Multibyte Character Sets in Filename", () => {
let entry1 = zip2.getEntry(file);
let text1 = zip2.readAsText(entry1, "utf16le");
assert(text1 === content, text1);
let entry2 = zip2.getEntry("test/" + file);
let entry2 = zip2.getEntry("./test/" + file);
let text2 = zip2.readAsText(entry2, "utf16le");
assert(text2 === content, text2);
assert(entry1.header.flags_efs);
Expand Down
129 changes: 39 additions & 90 deletions test/methods/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const rimraf = require("rimraf");
const Utils = require("../../util/utils");

describe("adm-zip.js - methods handling local files", () => {
const wrapList = (c) => pth.normalize(pth.join(destination, c));
const destination = "./test/xxx";
const testFileFolderList = [
{ name: "subfolder1/subfolder2/zipEntry1.txt", content: "zipEntry1" },
Expand All @@ -28,21 +29,21 @@ describe("adm-zip.js - methods handling local files", () => {
afterEach((done) => rimraf(destination, done));

describe(".extractAllTo() - sync", () => {
const ultrazip = [
"./attributes_test/asd/New Text Document.txt",
"./attributes_test/blank file.txt",
"./attributes_test/New folder/hidden.txt",
"./attributes_test/New folder/hidden_readonly.txt",
"./attributes_test/New folder/readonly.txt",
"./utes_test/New folder/somefile.txt"
].map(wrapList);

it("zip.extractAllTo(destination)", () => {
const zip = new Zip("./test/assets/ultra.zip");
zip.extractAllTo(destination);
const files = walk(destination);

expect(files.sort()).to.deep.equal(
[
pth.normalize("./test/xxx/attributes_test/asd/New Text Document.txt"),
pth.normalize("./test/xxx/attributes_test/blank file.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/hidden.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/hidden_readonly.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/readonly.txt"),
pth.normalize("./test/xxx/utes_test/New folder/somefile.txt")
].sort()
);
expect(files.sort()).to.deep.equal(ultrazip.sort());
});

it("zip.extractAllTo(destination) - streamed file", () => {
Expand All @@ -55,20 +56,20 @@ describe("adm-zip.js - methods handling local files", () => {
});

describe(".extractAllToAsync - sync", () => {
const ultrazip = [
"./attributes_test/asd/New Text Document.txt",
"./attributes_test/blank file.txt",
"./attributes_test/New folder/hidden.txt",
"./attributes_test/New folder/hidden_readonly.txt",
"./attributes_test/New folder/readonly.txt",
"./utes_test/New folder/somefile.txt"
].map(wrapList);

it("zip.extractAllToAsync(destination)", (done) => {
const zip = new Zip("./test/assets/ultra.zip");
zip.extractAllToAsync(destination, (error) => {
const files = walk(destination);
expect(files.sort()).to.deep.equal(
[
pth.normalize("./test/xxx/attributes_test/asd/New Text Document.txt"),
pth.normalize("./test/xxx/attributes_test/blank file.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/hidden.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/hidden_readonly.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/readonly.txt"),
pth.normalize("./test/xxx/utes_test/New folder/somefile.txt")
].sort()
);
expect(files.sort()).to.deep.equal(ultrazip.sort());
done();
});
});
Expand All @@ -78,33 +79,15 @@ describe("adm-zip.js - methods handling local files", () => {
// note the return
return zip.extractAllToAsync(destination).then(function (data) {
const files = walk(destination);
expect(files.sort()).to.deep.equal(
[
pth.normalize("./test/xxx/attributes_test/asd/New Text Document.txt"),
pth.normalize("./test/xxx/attributes_test/blank file.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/hidden.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/hidden_readonly.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/readonly.txt"),
pth.normalize("./test/xxx/utes_test/New folder/somefile.txt")
].sort()
);
expect(files.sort()).to.deep.equal(ultrazip.sort());
}); // no catch, it'll figure it out since the promise is rejected
});

it("zip.extractAllToAsync(destination, false, false, callback)", (done) => {
const zip = new Zip("./test/assets/ultra.zip");
zip.extractAllToAsync(destination, false, false, (error) => {
const files = walk(destination);
expect(files.sort()).to.deep.equal(
[
pth.normalize("./test/xxx/attributes_test/asd/New Text Document.txt"),
pth.normalize("./test/xxx/attributes_test/blank file.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/hidden.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/hidden_readonly.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/readonly.txt"),
pth.normalize("./test/xxx/utes_test/New folder/somefile.txt")
].sort()
);
expect(files.sort()).to.deep.equal(ultrazip.sort());
done();
});
});
Expand All @@ -114,33 +97,15 @@ describe("adm-zip.js - methods handling local files", () => {
// note the return
return zip.extractAllToAsync(destination, false, false).then(function (data) {
const files = walk(destination);
expect(files.sort()).to.deep.equal(
[
pth.normalize("./test/xxx/attributes_test/asd/New Text Document.txt"),
pth.normalize("./test/xxx/attributes_test/blank file.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/hidden.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/hidden_readonly.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/readonly.txt"),
pth.normalize("./test/xxx/utes_test/New folder/somefile.txt")
].sort()
);
expect(files.sort()).to.deep.equal(ultrazip.sort());
}); // no catch, it'll figure it out since the promise is rejected
});

it("zip.extractAllToAsync(destination, false, callback)", (done) => {
const zip = new Zip("./test/assets/ultra.zip");
zip.extractAllToAsync(destination, false, (error) => {
const files = walk(destination);
expect(files.sort()).to.deep.equal(
[
pth.normalize("./test/xxx/attributes_test/asd/New Text Document.txt"),
pth.normalize("./test/xxx/attributes_test/blank file.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/hidden.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/hidden_readonly.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/readonly.txt"),
pth.normalize("./test/xxx/utes_test/New folder/somefile.txt")
].sort()
);
expect(files.sort()).to.deep.equal(ultrazip.sort());
done();
});
});
Expand All @@ -150,16 +115,7 @@ describe("adm-zip.js - methods handling local files", () => {
// note the return
return zip.extractAllToAsync(destination, false).then(function (data) {
const files = walk(destination);
expect(files.sort()).to.deep.equal(
[
pth.normalize("./test/xxx/attributes_test/asd/New Text Document.txt"),
pth.normalize("./test/xxx/attributes_test/blank file.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/hidden.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/hidden_readonly.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/readonly.txt"),
pth.normalize("./test/xxx/utes_test/New folder/somefile.txt")
].sort()
);
expect(files.sort()).to.deep.equal(ultrazip.sort());
}); // no catch, it'll figure it out since the promise is rejected
});
});
Expand All @@ -171,16 +127,9 @@ describe("adm-zip.js - methods handling local files", () => {
zipEntries.forEach((e) => zip.extractEntryTo(e, destination, false, true));

const files = walk(destination);
expect(files.sort()).to.deep.equal(
[
pth.normalize("./test/xxx/blank file.txt"),
pth.normalize("./test/xxx/hidden.txt"),
pth.normalize("./test/xxx/hidden_readonly.txt"),
pth.normalize("./test/xxx/New Text Document.txt"),
pth.normalize("./test/xxx/readonly.txt"),
pth.normalize("./test/xxx/somefile.txt")
].sort()
);
const ultrazip = ["blank file.txt", "hidden.txt", "hidden_readonly.txt", "New Text Document.txt", "readonly.txt", "somefile.txt"].map(wrapList);

expect(files.sort()).to.deep.equal(ultrazip.sort());
});

it("zip.extractEntryTo(entry, destination, true, true)", () => {
Expand All @@ -189,16 +138,16 @@ describe("adm-zip.js - methods handling local files", () => {
zipEntries.forEach((e) => zip.extractEntryTo(e, destination, true, true));

const files = walk(destination);
expect(files.sort()).to.deep.equal(
[
pth.normalize("./test/xxx/attributes_test/asd/New Text Document.txt"),
pth.normalize("./test/xxx/attributes_test/blank file.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/hidden.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/hidden_readonly.txt"),
pth.normalize("./test/xxx/attributes_test/New folder/readonly.txt"),
pth.normalize("./test/xxx/utes_test/New folder/somefile.txt")
].sort()
);
const ultrazip = [
"./attributes_test/asd/New Text Document.txt",
"./attributes_test/blank file.txt",
"./attributes_test/New folder/hidden.txt",
"./attributes_test/New folder/hidden_readonly.txt",
"./attributes_test/New folder/readonly.txt",
"./utes_test/New folder/somefile.txt"
].map(wrapList);

expect(files.sort()).to.deep.equal(ultrazip.sort());
});
});

Expand Down
29 changes: 27 additions & 2 deletions test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
const { expect } = require("chai");
const { crc32, canonical, sanitize } = require("../util/utils");
const { crc32, canonical, sanitize, zipnamefix } = require("../util/utils");
const pth = require("path");

describe("utils", () => {
Expand Down Expand Up @@ -70,6 +70,31 @@ describe("utils", () => {
});

it("function canonical()", () => {
const tests = [
// no name
{ file: "", result: "" },
// file has name
{ file: "file", result: "file" },
{ file: "../file", result: "file" },
{ file: "../../../file", result: "file" },
{ file: "./../file", result: "file" },
{ file: "../../file", result: "file" },
{ file: "../../file1/../file2", result: "file2" },
{ file: "../subfolder/file2", result: pth.normalize("subfolder/file2") },
{ file: "../subfolder2/file2", result: pth.normalize("subfolder2/file2") },
{ file: "../subfolder/file2", result: pth.normalize("subfolder/file2") },
{ file: "../../subfolder2/file2", result: pth.normalize("subfolder2/file2") }
];

for (const { file, result } of Array.from(tests)) {
tests.push({ result, file: file.split("/").join("\\") });
}

for (let test of tests) {
expect(canonical(test.file)).to.equal(test.result);
}
});
it("function zipnamefix()", () => {
const tests = [
// no name
{ file: "", result: "" },
Expand All @@ -91,7 +116,7 @@ describe("utils", () => {
}

for (let test of tests) {
expect(canonical(test.file)).to.equal(test.result);
expect(zipnamefix(test.file)).to.equal(test.result);
}
});
});
Expand Down
Loading

0 comments on commit 336fd51

Please sign in to comment.