|
1 | 1 | import { createHash } from "crypto"; |
| 2 | +import { open } from "fs/promises"; |
2 | 3 | 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"; |
10 | 5 | import { dir, DirectoryResult } from "tmp-promise"; |
11 | 6 | import { |
12 | 7 | excludeDirectories, |
@@ -169,34 +164,26 @@ async function expectFile( |
169 | 164 | contents?: string; |
170 | 165 | }, |
171 | 166 | ) { |
172 | | - const stats = await stat(filePath); |
| 167 | + const file = await open(filePath, "r"); |
| 168 | + |
| 169 | + const stats = await file.stat(); |
173 | 170 | expect(stats.mode).toEqual(expectedMode); |
174 | 171 |
|
| 172 | + const contents = await file.readFile(); |
| 173 | + |
175 | 174 | if (expectedHash) { |
176 | | - const hash = await computeHash(filePath); |
| 175 | + const hash = await computeHash(contents); |
177 | 176 | expect(hash).toEqual(expectedHash); |
178 | 177 | } |
179 | 178 |
|
180 | 179 | if (expectedContents) { |
181 | | - const contents = await readFile(filePath); |
182 | 180 | expect(contents.toString("utf-8")).toEqual(expectedContents); |
183 | 181 | } |
184 | 182 | } |
185 | 183 |
|
186 | | -async function computeHash(filePath: string) { |
187 | | - const input = createReadStream(filePath); |
| 184 | +async function computeHash(contents: Buffer) { |
188 | 185 | 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); |
200 | 187 |
|
201 | 188 | return hash.digest("hex"); |
202 | 189 | } |
0 commit comments