Skip to content

Commit afebc1d

Browse files
move the subprocess code in a single file and modify test config variables
1 parent 4f70a72 commit afebc1d

File tree

5 files changed

+102
-95
lines changed

5 files changed

+102
-95
lines changed

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

-80
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,79 @@
1-
import { main } from "./send-to-sentry.js";
1+
import type { Event } from "@sentry/node";
22

3-
await main();
3+
import { writeUtf8File } from "@ignored/hardhat-vnext-utils/fs";
4+
import { captureEvent, captureMessage, init } from "@sentry/node";
5+
6+
import { Anonymizer } from "./anonymizer.js";
7+
import { SENTRY_DSN } from "./reporter.js";
8+
9+
try {
10+
init({
11+
dsn: SENTRY_DSN,
12+
});
13+
} catch (error) {
14+
process.exit(1);
15+
}
16+
17+
const serializedEvent = process.argv[2];
18+
const configPath = process.argv[3];
19+
20+
if (serializedEvent === undefined) {
21+
await sendMsgToSentry(
22+
"There was an error parsing an event: 'process.argv[2]' argument is not set",
23+
);
24+
25+
process.exit(1);
26+
}
27+
28+
let event: any;
29+
try {
30+
event = JSON.parse(serializedEvent);
31+
} catch {
32+
await sendMsgToSentry(
33+
"There was an error parsing an event: 'process.argv[2]' doesn't have a valid JSON",
34+
);
35+
36+
process.exit(1);
37+
}
38+
39+
try {
40+
const anonymizer = new Anonymizer(configPath);
41+
const anonymizedEvent = await anonymizer.anonymize(event);
42+
43+
if (anonymizedEvent.isRight()) {
44+
if (anonymizer.raisedByHardhat(anonymizedEvent.value)) {
45+
await sendEventToSentry(anonymizedEvent.value);
46+
}
47+
} else {
48+
await sendMsgToSentry(
49+
`There was an error anonymizing an event: ${anonymizedEvent.value}`,
50+
);
51+
}
52+
} catch (error: any) {
53+
await sendMsgToSentry(
54+
`There was an error capturing an event: ${error.message}`,
55+
);
56+
}
57+
58+
async function sendMsgToSentry(msg: string) {
59+
if (process.env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH !== undefined) {
60+
// ATTENTION: only for testing
61+
await writeUtf8File(process.env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH, msg);
62+
return;
63+
}
64+
65+
captureMessage(msg);
66+
}
67+
68+
async function sendEventToSentry(sentryEvent: Event) {
69+
if (process.env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH !== undefined) {
70+
// ATTENTION: only for testing
71+
await writeUtf8File(
72+
process.env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH,
73+
JSON.stringify(sentryEvent),
74+
);
75+
return;
76+
}
77+
78+
captureEvent(sentryEvent);
79+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function checkIfSubprocessWasExecuted(
2929
// Checks if the subprocess was executed by waiting for a file to be created.
3030
// Uses an interval to periodically check for the file. If the file isn't found
3131
// within a specified number of attempts, an error is thrown, indicating a failure in subprocess execution.
32-
const MAX_COUNTER = 20;
32+
const MAX_COUNTER = 100;
3333

3434
return new Promise((resolve, reject) => {
3535
let counter = 0;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const TEST_HARDHAT_ERROR = new HardhatError({
6767

6868
const PATH_TO_FIXTURE = path.join(ROOT_PATH_TO_FIXTURE, "sentry");
6969

70-
const RESULT_FILE_PATH = path.join(PATH_TO_FIXTURE, "result.txt");
70+
const RESULT_FILE_PATH = path.join(PATH_TO_FIXTURE, "reporter-result.txt");
7171

7272
describe("Reporter", () => {
7373
beforeEach(async () => {

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

+22-11
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,50 @@ import path from "node:path";
33
import { afterEach, beforeEach, describe, it } from "node:test";
44

55
import { readUtf8File, remove } from "@ignored/hardhat-vnext-utils/fs";
6+
import { spawnDetachedSubProcess } from "@ignored/hardhat-vnext-utils/subprocess";
67

7-
import { main } from "../../../../../src/internal/cli/telemetry/sentry/send-to-sentry.js";
88
import {
99
checkIfSubprocessWasExecuted,
1010
ROOT_PATH_TO_FIXTURE,
1111
} from "../helpers.js";
1212

1313
const PATH_TO_FIXTURE = path.join(ROOT_PATH_TO_FIXTURE, "sentry");
1414
const RESULT_FILE_PATH = path.join(PATH_TO_FIXTURE, "sub-process-result.txt");
15+
const SUBPROCESS_FILE_PATH = path.join(
16+
process.cwd(),
17+
"src",
18+
"internal",
19+
"cli",
20+
"telemetry",
21+
"sentry",
22+
"subprocess.ts",
23+
);
1524

1625
describe("sentry-subprocess", function () {
1726
beforeEach(async () => {
18-
process.env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH = RESULT_FILE_PATH;
1927
await remove(RESULT_FILE_PATH);
2028
});
2129

2230
afterEach(async () => {
23-
delete process.env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH;
2431
await remove(RESULT_FILE_PATH);
2532
});
2633

2734
it("should send an event to Sentry", async () => {
28-
process.argv[2] = "{}"; // Send an empty object, the real payload will be tested in the tests for the "Reporter"
35+
await spawnDetachedSubProcess(SUBPROCESS_FILE_PATH, ["{}"], {
36+
HARDHAT_TEST_SUBPROCESS_RESULT_PATH: RESULT_FILE_PATH,
37+
});
2938

30-
await main();
3139
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH);
3240
const fileRes = await readUtf8File(RESULT_FILE_PATH);
3341

3442
assert.equal(fileRes, "{}");
3543
});
3644

3745
it("should send a failure message to Sentry because no argument is passed in argv[2] (serializedEvent)", async () => {
38-
delete process.argv[2];
46+
await spawnDetachedSubProcess(SUBPROCESS_FILE_PATH, [], {
47+
HARDHAT_TEST_SUBPROCESS_RESULT_PATH: RESULT_FILE_PATH,
48+
});
3949

40-
await main();
4150
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH);
4251
const fileRes = await readUtf8File(RESULT_FILE_PATH);
4352

@@ -48,9 +57,10 @@ describe("sentry-subprocess", function () {
4857
});
4958

5059
it("should send a failure message to Sentry because the argument in argv[2] is not a valid JSON", async () => {
51-
process.argv[2] = "not a valid JSON";
60+
await spawnDetachedSubProcess(SUBPROCESS_FILE_PATH, ["not a valid JSON"], {
61+
HARDHAT_TEST_SUBPROCESS_RESULT_PATH: RESULT_FILE_PATH,
62+
});
5263

53-
await main();
5464
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH);
5565
const fileRes = await readUtf8File(RESULT_FILE_PATH);
5666

@@ -61,9 +71,10 @@ describe("sentry-subprocess", function () {
6171
});
6272

6373
it("should send a failure message to Sentry because there is an anonymization error", async () => {
64-
process.argv[2] = "null";
74+
await spawnDetachedSubProcess(SUBPROCESS_FILE_PATH, ["null"], {
75+
HARDHAT_TEST_SUBPROCESS_RESULT_PATH: RESULT_FILE_PATH,
76+
});
6577

66-
await main();
6778
await checkIfSubprocessWasExecuted(RESULT_FILE_PATH);
6879
const fileRes = await readUtf8File(RESULT_FILE_PATH);
6980

0 commit comments

Comments
 (0)