Fix race condition in unzip tests
This commit is contained in:
@@ -1,12 +1,7 @@
|
|||||||
import { createHash } from "crypto";
|
import { createHash } from "crypto";
|
||||||
|
import { open } from "fs/promises";
|
||||||
import { join, relative, resolve, sep } from "path";
|
import { join, relative, resolve, sep } from "path";
|
||||||
import {
|
import { pathExists, readdir } from "fs-extra";
|
||||||
createReadStream,
|
|
||||||
pathExists,
|
|
||||||
readdir,
|
|
||||||
readFile,
|
|
||||||
stat,
|
|
||||||
} from "fs-extra";
|
|
||||||
import { dir, DirectoryResult } from "tmp-promise";
|
import { dir, DirectoryResult } from "tmp-promise";
|
||||||
import {
|
import {
|
||||||
excludeDirectories,
|
excludeDirectories,
|
||||||
@@ -169,34 +164,26 @@ async function expectFile(
|
|||||||
contents?: string;
|
contents?: string;
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const stats = await stat(filePath);
|
const file = await open(filePath, "r");
|
||||||
|
|
||||||
|
const stats = await file.stat();
|
||||||
expect(stats.mode).toEqual(expectedMode);
|
expect(stats.mode).toEqual(expectedMode);
|
||||||
|
|
||||||
|
const contents = await file.readFile();
|
||||||
|
|
||||||
if (expectedHash) {
|
if (expectedHash) {
|
||||||
const hash = await computeHash(filePath);
|
const hash = await computeHash(contents);
|
||||||
expect(hash).toEqual(expectedHash);
|
expect(hash).toEqual(expectedHash);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (expectedContents) {
|
if (expectedContents) {
|
||||||
const contents = await readFile(filePath);
|
|
||||||
expect(contents.toString("utf-8")).toEqual(expectedContents);
|
expect(contents.toString("utf-8")).toEqual(expectedContents);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function computeHash(filePath: string) {
|
async function computeHash(contents: Buffer) {
|
||||||
const input = createReadStream(filePath);
|
|
||||||
const hash = createHash("sha256");
|
const hash = createHash("sha256");
|
||||||
|
hash.update(contents);
|
||||||
input.pipe(hash);
|
|
||||||
|
|
||||||
await new Promise((resolve, reject) => {
|
|
||||||
input.on("error", (err) => {
|
|
||||||
reject(err);
|
|
||||||
});
|
|
||||||
input.on("end", () => {
|
|
||||||
resolve(undefined);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return hash.digest("hex");
|
return hash.digest("hex");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user