|
| 1 | +import assert from "assert"; |
| 2 | +import path from "path"; |
| 3 | +import os from "os"; |
| 4 | +import fs from "fs"; |
| 5 | + |
| 6 | +import * as vscode from "vscode"; |
| 7 | +import sinon from "sinon"; |
| 8 | +import { afterEach, beforeEach } from "mocha"; |
| 9 | + |
| 10 | +import { Rv } from "../../../ruby/rv"; |
| 11 | +import { WorkspaceChannel } from "../../../workspaceChannel"; |
| 12 | +import * as common from "../../../common"; |
| 13 | +import { ACTIVATION_SEPARATOR, FIELD_SEPARATOR, VALUE_SEPARATOR } from "../../../ruby/versionManager"; |
| 14 | +import { createContext, FakeContext } from "../helpers"; |
| 15 | + |
| 16 | +suite("Rv", () => { |
| 17 | + if (os.platform() === "win32") { |
| 18 | + // eslint-disable-next-line no-console |
| 19 | + console.log("Skipping Rv tests on Windows"); |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + let activationPath: vscode.Uri; |
| 24 | + let sandbox: sinon.SinonSandbox; |
| 25 | + let context: FakeContext; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + sandbox = sinon.createSandbox(); |
| 29 | + context = createContext(); |
| 30 | + activationPath = vscode.Uri.joinPath(context.extensionUri, "activation.rb"); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + sandbox.restore(); |
| 35 | + context.dispose(); |
| 36 | + }); |
| 37 | + |
| 38 | + test("Activates with auto-detected version", async () => { |
| 39 | + const workspacePath = process.env.PWD!; |
| 40 | + const workspaceFolder = { |
| 41 | + uri: vscode.Uri.from({ scheme: "file", path: workspacePath }), |
| 42 | + name: path.basename(workspacePath), |
| 43 | + index: 0, |
| 44 | + }; |
| 45 | + const outputChannel = new WorkspaceChannel("fake", common.LOG_CHANNEL); |
| 46 | + |
| 47 | + const rv = new Rv(workspaceFolder, outputChannel, context, async () => {}); |
| 48 | + |
| 49 | + const envStub = ["3.4.8", "/path/to/gems", "true", `ANY${VALUE_SEPARATOR}true`].join(FIELD_SEPARATOR); |
| 50 | + |
| 51 | + const execStub = sandbox.stub(common, "asyncExec").resolves({ |
| 52 | + stdout: "", |
| 53 | + stderr: `${ACTIVATION_SEPARATOR}${envStub}${ACTIVATION_SEPARATOR}`, |
| 54 | + }); |
| 55 | + |
| 56 | + // Stub findRv to return the executable path |
| 57 | + sandbox.stub(rv, "findRv" as any).resolves("rv"); |
| 58 | + |
| 59 | + const { env, version, yjit } = await rv.activate(); |
| 60 | + |
| 61 | + assert.ok( |
| 62 | + execStub.calledOnceWithExactly(`rv ruby run -- -EUTF-8:UTF-8 '${activationPath.fsPath}'`, { |
| 63 | + cwd: workspacePath, |
| 64 | + shell: vscode.env.shell, |
| 65 | + |
| 66 | + env: process.env, |
| 67 | + encoding: "utf-8", |
| 68 | + }), |
| 69 | + ); |
| 70 | + |
| 71 | + assert.strictEqual(version, "3.4.8"); |
| 72 | + assert.strictEqual(yjit, true); |
| 73 | + assert.strictEqual(env.ANY, "true"); |
| 74 | + }); |
| 75 | + |
| 76 | + test("Allows configuring where rv is installed", async () => { |
| 77 | + const workspacePath = fs.mkdtempSync(path.join(os.tmpdir(), "ruby-lsp-test-")); |
| 78 | + const workspaceFolder = { |
| 79 | + uri: vscode.Uri.from({ scheme: "file", path: workspacePath }), |
| 80 | + name: path.basename(workspacePath), |
| 81 | + index: 0, |
| 82 | + }; |
| 83 | + const outputChannel = new WorkspaceChannel("fake", common.LOG_CHANNEL); |
| 84 | + |
| 85 | + const rv = new Rv(workspaceFolder, outputChannel, context, async () => {}); |
| 86 | + |
| 87 | + const envStub = ["3.4.8", "/path/to/gems", "true", `ANY${VALUE_SEPARATOR}true`].join(FIELD_SEPARATOR); |
| 88 | + |
| 89 | + const execStub = sandbox.stub(common, "asyncExec").resolves({ |
| 90 | + stdout: "", |
| 91 | + stderr: `${ACTIVATION_SEPARATOR}${envStub}${ACTIVATION_SEPARATOR}`, |
| 92 | + }); |
| 93 | + |
| 94 | + const rvPath = path.join(workspacePath, "rv"); |
| 95 | + fs.writeFileSync(rvPath, "fakeRvBinary"); |
| 96 | + |
| 97 | + // Stub findRv to return the configured executable path |
| 98 | + sandbox.stub(rv, "findRv" as any).resolves(rvPath); |
| 99 | + |
| 100 | + const configStub = sinon.stub(vscode.workspace, "getConfiguration").returns({ |
| 101 | + get: (name: string) => { |
| 102 | + if (name === "rubyVersionManager.rvExecutablePath") { |
| 103 | + return rvPath; |
| 104 | + } |
| 105 | + return ""; |
| 106 | + }, |
| 107 | + } as any); |
| 108 | + |
| 109 | + const { env, version, yjit } = await rv.activate(); |
| 110 | + |
| 111 | + assert.ok( |
| 112 | + execStub.calledOnceWithExactly(`${rvPath} ruby run -- -EUTF-8:UTF-8 '${activationPath.fsPath}'`, { |
| 113 | + cwd: workspacePath, |
| 114 | + shell: vscode.env.shell, |
| 115 | + |
| 116 | + env: process.env, |
| 117 | + encoding: "utf-8", |
| 118 | + }), |
| 119 | + ); |
| 120 | + |
| 121 | + assert.strictEqual(version, "3.4.8"); |
| 122 | + assert.strictEqual(yjit, true); |
| 123 | + assert.deepStrictEqual(env.ANY, "true"); |
| 124 | + |
| 125 | + execStub.restore(); |
| 126 | + configStub.restore(); |
| 127 | + fs.rmSync(workspacePath, { recursive: true, force: true }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments