Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debugger test with project Bundler settings #3093

Draft
wants to merge 1 commit into
base: graphite-base/3093
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions vscode/src/debugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,24 @@ export class Debugger
debugConfiguration: vscode.DebugConfiguration,
_token?: vscode.CancellationToken,
): vscode.ProviderResult<vscode.DebugConfiguration> {
const workspace = this.workspaceResolver(folder?.uri);
// On certain occasions, the objects passed to this method are serialized. In particular for the URI, we have to
// ensure we're dealing with a `vscode.Uri` object and not a plain object
const uriAttributes =
folder?.uri ?? debugConfiguration.workspaceFolder?.uri;

if (!uriAttributes) {
throw new Error(`Couldn't find a workspace to start debugging`);
}

const uri =
uriAttributes instanceof vscode.Uri
? uriAttributes
: vscode.Uri.from(uriAttributes);

const workspace = this.workspaceResolver(uri);

if (!workspace) {
throw new Error(
`Couldn't find a workspace for URI: ${folder?.uri} or editor: ${vscode.window.activeTextEditor}`,
);
throw new Error(`Couldn't find a workspace for URI: ${uri}`);
}

if (debugConfiguration.env) {
Expand All @@ -120,10 +132,8 @@ export class Debugger
debugConfiguration.env = workspace.ruby.env;
}

const workspaceUri = workspace.workspaceFolder.uri;

debugConfiguration.targetFolder = {
path: workspaceUri.fsPath,
path: uri.fsPath,
name: workspace.workspaceFolder.name,
};

Expand All @@ -133,7 +143,7 @@ export class Debugger
return debugConfiguration;
}

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

return vscode.workspace.fs.readDirectory(customBundleUri).then(
(value) => {
Expand Down
8 changes: 4 additions & 4 deletions vscode/src/rubyLsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ export class RubyLsp {
}
}

getWorkspace(uri: vscode.Uri): Workspace | undefined {
return this.workspaces.get(uri.toString());
}

private async activateWorkspace(
workspaceFolder: vscode.WorkspaceFolder,
eager: boolean,
Expand Down Expand Up @@ -693,10 +697,6 @@ export class RubyLsp {
return this.getWorkspace(workspaceFolder.uri);
}

private getWorkspace(uri: vscode.Uri): Workspace | undefined {
return this.workspaces.get(uri.toString());
}

private workspaceResolver(
uri: vscode.Uri | undefined,
): Workspace | undefined {
Expand Down
12 changes: 2 additions & 10 deletions vscode/src/test/suite/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { WorkspaceChannel } from "../../workspaceChannel";
import { MAJOR, MINOR } from "../rubyVersion";

import { FAKE_TELEMETRY, FakeLogger } from "./fakeTelemetry";
import { createRubySymlinks } from "./helpers";
import { createRubySymlinks, fakeContext } from "./helpers";

async function launchClient(workspaceUri: vscode.Uri) {
const workspaceFolder: vscode.WorkspaceFolder = {
Expand All @@ -43,15 +43,7 @@ async function launchClient(workspaceUri: vscode.Uri) {
index: 0,
};

const context = {
extensionMode: vscode.ExtensionMode.Test,
subscriptions: [],
workspaceState: {
get: (_name: string) => undefined,
update: (_name: string, _value: any) => Promise.resolve(),
},
extensionUri: vscode.Uri.file(path.join(workspaceUri.fsPath, "vscode")),
} as unknown as vscode.ExtensionContext;
const context = fakeContext();
const fakeLogger = new FakeLogger();
const outputChannel = new WorkspaceChannel("fake", fakeLogger as any);

Expand Down
21 changes: 7 additions & 14 deletions vscode/src/test/suite/debugger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { LOG_CHANNEL, asyncExec } from "../../common";
import { RUBY_VERSION } from "../rubyVersion";

import { FAKE_TELEMETRY } from "./fakeTelemetry";
import { createRubySymlinks } from "./helpers";
import { createRubySymlinks, fakeContext } from "./helpers";

suite("Debugger", () => {
const original = vscode.workspace
Expand All @@ -34,7 +34,7 @@ suite("Debugger", () => {
});

test("Provide debug configurations returns the default configs", () => {
const context = { subscriptions: [] } as unknown as vscode.ExtensionContext;
const context = fakeContext();
const debug = new Debugger(context, () => {
return undefined;
});
Expand Down Expand Up @@ -69,7 +69,7 @@ suite("Debugger", () => {
});

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

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

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

const extensionPath = path.dirname(path.dirname(path.dirname(__dirname)));
const context = {
subscriptions: [],
workspaceState: {
get: () => undefined,
update: () => undefined,
},
extensionUri: vscode.Uri.file(extensionPath),
} as unknown as vscode.ExtensionContext;
const context = fakeContext();
const outputChannel = new WorkspaceChannel("fake", LOG_CHANNEL);
const workspaceFolder: vscode.WorkspaceFolder = {
uri: vscode.Uri.file(tmpPath),
Expand Down Expand Up @@ -247,6 +239,7 @@ suite("Debugger", () => {
name: "Debug",
request: "launch",
program: `ruby ${path.join(tmpPath, "test.rb")}`,
workspaceFolder,
});
} catch (error: any) {
assert.fail(`Failed to launch debugger: ${error.message}`);
Expand Down
16 changes: 16 additions & 0 deletions vscode/src/test/suite/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import path from "path";
import os from "os";
import fs from "fs";

import * as vscode from "vscode";

import { MAJOR, MINOR, RUBY_VERSION } from "../rubyVersion";

export function createRubySymlinks() {
Expand Down Expand Up @@ -41,3 +43,17 @@ export function createRubySymlinks() {
}
}
}

export function fakeContext(): vscode.ExtensionContext {
return {
extensionMode: vscode.ExtensionMode.Test,
subscriptions: [],
workspaceState: {
get: (_name: string) => undefined,
update: (_name: string, _value: any) => Promise.resolve(),
},
extensionUri: vscode.Uri.file(
path.dirname(path.dirname(path.dirname(__dirname))),
),
} as unknown as vscode.ExtensionContext;
}
12 changes: 2 additions & 10 deletions vscode/src/test/suite/launch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { WorkspaceChannel } from "../../workspaceChannel";
import * as common from "../../common";

import { FAKE_TELEMETRY, FakeLogger } from "./fakeTelemetry";
import { createRubySymlinks } from "./helpers";
import { createRubySymlinks, fakeContext } from "./helpers";

suite("Launch integrations", () => {
const workspacePath = path.dirname(
Expand All @@ -27,15 +27,7 @@ suite("Launch integrations", () => {
index: 0,
};

const context = {
extensionMode: vscode.ExtensionMode.Test,
subscriptions: [],
workspaceState: {
get: (_name: string) => undefined,
update: (_name: string, _value: any) => Promise.resolve(),
},
extensionUri: vscode.Uri.joinPath(workspaceUri, "vscode"),
} as unknown as vscode.ExtensionContext;
const context = fakeContext();
const fakeLogger = new FakeLogger();
const outputChannel = new WorkspaceChannel("fake", fakeLogger as any);

Expand Down
10 changes: 2 additions & 8 deletions vscode/src/test/suite/ruby.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from "../../ruby/versionManager";

import { FAKE_TELEMETRY } from "./fakeTelemetry";
import { fakeContext } from "./helpers";

suite("Ruby environment activation", () => {
const workspacePath = path.dirname(
Expand All @@ -27,14 +28,7 @@ suite("Ruby environment activation", () => {
name: path.basename(workspacePath),
index: 0,
};
const context = {
extensionMode: vscode.ExtensionMode.Test,
workspaceState: {
get: () => undefined,
update: () => undefined,
},
extensionUri: vscode.Uri.file(path.join(workspacePath, "vscode")),
} as unknown as vscode.ExtensionContext;
const context = fakeContext();
const outputChannel = new WorkspaceChannel("fake", LOG_CHANNEL);

test("Activate fetches Ruby information when outside of Ruby LSP", async () => {
Expand Down
17 changes: 6 additions & 11 deletions vscode/src/test/suite/ruby/asdf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,15 @@ import {
FIELD_SEPARATOR,
VALUE_SEPARATOR,
} from "../../../ruby/versionManager";
import { fakeContext } from "../helpers";

suite("Asdf", () => {
if (os.platform() === "win32") {
// eslint-disable-next-line no-console
console.log("Skipping Asdf tests on Windows");
return;
}
const context = {
extensionMode: vscode.ExtensionMode.Test,
subscriptions: [],
workspaceState: {
get: (_name: string) => undefined,
update: (_name: string, _value: any) => Promise.resolve(),
},
extensionUri: vscode.Uri.parse("file:///fake"),
} as unknown as vscode.ExtensionContext;
const context = fakeContext();

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

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

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

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

assert.ok(
execStub.calledOnceWithExactly(
`. ${os.homedir()}/.asdf/asdf.fish && asdf exec ruby -EUTF-8:UTF-8 '/fake/activation.rb'`,
`${baseCommand} -EUTF-8:UTF-8 '${context.extensionUri.fsPath}/activation.rb'`,
{
cwd: workspacePath,
shell: "/opt/homebrew/bin/fish",
Expand Down
11 changes: 2 additions & 9 deletions vscode/src/test/suite/ruby/chruby.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { WorkspaceChannel } from "../../../workspaceChannel";
import { LOG_CHANNEL } from "../../../common";
import { RUBY_VERSION, MAJOR, MINOR, VERSION_REGEX } from "../../rubyVersion";
import { ActivationResult } from "../../../ruby/versionManager";
import { fakeContext } from "../helpers";

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

const context = {
extensionMode: vscode.ExtensionMode.Test,
subscriptions: [],
workspaceState: {
get: (_name: string) => undefined,
update: (_name: string, _value: any) => Promise.resolve(),
},
extensionUri: vscode.Uri.parse("file:///fake"),
} as unknown as vscode.ExtensionContext;
const context = fakeContext();

let rootPath: string;
let workspacePath: string;
Expand Down
11 changes: 2 additions & 9 deletions vscode/src/test/suite/ruby/custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,10 @@ import {
FIELD_SEPARATOR,
VALUE_SEPARATOR,
} from "../../../ruby/versionManager";
import { fakeContext } from "../helpers";

suite("Custom", () => {
const context = {
extensionMode: vscode.ExtensionMode.Test,
subscriptions: [],
workspaceState: {
get: (_name: string) => undefined,
update: (_name: string, _value: any) => Promise.resolve(),
},
extensionUri: vscode.Uri.parse("file:///fake"),
} as unknown as vscode.ExtensionContext;
const context = fakeContext();

test("Invokes custom script and then Ruby", async () => {
const workspacePath = fs.mkdtempSync(
Expand Down
15 changes: 4 additions & 11 deletions vscode/src/test/suite/ruby/mise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
FIELD_SEPARATOR,
VALUE_SEPARATOR,
} from "../../../ruby/versionManager";
import { fakeContext } from "../helpers";

suite("Mise", () => {
if (os.platform() === "win32") {
Expand All @@ -22,15 +23,7 @@ suite("Mise", () => {
return;
}

const context = {
extensionMode: vscode.ExtensionMode.Test,
subscriptions: [],
workspaceState: {
get: (_name: string) => undefined,
update: (_name: string, _value: any) => Promise.resolve(),
},
extensionUri: vscode.Uri.parse("file:///fake"),
} as unknown as vscode.ExtensionContext;
const context = fakeContext();

test("Finds Ruby only binary path is appended to PATH", async () => {
// eslint-disable-next-line no-process-env
Expand Down Expand Up @@ -74,7 +67,7 @@ suite("Mise", () => {

assert.ok(
execStub.calledOnceWithExactly(
`${os.homedir()}/.local/bin/mise x -- ruby -EUTF-8:UTF-8 '/fake/activation.rb'`,
`${os.homedir()}/.local/bin/mise x -- ruby -EUTF-8:UTF-8 '${context.extensionUri.fsPath}/activation.rb'`,
{
cwd: workspacePath,
shell: vscode.env.shell,
Expand Down Expand Up @@ -140,7 +133,7 @@ suite("Mise", () => {

assert.ok(
execStub.calledOnceWithExactly(
`${misePath} x -- ruby -EUTF-8:UTF-8 '/fake/activation.rb'`,
`${misePath} x -- ruby -EUTF-8:UTF-8 '${context.extensionUri.fsPath}/activation.rb'`,
{
cwd: workspacePath,
shell: vscode.env.shell,
Expand Down
Loading
Loading