|
| 1 | +import { resolve } from "path"; |
| 2 | +import { |
| 3 | + excludeDirectories, |
| 4 | + openZip, |
| 5 | + openZipBuffer, |
| 6 | + readZipEntries, |
| 7 | +} from "../../../src/common/unzip"; |
| 8 | + |
| 9 | +const zipWithSingleFilePath = resolve( |
| 10 | + __dirname, |
| 11 | + "../../vscode-tests/no-workspace/data/archive-filesystem-provider-test/single_file.zip", |
| 12 | +); |
| 13 | +const zipWithFolderPath = resolve( |
| 14 | + __dirname, |
| 15 | + "../../vscode-tests/no-workspace/data/archive-filesystem-provider-test/zip_with_folder.zip", |
| 16 | +); |
| 17 | + |
| 18 | +describe("openZip", () => { |
| 19 | + it("can open a zip file", async () => { |
| 20 | + const zipFile = await openZip(zipWithFolderPath, { |
| 21 | + lazyEntries: false, |
| 22 | + }); |
| 23 | + |
| 24 | + expect(zipFile.entryCount).toEqual(8); |
| 25 | + }); |
| 26 | +}); |
| 27 | + |
| 28 | +describe("readZipEntries", () => { |
| 29 | + it("can read the entries when there is a single file", async () => { |
| 30 | + const zipFile = await openZip(zipWithSingleFilePath, { |
| 31 | + lazyEntries: true, |
| 32 | + }); |
| 33 | + const entries = await readZipEntries(zipFile); |
| 34 | + |
| 35 | + expect(entries.map((entry) => entry.fileName).sort()).toEqual([ |
| 36 | + "src_archive/", |
| 37 | + "src_archive/aFileName.txt", |
| 38 | + ]); |
| 39 | + }); |
| 40 | + |
| 41 | + it("can read the entries when there are multiple folders", async () => { |
| 42 | + const zipFile = await openZip(zipWithFolderPath, { |
| 43 | + lazyEntries: true, |
| 44 | + }); |
| 45 | + const entries = await readZipEntries(zipFile); |
| 46 | + |
| 47 | + expect(entries.map((entry) => entry.fileName).sort()).toEqual([ |
| 48 | + "__MACOSX/._folder1", |
| 49 | + "__MACOSX/folder1/._textFile.txt", |
| 50 | + "__MACOSX/folder1/._textFile2.txt", |
| 51 | + "folder1/", |
| 52 | + "folder1/folder2/", |
| 53 | + "folder1/folder2/textFile3.txt", |
| 54 | + "folder1/textFile.txt", |
| 55 | + "folder1/textFile2.txt", |
| 56 | + ]); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe("excludeDirectories", () => { |
| 61 | + it("excludes directories when there is a single file", async () => { |
| 62 | + const zipFile = await openZip(zipWithSingleFilePath, { |
| 63 | + lazyEntries: true, |
| 64 | + }); |
| 65 | + const entries = await readZipEntries(zipFile); |
| 66 | + const entriesWithoutDirectories = excludeDirectories(entries); |
| 67 | + |
| 68 | + expect( |
| 69 | + entriesWithoutDirectories.map((entry) => entry.fileName).sort(), |
| 70 | + ).toEqual(["src_archive/aFileName.txt"]); |
| 71 | + }); |
| 72 | + |
| 73 | + it("excludes directories when there are multiple folders", async () => { |
| 74 | + const zipFile = await openZip(zipWithFolderPath, { |
| 75 | + lazyEntries: true, |
| 76 | + }); |
| 77 | + const entries = await readZipEntries(zipFile); |
| 78 | + const entriesWithoutDirectories = excludeDirectories(entries); |
| 79 | + |
| 80 | + expect( |
| 81 | + entriesWithoutDirectories.map((entry) => entry.fileName).sort(), |
| 82 | + ).toEqual([ |
| 83 | + "__MACOSX/._folder1", |
| 84 | + "__MACOSX/folder1/._textFile.txt", |
| 85 | + "__MACOSX/folder1/._textFile2.txt", |
| 86 | + "folder1/folder2/textFile3.txt", |
| 87 | + "folder1/textFile.txt", |
| 88 | + "folder1/textFile2.txt", |
| 89 | + ]); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +describe("openZipBuffer", () => { |
| 94 | + it("can read an entry in the zip file", async () => { |
| 95 | + const zipFile = await openZip(zipWithFolderPath, { |
| 96 | + lazyEntries: true, |
| 97 | + autoClose: false, |
| 98 | + }); |
| 99 | + const entries = await readZipEntries(zipFile); |
| 100 | + |
| 101 | + const entry = entries.find( |
| 102 | + (entry) => entry.fileName === "folder1/textFile.txt", |
| 103 | + ); |
| 104 | + expect(entry).toBeDefined(); |
| 105 | + if (!entry) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + const buffer = await openZipBuffer(zipFile, entry); |
| 110 | + expect(buffer).toHaveLength(12); |
| 111 | + expect(buffer.toString("utf8")).toEqual("I am a text\n"); |
| 112 | + }); |
| 113 | +}); |
0 commit comments