Skip to content

Commit

Permalink
Create test case recorder class
Browse files Browse the repository at this point in the history
  • Loading branch information
brxck committed Jul 30, 2021
1 parent b8df923 commit f278cc8
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 71 deletions.
123 changes: 123 additions & 0 deletions src/TestCaseRecorder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import * as vscode from "vscode";
import * as path from "path";
import * as fs from "fs";

export class TestCaseRecorder {
active: boolean = false;
outPath: string | null = null;
talonCommand: string | null = null;
workspacePath: string | null;
workSpaceFolder: string | null;
fixtureRoot: string | null;
fixtureSubdirectory: string | null = null;

constructor() {
this.workspacePath =
vscode.workspace.workspaceFolders?.[0].uri.path ?? null;

this.workSpaceFolder = this.workspacePath
? path.basename(this.workspacePath)
: null;

this.fixtureRoot = this.workspacePath
? path.join(this.workspacePath, "src/test/suite/fixtures/recorded")
: null;
}

start(): Promise<void> {
return this.promptTalonCommand();
}

private async promptTalonCommand(): Promise<void> {
const result = await vscode.window.showInputBox({
prompt: "Talon Command",
ignoreFocusOut: true,
validateInput: (input) => (input.trim().length > 0 ? null : "Required"),
});

// Inputs return undefined when a user cancels by hitting 'escape'
if (result === undefined) {
this.active = false;
return;
}

this.talonCommand = result;
return this.promptSubdirectory();
}

private async promptSubdirectory(): Promise<void> {
if (
this.workspacePath == null ||
this.fixtureRoot == null ||
this.workSpaceFolder !== "cursorless-vscode"
) {
return;
}

const subdirectories = fs
.readdirSync(this.fixtureRoot, { withFileTypes: true })
.filter((item) => item.isDirectory())
.map((directory) => directory.name);

const createNewSubdirectory = "Create new folder →";
const subdirectorySelection = await vscode.window.showQuickPick([
...subdirectories,
createNewSubdirectory,
]);

if (subdirectorySelection === undefined) {
return this.promptTalonCommand(); // go back a prompt
} else if (subdirectorySelection === createNewSubdirectory) {
return this.promptNewSubdirectory();
} else {
this.fixtureSubdirectory = subdirectorySelection;
return this.promptFileName();
}
}

private async promptNewSubdirectory(): Promise<void> {
if (this.fixtureRoot == null) {
throw new Error("Missing fixture root. Not in cursorless workspace?");
}

const subdirectory = await vscode.window.showInputBox({
prompt: "New Folder Name",
ignoreFocusOut: true,
validateInput: (input) => (input.trim().length > 0 ? null : "Required"),
});

if (subdirectory === undefined) {
return this.promptSubdirectory(); // go back a prompt
}

this.fixtureSubdirectory = subdirectory;
return this.promptFileName();
}

private async promptFileName(): Promise<void> {
if (this.fixtureRoot == null) {
throw new Error("Missing fixture root. Not in cursorless workspace?");
}

const filename = await vscode.window.showInputBox({
prompt: "Fixture Filename",
});

if (filename === undefined || this.fixtureSubdirectory == null) {
this.promptSubdirectory(); // go back a prompt
return;
}

const targetDirectory = path.join(
this.fixtureRoot,
this.fixtureSubdirectory,
`${filename}.yml`
);

if (!fs.existsSync(targetDirectory)) {
fs.mkdirSync(targetDirectory);
}

this.outPath = path.join(targetDirectory, `${filename}.yml`);
}
}
74 changes: 3 additions & 71 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import * as vscode from "vscode";
import * as path from "path";
import * as fs from "fs";
import { addDecorationsToEditors } from "./addDecorationsToEditor";
import { DEBOUNCE_DELAY } from "./constants";
import Decorations from "./Decorations";
Expand All @@ -14,6 +12,7 @@ import { logBranchTypes } from "./debug";
import { TestCase } from "./TestCase";
import { ThatMark } from "./ThatMark";
import { Clipboard } from "./Clipboard";
import { TestCaseRecorder } from "./TestCaseRecorder";

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

const graph = makeGraph(graphConstructors);
const thatMark = new ThatMark();
const testCaseRecorder = { active: false, talonCommand: "", outPath: "" };
const testCaseRecorder = new TestCaseRecorder();
const cursorlessRecordTestCaseDisposable = vscode.commands.registerCommand(
"cursorless.recordTestCase",
async () => {
console.log("Recording test case for next command");

const talonCommand = await vscode.window.showInputBox({
prompt: "Talon Command",
ignoreFocusOut: true,
validateInput: (input) => (input.trim().length > 0 ? null : "Required"),
});

if (!talonCommand) {
return;
}

const workspacePath = vscode.workspace.workspaceFolders?.[0].uri.path;
const workSpaceFolder = path.basename(workspacePath ?? "");

if (workspacePath && workSpaceFolder === "cursorless-vscode") {
const fixtureRoot = path.join(
workspacePath,
"src/test/suite/fixtures/recorded"
);
const subdirectories = fs
.readdirSync(fixtureRoot, { withFileTypes: true })
.filter((item) => item.isDirectory())
.map((directory) => directory.name);

const createNewSubdirectory = "Create new folder →";
const subdirectorySelection = await vscode.window.showQuickPick([
...subdirectories,
createNewSubdirectory,
]);
let subdirectory: string | undefined;

if (subdirectorySelection === createNewSubdirectory) {
subdirectory = await vscode.window.showInputBox({
prompt: "New Folder Name",
ignoreFocusOut: true,
validateInput: (input) => {
if (input.trim().length === 0) {
return "Required";
} else if (fs.existsSync(path.join(fixtureRoot, input))) {
return "Folder already exists";
}
},
});

if (!subdirectory) {
return;
}

fs.mkdirSync(path.join(fixtureRoot, subdirectory));
}

const filename = await vscode.window.showInputBox({
prompt: "Fixture Filename",
});

if (!filename || !subdirectory) {
return;
}

testCaseRecorder.outPath = path.join(
fixtureRoot,
subdirectory,
`${filename}.yml`
);
}

testCaseRecorder.active = true;
testCaseRecorder.talonCommand = talonCommand ?? "";
testCaseRecorder.start();
}
);
const cursorlessCommandDisposable = vscode.commands.registerCommand(
Expand Down

0 comments on commit f278cc8

Please sign in to comment.