Skip to content

Commit b6b8277

Browse files
convert all subprocess files in json files
1 parent a0d248b commit b6b8277

File tree

5 files changed

+27
-41
lines changed

5 files changed

+27
-41
lines changed

v-next/hardhat/src/internal/cli/telemetry/sentry/subprocess.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Event } from "@sentry/node";
22

3-
import { writeUtf8File } from "@ignored/hardhat-vnext-utils/fs";
3+
import { writeJsonFile } from "@ignored/hardhat-vnext-utils/fs";
44
import { captureEvent, captureMessage, init } from "@sentry/node";
55

66
import { Anonymizer } from "./anonymizer.js";
@@ -58,7 +58,9 @@ try {
5858
async function sendMsgToSentry(msg: string) {
5959
if (process.env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH !== undefined) {
6060
// ATTENTION: only for testing
61-
await writeUtf8File(process.env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH, msg);
61+
await writeJsonFile(process.env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH, {
62+
msg,
63+
});
6264
return;
6365
}
6466

@@ -68,10 +70,7 @@ async function sendMsgToSentry(msg: string) {
6870
async function sendEventToSentry(e: Event) {
6971
if (process.env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH !== undefined) {
7072
// ATTENTION: only for testing
71-
await writeUtf8File(
72-
process.env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH,
73-
JSON.stringify(e),
74-
);
73+
await writeJsonFile(process.env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH, e);
7574
return;
7675
}
7776

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

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

73-
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH, true);
73+
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH);
7474

7575
const result = await readJsonFile(RESULT_FILE_PATH);
7676

@@ -92,7 +92,7 @@ describe("analytics", () => {
9292
it("should create the correct payload for the telemetry consent (negative consent)", async () => {
9393
await sendTelemetryConsentAnalytics(false);
9494

95-
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH, true);
95+
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH);
9696

9797
const result = await readJsonFile(RESULT_FILE_PATH);
9898

@@ -114,7 +114,7 @@ describe("analytics", () => {
114114
it("should create the correct payload for the task analytics", async () => {
115115
const wasSent = await sendTaskAnalytics(["task", "subtask"]);
116116

117-
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH, true);
117+
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH);
118118

119119
const result: Payload = await readJsonFile(RESULT_FILE_PATH);
120120

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

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

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

95
export const ROOT_PATH_TO_FIXTURE: string = path.join(
106
process.cwd(),
@@ -24,7 +20,6 @@ export const TELEMETRY_FOLDER_PATH: string = path.join(
2420

2521
export async function checkIfSubprocessWasExecuted(
2622
resultFilePath: string,
27-
isJsonFile: boolean = false,
2823
): Promise<boolean> {
2924
// Checks if the subprocess was executed by waiting for a file to be created.
3025
// Uses an interval to periodically check for the file. If the file isn't found
@@ -40,12 +35,7 @@ export async function checkIfSubprocessWasExecuted(
4035
if (await exists(resultFilePath)) {
4136
try {
4237
// 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-
38+
await readJsonFile(resultFilePath);
4939
clearInterval(intervalId);
5040
resolve(true);
5141
} catch (_err) {}

v-next/hardhat/test/internal/cli/telemetry/sentry/reporter.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ describe("Reporter", () => {
134134

135135
const wasSent = await sendErrorTelemetry(HARDHAT_ERROR);
136136

137-
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH, true);
137+
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH);
138138

139139
const resFile: SentryEvent = await readJsonFile(RESULT_FILE_PATH);
140140

@@ -176,7 +176,7 @@ describe("Reporter", () => {
176176

177177
const wasSent = await sendErrorTelemetry(ERROR);
178178

179-
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH, true);
179+
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH);
180180

181181
const resFile: SentryEvent = await readJsonFile(RESULT_FILE_PATH);
182182

v-next/hardhat/test/internal/cli/telemetry/sentry/subprocess.ts

+15-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from "node:assert/strict";
22
import path from "node:path";
33
import { afterEach, beforeEach, describe, it } from "node:test";
44

5-
import { readUtf8File, remove } from "@ignored/hardhat-vnext-utils/fs";
5+
import { readJsonFile, remove } from "@ignored/hardhat-vnext-utils/fs";
66
import { spawnDetachedSubProcess } from "@ignored/hardhat-vnext-utils/subprocess";
77

88
import {
@@ -40,9 +40,9 @@ describe("sentry-subprocess", function () {
4040
});
4141

4242
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH);
43-
const fileRes = await readUtf8File(RESULT_FILE_PATH);
43+
const fileRes = await readJsonFile(RESULT_FILE_PATH);
4444

45-
assert.equal(fileRes, "{}");
45+
assert.deepEqual(fileRes, {});
4646
});
4747

4848
it("should send a failure message to Sentry because no argument is passed in argv[2] (serializedEvent)", async () => {
@@ -51,12 +51,11 @@ describe("sentry-subprocess", function () {
5151
});
5252

5353
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH);
54-
const fileRes = await readUtf8File(RESULT_FILE_PATH);
54+
const fileRes = await readJsonFile(RESULT_FILE_PATH);
5555

56-
assert.equal(
57-
fileRes,
58-
"There was an error parsing an event: 'process.argv[2]' argument is not set",
59-
);
56+
assert.deepEqual(fileRes, {
57+
msg: "There was an error parsing an event: 'process.argv[2]' argument is not set",
58+
});
6059
});
6160

6261
it("should send a failure message to Sentry because the argument in argv[2] is not a valid JSON", async () => {
@@ -65,12 +64,11 @@ describe("sentry-subprocess", function () {
6564
});
6665

6766
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH);
68-
const fileRes = await readUtf8File(RESULT_FILE_PATH);
67+
const fileRes = await readJsonFile(RESULT_FILE_PATH);
6968

70-
assert.equal(
71-
fileRes,
72-
"There was an error parsing an event: 'process.argv[2]' doesn't have a valid JSON",
73-
);
69+
assert.deepEqual(fileRes, {
70+
msg: "There was an error parsing an event: 'process.argv[2]' doesn't have a valid JSON",
71+
});
7472
});
7573

7674
it("should send a failure message to Sentry because there is an anonymization error", async () => {
@@ -79,11 +77,10 @@ describe("sentry-subprocess", function () {
7977
});
8078

8179
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH);
82-
const fileRes = await readUtf8File(RESULT_FILE_PATH);
80+
const fileRes = await readJsonFile(RESULT_FILE_PATH);
8381

84-
assert.equal(
85-
fileRes,
86-
"There was an error anonymizing an event: event is null or undefined",
87-
);
82+
assert.deepEqual(fileRes, {
83+
msg: "There was an error anonymizing an event: event is null or undefined",
84+
});
8885
});
8986
});

0 commit comments

Comments
 (0)