-
Notifications
You must be signed in to change notification settings - Fork 226
Add simple tests for yauzl-based unzip functions
#3153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2e6eb1c
Add simple tests for yauzl-based unzip functions
koesie10 4eee14b
Merge remote-tracking branch 'origin/main' into koesie10/yauzl-unzip-…
koesie10 1055515
Use different ZIP file for unzip tests
koesie10 ba9b284
Add tests for unzipToDirectory
koesie10 f45b790
Fix file path in tests on Windows
koesie10 a80098d
Fix race condition in unzip tests
koesie10 be40598
Do not test file modes on Windows
koesie10 f1fe4a2
`chmod` tmp dir before cleanup
koesie10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
extensions/ql-vscode/test/unit-tests/common/unzip.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| import { createHash } from "crypto"; | ||
| import { open } from "fs/promises"; | ||
| import { join, relative, resolve, sep } from "path"; | ||
| import { chmod, pathExists, readdir } from "fs-extra"; | ||
| import { dir, DirectoryResult } from "tmp-promise"; | ||
| import { | ||
| excludeDirectories, | ||
| openZip, | ||
| openZipBuffer, | ||
| readZipEntries, | ||
| unzipToDirectory, | ||
| } from "../../../src/common/unzip"; | ||
| import { walkDirectory } from "../../../src/common/files"; | ||
|
|
||
| const zipPath = resolve(__dirname, "../data/unzip/test-zip.zip"); | ||
|
|
||
| describe("openZip", () => { | ||
| it("can open a zip file", async () => { | ||
| const zipFile = await openZip(zipPath, { | ||
| lazyEntries: false, | ||
| }); | ||
|
|
||
| expect(zipFile.entryCount).toEqual(11); | ||
| }); | ||
| }); | ||
|
|
||
| describe("readZipEntries", () => { | ||
| it("can read the entries when there are multiple directories", async () => { | ||
| const zipFile = await openZip(zipPath, { | ||
| lazyEntries: true, | ||
| }); | ||
| const entries = await readZipEntries(zipFile); | ||
|
|
||
| expect(entries.map((entry) => entry.fileName).sort()).toEqual([ | ||
| "directory/", | ||
| "directory/file.txt", | ||
| "directory/file2.txt", | ||
| "directory2/", | ||
| "directory2/file.txt", | ||
| "empty-directory/", | ||
| "tools/", | ||
| "tools/osx64/", | ||
| "tools/osx64/java-aarch64/", | ||
| "tools/osx64/java-aarch64/bin/", | ||
| "tools/osx64/java-aarch64/bin/java", | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("excludeDirectories", () => { | ||
| it("excludes directories", async () => { | ||
| const zipFile = await openZip(zipPath, { | ||
| lazyEntries: true, | ||
| }); | ||
| const entries = await readZipEntries(zipFile); | ||
| const entriesWithoutDirectories = excludeDirectories(entries); | ||
|
|
||
| expect( | ||
| entriesWithoutDirectories.map((entry) => entry.fileName).sort(), | ||
| ).toEqual([ | ||
| "directory/file.txt", | ||
| "directory/file2.txt", | ||
| "directory2/file.txt", | ||
| "tools/osx64/java-aarch64/bin/java", | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("openZipBuffer", () => { | ||
| it("can read an entry in the zip file", async () => { | ||
| const zipFile = await openZip(zipPath, { | ||
| lazyEntries: true, | ||
| autoClose: false, | ||
| }); | ||
| const entries = await readZipEntries(zipFile); | ||
|
|
||
| const entry = entries.find( | ||
| (entry) => entry.fileName === "directory/file.txt", | ||
| ); | ||
| expect(entry).toBeDefined(); | ||
| if (!entry) { | ||
| return; | ||
| } | ||
|
|
||
| const buffer = await openZipBuffer(zipFile, entry); | ||
| expect(buffer).toHaveLength(13); | ||
| expect(buffer.toString("utf8")).toEqual("I am a file\n\n"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("unzipToDirectory", () => { | ||
| let tmpDir: DirectoryResult; | ||
|
|
||
| beforeEach(async () => { | ||
| tmpDir = await dir({ | ||
| unsafeCleanup: true, | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| for await (const file of walkDirectory(tmpDir.path)) { | ||
| await chmod(file, 0o777); | ||
| } | ||
|
|
||
| await tmpDir?.cleanup(); | ||
| }); | ||
|
|
||
| it("extracts all files", async () => { | ||
| await unzipToDirectory(zipPath, tmpDir.path); | ||
|
|
||
| const allFiles = []; | ||
| for await (const file of walkDirectory(tmpDir.path, true)) { | ||
| allFiles.push(file); | ||
| } | ||
|
|
||
| expect( | ||
| allFiles | ||
| .map((filePath) => relative(tmpDir.path, filePath).split(sep).join("/")) | ||
| .sort(), | ||
| ).toEqual([ | ||
| "directory", | ||
| "directory/file.txt", | ||
| "directory/file2.txt", | ||
| "directory2", | ||
| "directory2/file.txt", | ||
| "empty-directory", | ||
| "tools", | ||
| "tools/osx64", | ||
| "tools/osx64/java-aarch64", | ||
| "tools/osx64/java-aarch64/bin", | ||
| "tools/osx64/java-aarch64/bin/java", | ||
| ]); | ||
|
|
||
| await expectFile(join(tmpDir.path, "directory", "file.txt"), { | ||
| mode: 0o100644, | ||
| contents: "I am a file\n\n", | ||
| }); | ||
| await expectFile(join(tmpDir.path, "directory", "file2.txt"), { | ||
| mode: 0o100644, | ||
| contents: "I am another file\n\n", | ||
| }); | ||
| await expectFile(join(tmpDir.path, "directory2", "file.txt"), { | ||
| mode: 0o100600, | ||
| contents: "I am secret\n", | ||
| }); | ||
| await expectFile( | ||
| join(tmpDir.path, "tools", "osx64", "java-aarch64", "bin", "java"), | ||
| { | ||
| mode: 0o100755, | ||
| hash: "68b832b5c0397c5baddbbb0a76cf5407b7ea5eee8f84f9ab9488f04a52e529eb", | ||
| }, | ||
| ); | ||
|
|
||
| expect(await pathExists(join(tmpDir.path, "empty-directory"))).toBe(true); | ||
| expect(await readdir(join(tmpDir.path, "empty-directory"))).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| async function expectFile( | ||
| filePath: string, | ||
| { | ||
| mode: expectedMode, | ||
| hash: expectedHash, | ||
| contents: expectedContents, | ||
| }: { | ||
| mode: number; | ||
| hash?: string; | ||
| contents?: string; | ||
| }, | ||
| ) { | ||
| const file = await open(filePath, "r"); | ||
|
|
||
| // Windows doesn't really support file modes | ||
| if (process.platform !== "win32") { | ||
| const stats = await file.stat(); | ||
| expect(stats.mode).toEqual(expectedMode); | ||
| } | ||
|
|
||
| const contents = await file.readFile(); | ||
|
|
||
| if (expectedHash) { | ||
| const hash = await computeHash(contents); | ||
| expect(hash).toEqual(expectedHash); | ||
| } | ||
|
|
||
| if (expectedContents) { | ||
| expect(contents.toString("utf-8")).toEqual(expectedContents); | ||
| } | ||
| } | ||
|
|
||
| async function computeHash(contents: Buffer) { | ||
| const hash = createHash("sha256"); | ||
| hash.update(contents); | ||
|
|
||
| return hash.digest("hex"); | ||
| } | ||
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.