Skip to content

Commit f278cc8

Browse files
committed
Create test case recorder class
1 parent b8df923 commit f278cc8

File tree

2 files changed

+126
-71
lines changed

2 files changed

+126
-71
lines changed

src/TestCaseRecorder.ts

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import * as vscode from "vscode";
2+
import * as path from "path";
3+
import * as fs from "fs";
4+
5+
export class TestCaseRecorder {
6+
active: boolean = false;
7+
outPath: string | null = null;
8+
talonCommand: string | null = null;
9+
workspacePath: string | null;
10+
workSpaceFolder: string | null;
11+
fixtureRoot: string | null;
12+
fixtureSubdirectory: string | null = null;
13+
14+
constructor() {
15+
this.workspacePath =
16+
vscode.workspace.workspaceFolders?.[0].uri.path ?? null;
17+
18+
this.workSpaceFolder = this.workspacePath
19+
? path.basename(this.workspacePath)
20+
: null;
21+
22+
this.fixtureRoot = this.workspacePath
23+
? path.join(this.workspacePath, "src/test/suite/fixtures/recorded")
24+
: null;
25+
}
26+
27+
start(): Promise<void> {
28+
return this.promptTalonCommand();
29+
}
30+
31+
private async promptTalonCommand(): Promise<void> {
32+
const result = await vscode.window.showInputBox({
33+
prompt: "Talon Command",
34+
ignoreFocusOut: true,
35+
validateInput: (input) => (input.trim().length > 0 ? null : "Required"),
36+
});
37+
38+
// Inputs return undefined when a user cancels by hitting 'escape'
39+
if (result === undefined) {
40+
this.active = false;
41+
return;
42+
}
43+
44+
this.talonCommand = result;
45+
return this.promptSubdirectory();
46+
}
47+
48+
private async promptSubdirectory(): Promise<void> {
49+
if (
50+
this.workspacePath == null ||
51+
this.fixtureRoot == null ||
52+
this.workSpaceFolder !== "cursorless-vscode"
53+
) {
54+
return;
55+
}
56+
57+
const subdirectories = fs
58+
.readdirSync(this.fixtureRoot, { withFileTypes: true })
59+
.filter((item) => item.isDirectory())
60+
.map((directory) => directory.name);
61+
62+
const createNewSubdirectory = "Create new folder →";
63+
const subdirectorySelection = await vscode.window.showQuickPick([
64+
...subdirectories,
65+
createNewSubdirectory,
66+
]);
67+
68+
if (subdirectorySelection === undefined) {
69+
return this.promptTalonCommand(); // go back a prompt
70+
} else if (subdirectorySelection === createNewSubdirectory) {
71+
return this.promptNewSubdirectory();
72+
} else {
73+
this.fixtureSubdirectory = subdirectorySelection;
74+
return this.promptFileName();
75+
}
76+
}
77+
78+
private async promptNewSubdirectory(): Promise<void> {
79+
if (this.fixtureRoot == null) {
80+
throw new Error("Missing fixture root. Not in cursorless workspace?");
81+
}
82+
83+
const subdirectory = await vscode.window.showInputBox({
84+
prompt: "New Folder Name",
85+
ignoreFocusOut: true,
86+
validateInput: (input) => (input.trim().length > 0 ? null : "Required"),
87+
});
88+
89+
if (subdirectory === undefined) {
90+
return this.promptSubdirectory(); // go back a prompt
91+
}
92+
93+
this.fixtureSubdirectory = subdirectory;
94+
return this.promptFileName();
95+
}
96+
97+
private async promptFileName(): Promise<void> {
98+
if (this.fixtureRoot == null) {
99+
throw new Error("Missing fixture root. Not in cursorless workspace?");
100+
}
101+
102+
const filename = await vscode.window.showInputBox({
103+
prompt: "Fixture Filename",
104+
});
105+
106+
if (filename === undefined || this.fixtureSubdirectory == null) {
107+
this.promptSubdirectory(); // go back a prompt
108+
return;
109+
}
110+
111+
const targetDirectory = path.join(
112+
this.fixtureRoot,
113+
this.fixtureSubdirectory,
114+
`${filename}.yml`
115+
);
116+
117+
if (!fs.existsSync(targetDirectory)) {
118+
fs.mkdirSync(targetDirectory);
119+
}
120+
121+
this.outPath = path.join(targetDirectory, `${filename}.yml`);
122+
}
123+
}

src/extension.ts

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import * as vscode from "vscode";
2-
import * as path from "path";
3-
import * as fs from "fs";
42
import { addDecorationsToEditors } from "./addDecorationsToEditor";
53
import { DEBOUNCE_DELAY } from "./constants";
64
import Decorations from "./Decorations";
@@ -14,6 +12,7 @@ import { logBranchTypes } from "./debug";
1412
import { TestCase } from "./TestCase";
1513
import { ThatMark } from "./ThatMark";
1614
import { Clipboard } from "./Clipboard";
15+
import { TestCaseRecorder } from "./TestCaseRecorder";
1716

1817
export async function activate(context: vscode.ExtensionContext) {
1918
const fontMeasurements = new FontMeasurements(context);
@@ -79,79 +78,12 @@ export async function activate(context: vscode.ExtensionContext) {
7978

8079
const graph = makeGraph(graphConstructors);
8180
const thatMark = new ThatMark();
82-
const testCaseRecorder = { active: false, talonCommand: "", outPath: "" };
81+
const testCaseRecorder = new TestCaseRecorder();
8382
const cursorlessRecordTestCaseDisposable = vscode.commands.registerCommand(
8483
"cursorless.recordTestCase",
8584
async () => {
8685
console.log("Recording test case for next command");
87-
88-
const talonCommand = await vscode.window.showInputBox({
89-
prompt: "Talon Command",
90-
ignoreFocusOut: true,
91-
validateInput: (input) => (input.trim().length > 0 ? null : "Required"),
92-
});
93-
94-
if (!talonCommand) {
95-
return;
96-
}
97-
98-
const workspacePath = vscode.workspace.workspaceFolders?.[0].uri.path;
99-
const workSpaceFolder = path.basename(workspacePath ?? "");
100-
101-
if (workspacePath && workSpaceFolder === "cursorless-vscode") {
102-
const fixtureRoot = path.join(
103-
workspacePath,
104-
"src/test/suite/fixtures/recorded"
105-
);
106-
const subdirectories = fs
107-
.readdirSync(fixtureRoot, { withFileTypes: true })
108-
.filter((item) => item.isDirectory())
109-
.map((directory) => directory.name);
110-
111-
const createNewSubdirectory = "Create new folder →";
112-
const subdirectorySelection = await vscode.window.showQuickPick([
113-
...subdirectories,
114-
createNewSubdirectory,
115-
]);
116-
let subdirectory: string | undefined;
117-
118-
if (subdirectorySelection === createNewSubdirectory) {
119-
subdirectory = await vscode.window.showInputBox({
120-
prompt: "New Folder Name",
121-
ignoreFocusOut: true,
122-
validateInput: (input) => {
123-
if (input.trim().length === 0) {
124-
return "Required";
125-
} else if (fs.existsSync(path.join(fixtureRoot, input))) {
126-
return "Folder already exists";
127-
}
128-
},
129-
});
130-
131-
if (!subdirectory) {
132-
return;
133-
}
134-
135-
fs.mkdirSync(path.join(fixtureRoot, subdirectory));
136-
}
137-
138-
const filename = await vscode.window.showInputBox({
139-
prompt: "Fixture Filename",
140-
});
141-
142-
if (!filename || !subdirectory) {
143-
return;
144-
}
145-
146-
testCaseRecorder.outPath = path.join(
147-
fixtureRoot,
148-
subdirectory,
149-
`${filename}.yml`
150-
);
151-
}
152-
153-
testCaseRecorder.active = true;
154-
testCaseRecorder.talonCommand = talonCommand ?? "";
86+
testCaseRecorder.start();
15587
}
15688
);
15789
const cursorlessCommandDisposable = vscode.commands.registerCommand(

0 commit comments

Comments
 (0)