Skip to content

Commit 79d34e6

Browse files
authored
Standardize test fixture serialization (#1702)
- Depends on #1700 ## Checklist - [ ] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [ ] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [ ] I have not broken the cheatsheet
1 parent 1c17033 commit 79d34e6

File tree

7 files changed

+27
-34
lines changed

7 files changed

+27
-34
lines changed

packages/common/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export * from "./testUtil/getFixturePaths";
5959
export * from "./testUtil/getCursorlessRepoRoot";
6060
export * from "./testUtil/serialize";
6161
export * from "./testUtil/TestCaseSnapshot";
62+
export * from "./testUtil/serializeTestFixture";
6263
export * from "./util/typeUtils";
6364
export * from "./ide/types/hatStyles.types";
6465
export * from "./errors";
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
import {
2-
EnforceUndefined,
3-
TestCaseFixture,
4-
TestCaseFixtureLegacy,
5-
} from "@cursorless/common";
1+
import { TestCaseFixtureLegacy } from "../types/TestCaseFixture";
2+
import { EnforceUndefined } from "../util/typeUtils";
3+
import { serialize } from "./serialize";
64

7-
export function reorderFields(
8-
fixture: TestCaseFixture,
9-
): EnforceUndefined<TestCaseFixture>;
10-
export function reorderFields(
5+
function reorderFields(
116
fixture: TestCaseFixtureLegacy,
127
): EnforceUndefined<TestCaseFixtureLegacy> {
138
return {
149
languageId: fixture.languageId,
10+
postEditorOpenSleepTimeMs: fixture.postEditorOpenSleepTimeMs,
11+
postCommandSleepTimeMs: fixture.postCommandSleepTimeMs,
1512
command: fixture.command,
1613
marksToCheck: fixture.marksToCheck,
1714
initialState: fixture.initialState,
1815
finalState: fixture.finalState,
1916
returnValue: fixture.returnValue,
2017
thrownError: fixture.thrownError,
2118
ide: fixture.ide,
22-
postEditorOpenSleepTimeMs: fixture.postEditorOpenSleepTimeMs,
23-
postCommandSleepTimeMs: fixture.postCommandSleepTimeMs,
2419
};
2520
}
21+
22+
export function serializeTestFixture(fixture: TestCaseFixtureLegacy): string {
23+
return serialize(reorderFields(fixture));
24+
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { serialize } from "@cursorless/common";
21
import { promises as fsp } from "fs";
32
import * as yaml from "js-yaml";
4-
import { TestCaseFixture } from "@cursorless/common";
3+
import { TestCaseFixture, serializeTestFixture } from "@cursorless/common";
54
import { FixtureTransformation } from "./types";
65

76
export async function transformFile(
@@ -12,6 +11,6 @@ export async function transformFile(
1211
const inputFixture = yaml.load(buffer.toString()) as TestCaseFixture;
1312
const outputFixture = transformation(inputFixture);
1413
if (outputFixture != null) {
15-
await fsp.writeFile(file, serialize(outputFixture));
14+
await fsp.writeFile(file, serializeTestFixture(outputFixture));
1615
}
1716
}

packages/cursorless-engine/src/scripts/transformRecordedTests/transformations/upgrade.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { TestCaseFixture, TestCaseFixtureLegacy } from "@cursorless/common";
2-
import { flow } from "lodash";
32
import { canonicalizeAndValidateCommand } from "../../../core/commandVersionUpgrades/canonicalizeAndValidateCommand";
4-
import { reorderFields } from "./reorderFields";
53

6-
export const upgrade = flow(upgradeCommand, reorderFields);
7-
8-
function upgradeCommand(fixture: TestCaseFixtureLegacy): TestCaseFixture {
4+
export function upgrade(fixture: TestCaseFixtureLegacy): TestCaseFixture {
95
return {
106
...fixture,
117
command: canonicalizeAndValidateCommand(fixture.command),

packages/cursorless-engine/src/scripts/transformRecordedTests/upgradeDecorations.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import {
22
FlashStyle,
33
GeneralizedRangePlainObject,
44
PositionPlainObject,
5-
TestCaseFixture,
65
TestCaseFixtureLegacy,
76
} from "@cursorless/common";
87
import { groupBy, partition } from "lodash";
9-
import { reorderFields } from "./transformations/reorderFields";
108
import { FixtureTransformation } from "./types";
119

1210
interface PlainTestDecoration {
@@ -64,7 +62,7 @@ export const upgradeDecorations: FixtureTransformation = (
6462
})),
6563
};
6664

67-
return reorderFields(fixture as TestCaseFixture);
65+
return fixture;
6866
};
6967

7068
function extractHighlightName(name: string): string {

packages/cursorless-engine/src/testCaseRecorder/TestCase.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import {
22
ActionType,
33
CommandLatest,
4+
EnforceUndefined,
45
extractTargetedMarks,
56
ExtraSnapshotField,
67
marksToPlainObject,
78
PartialTargetDescriptor,
89
PlainSpyIDERecordedValues,
910
ReadOnlyHatMap,
10-
serialize,
1111
SerializedMarks,
12+
serializeTestFixture,
1213
SpyIDE,
1314
spyIDERecordedValuesToPlainObject,
1415
TestCaseFixture,
@@ -130,8 +131,10 @@ export class TestCase {
130131
) {
131132
throw Error("Two snapshots must be taken before serializing");
132133
}
133-
const fixture: TestCaseFixture = {
134+
const fixture: EnforceUndefined<TestCaseFixture> = {
134135
languageId: this.languageId,
136+
postEditorOpenSleepTimeMs: undefined,
137+
postCommandSleepTimeMs: undefined,
135138
command: this.command,
136139
marksToCheck: this.marksToCheck,
137140
initialState: this.initialState,
@@ -140,7 +143,7 @@ export class TestCase {
140143
thrownError: this.thrownError,
141144
ide: this.spyIdeValues,
142145
};
143-
return serialize(fixture);
146+
return serializeTestFixture(fixture);
144147
}
145148

146149
async recordInitialState() {

packages/cursorless-vscode-e2e/src/suite/recorded.vscode.test.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {
1111
rangeToPlainObject,
1212
ReadOnlyHatMap,
1313
SelectionPlainObject,
14-
serialize,
1514
SerializedMarks,
15+
serializeTestFixture,
1616
splitKey,
1717
SpyIDE,
1818
spyIDERecordedValuesToPlainObject,
@@ -140,7 +140,7 @@ async function runTest(file: string, spyIde: SpyIDE) {
140140
thrownError: { name: error.name },
141141
};
142142

143-
await fsp.writeFile(file, serialize(outputFixture));
143+
await fsp.writeFile(file, serializeTestFixture(outputFixture));
144144
} else if (fixture.thrownError != null) {
145145
assert.strictEqual(error.name, fixture.thrownError.name);
146146
} else {
@@ -196,21 +196,18 @@ async function runTest(file: string, spyIde: SpyIDE) {
196196
const actualSpyIdeValues =
197197
rawSpyIdeValues == null
198198
? undefined
199-
: omitByDeep(
200-
spyIDERecordedValuesToPlainObject(rawSpyIdeValues),
201-
isUndefined,
202-
);
199+
: spyIDERecordedValuesToPlainObject(rawSpyIdeValues);
203200

204201
if (shouldUpdateFixtures()) {
205-
const outputFixture = {
202+
const outputFixture: TestCaseFixtureLegacy = {
206203
...fixture,
207204
finalState: resultState,
208205
returnValue,
209206
ide: actualSpyIdeValues,
210207
thrownError: undefined,
211208
};
212209

213-
await fsp.writeFile(file, serialize(outputFixture));
210+
await fsp.writeFile(file, serializeTestFixture(outputFixture));
214211
} else {
215212
if (fixture.thrownError != null) {
216213
throw Error(
@@ -231,7 +228,7 @@ async function runTest(file: string, spyIde: SpyIDE) {
231228
);
232229

233230
assert.deepStrictEqual(
234-
actualSpyIdeValues,
231+
omitByDeep(actualSpyIdeValues, isUndefined),
235232
fixture.ide,
236233
"Unexpected ide captured values",
237234
);

0 commit comments

Comments
 (0)