From 650fdae98e2a3c83c3f7019d9aee3621fe621ad3 Mon Sep 17 00:00:00 2001 From: Ian Thomas Date: Mon, 23 Sep 2024 08:32:34 +0100 Subject: [PATCH] Prevent use of color in command output to file --- src/commands/wasm_command_runner.ts | 2 +- src/environment.ts | 6 ++++-- test/tests/shell.test.ts | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/commands/wasm_command_runner.ts b/src/commands/wasm_command_runner.ts index 4d7ed2f..5e113dc 100644 --- a/src/commands/wasm_command_runner.ts +++ b/src/commands/wasm_command_runner.ts @@ -82,7 +82,7 @@ export abstract class WasmCommandRunner implements ICommandRunner { if (Object.prototype.hasOwnProperty.call(module, 'ENV')) { // Copy environment variables into command. - context.environment.copyIntoCommand(module.ENV); + context.environment.copyIntoCommand(module.ENV, stdout.supportsAnsiEscapes()); } if (Object.prototype.hasOwnProperty.call(module, 'TTY')) { diff --git a/src/environment.ts b/src/environment.ts index 3ac0a1b..4fb949c 100644 --- a/src/environment.ts +++ b/src/environment.ts @@ -31,9 +31,11 @@ export class Environment extends Map { /** * Copy environment variables into a command before it is run. */ - copyIntoCommand(target: { [key: string]: string }) { + copyIntoCommand(target: { [key: string]: string }, supportsAnsiEscapes: boolean) { for (const [key, value] of this.entries()) { - target[key] = value; + if (supportsAnsiEscapes || key !== 'TERM') { + target[key] = value; + } } } diff --git a/test/tests/shell.test.ts b/test/tests/shell.test.ts index aa29bac..ed32088 100644 --- a/test/tests/shell.test.ts +++ b/test/tests/shell.test.ts @@ -40,6 +40,20 @@ test.describe('Shell', () => { expect(output[5]).toMatch('\r\n 2 2 14 out\r\n'); }); + test('should output redirect to file without ansi escapes', async ({ page }) => { + // grep to terminal is colored + const output_direct = await shellLineSimple(page, 'grep of file1', { color: true }); + const start = '\x1B[01;31m\x1B[K'; + const end = '\x1B[m\x1B[K'; + expect(output_direct).toMatch(`\r\nContents ${start}of${end} the file\r\n`); + + // grep to file is not colored + const output_file = await shellLineSimpleN(page, ['grep of file1 > output', 'cat output'], { + color: false + }); + expect(output_file[1]).toMatch(/^cat output\r\nContents of the file\r\n/); + }); + test('should input redirect from file', async ({ page }) => { expect(await shellLineSimple(page, 'wc < file2')).toMatch(' 1 5 27\r\n'); });