Skip to content

Commit f153d23

Browse files
committed
Add debugger test with project Bundler settings
1 parent 3a3d4b3 commit f153d23

19 files changed

+236
-162
lines changed

vscode/src/debugger.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,24 @@ export class Debugger
102102
debugConfiguration: vscode.DebugConfiguration,
103103
_token?: vscode.CancellationToken,
104104
): vscode.ProviderResult<vscode.DebugConfiguration> {
105-
const workspace = this.workspaceResolver(folder?.uri);
105+
// On certain occasions, the objects passed to this method are serialized. In particular for the URI, we have to
106+
// ensure we're dealing with a `vscode.Uri` object and not a plain object
107+
const uriAttributes =
108+
folder?.uri ?? debugConfiguration.workspaceFolder?.uri;
109+
110+
if (!uriAttributes) {
111+
throw new Error(`Couldn't find a workspace to start debugging`);
112+
}
113+
114+
const uri =
115+
uriAttributes instanceof vscode.Uri
116+
? uriAttributes
117+
: vscode.Uri.from(uriAttributes);
118+
119+
const workspace = this.workspaceResolver(uri);
106120

107121
if (!workspace) {
108-
throw new Error(
109-
`Couldn't find a workspace for URI: ${folder?.uri} or editor: ${vscode.window.activeTextEditor}`,
110-
);
122+
throw new Error(`Couldn't find a workspace for URI: ${uri}`);
111123
}
112124

113125
if (debugConfiguration.env) {
@@ -120,10 +132,8 @@ export class Debugger
120132
debugConfiguration.env = workspace.ruby.env;
121133
}
122134

123-
const workspaceUri = workspace.workspaceFolder.uri;
124-
125135
debugConfiguration.targetFolder = {
126-
path: workspaceUri.fsPath,
136+
path: uri.fsPath,
127137
name: workspace.workspaceFolder.name,
128138
};
129139

@@ -133,7 +143,7 @@ export class Debugger
133143
return debugConfiguration;
134144
}
135145

136-
const customBundleUri = vscode.Uri.joinPath(workspaceUri, ".ruby-lsp");
146+
const customBundleUri = vscode.Uri.joinPath(uri, ".ruby-lsp");
137147

138148
return vscode.workspace.fs.readDirectory(customBundleUri).then(
139149
(value) => {

vscode/src/rubyLsp.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ export class RubyLsp {
176176
}
177177
}
178178

179+
getWorkspace(uri: vscode.Uri): Workspace | undefined {
180+
return this.workspaces.get(uri.toString());
181+
}
182+
179183
private async activateWorkspace(
180184
workspaceFolder: vscode.WorkspaceFolder,
181185
eager: boolean,
@@ -693,10 +697,6 @@ export class RubyLsp {
693697
return this.getWorkspace(workspaceFolder.uri);
694698
}
695699

696-
private getWorkspace(uri: vscode.Uri): Workspace | undefined {
697-
return this.workspaces.get(uri.toString());
698-
}
699-
700700
private workspaceResolver(
701701
uri: vscode.Uri | undefined,
702702
): Workspace | undefined {

vscode/src/test/suite/client.test.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { WorkspaceChannel } from "../../workspaceChannel";
3434
import { MAJOR, MINOR } from "../rubyVersion";
3535

3636
import { FAKE_TELEMETRY, FakeLogger } from "./fakeTelemetry";
37-
import { createRubySymlinks } from "./helpers";
37+
import { createRubySymlinks, fakeContext } from "./helpers";
3838

3939
async function launchClient(workspaceUri: vscode.Uri) {
4040
const workspaceFolder: vscode.WorkspaceFolder = {
@@ -43,15 +43,7 @@ async function launchClient(workspaceUri: vscode.Uri) {
4343
index: 0,
4444
};
4545

46-
const context = {
47-
extensionMode: vscode.ExtensionMode.Test,
48-
subscriptions: [],
49-
workspaceState: {
50-
get: (_name: string) => undefined,
51-
update: (_name: string, _value: any) => Promise.resolve(),
52-
},
53-
extensionUri: vscode.Uri.file(path.join(workspaceUri.fsPath, "vscode")),
54-
} as unknown as vscode.ExtensionContext;
46+
const context = fakeContext();
5547
const fakeLogger = new FakeLogger();
5648
const outputChannel = new WorkspaceChannel("fake", fakeLogger as any);
5749

vscode/src/test/suite/debugger.test.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { LOG_CHANNEL, asyncExec } from "../../common";
1414
import { RUBY_VERSION } from "../rubyVersion";
1515

1616
import { FAKE_TELEMETRY } from "./fakeTelemetry";
17-
import { createRubySymlinks } from "./helpers";
17+
import { createRubySymlinks, fakeContext } from "./helpers";
1818

1919
suite("Debugger", () => {
2020
const original = vscode.workspace
@@ -34,7 +34,7 @@ suite("Debugger", () => {
3434
});
3535

3636
test("Provide debug configurations returns the default configs", () => {
37-
const context = { subscriptions: [] } as unknown as vscode.ExtensionContext;
37+
const context = fakeContext();
3838
const debug = new Debugger(context, () => {
3939
return undefined;
4040
});
@@ -69,7 +69,7 @@ suite("Debugger", () => {
6969
});
7070

7171
test("Resolve configuration injects Ruby environment", async () => {
72-
const context = { subscriptions: [] } as unknown as vscode.ExtensionContext;
72+
const context = fakeContext();
7373
const ruby = { env: { bogus: "hello!" } } as unknown as Ruby;
7474
const workspaceFolder = {
7575
name: "fake",
@@ -99,7 +99,7 @@ suite("Debugger", () => {
9999
});
100100

101101
test("Resolve configuration injects Ruby environment and allows users custom environment", async () => {
102-
const context = { subscriptions: [] } as unknown as vscode.ExtensionContext;
102+
const context = fakeContext();
103103
const ruby = { env: { bogus: "hello!" } } as unknown as Ruby;
104104
const workspaceFolder = {
105105
name: "fake",
@@ -134,7 +134,7 @@ suite("Debugger", () => {
134134
fs.mkdirSync(path.join(tmpPath, ".ruby-lsp"));
135135
fs.writeFileSync(path.join(tmpPath, ".ruby-lsp", "Gemfile"), "hello!");
136136

137-
const context = { subscriptions: [] } as unknown as vscode.ExtensionContext;
137+
const context = fakeContext();
138138
const ruby = { env: { bogus: "hello!" } } as unknown as Ruby;
139139
const workspaceFolder = {
140140
name: "fake",
@@ -198,15 +198,7 @@ suite("Debugger", () => {
198198
'source "https://rubygems.org"\ngem "debug"',
199199
);
200200

201-
const extensionPath = path.dirname(path.dirname(path.dirname(__dirname)));
202-
const context = {
203-
subscriptions: [],
204-
workspaceState: {
205-
get: () => undefined,
206-
update: () => undefined,
207-
},
208-
extensionUri: vscode.Uri.file(extensionPath),
209-
} as unknown as vscode.ExtensionContext;
201+
const context = fakeContext();
210202
const outputChannel = new WorkspaceChannel("fake", LOG_CHANNEL);
211203
const workspaceFolder: vscode.WorkspaceFolder = {
212204
uri: vscode.Uri.file(tmpPath),
@@ -247,6 +239,7 @@ suite("Debugger", () => {
247239
name: "Debug",
248240
request: "launch",
249241
program: `ruby ${path.join(tmpPath, "test.rb")}`,
242+
workspaceFolder,
250243
});
251244
} catch (error: any) {
252245
assert.fail(`Failed to launch debugger: ${error.message}`);

vscode/src/test/suite/helpers.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import path from "path";
33
import os from "os";
44
import fs from "fs";
55

6+
import * as vscode from "vscode";
7+
68
import { MAJOR, MINOR, RUBY_VERSION } from "../rubyVersion";
79

810
export function createRubySymlinks() {
@@ -41,3 +43,17 @@ export function createRubySymlinks() {
4143
}
4244
}
4345
}
46+
47+
export function fakeContext(): vscode.ExtensionContext {
48+
return {
49+
extensionMode: vscode.ExtensionMode.Test,
50+
subscriptions: [],
51+
workspaceState: {
52+
get: (_name: string) => undefined,
53+
update: (_name: string, _value: any) => Promise.resolve(),
54+
},
55+
extensionUri: vscode.Uri.file(
56+
path.dirname(path.dirname(path.dirname(__dirname))),
57+
),
58+
} as unknown as vscode.ExtensionContext;
59+
}

vscode/src/test/suite/launch.test.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { WorkspaceChannel } from "../../workspaceChannel";
1414
import * as common from "../../common";
1515

1616
import { FAKE_TELEMETRY, FakeLogger } from "./fakeTelemetry";
17-
import { createRubySymlinks } from "./helpers";
17+
import { createRubySymlinks, fakeContext } from "./helpers";
1818

1919
suite("Launch integrations", () => {
2020
const workspacePath = path.dirname(
@@ -27,15 +27,7 @@ suite("Launch integrations", () => {
2727
index: 0,
2828
};
2929

30-
const context = {
31-
extensionMode: vscode.ExtensionMode.Test,
32-
subscriptions: [],
33-
workspaceState: {
34-
get: (_name: string) => undefined,
35-
update: (_name: string, _value: any) => Promise.resolve(),
36-
},
37-
extensionUri: vscode.Uri.joinPath(workspaceUri, "vscode"),
38-
} as unknown as vscode.ExtensionContext;
30+
const context = fakeContext();
3931
const fakeLogger = new FakeLogger();
4032
const outputChannel = new WorkspaceChannel("fake", fakeLogger as any);
4133

vscode/src/test/suite/ruby.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from "../../ruby/versionManager";
1818

1919
import { FAKE_TELEMETRY } from "./fakeTelemetry";
20+
import { fakeContext } from "./helpers";
2021

2122
suite("Ruby environment activation", () => {
2223
const workspacePath = path.dirname(
@@ -27,14 +28,7 @@ suite("Ruby environment activation", () => {
2728
name: path.basename(workspacePath),
2829
index: 0,
2930
};
30-
const context = {
31-
extensionMode: vscode.ExtensionMode.Test,
32-
workspaceState: {
33-
get: () => undefined,
34-
update: () => undefined,
35-
},
36-
extensionUri: vscode.Uri.file(path.join(workspacePath, "vscode")),
37-
} as unknown as vscode.ExtensionContext;
31+
const context = fakeContext();
3832
const outputChannel = new WorkspaceChannel("fake", LOG_CHANNEL);
3933

4034
test("Activate fetches Ruby information when outside of Ruby LSP", async () => {

vscode/src/test/suite/ruby/asdf.test.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,15 @@ import {
1313
FIELD_SEPARATOR,
1414
VALUE_SEPARATOR,
1515
} from "../../../ruby/versionManager";
16+
import { fakeContext } from "../helpers";
1617

1718
suite("Asdf", () => {
1819
if (os.platform() === "win32") {
1920
// eslint-disable-next-line no-console
2021
console.log("Skipping Asdf tests on Windows");
2122
return;
2223
}
23-
const context = {
24-
extensionMode: vscode.ExtensionMode.Test,
25-
subscriptions: [],
26-
workspaceState: {
27-
get: (_name: string) => undefined,
28-
update: (_name: string, _value: any) => Promise.resolve(),
29-
},
30-
extensionUri: vscode.Uri.parse("file:///fake"),
31-
} as unknown as vscode.ExtensionContext;
24+
const context = fakeContext();
3225

3326
test("Finds Ruby based on .tool-versions", async () => {
3427
// eslint-disable-next-line no-process-env
@@ -63,10 +56,11 @@ suite("Asdf", () => {
6356
const shellStub = sinon.stub(vscode.env, "shell").get(() => "/bin/bash");
6457

6558
const { env, version, yjit } = await asdf.activate();
59+
const baseCommand = `. ${os.homedir()}/.asdf/asdf.sh && asdf exec ruby`;
6660

6761
assert.ok(
6862
execStub.calledOnceWithExactly(
69-
`. ${os.homedir()}/.asdf/asdf.sh && asdf exec ruby -EUTF-8:UTF-8 '/fake/activation.rb'`,
63+
`${baseCommand} -EUTF-8:UTF-8 '${context.extensionUri.fsPath}/activation.rb'`,
7064
{
7165
cwd: workspacePath,
7266
shell: "/bin/bash",
@@ -121,10 +115,11 @@ suite("Asdf", () => {
121115
.get(() => "/opt/homebrew/bin/fish");
122116

123117
const { env, version, yjit } = await asdf.activate();
118+
const baseCommand = `. ${os.homedir()}/.asdf/asdf.fish && asdf exec ruby`;
124119

125120
assert.ok(
126121
execStub.calledOnceWithExactly(
127-
`. ${os.homedir()}/.asdf/asdf.fish && asdf exec ruby -EUTF-8:UTF-8 '/fake/activation.rb'`,
122+
`${baseCommand} -EUTF-8:UTF-8 '${context.extensionUri.fsPath}/activation.rb'`,
128123
{
129124
cwd: workspacePath,
130125
shell: "/opt/homebrew/bin/fish",

vscode/src/test/suite/ruby/chruby.test.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { WorkspaceChannel } from "../../../workspaceChannel";
1313
import { LOG_CHANNEL } from "../../../common";
1414
import { RUBY_VERSION, MAJOR, MINOR, VERSION_REGEX } from "../../rubyVersion";
1515
import { ActivationResult } from "../../../ruby/versionManager";
16+
import { fakeContext } from "../helpers";
1617

1718
// Create links to the real Ruby installations on CI and on our local machines
1819
function createRubySymlinks(destination: string) {
@@ -50,15 +51,7 @@ suite("Chruby", () => {
5051
return;
5152
}
5253

53-
const context = {
54-
extensionMode: vscode.ExtensionMode.Test,
55-
subscriptions: [],
56-
workspaceState: {
57-
get: (_name: string) => undefined,
58-
update: (_name: string, _value: any) => Promise.resolve(),
59-
},
60-
extensionUri: vscode.Uri.parse("file:///fake"),
61-
} as unknown as vscode.ExtensionContext;
54+
const context = fakeContext();
6255

6356
let rootPath: string;
6457
let workspacePath: string;

vscode/src/test/suite/ruby/custom.test.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,10 @@ import {
1414
FIELD_SEPARATOR,
1515
VALUE_SEPARATOR,
1616
} from "../../../ruby/versionManager";
17+
import { fakeContext } from "../helpers";
1718

1819
suite("Custom", () => {
19-
const context = {
20-
extensionMode: vscode.ExtensionMode.Test,
21-
subscriptions: [],
22-
workspaceState: {
23-
get: (_name: string) => undefined,
24-
update: (_name: string, _value: any) => Promise.resolve(),
25-
},
26-
extensionUri: vscode.Uri.parse("file:///fake"),
27-
} as unknown as vscode.ExtensionContext;
20+
const context = fakeContext();
2821

2922
test("Invokes custom script and then Ruby", async () => {
3023
const workspacePath = fs.mkdtempSync(

0 commit comments

Comments
 (0)