|
| 1 | +import path from "path"; |
| 2 | +import assert from "assert"; |
| 3 | +import fs from "fs"; |
| 4 | +import os from "os"; |
| 5 | + |
| 6 | +import sinon from "sinon"; |
| 7 | +import * as vscode from "vscode"; |
| 8 | +import { beforeEach, afterEach, before, after } from "mocha"; |
| 9 | +import { State } from "vscode-languageclient"; |
| 10 | + |
| 11 | +import { RubyLsp } from "../../rubyLsp"; |
| 12 | +import { RUBY_VERSION } from "../rubyVersion"; |
| 13 | + |
| 14 | +import { FAKE_TELEMETRY } from "./fakeTelemetry"; |
| 15 | +import { ensureRubyInstallationPaths } from "./testHelpers"; |
| 16 | + |
| 17 | +suite("Ruby LSP", () => { |
| 18 | + const context = { |
| 19 | + extensionMode: vscode.ExtensionMode.Test, |
| 20 | + subscriptions: [], |
| 21 | + workspaceState: { |
| 22 | + get: (_name: string) => undefined, |
| 23 | + update: (_name: string, _value: any) => Promise.resolve(), |
| 24 | + }, |
| 25 | + extensionUri: vscode.Uri.file( |
| 26 | + path.dirname(path.dirname(path.dirname(__dirname))), |
| 27 | + ), |
| 28 | + } as unknown as vscode.ExtensionContext; |
| 29 | + let workspacePath: string; |
| 30 | + let workspaceUri: vscode.Uri; |
| 31 | + let workspaceFolder: vscode.WorkspaceFolder; |
| 32 | + const originalSaveBeforeStart = vscode.workspace |
| 33 | + .getConfiguration("debug") |
| 34 | + .get("saveBeforeStart"); |
| 35 | + |
| 36 | + before(async () => { |
| 37 | + await vscode.workspace |
| 38 | + .getConfiguration("debug") |
| 39 | + .update("saveBeforeStart", "none", true); |
| 40 | + }); |
| 41 | + |
| 42 | + after(async () => { |
| 43 | + await vscode.workspace |
| 44 | + .getConfiguration("debug") |
| 45 | + .update("saveBeforeStart", originalSaveBeforeStart, true); |
| 46 | + }); |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + workspacePath = fs.mkdtempSync( |
| 50 | + path.join(os.tmpdir(), "ruby-lsp-integration-test-"), |
| 51 | + ); |
| 52 | + workspaceUri = vscode.Uri.file(workspacePath); |
| 53 | + workspaceFolder = { |
| 54 | + uri: workspaceUri, |
| 55 | + name: path.basename(workspacePath), |
| 56 | + index: 0, |
| 57 | + }; |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(() => { |
| 61 | + fs.rmSync(workspacePath, { recursive: true, force: true }); |
| 62 | + }); |
| 63 | + |
| 64 | + test("launching debugger in a project with local Bundler settings and composed bundle", async () => { |
| 65 | + fs.writeFileSync(path.join(workspacePath, "test.rb"), "1 + 1"); |
| 66 | + fs.writeFileSync(path.join(workspacePath, ".ruby-version"), RUBY_VERSION); |
| 67 | + fs.writeFileSync( |
| 68 | + path.join(workspacePath, "Gemfile"), |
| 69 | + 'source "https://rubygems.org"\n', |
| 70 | + ); |
| 71 | + fs.writeFileSync( |
| 72 | + path.join(workspacePath, "Gemfile.lock"), |
| 73 | + [ |
| 74 | + "GEM", |
| 75 | + " remote: https://rubygems.org/", |
| 76 | + " specs:", |
| 77 | + "", |
| 78 | + "PLATFORMS", |
| 79 | + " arm64-darwin-23", |
| 80 | + " ruby", |
| 81 | + "", |
| 82 | + "DEPENDENCIES", |
| 83 | + "", |
| 84 | + "BUNDLED WITH", |
| 85 | + " 2.5.16", |
| 86 | + ].join("\n"), |
| 87 | + ); |
| 88 | + fs.mkdirSync(path.join(workspacePath, ".bundle")); |
| 89 | + fs.writeFileSync( |
| 90 | + path.join(workspacePath, ".bundle", "config"), |
| 91 | + `BUNDLE_PATH: ${path.join("vendor", "bundle")}`, |
| 92 | + ); |
| 93 | + |
| 94 | + await ensureRubyInstallationPaths(); |
| 95 | + |
| 96 | + const rubyLsp = new RubyLsp(context, FAKE_TELEMETRY); |
| 97 | + |
| 98 | + try { |
| 99 | + await rubyLsp.activate(workspaceFolder); |
| 100 | + |
| 101 | + const client = rubyLsp.workspaces.get( |
| 102 | + workspaceFolder.uri.toString(), |
| 103 | + )!.lspClient!; |
| 104 | + |
| 105 | + if (client.state !== State.Running) { |
| 106 | + await new Promise<void>((resolve) => { |
| 107 | + const callback = client.onDidChangeState(() => { |
| 108 | + if (client.state === State.Running) { |
| 109 | + callback.dispose(); |
| 110 | + resolve(); |
| 111 | + } |
| 112 | + }); |
| 113 | + }); |
| 114 | + } |
| 115 | + } catch (error: any) { |
| 116 | + assert.fail( |
| 117 | + `Failed to activate Ruby LSP: ${error.message}\n\n${error.stack}`, |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + const stub = sinon.stub(vscode.window, "activeTextEditor").get(() => { |
| 122 | + return { |
| 123 | + document: { |
| 124 | + uri: vscode.Uri.file(path.join(workspacePath, "test.rb")), |
| 125 | + }, |
| 126 | + } as vscode.TextEditor; |
| 127 | + }); |
| 128 | + |
| 129 | + const getWorkspaceStub = sinon |
| 130 | + .stub(vscode.workspace, "getWorkspaceFolder") |
| 131 | + .returns(workspaceFolder); |
| 132 | + |
| 133 | + try { |
| 134 | + await vscode.debug.startDebugging(workspaceFolder, { |
| 135 | + type: "ruby_lsp", |
| 136 | + name: "Debug", |
| 137 | + request: "launch", |
| 138 | + program: `ruby ${path.join(workspacePath, "test.rb")}`, |
| 139 | + }); |
| 140 | + } catch (error: any) { |
| 141 | + assert.fail(`Failed to launch debugger: ${error.message}`); |
| 142 | + } |
| 143 | + |
| 144 | + // The debugger might take a bit of time to disconnect from the editor. We need to perform cleanup when we receive |
| 145 | + // the termination callback or else we try to dispose of the debugger client too early, but we need to wait for that |
| 146 | + // so that we can clean up stubs otherwise they leak into other tests. |
| 147 | + await new Promise<void>((resolve) => { |
| 148 | + vscode.debug.onDidTerminateDebugSession((_session) => { |
| 149 | + stub.restore(); |
| 150 | + getWorkspaceStub.restore(); |
| 151 | + |
| 152 | + context.subscriptions.forEach((subscription) => { |
| 153 | + if (!("logLevel" in subscription)) { |
| 154 | + subscription.dispose(); |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + resolve(); |
| 159 | + }); |
| 160 | + }); |
| 161 | + }).timeout(90000); |
| 162 | +}); |
0 commit comments