From 5cfd4a2d5f13ef983d12e2bb45f48bec30c7d1b5 Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Thu, 2 Feb 2023 15:17:14 -0800 Subject: [PATCH] testing: fix incorrect offsets showing escape codes in output Fixes #166270 --- .../contrib/testing/common/testResult.ts | 20 +++++++++---------- .../test/common/testResultService.test.ts | 10 ++++++++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/vs/workbench/contrib/testing/common/testResult.ts b/src/vs/workbench/contrib/testing/common/testResult.ts index e0e11f2b28906..c136d1f21173f 100644 --- a/src/vs/workbench/contrib/testing/common/testResult.ts +++ b/src/vs/workbench/contrib/testing/common/testResult.ts @@ -156,24 +156,24 @@ export class LiveOutputController { /** * Appends data to the output. */ - public append(data: VSBuffer, marker?: number): Promise | void { + public append(data: VSBuffer, marker?: number): { offset: number; done: Promise | void } { if (this.closed) { - return this.closed; + return { offset: this._offset, done: this.closed }; } + let startOffset = this._offset; if (marker !== undefined) { - data = VSBuffer.concat([ - VSBuffer.fromString(getMarkCode(marker, true)), - data, - VSBuffer.fromString(getMarkCode(marker, false)), - ]); + const start = VSBuffer.fromString(getMarkCode(marker, true)); + const end = VSBuffer.fromString(getMarkCode(marker, false)); + startOffset += start.byteLength; + data = VSBuffer.concat([start, data, end]); } this.previouslyWritten?.push(data); this.dataEmitter.fire(data); this._offset += data.byteLength; - return this.writer.value[0].write(data); + return { offset: startOffset, done: this.writer.value[0].write(data) }; } /** @@ -359,16 +359,16 @@ export class LiveTestResult implements ITestResult { marker = this.testMarkerCounter++; } + const { offset } = this.output.append(output, marker); const message: ITestOutputMessage = { location, message: removeAnsiEscapeCodes(preview), - offset: this.output.offset, + offset, length: output.byteLength, marker: marker, type: TestMessageType.Output, }; - this.output.append(output, marker); const index = this.mustGetTaskIndex(taskId); if (testId) { diff --git a/src/vs/workbench/contrib/testing/test/common/testResultService.test.ts b/src/vs/workbench/contrib/testing/test/common/testResultService.test.ts index 0051972e9b6b1..4d53c4ddd790a 100644 --- a/src/vs/workbench/contrib/testing/test/common/testResultService.test.ts +++ b/src/vs/workbench/contrib/testing/test/common/testResultService.test.ts @@ -367,6 +367,16 @@ suite('Workbench - Test Results Service', () => { assert.deepStrictEqual(await ctrl.getRange(15, 10), VSBuffer.fromString('67890')); }); + test('corrects offsets for marked ranges', async () => { + const ctrl = storage.getOutputController('a'); + + const a1 = ctrl.append(VSBuffer.fromString('12345'), 1); + const a2 = ctrl.append(VSBuffer.fromString('67890'), 1234); + + assert.deepStrictEqual(await ctrl.getRange(a1.offset, 5), VSBuffer.fromString('12345')); + assert.deepStrictEqual(await ctrl.getRange(a2.offset, 5), VSBuffer.fromString('67890')); + }); + test('reads stored output ranges', async () => { const ctrl = storage.getOutputController('a');