diff --git a/src/core/commandRunner/CommandRunner.ts b/src/core/commandRunner/CommandRunner.ts index 6ed3156b58..ac58f3b95f 100644 --- a/src/core/commandRunner/CommandRunner.ts +++ b/src/core/commandRunner/CommandRunner.ts @@ -177,7 +177,9 @@ export default class CommandRunner { console.error(err.stack); throw err; } finally { - this.graph.testCaseRecorder.finallyHook(); + if (this.graph.testCaseRecorder.isActive()) { + this.graph.testCaseRecorder.finallyHook(); + } } } diff --git a/src/test/suite/recorded.test.ts b/src/test/suite/recorded.test.ts index 999101bba8..35a72da40c 100644 --- a/src/test/suite/recorded.test.ts +++ b/src/test/suite/recorded.test.ts @@ -1,5 +1,5 @@ -import { promises as fsp } from "fs"; import { assert } from "chai"; +import { promises as fsp } from "fs"; import * as yaml from "js-yaml"; import * as vscode from "vscode"; import HatTokenMap from "../../core/HatTokenMap"; @@ -13,6 +13,7 @@ import { takeSnapshot, } from "../../testUtil/takeSnapshot"; import { TestCaseFixture } from "../../testUtil/TestCase"; +import { DEFAULT_TEXT_EDITOR_OPTIONS_FOR_TEST } from "../../testUtil/testConstants"; import { marksToPlainObject, PositionPlainObject, @@ -74,6 +75,9 @@ async function runTest(file: string) { fixture.languageId, ); + // Override any user settings and make sure tests run with default tabs. + editor.options = DEFAULT_TEXT_EDITOR_OPTIONS_FOR_TEST; + if (fixture.postEditorOpenSleepTimeMs != null) { await sleepWithBackoff(fixture.postEditorOpenSleepTimeMs); } diff --git a/src/testUtil/TestCaseRecorder.ts b/src/testUtil/TestCaseRecorder.ts index 69fdcd4c77..289c9319be 100644 --- a/src/testUtil/TestCaseRecorder.ts +++ b/src/testUtil/TestCaseRecorder.ts @@ -14,6 +14,7 @@ import { extractTargetedMarks } from "./extractTargetedMarks"; import serialize from "./serialize"; import { ExtraSnapshotField, takeSnapshot } from "./takeSnapshot"; import { TestCase, TestCaseCommand, TestCaseContext } from "./TestCase"; +import { DEFAULT_TEXT_EDITOR_OPTIONS_FOR_TEST } from "./testConstants"; import { marksToPlainObject, SerializedMarks } from "./toPlainObject"; import { walkDirsSync } from "./walkSync"; @@ -82,6 +83,8 @@ export class TestCaseRecorder { private extraSnapshotFields?: ExtraSnapshotField[]; private paused: boolean = false; private isErrorTest: boolean = false; + /** We use this variable to capture editor settings and then restore them */ + private originalTextEditorOptions: vscode.TextEditorOptions = {}; private calibrationStyle = vscode.window.createTextEditorDecorationType({ backgroundColor: CALIBRATION_DISPLAY_BACKGROUND_COLOR, }); @@ -313,6 +316,12 @@ export class TestCaseRecorder { ); await this.testCase.recordInitialState(); + + const editor = vscode.window.activeTextEditor!; + // NB: We need to copy the editor options rather than storing a reference + // because its properties are lazy + this.originalTextEditorOptions = { ...editor.options }; + editor.options = DEFAULT_TEXT_EDITOR_OPTIONS_FOR_TEST; } } @@ -439,6 +448,9 @@ export class TestCaseRecorder { finallyHook() { this.spyInfo?.dispose(); this.spyInfo = undefined; + + const editor = vscode.window.activeTextEditor!; + editor.options = this.originalTextEditorOptions; } dispose() { diff --git a/src/testUtil/testConstants.ts b/src/testUtil/testConstants.ts new file mode 100644 index 0000000000..22bc06de80 --- /dev/null +++ b/src/testUtil/testConstants.ts @@ -0,0 +1,6 @@ +import * as vscode from "vscode"; + +export const DEFAULT_TEXT_EDITOR_OPTIONS_FOR_TEST: vscode.TextEditorOptions = { + tabSize: 4, + insertSpaces: true, +};