Skip to content

Commit bcb5c31

Browse files
committed
fixup! Extract logic to detect Asdf version manager into the Asdf class
1 parent 81c9a4b commit bcb5c31

2 files changed

Lines changed: 56 additions & 15 deletions

File tree

vscode/src/ruby/asdf.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export class Asdf extends VersionManager {
3434
}
3535

3636
static async detect(
37-
_workspaceFolder: vscode.WorkspaceFolder,
38-
_outputChannel: WorkspaceChannel,
37+
workspaceFolder: vscode.WorkspaceFolder,
38+
outputChannel: WorkspaceChannel,
3939
): Promise<DetectionResult> {
4040
// Check for v0.16+ executables first
4141
const executablePaths = Asdf.getPossibleExecutablePaths();
@@ -51,6 +51,12 @@ export class Asdf extends VersionManager {
5151
return { type: "path", uri: scriptResult };
5252
}
5353

54+
// check on PATH
55+
const toolExists = await VersionManager.toolExists("asdf", workspaceFolder, outputChannel);
56+
if (toolExists) {
57+
return { type: "semantic", marker: "asdf" };
58+
}
59+
5460
return { type: "none" };
5561
}
5662

@@ -63,24 +69,31 @@ export class Asdf extends VersionManager {
6369
asdfUri = vscode.Uri.file(configuredPath);
6470
} else {
6571
const result = await Asdf.detect(this.workspaceFolder, this.outputChannel);
72+
6673
if (result.type === "path") {
6774
asdfUri = result.uri;
75+
} else if (result.type === "semantic") {
76+
// Use ASDF from PATH
77+
} else {
78+
throw new Error(
79+
`Could not find ASDF installation. Searched in ${[
80+
...Asdf.getPossibleExecutablePaths(),
81+
...Asdf.getPossibleScriptPaths(),
82+
].join(", ")}`,
83+
);
6884
}
6985
}
7086

71-
if (!asdfUri) {
72-
throw new Error(
73-
`Could not find ASDF installation. Searched in ${[
74-
...Asdf.getPossibleExecutablePaths(),
75-
...Asdf.getPossibleScriptPaths(),
76-
].join(", ")}`,
77-
);
78-
}
87+
let baseCommand: string;
7988

80-
const asdfPath = asdfUri.fsPath;
81-
// If there's no extension name, then we are using the ASDF executable directly. If there is an extension, then it's
82-
// a shell script and we have to source it first
83-
const baseCommand = path.extname(asdfPath) === "" ? asdfPath : `. ${asdfPath} && asdf`;
89+
if (asdfUri) {
90+
const asdfPath = asdfUri.fsPath;
91+
// If there's no extension name, then we are using the ASDF executable directly. If there is an extension, then it's
92+
// a shell script and we have to source it first
93+
baseCommand = path.extname(asdfPath) === "" ? asdfPath : `. ${asdfPath} && asdf`;
94+
} else {
95+
baseCommand = "asdf";
96+
}
8497

8598
const parsedResult = await this.runEnvActivationScript(`${baseCommand} exec ruby`);
8699

vscode/src/test/suite/ruby/asdf.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { afterEach, beforeEach } from "mocha";
99
import { Asdf } from "../../../ruby/asdf";
1010
import { WorkspaceChannel } from "../../../workspaceChannel";
1111
import * as common from "../../../common";
12-
import { ACTIVATION_SEPARATOR, FIELD_SEPARATOR, VALUE_SEPARATOR } from "../../../ruby/versionManager";
12+
import { ACTIVATION_SEPARATOR, FIELD_SEPARATOR, VALUE_SEPARATOR, VersionManager } from "../../../ruby/versionManager";
1313
import { createContext, FakeContext } from "../helpers";
1414

1515
suite("Asdf", () => {
@@ -136,4 +136,32 @@ suite("Asdf", () => {
136136
assert.strictEqual(yjit, true);
137137
assert.strictEqual(env.ANY, "true");
138138
});
139+
140+
test("Uses ASDF executable in PATH if script and Homebrew executable are not available", async () => {
141+
const asdf = new Asdf(workspaceFolder, outputChannel, context, async () => {});
142+
143+
const envStub = ["3.0.0", "/path/to/gems", "true", `ANY${VALUE_SEPARATOR}true`].join(FIELD_SEPARATOR);
144+
const execStub = sandbox.stub(common, "asyncExec").resolves({
145+
stdout: "",
146+
stderr: `${ACTIVATION_SEPARATOR}${envStub}${ACTIVATION_SEPARATOR}`,
147+
});
148+
149+
sandbox.stub(VersionManager, "toolExists").resolves(true);
150+
151+
const { env, version, yjit } = await asdf.activate();
152+
153+
assert.ok(
154+
execStub.calledOnceWithExactly(`asdf exec ruby -EUTF-8:UTF-8 '${activationPath.fsPath}'`, {
155+
cwd: workspacePath,
156+
shell: vscode.env.shell,
157+
158+
env: process.env,
159+
encoding: "utf-8",
160+
}),
161+
);
162+
163+
assert.strictEqual(version, "3.0.0");
164+
assert.strictEqual(yjit, true);
165+
assert.strictEqual(env.ANY, "true");
166+
});
139167
});

0 commit comments

Comments
 (0)