diff --git a/vscode/src/test/suite/launch.test.ts b/vscode/src/test/suite/launch.test.ts new file mode 100644 index 000000000..1d89e2eec --- /dev/null +++ b/vscode/src/test/suite/launch.test.ts @@ -0,0 +1,158 @@ +/* eslint-disable no-process-env */ +import assert from "assert"; +import path from "path"; +import os from "os"; +import fs from "fs"; + +import * as vscode from "vscode"; +import { State } from "vscode-languageclient/node"; +import { afterEach, beforeEach } from "mocha"; +import sinon from "sinon"; + +import { Ruby, ManagerIdentifier } from "../../ruby"; +import Client from "../../client"; +import { WorkspaceChannel } from "../../workspaceChannel"; +import { RUBY_VERSION, MAJOR, MINOR } from "../rubyVersion"; +import { LOG_CHANNEL } from "../../common"; +import * as common from "../../common"; + +import { FAKE_TELEMETRY } from "./fakeTelemetry"; + +suite("Launch integrations", () => { + beforeEach(async () => { + // Ensure that we're activating the correct Ruby version on CI + if (process.env.CI) { + if (os.platform() === "linux") { + await vscode.workspace + .getConfiguration("rubyLsp") + .update( + "rubyVersionManager", + { identifier: ManagerIdentifier.Chruby }, + true, + ); + + fs.mkdirSync(path.join(os.homedir(), ".rubies"), { recursive: true }); + fs.symlinkSync( + `/opt/hostedtoolcache/Ruby/${RUBY_VERSION}/x64`, + path.join(os.homedir(), ".rubies", RUBY_VERSION), + ); + } else if (os.platform() === "darwin") { + await vscode.workspace + .getConfiguration("rubyLsp") + .update( + "rubyVersionManager", + { identifier: ManagerIdentifier.Chruby }, + true, + ); + + fs.mkdirSync(path.join(os.homedir(), ".rubies"), { recursive: true }); + fs.symlinkSync( + `/Users/runner/hostedtoolcache/Ruby/${RUBY_VERSION}/arm64`, + path.join(os.homedir(), ".rubies", RUBY_VERSION), + ); + } else { + await vscode.workspace + .getConfiguration("rubyLsp") + .update( + "rubyVersionManager", + { identifier: ManagerIdentifier.RubyInstaller }, + true, + ); + + fs.symlinkSync( + path.join( + "C:", + "hostedtoolcache", + "windows", + "Ruby", + RUBY_VERSION, + "x64", + ), + path.join("C:", `Ruby${MAJOR}${MINOR}-${os.arch()}`), + ); + } + } + }); + + afterEach(async () => { + if (process.env.CI) { + if (os.platform() === "linux" || os.platform() === "darwin") { + fs.rmSync(path.join(os.homedir(), ".rubies"), { + recursive: true, + force: true, + }); + } else { + fs.rmSync(path.join("C:", `Ruby${MAJOR}${MINOR}-${os.arch()}`), { + recursive: true, + force: true, + }); + } + } + }); + + const workspacePath = path.dirname( + path.dirname(path.dirname(path.dirname(__dirname))), + ); + const workspaceUri = vscode.Uri.file(workspacePath); + const workspaceFolder: vscode.WorkspaceFolder = { + uri: workspaceUri, + name: path.basename(workspaceUri.fsPath), + index: 0, + }; + + const context = { + extensionMode: vscode.ExtensionMode.Test, + subscriptions: [], + workspaceState: { + get: (_name: string) => undefined, + update: (_name: string, _value: any) => Promise.resolve(), + }, + } as unknown as vscode.ExtensionContext; + const outputChannel = new WorkspaceChannel("fake", LOG_CHANNEL); + + async function createClient() { + const ruby = new Ruby( + context, + workspaceFolder, + outputChannel, + FAKE_TELEMETRY, + ); + await ruby.activateRuby(); + + const client = new Client( + context, + FAKE_TELEMETRY, + ruby, + () => {}, + workspaceFolder, + outputChannel, + new Map(), + ); + + client.clientOptions.initializationFailedHandler = (error) => { + assert.fail(`Failed to start server ${error.message}\n`); + }; + return client; + } + + test("with launcher mode enabled", async () => { + const featureStub = sinon.stub(common, "featureEnabled").returns(true); + const client = await createClient(); + featureStub.restore(); + + try { + await client.start(); + } catch (error: any) { + assert.fail(`Failed to start server ${error.message}`); + } + + assert.strictEqual(client.state, State.Running); + + try { + await client.stop(); + await client.dispose(); + } catch (error: any) { + assert.fail(`Failed to stop server: ${error.message}`); + } + }).timeout(60000); +});