Skip to content

Commit

Permalink
Start discovering all available tests lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Jan 31, 2025
1 parent c2ba939 commit b0f99fc
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 6 deletions.
4 changes: 4 additions & 0 deletions vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,10 @@
"description": "Opt-in/out of the Tapioca add-on",
"type": "boolean"
},
"newTestExperience": {
"description": "UNDER DEVEOPMENT. Opt-in/out of the new test experience",
"type": "boolean"
},
"launcher": {
"description": "Opt-in/out of the new launcher mode",
"type": "boolean"
Expand Down
1 change: 1 addition & 0 deletions vscode/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const SUPPORTED_LANGUAGE_IDS = ["ruby", "erb"];
export const FEATURE_FLAGS = {
tapiocaAddon: 0.3,
launcher: 0.05,
newTestExperience: -1,
};

type FeatureFlagConfigurationKey = keyof typeof FEATURE_FLAGS | "all";
Expand Down
151 changes: 149 additions & 2 deletions vscode/src/test/suite/testController.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import * as assert from "assert";
import path from "path";
import fs from "fs";
import os from "os";

import * as vscode from "vscode";
import { CodeLens } from "vscode-languageclient/node";
import { afterEach } from "mocha";
import sinon from "sinon";

import { TestController } from "../../testController";
import { Command } from "../../common";
import * as common from "../../common";

import { FAKE_TELEMETRY } from "./fakeTelemetry";

suite("TestController", () => {
const workspacePath = path.dirname(
path.dirname(path.dirname(path.dirname(__dirname))),
);
const workspaceUri = vscode.Uri.file(workspacePath);
const workspaceFolder: vscode.WorkspaceFolder = {
uri: workspaceUri,
name: path.basename(workspaceUri.fsPath),
index: 0,
};
const context = {
extensionMode: vscode.ExtensionMode.Test,
subscriptions: [],
Expand All @@ -18,6 +32,10 @@ suite("TestController", () => {
},
} as unknown as vscode.ExtensionContext;

afterEach(() => {
context.subscriptions.forEach((subscription) => subscription.dispose());
});

test("createTestItems doesn't break when there's a missing group", () => {
const controller = new TestController(
context,
Expand All @@ -30,7 +48,7 @@ suite("TestController", () => {
range: new vscode.Range(0, 0, 10, 10),
command: {
title: "Run",
command: Command.RunTest,
command: common.Command.RunTest,
arguments: [
"test/fake_test.rb",
"test_do_something",
Expand Down Expand Up @@ -58,4 +76,133 @@ suite("TestController", () => {
controller.createTestItems(codeLensItems);
});
});

test("populates test structure directly if there's only one workspace", async () => {
const stub = sinon.stub(common, "featureEnabled").returns(true);
const controller = new TestController(
context,
FAKE_TELEMETRY,
() => undefined,
);
stub.restore();

const workspacesStub = sinon
.stub(vscode.workspace, "workspaceFolders")
.get(() => [workspaceFolder]);

const relativePathStub = sinon
.stub(vscode.workspace, "asRelativePath")
.callsFake((uri) =>
path.relative(workspacePath, (uri as vscode.Uri).fsPath),
);

await controller.testController.resolveHandler!(undefined);
workspacesStub.restore();
relativePathStub.restore();

const collection = controller.testController.items;

const testDir = collection.get(
vscode.Uri.joinPath(workspaceUri, "test").toString(),
);
assert.ok(testDir);

const serverTest = testDir!.children.get(
vscode.Uri.joinPath(workspaceUri, "test", "server_test.rb").toString(),
);
assert.ok(serverTest);
});

test("makes the workspaces the top level when there's more than one", async () => {
const stub = sinon.stub(common, "featureEnabled").returns(true);
const controller = new TestController(
context,
FAKE_TELEMETRY,
() => undefined,
);
stub.restore();

const secondWorkspacePath = fs.mkdtempSync(
path.join(os.tmpdir(), "ruby-lsp-test-controller-"),
);
const secondWorkspaceUri = vscode.Uri.file(secondWorkspacePath);

fs.mkdirSync(path.join(secondWorkspaceUri.fsPath, "test"));
fs.writeFileSync(
path.join(secondWorkspaceUri.fsPath, "test", "other_test.rb"),
"require 'test_helper'\n\nclass OtherTest < Minitest::Test; end",
);

const secondWorkspaceFolder: vscode.WorkspaceFolder = {
uri: secondWorkspaceUri,
name: "second_workspace",
index: 1,
};
const workspacesStub = sinon
.stub(vscode.workspace, "workspaceFolders")
.get(() => [workspaceFolder, secondWorkspaceFolder]);

const relativePathStub = sinon
.stub(vscode.workspace, "asRelativePath")
.callsFake((uri) => {
const filePath = (uri as vscode.Uri).fsPath;

if (path.basename(filePath) === "other_test.rb") {
return path.relative(secondWorkspacePath, filePath);
} else {
return path.relative(workspacePath, filePath);
}
});

const getWorkspaceStub = sinon
.stub(vscode.workspace, "getWorkspaceFolder")
.callsFake((uri) => {
if (uri === secondWorkspaceUri) {
return secondWorkspaceFolder;
} else {
return workspaceFolder;
}
});

await controller.testController.resolveHandler!(undefined);

const collection = controller.testController.items;

// First workspace
const workspaceItem = collection.get(workspaceUri.toString());
assert.ok(workspaceItem);
await controller.testController.resolveHandler!(workspaceItem);

const testDir = workspaceItem!.children.get(
vscode.Uri.joinPath(workspaceUri, "test").toString(),
);
assert.ok(testDir);

const serverTest = testDir!.children.get(
vscode.Uri.joinPath(workspaceUri, "test", "server_test.rb").toString(),
);
assert.ok(serverTest);

// Second workspace
const secondWorkspaceItem = collection.get(secondWorkspaceUri.toString());
assert.ok(secondWorkspaceItem);
await controller.testController.resolveHandler!(secondWorkspaceItem);

const secondTestDir = secondWorkspaceItem!.children.get(
vscode.Uri.joinPath(secondWorkspaceUri, "test").toString(),
);
assert.ok(secondTestDir);

const otherTest = secondTestDir!.children.get(
vscode.Uri.joinPath(
secondWorkspaceUri,
"test",
"other_test.rb",
).toString(),
);
assert.ok(otherTest);
workspacesStub.restore();
relativePathStub.restore();
getWorkspaceStub.restore();
});
});
Loading

0 comments on commit b0f99fc

Please sign in to comment.