-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsentry.cli.test.js
168 lines (146 loc) · 5.64 KB
/
sentry.cli.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"use strict";
import {
dirname as pathDirname,
join as pathJoin,
resolve as pathResolve,
} from "node:path";
import { spawn } from "node:child_process";
import { createServer } from "node:http";
import { test } from "tap";
import { once } from "node:events";
import { fileURLToPath } from "node:url";
const SENTRY_DSN = "http://[email protected]/1234";
const __dirname = pathDirname(fileURLToPath(import.meta.url));
const cliPath = pathResolve(pathJoin(__dirname, "..", "bin", "cli.js"));
const nodeBinaryPath = process.argv[0];
const errorLine =
'{"level":50,"time":1597399283686,"pid":35269,"hostname":"Gregors-MacBook-Pro.local","name":"probot","status":500,"event":{"event":"installation_repositories.added","id":"123","installation":456},"headers":{"x-github-request-id":"789"},"request":{"headers":{"accept":"application/vnd.github.v3+json","authorization":"[Filtered]","user-agent":"probot/10.0.0"},"method":"GET","url":"https://api.github.com/repos/octocat/hello-world/"},"stack":"Error: Oops\\n at Object.<anonymous> (/Users/gregor/Projects/probot/pino/example.js:37:15)\\n at Module._compile (internal/modules/cjs/loader.js:1137:30)\\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)\\n at Module.load (internal/modules/cjs/loader.js:985:32)\\n at Function.Module._load (internal/modules/cjs/loader.js:878:14)\\n at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)\\n at internal/main/run_main_module.js:17:47","type":"Error","msg":"Oops"}\n';
const fatalErrorLine =
'{"level":60,"time":1597426544906,"pid":43024,"hostname":"Gregors-MacBook-Pro.local","name":"probot","stack":"Error: Oh no!\\n at Object.<anonymous> (/Users/gregor/Projects/probot/pino/example.js:59:12)\\n at Module._compile (internal/modules/cjs/loader.js:1137:30)\\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)\\n at Module.load (internal/modules/cjs/loader.js:985:32)\\n at Function.Module._load (internal/modules/cjs/loader.js:878:14)\\n at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)\\n at internal/main/run_main_module.js:17:47","type":"Error","msg":"Oh no!"}\n';
const stripAnsiColorRE =
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
const env = {
// disable colors
TERM: "dumb",
};
test("SENTRY_DSN", (t) => {
t.plan(2);
t.test("SENTRY_DSN with ERROR error", async (t) => {
t.plan(2);
const server = createServer((request, response) => {
// we can access HTTP headers
let body = "";
request.on("data", (chunk) => {
body += chunk.toString();
});
request.on("end", () => {
const data = JSON.parse(body.split("\n")[2]);
const error = data.exception.values[0];
t.equal(error.type, "Error");
t.equal(error.value, "Oops");
t.strictSame(data.extra, {
event: {
event: "installation_repositories.added",
id: "123",
installation: 456,
},
headers: { "x-github-request-id": "789" },
request: {
headers: {
accept: "application/vnd.github.v3+json",
authorization: "[Filtered]",
"user-agent": "probot/10.0.0",
},
method: "GET",
url: "https://api.github.com/repos/octocat/hello-world/",
},
status: 500,
});
});
response.writeHead(200);
response.write("ok");
response.end();
});
server.listen(0);
await once(server, "listening");
const child = spawn(nodeBinaryPath, [cliPath], {
env: {
...env,
SENTRY_DSN,
},
});
child.on("error", t.threw);
child.stdout.on("data", (data) => {
const errorStringLines = data
.toString()
.replace(stripAnsiColorRE, "")
.split(/\n/);
t.equal(errorStringLines[0].trim(), "ERROR (probot): Oops");
// skip the error stack, normalize Sentry Event ID, compare error details only
t.equal(
errorStringLines
.slice(9)
.join("\n")
.trim()
.replace(/sentryEventId: \w+$/, "sentryEventId: 123"),
`event: {
id: "123"
}
status: 500
headers: {
x-github-request-id: "789"
}
request: {
method: "GET"
url: "https://api.github.com/repos/octocat/hello-world/"
}
sentryEventId: 123`,
);
});
child.stdin.write(errorLine);
t.teardown(() => {
child.kill();
server.closeAllConnections();
server.close();
});
});
t.test("SENTRY_DSN with FATAL error", async (t) => {
t.plan(1);
const server = createServer((request, response) => {
let body = "";
request.on("data", (chunk) => {
body += chunk.toString();
});
request.on("end", () => {
const data = JSON.parse(body.split("\n")[2]);
const error = data.exception.values[0];
t.equal(error.type, "Error");
t.equal(error.value, "Oh no!");
});
response.writeHead(200);
response.write("ok");
response.end();
});
server.listen(0);
await once(server, "listening");
const child = spawn(nodeBinaryPath, [cliPath], {
env: {
...env,
SENTRY_DSN,
},
});
child.on("error", t.threw);
child.stdout.on("data", (data) => {
t.match(
data.toString().replace(stripAnsiColorRE, ""),
/^FATAL \(probot\): Oh no!\n/,
);
});
child.stdin.write(fatalErrorLine);
t.teardown(() => {
child.kill();
server.closeAllConnections();
server.close();
});
});
});