Skip to content

Commit

Permalink
strip ansi color
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Aug 29, 2024
1 parent 0946c2c commit 974e97e
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const errorLine =
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",
Expand All @@ -27,7 +29,7 @@ test("cli", (t) => {
const child = spawn(nodeBinaryPath, [cliPath], { env });
child.on("error", t.threw);
child.stdout.on("data", (data) => {
t.equal(data.toString(), `INFO (probot): hello future\n`);
t.equal(data.toString().replace(stripAnsiColorRE, ''), `INFO (probot): hello future\n`);
});
child.stdin.write(logLine);
t.teardown(() => child.kill());
Expand All @@ -39,9 +41,9 @@ test("cli", (t) => {
const child = spawn(nodeBinaryPath, [cliPath], { env });
child.on("error", t.threw);
child.stdout.on("data", (data) => {
t.match(data.toString(), /event: "installation_repositories.added"/);
t.match(data.toString(), /status: 500/);
t.match(data.toString(), /x-github-request-id: "789"/);
t.match(data.toString().replace(stripAnsiColorRE, ''), /event: "installation_repositories.added"/);
t.match(data.toString().replace(stripAnsiColorRE, ''), /status: 500/);
t.match(data.toString().replace(stripAnsiColorRE, ''), /x-github-request-id: "789"/);
t.match(
data.toString(),
/url: "https:\/\/api.github.com\/repos\/octocat\/hello-world\/"/
Expand All @@ -58,7 +60,7 @@ test("cli", (t) => {
});
child.on("error", t.threw);
child.stdout.on("data", (data) => {
t.equal(data.toString(), logLine);
t.equal(data.toString().replace(stripAnsiColorRE, ''), logLine);
});
child.stdin.write(logLine);
t.teardown(() => child.kill());
Expand All @@ -71,7 +73,7 @@ test("cli", (t) => {
});
child.on("error", t.threw);
child.stdout.on("data", (data) => {
t.equal(data.toString(), logLine.replace('"level":30', '"level":"info"'));
t.equal(data.toString().replace(stripAnsiColorRE, ''), logLine.replace('"level":30', '"level":"info"'));
});
child.stdin.write(logLine);
t.teardown(() => child.kill());
Expand Down Expand Up @@ -128,7 +130,7 @@ test("cli", (t) => {
});
child.on("error", t.threw);
child.stdout.on("data", (data) => {
const errorStringLines = data.toString().split(/\n/);
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
Expand Down Expand Up @@ -190,7 +192,7 @@ sentryEventId: 123`
});
child.on("error", t.threw);
child.stdout.on("data", (data) => {
t.match(data.toString(), /^FATAL \(probot\): Oh no!\n/);
t.match(data.toString().replace(stripAnsiColorRE, ''), /^FATAL \(probot\): Oh no!\n/);
});
child.stdin.write(fatalErrorLine);

Expand Down

0 comments on commit 974e97e

Please sign in to comment.