Skip to content

Commit a80098d

Browse files
committed
Fix race condition in unzip tests
1 parent f45b790 commit a80098d

1 file changed

Lines changed: 10 additions & 23 deletions

File tree

extensions/ql-vscode/test/unit-tests/common/unzip.test.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import { createHash } from "crypto";
2+
import { open } from "fs/promises";
23
import { join, relative, resolve, sep } from "path";
3-
import {
4-
createReadStream,
5-
pathExists,
6-
readdir,
7-
readFile,
8-
stat,
9-
} from "fs-extra";
4+
import { pathExists, readdir } from "fs-extra";
105
import { dir, DirectoryResult } from "tmp-promise";
116
import {
127
excludeDirectories,
@@ -169,34 +164,26 @@ async function expectFile(
169164
contents?: string;
170165
},
171166
) {
172-
const stats = await stat(filePath);
167+
const file = await open(filePath, "r");
168+
169+
const stats = await file.stat();
173170
expect(stats.mode).toEqual(expectedMode);
174171

172+
const contents = await file.readFile();
173+
175174
if (expectedHash) {
176-
const hash = await computeHash(filePath);
175+
const hash = await computeHash(contents);
177176
expect(hash).toEqual(expectedHash);
178177
}
179178

180179
if (expectedContents) {
181-
const contents = await readFile(filePath);
182180
expect(contents.toString("utf-8")).toEqual(expectedContents);
183181
}
184182
}
185183

186-
async function computeHash(filePath: string) {
187-
const input = createReadStream(filePath);
184+
async function computeHash(contents: Buffer) {
188185
const hash = createHash("sha256");
189-
190-
input.pipe(hash);
191-
192-
await new Promise((resolve, reject) => {
193-
input.on("error", (err) => {
194-
reject(err);
195-
});
196-
input.on("end", () => {
197-
resolve(undefined);
198-
});
199-
});
186+
hash.update(contents);
200187

201188
return hash.digest("hex");
202189
}

0 commit comments

Comments
 (0)