mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-01-22 22:44:13 +08:00
fix: Strip ANSI color sequences from tool output
Add stripAnsiCodes function to sanitizer and apply it in formatResultContent to remove terminal color escape codes (e.g., [1;33m for yellow/bold) from tool output before displaying it in GitHub comments. This ensures clean, readable output without raw ANSI escape sequences appearing in the formatted tool results.
This commit is contained in:
@@ -111,6 +111,27 @@ describe("formatResultContent", () => {
|
||||
const result = formatResultContent(JSON.stringify(structuredContent));
|
||||
expect(result).toBe("**→** Hello world\n\n");
|
||||
});
|
||||
|
||||
test("strips ANSI color codes from terminal output", () => {
|
||||
// Test bold yellow warning (the issue reported: [1;33m)
|
||||
const coloredOutput = "\x1B[1;33mWarning: something happened\x1B[0m";
|
||||
const result = formatResultContent(coloredOutput);
|
||||
expect(result).toBe("**→** Warning: something happened\n\n");
|
||||
expect(result).not.toContain("\x1B");
|
||||
expect(result).not.toContain("[1;33m");
|
||||
});
|
||||
|
||||
test("strips ANSI codes from longer output in code blocks", () => {
|
||||
const longColoredOutput =
|
||||
"\x1B[32m✓\x1B[0m Test 1 passed\n" +
|
||||
"\x1B[32m✓\x1B[0m Test 2 passed\n" +
|
||||
"\x1B[31m✗\x1B[0m Test 3 failed\n" +
|
||||
"Some additional output to make it longer";
|
||||
const result = formatResultContent(longColoredOutput);
|
||||
expect(result).toContain("✓ Test 1 passed");
|
||||
expect(result).toContain("✗ Test 3 failed");
|
||||
expect(result).not.toContain("\x1B");
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatToolWithResult", () => {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
import {
|
||||
stripAnsiCodes,
|
||||
stripInvisibleCharacters,
|
||||
stripMarkdownImageAltText,
|
||||
stripMarkdownLinkTitles,
|
||||
@@ -10,6 +11,51 @@ import {
|
||||
redactGitHubTokens,
|
||||
} from "../src/github/utils/sanitizer";
|
||||
|
||||
describe("stripAnsiCodes", () => {
|
||||
it("should remove color codes", () => {
|
||||
// Bold yellow text: \x1B[1;33m
|
||||
expect(stripAnsiCodes("\x1B[1;33mWarning\x1B[0m")).toBe("Warning");
|
||||
// Red text: \x1B[31m
|
||||
expect(stripAnsiCodes("\x1B[31mError\x1B[0m")).toBe("Error");
|
||||
// Green text: \x1B[32m
|
||||
expect(stripAnsiCodes("\x1B[32mSuccess\x1B[0m")).toBe("Success");
|
||||
});
|
||||
|
||||
it("should remove bold and other style codes", () => {
|
||||
// Bold: \x1B[1m
|
||||
expect(stripAnsiCodes("\x1B[1mBold text\x1B[0m")).toBe("Bold text");
|
||||
// Underline: \x1B[4m
|
||||
expect(stripAnsiCodes("\x1B[4mUnderlined\x1B[0m")).toBe("Underlined");
|
||||
});
|
||||
|
||||
it("should remove cursor movement codes", () => {
|
||||
// Clear line: \x1B[K
|
||||
expect(stripAnsiCodes("Text\x1B[K")).toBe("Text");
|
||||
// Cursor up: \x1B[A
|
||||
expect(stripAnsiCodes("Line1\x1B[ALine2")).toBe("Line1Line2");
|
||||
});
|
||||
|
||||
it("should handle multiple ANSI codes in one string", () => {
|
||||
const input = "\x1B[1;31mError:\x1B[0m \x1B[33mWarning\x1B[0m text";
|
||||
expect(stripAnsiCodes(input)).toBe("Error: Warning text");
|
||||
});
|
||||
|
||||
it("should preserve text without ANSI codes", () => {
|
||||
expect(stripAnsiCodes("Normal text")).toBe("Normal text");
|
||||
expect(stripAnsiCodes("Text with [brackets]")).toBe("Text with [brackets]");
|
||||
});
|
||||
|
||||
it("should handle empty string", () => {
|
||||
expect(stripAnsiCodes("")).toBe("");
|
||||
});
|
||||
|
||||
it("should handle complex terminal output", () => {
|
||||
// Simulates npm/yarn output with colors
|
||||
const input = "\x1B[2K\x1B[1G\x1B[32m✓\x1B[0m Tests passed";
|
||||
expect(stripAnsiCodes(input)).toBe("✓ Tests passed");
|
||||
});
|
||||
});
|
||||
|
||||
describe("stripInvisibleCharacters", () => {
|
||||
it("should remove zero-width characters", () => {
|
||||
expect(stripInvisibleCharacters("Hello\u200BWorld")).toBe("HelloWorld");
|
||||
|
||||
Reference in New Issue
Block a user