Skip to content

Commit

Permalink
chore: fix ansi escaping for diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Nov 14, 2024
1 parent 409036e commit 5a4a82c
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 63 deletions.
5 changes: 3 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ import { SettingsModel } from './settingsModel';
import { SettingsView } from './settingsView';
import { TestModel, TestModelCollection } from './testModel';
import { TestTree } from './testTree';
import { NodeJSNotFoundError, ansiToHtml, getPlaywrightInfo, stripAnsi, stripBabelFrame } from './utils';
import { NodeJSNotFoundError, getPlaywrightInfo, stripAnsi, stripBabelFrame } from './utils';
import * as vscodeTypes from './vscodeTypes';
import { WorkspaceChange, WorkspaceObserver } from './workspaceObserver';
import { registerTerminalLinkProvider } from './terminalLinkProvider';
import { RunHooks, TestConfig } from './playwrightTestTypes';
import { ansi2html } from './ansi2html';

Check failure on line 32 in src/extension.ts

View workflow job for this annotation

GitHub Actions / Run tests on ubuntu-latest, Node 20

Cannot find module './ansi2html' or its corresponding type declarations.

Check failure on line 32 in src/extension.ts

View workflow job for this annotation

GitHub Actions / Run tests on windows-latest, Node 20

Cannot find module './ansi2html' or its corresponding type declarations.

Check failure on line 32 in src/extension.ts

View workflow job for this annotation

GitHub Actions / Run tests on macos-latest, Node 20

Cannot find module './ansi2html' or its corresponding type declarations.

Check failure on line 32 in src/extension.ts

View workflow job for this annotation

GitHub Actions / Run tests on ubuntu-latest, Node 18

Cannot find module './ansi2html' or its corresponding type declarations.

const stackUtils = new StackUtils({
cwd: '/ensure_absolute_paths'
Expand Down Expand Up @@ -712,7 +713,7 @@ export class Extension implements RunHooks {
const markdownString = new this._vscode.MarkdownString();
markdownString.isTrusted = true;
markdownString.supportHtml = true;
markdownString.appendMarkdown(ansiToHtml(this._linkifyStack(text)));
markdownString.appendMarkdown(ansi2html(this._linkifyStack(text)));
return new this._vscode.TestMessage(markdownString);
}

Expand Down
57 changes: 0 additions & 57 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,63 +40,6 @@ export function stripBabelFrame(text: string) {
return result.join('\n').trim();
}

export function ansiToHtml(text: string): string {
let isOpen = false;
let hasTags = false;
const tokens: string[] = [];
for (let i = 0; i < text.length; ++i) {
const c = text.charAt(i);
if (c === '\u001b') {
hasTags = true;
const end = text.indexOf('m', i + 1);
const code = text.substring(i + 1, end);
if (!code.match(/\[\d+/))
continue;
if (isOpen) {
tokens.push('</span>');
isOpen = false;
}
switch (code) {
case '[2': {
tokens.push(`<span style='color:#666;'>`);
isOpen = true;
break;
}
case '[22': break;
case '[31': {
tokens.push(`<span style='color:#f14c4c;'>`);
isOpen = true;
break;
}
case '[32': {
tokens.push(`<span style='color:#73c991;'>`);
isOpen = true;
break;
}
case '[39': break;
}
i = end;
} else {
if (c === '\n') {
// Don't close to work around html parsing bug.
tokens.push('\n<br>\n');
} else if (c === ' ') {
tokens.push('&nbsp;');
} else {
tokens.push(escapeHTML(c));
}
}
}
// Work around html parsing bugs.
if (hasTags)
tokens.push('\n</span></br>');
return tokens.join('');
}

function escapeHTML(text: string): string {
return text.replace(/[&"<>]/g, c => ({ '&': '&amp;', '"': '&quot;', '<': '<b>&lt;</b>', '>': '<b>&gt;</b>' }[c]!));
}

export async function spawnAsync(executable: string, args: string[], cwd?: string, settingsEnv?: NodeJS.ProcessEnv): Promise<string> {
const childProcess = spawn(executable, args, {
stdio: 'pipe',
Expand Down
5 changes: 1 addition & 4 deletions tests/run-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ test('should show error message', async ({ activate }) => {
Received: <span style='color:#f14c4c;'>1</span>
<br>
at tests/test.spec.ts:4:19
</span></br>
`);
});

Expand All @@ -286,7 +285,7 @@ test('should escape error log', async ({ activate }) => {
const testRun = await testController.run(testItems);

expect(testRun.renderLog({ messages: true })).toContain(
`<b>&lt;</b>div class=&quot;foo bar baz&quot;<b>&gt;</b><b>&lt;</b>/div<b>&gt;`);
`&lt;div class=&quot;foo bar baz&quot;&gt;&lt;/div&gt;`);
});

test('should show soft error messages', async ({ activate }) => {
Expand Down Expand Up @@ -321,7 +320,6 @@ test('should show soft error messages', async ({ activate }) => {
Received: <span style='color:#f14c4c;'>1</span>
<br>
at tests/test.spec.ts:4:24
</span></br>
test.spec.ts:[4:23 - 4:23]
Error: <span style='color:#666;'>expect(</span><span style='color:#f14c4c;'>received</span><span style='color:#666;'>).</span>toBe<span style='color:#666;'>(</span><span style='color:#73c991;'>expected</span><span style='color:#666;'>) // Object.is equality</span>
<br>
Expand All @@ -332,7 +330,6 @@ test('should show soft error messages', async ({ activate }) => {
Received: <span style='color:#f14c4c;'>2</span>
<br>
at tests/test.spec.ts:5:24
</span></br>
`);
});

Expand Down

0 comments on commit 5a4a82c

Please sign in to comment.