Skip to content

Commit

Permalink
Search for asdf.fish when the shell is fish (#2111)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed May 30, 2024
1 parent 98752bc commit 30a9aef
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
12 changes: 8 additions & 4 deletions vscode/src/ruby/asdf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-process-env */

import os from "os";
import path from "path";

import * as vscode from "vscode";

Expand Down Expand Up @@ -37,23 +38,26 @@ export class Asdf extends VersionManager {

// Only public for testing. Finds the ASDF installation URI based on what's advertised in the ASDF documentation
async findAsdfInstallation(): Promise<vscode.Uri> {
const scriptName =
path.basename(vscode.env.shell) === "fish" ? "asdf.fish" : "asdf.sh";

// Possible ASDF installation paths as described in https://asdf-vm.com/guide/getting-started.html#_3-install-asdf.
// In order, the methods of installation are:
// 1. Git
// 2. Pacman
// 3. Homebrew M series
// 4. Homebrew Intel series
const possiblePaths = [
vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".asdf", "asdf.sh"),
vscode.Uri.joinPath(vscode.Uri.file("/"), "opt", "asdf-vm", "asdf.sh"),
vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".asdf", scriptName),
vscode.Uri.joinPath(vscode.Uri.file("/"), "opt", "asdf-vm", scriptName),
vscode.Uri.joinPath(
vscode.Uri.file("/"),
"opt",
"homebrew",
"opt",
"asdf",
"libexec",
"asdf.sh",
scriptName,
),
vscode.Uri.joinPath(
vscode.Uri.file("/"),
Expand All @@ -62,7 +66,7 @@ export class Asdf extends VersionManager {
"opt",
"asdf",
"libexec",
"asdf.sh",
scriptName,
),
];

Expand Down
52 changes: 52 additions & 0 deletions vscode/src/test/suite/ruby/asdf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,56 @@ suite("Asdf", () => {
findInstallationStub.restore();
shellStub.restore();
});

test("Searches for asdf.fish when using the fish shell", async () => {
// eslint-disable-next-line no-process-env
const workspacePath = process.env.PWD!;
const workspaceFolder = {
uri: vscode.Uri.from({ scheme: "file", path: workspacePath }),
name: path.basename(workspacePath),
index: 0,
};
const outputChannel = new WorkspaceChannel("fake", common.LOG_CHANNEL);
const asdf = new Asdf(workspaceFolder, outputChannel);
const activationScript =
"STDERR.print({env: ENV.to_h,yjit:!!defined?(RubyVM::YJIT),version:RUBY_VERSION}.to_json)";

const execStub = sinon.stub(common, "asyncExec").resolves({
stdout: "",
stderr: JSON.stringify({
env: { ANY: "true" },
yjit: true,
version: "3.0.0",
}),
});

const findInstallationStub = sinon
.stub(asdf, "findAsdfInstallation")
.resolves(vscode.Uri.file(`${os.homedir()}/.asdf/asdf.fish`));
const shellStub = sinon
.stub(vscode.env, "shell")
.get(() => "/opt/homebrew/bin/fish");

const { env, version, yjit } = await asdf.activate();

assert.ok(
execStub.calledOnceWithExactly(
`. ${os.homedir()}/.asdf/asdf.fish && asdf exec ruby -W0 -rjson -e '${activationScript}'`,
{
cwd: workspacePath,
shell: "/opt/homebrew/bin/fish",
// eslint-disable-next-line no-process-env
env: process.env,
},
),
);

assert.strictEqual(version, "3.0.0");
assert.strictEqual(yjit, true);
assert.strictEqual(env.ANY, "true");

execStub.restore();
findInstallationStub.restore();
shellStub.restore();
});
});

0 comments on commit 30a9aef

Please sign in to comment.