Skip to content

Commit 39a1511

Browse files
add logic to wait for the file to be readable
1 parent 3e59fa5 commit 39a1511

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

v-next/hardhat/test/internal/cli/telemetry/analytics/analytics.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe("analytics", () => {
6767
it("should create the correct payload for the telemetry consent (positive consent)", async () => {
6868
await sendTelemetryConsentAnalytics(true);
6969

70-
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH);
70+
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH, true);
7171

7272
const result = await readJsonFile(RESULT_FILE_PATH);
7373

@@ -89,7 +89,7 @@ describe("analytics", () => {
8989
it("should create the correct payload for the telemetry consent (negative consent)", async () => {
9090
await sendTelemetryConsentAnalytics(false);
9191

92-
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH);
92+
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH, true);
9393

9494
const result = await readJsonFile(RESULT_FILE_PATH);
9595

@@ -111,7 +111,7 @@ describe("analytics", () => {
111111
it("should create the correct payload for the task analytics", async () => {
112112
const wasSent = await sendTaskAnalytics(["task", "subtask"]);
113113

114-
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH);
114+
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH, true);
115115

116116
const result: Payload = await readJsonFile(RESULT_FILE_PATH);
117117

v-next/hardhat/test/internal/cli/telemetry/helpers.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import path from "node:path";
22

3-
import { exists, readUtf8File } from "@ignored/hardhat-vnext-utils/fs";
3+
import {
4+
exists,
5+
readJsonFile,
6+
readUtf8File,
7+
} from "@ignored/hardhat-vnext-utils/fs";
48

59
export const ROOT_PATH_TO_FIXTURE: string = path.join(
610
process.cwd(),
@@ -20,6 +24,7 @@ export const TELEMETRY_FOLDER_PATH: string = path.join(
2024

2125
export async function checkIfSubprocessWasExecuted(
2226
resultFilePath: string,
27+
isJsonFile: boolean = false,
2328
): Promise<boolean> {
2429
// Checks if the subprocess was executed by waiting for a file to be created.
2530
// Uses an interval to periodically check for the file. If the file isn't found
@@ -34,7 +39,13 @@ export async function checkIfSubprocessWasExecuted(
3439

3540
if (await exists(resultFilePath)) {
3641
try {
37-
await readUtf8File(resultFilePath); // Wait for the file to be readable
42+
// Wait for the file to be readable. The file could exist but the writing could be in progress.
43+
if (isJsonFile) {
44+
await readJsonFile(resultFilePath);
45+
} else {
46+
await readUtf8File(resultFilePath);
47+
}
48+
3849
clearInterval(intervalId);
3950
resolve(true);
4051
} catch (_err) {}

0 commit comments

Comments
 (0)