Skip to content

Commit

Permalink
Use stubs to setup Ruby and Debugger test
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Apr 5, 2024
1 parent 9f3505a commit bb5ede9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 39 deletions.
53 changes: 30 additions & 23 deletions vscode/src/test/suite/debugger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from "path";
import * as os from "os";

import * as vscode from "vscode";
import sinon from "sinon";

import { Debugger } from "../../debugger";
import { Ruby, ManagerIdentifier } from "../../ruby";
Expand Down Expand Up @@ -158,21 +159,25 @@ suite("Debugger", () => {

test("Launching the debugger", async () => {
// eslint-disable-next-line no-process-env
if (process.env.CI) {
await vscode.workspace
.getConfiguration("rubyLsp")
.update("rubyVersionManager", ManagerIdentifier.None, true, true);
}

// By default, VS Code always saves all open files when launching a debugging session. This is a problem for tests
// because it attempts to save an untitled test file and then we get stuck in the save file dialog with no way of
// closing it. We have to disable that before running this test
const currentSaveBeforeStart = await vscode.workspace
.getConfiguration("debug")
.get("saveBeforeStart");
await vscode.workspace
.getConfiguration("debug")
.update("saveBeforeStart", "none", true, true);
const manager = process.env.CI
? ManagerIdentifier.None
: ManagerIdentifier.Chruby;

const configStub = sinon
.stub(vscode.workspace, "getConfiguration")
.returns({
get: (name: string) => {
if (name === "rubyVersionManager") {
return manager;
} else if (name === "bundleGemfile") {
return "";
} else if (name === "saveBeforeStart") {
return "none";
}

return undefined;
},
} as unknown as vscode.WorkspaceConfiguration);

const tmpPath = fs.mkdtempSync(
path.join(os.tmpdir(), "ruby-lsp-test-debugger"),
Expand Down Expand Up @@ -225,14 +230,16 @@ suite("Debugger", () => {
}

// The debugger might take a bit of time to disconnect from the editor. We need to perform cleanup when we receive
// the termination callback or else we try to dispose of the debugger client too early
vscode.debug.onDidTerminateDebugSession(async (_session) => {
debug.dispose();
context.subscriptions.forEach((subscription) => subscription.dispose());
fs.rmSync(tmpPath, { recursive: true, force: true });
await vscode.workspace
.getConfiguration("debug")
.update("saveBeforeStart", currentSaveBeforeStart, true, true);
// the termination callback or else we try to dispose of the debugger client too early, but we need to wait for that
// so that we can clean up stubs otherwise they leak into other tests.
await new Promise<void>((resolve) => {
vscode.debug.onDidTerminateDebugSession((_session) => {
configStub.restore();
debug.dispose();
context.subscriptions.forEach((subscription) => subscription.dispose());
fs.rmSync(tmpPath, { recursive: true, force: true });
resolve();
});
});
}).timeout(45000);
});
45 changes: 29 additions & 16 deletions vscode/src/test/suite/ruby.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as assert from "assert";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";

import * as vscode from "vscode";
import sinon from "sinon";

import { Ruby, ManagerIdentifier } from "../../ruby";
import { WorkspaceChannel } from "../../workspaceChannel";
Expand All @@ -13,13 +12,28 @@ suite("Ruby environment activation", () => {
let ruby: Ruby;

test("Activate fetches Ruby information when outside of Ruby LSP", async () => {
if (os.platform() !== "win32") {
// eslint-disable-next-line no-process-env
process.env.SHELL = "/bin/bash";
}

const tmpPath = fs.mkdtempSync(path.join(os.tmpdir(), "ruby-lsp-test-"));
fs.writeFileSync(path.join(tmpPath, ".ruby-version"), "3.3.0");
// eslint-disable-next-line no-process-env
const manager = process.env.CI
? ManagerIdentifier.None
: ManagerIdentifier.Chruby;

const configStub = sinon
.stub(vscode.workspace, "getConfiguration")
.returns({
get: (name: string) => {
if (name === "rubyVersionManager") {
return manager;
} else if (name === "bundleGemfile") {
return "";
}

return undefined;
},
} as unknown as vscode.WorkspaceConfiguration);

const workspacePath = path.dirname(
path.dirname(path.dirname(path.dirname(__dirname))),
);

const context = {
extensionMode: vscode.ExtensionMode.Test,
Expand All @@ -29,14 +43,13 @@ suite("Ruby environment activation", () => {
ruby = new Ruby(
context,
{
uri: vscode.Uri.file(tmpPath),
uri: vscode.Uri.file(workspacePath),
name: path.basename(workspacePath),
index: 0,
} as vscode.WorkspaceFolder,
outputChannel,
);
await ruby.activateRuby(
// eslint-disable-next-line no-process-env
process.env.CI ? ManagerIdentifier.None : ManagerIdentifier.Chruby,
);
await ruby.activateRuby();

assert.ok(ruby.rubyVersion, "Expected Ruby version to be set");
assert.notStrictEqual(
Expand All @@ -45,6 +58,6 @@ suite("Ruby environment activation", () => {
"Expected YJIT support to be set to true or false",
);

fs.rmSync(tmpPath, { recursive: true, force: true });
});
configStub.restore();
}).timeout(10000);
});

0 comments on commit bb5ede9

Please sign in to comment.