|
| 1 | +/* eslint-disable no-process-env */ |
| 2 | +import path from "path"; |
| 3 | +import assert from "assert"; |
| 4 | +import fs from "fs"; |
| 5 | +import os from "os"; |
| 6 | + |
| 7 | +import sinon from "sinon"; |
| 8 | +import * as vscode from "vscode"; |
| 9 | +import { beforeEach, afterEach } from "mocha"; |
| 10 | +import { State } from "vscode-languageclient"; |
| 11 | + |
| 12 | +import { RubyLsp } from "../../rubyLsp"; |
| 13 | +import { RUBY_VERSION } from "../rubyVersion"; |
| 14 | +import { ManagerIdentifier } from "../../ruby"; |
| 15 | + |
| 16 | +import { FAKE_TELEMETRY } from "./fakeTelemetry"; |
| 17 | +import { createRubySymlinks } from "./helpers"; |
| 18 | + |
| 19 | +suite("Ruby LSP", () => { |
| 20 | + const context = { |
| 21 | + extensionMode: vscode.ExtensionMode.Test, |
| 22 | + subscriptions: [], |
| 23 | + workspaceState: { |
| 24 | + get: (_name: string) => undefined, |
| 25 | + update: (_name: string, _value: any) => Promise.resolve(), |
| 26 | + }, |
| 27 | + extensionUri: vscode.Uri.file( |
| 28 | + path.dirname(path.dirname(path.dirname(__dirname))), |
| 29 | + ), |
| 30 | + } as unknown as vscode.ExtensionContext; |
| 31 | + let workspacePath: string; |
| 32 | + let workspaceUri: vscode.Uri; |
| 33 | + let workspaceFolder: vscode.WorkspaceFolder; |
| 34 | + const originalSaveBeforeStart = vscode.workspace |
| 35 | + .getConfiguration("debug") |
| 36 | + .get("saveBeforeStart"); |
| 37 | + let workspacesStub: sinon.SinonStub; |
| 38 | + |
| 39 | + beforeEach(async () => { |
| 40 | + await vscode.workspace |
| 41 | + .getConfiguration("debug") |
| 42 | + .update("saveBeforeStart", "none", true); |
| 43 | + workspacePath = fs.mkdtempSync( |
| 44 | + path.join(os.tmpdir(), "ruby-lsp-integration-test-"), |
| 45 | + ); |
| 46 | + workspaceUri = vscode.Uri.file(workspacePath); |
| 47 | + workspaceFolder = { |
| 48 | + uri: workspaceUri, |
| 49 | + name: path.basename(workspacePath), |
| 50 | + index: 0, |
| 51 | + }; |
| 52 | + |
| 53 | + workspacesStub = sinon |
| 54 | + .stub(vscode.workspace, "workspaceFolders") |
| 55 | + .get(() => [workspaceFolder]); |
| 56 | + |
| 57 | + if (process.env.CI) { |
| 58 | + createRubySymlinks(); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + afterEach(async () => { |
| 63 | + workspacesStub.restore(); |
| 64 | + fs.rmSync(workspacePath, { recursive: true, force: true }); |
| 65 | + |
| 66 | + await vscode.workspace |
| 67 | + .getConfiguration("debug") |
| 68 | + .update("saveBeforeStart", originalSaveBeforeStart, true); |
| 69 | + }); |
| 70 | + |
| 71 | + function writeFileSetup() { |
| 72 | + fs.writeFileSync(path.join(workspacePath, "test.rb"), "1 + 1"); |
| 73 | + fs.writeFileSync(path.join(workspacePath, ".ruby-version"), RUBY_VERSION); |
| 74 | + fs.writeFileSync( |
| 75 | + path.join(workspacePath, "Gemfile"), |
| 76 | + 'source "https://rubygems.org"\n', |
| 77 | + ); |
| 78 | + fs.writeFileSync( |
| 79 | + path.join(workspacePath, "Gemfile.lock"), |
| 80 | + [ |
| 81 | + "GEM", |
| 82 | + " remote: https://rubygems.org/", |
| 83 | + " specs:", |
| 84 | + "", |
| 85 | + "PLATFORMS", |
| 86 | + " arm64-darwin-23", |
| 87 | + " ruby", |
| 88 | + "", |
| 89 | + "DEPENDENCIES", |
| 90 | + "", |
| 91 | + "BUNDLED WITH", |
| 92 | + " 2.5.16", |
| 93 | + ].join("\n"), |
| 94 | + ); |
| 95 | + fs.mkdirSync(path.join(workspacePath, ".bundle")); |
| 96 | + fs.writeFileSync( |
| 97 | + path.join(workspacePath, ".bundle", "config"), |
| 98 | + `BUNDLE_PATH: ${path.join("vendor", "bundle")}`, |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + test("launching debugger in a project with local Bundler settings and composed bundle", async () => { |
| 103 | + writeFileSetup(); |
| 104 | + |
| 105 | + if (process.env.CI && os.platform() === "win32") { |
| 106 | + await vscode.workspace |
| 107 | + .getConfiguration("rubyLsp") |
| 108 | + .update( |
| 109 | + "rubyVersionManager", |
| 110 | + { identifier: ManagerIdentifier.RubyInstaller }, |
| 111 | + true, |
| 112 | + ); |
| 113 | + } else if (process.env.CI) { |
| 114 | + await vscode.workspace |
| 115 | + .getConfiguration("rubyLsp") |
| 116 | + .update( |
| 117 | + "rubyVersionManager", |
| 118 | + { identifier: ManagerIdentifier.Chruby }, |
| 119 | + true, |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + const rubyLsp = new RubyLsp(context, FAKE_TELEMETRY); |
| 124 | + |
| 125 | + try { |
| 126 | + await rubyLsp.activate(); |
| 127 | + } catch (error: any) { |
| 128 | + assert.fail( |
| 129 | + `Failed to activate Ruby LSP: ${error.message}\n\n${error.stack}`, |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + const client = rubyLsp.getWorkspace(workspaceFolder.uri)!.lspClient!; |
| 134 | + |
| 135 | + if (client.state === State.Starting) { |
| 136 | + await new Promise<void>((resolve) => { |
| 137 | + const callback = client.onDidChangeState((event) => { |
| 138 | + if (event.newState === State.Running) { |
| 139 | + callback.dispose(); |
| 140 | + resolve(); |
| 141 | + } |
| 142 | + }); |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + assert.strictEqual(client.state, State.Running); |
| 147 | + assert.ok( |
| 148 | + fs.existsSync(path.join(workspacePath, ".ruby-lsp", "Gemfile.lock")), |
| 149 | + ); |
| 150 | + assert.match( |
| 151 | + fs |
| 152 | + .readFileSync(path.join(workspacePath, ".ruby-lsp", "Gemfile.lock")) |
| 153 | + .toString(), |
| 154 | + /debug/, |
| 155 | + ); |
| 156 | + |
| 157 | + try { |
| 158 | + await vscode.debug.startDebugging(workspaceFolder, { |
| 159 | + type: "ruby_lsp", |
| 160 | + name: "Debug", |
| 161 | + request: "launch", |
| 162 | + program: `ruby ${path.join(workspacePath, "test.rb")}`, |
| 163 | + workspaceFolder, |
| 164 | + }); |
| 165 | + } catch (error: any) { |
| 166 | + assert.fail(`Failed to launch debugger: ${error.message}`); |
| 167 | + } |
| 168 | + |
| 169 | + await new Promise<void>((resolve) => { |
| 170 | + const callback = vscode.debug.onDidTerminateDebugSession((_session) => { |
| 171 | + context.subscriptions.forEach((subscription) => { |
| 172 | + if (!("logLevel" in subscription)) { |
| 173 | + subscription.dispose(); |
| 174 | + } |
| 175 | + }); |
| 176 | + |
| 177 | + callback.dispose(); |
| 178 | + resolve(); |
| 179 | + }); |
| 180 | + }); |
| 181 | + }).timeout(90000); |
| 182 | +}); |
0 commit comments