Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/toolchain/swiftly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class Swiftly {
}
}

private static isSupported() {
public static isSupported() {
return process.platform === "linux" || process.platform === "darwin";
}

Expand Down Expand Up @@ -235,4 +235,16 @@ export class Swiftly {
);
return JSON.parse(swiftlyConfigRaw);
}

public static async isInstalled(): Promise<boolean> {
if (!this.isSupported()) {
return false;
}
try {
const { stdout } = await execFile("which", ["swiftly"]);
return stdout.trim().length > 0;
} catch (error) {
return false;
}
}
}
2 changes: 1 addition & 1 deletion src/ui/ToolchainSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ async function getQuickPickItems(
}
// Various actions that the user can perform (e.g. to install new toolchains)
const actionItems: ActionItem[] = [];
if (process.platform === "linux" || process.platform === "darwin") {
if (Swiftly.isSupported() && !(await Swiftly.isInstalled())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: great idea to reuse the existing logic

const platformName = process.platform === "linux" ? "Linux" : "macOS";
actionItems.push({
type: "action",
Expand Down
44 changes: 44 additions & 0 deletions test/unit-tests/toolchain/swiftly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,48 @@ suite("Swiftly Unit Tests", () => {
expect(mockUtilities.execFile).not.have.been.called;
});
});

suite("isInstalled", () => {
test("should return true when swiftly is found", async () => {
mockUtilities.execFile.withArgs("which", ["swiftly"]).resolves({
stdout: "/usr/local/bin/swiftly\n",
stderr: "",
});

const result = await Swiftly.isInstalled();

expect(result).to.be.true;
expect(mockUtilities.execFile).to.have.been.calledWith("which", ["swiftly"]);
});

test("should return false when swiftly is not found", async () => {
mockUtilities.execFile.withArgs("which", ["swiftly"]).rejects(new Error("not found"));

const result = await Swiftly.isInstalled();

expect(result).to.be.false;
expect(mockUtilities.execFile).to.have.been.calledWith("which", ["swiftly"]);
});

test("should return false when which returns empty output", async () => {
mockUtilities.execFile.withArgs("which", ["swiftly"]).resolves({
stdout: "",
stderr: "",
});

const result = await Swiftly.isInstalled();

expect(result).to.be.false;
expect(mockUtilities.execFile).to.have.been.calledWith("which", ["swiftly"]);
});

test("should return false when platform is not supported", async () => {
mockedPlatform.setValue("win32");

const result = await Swiftly.isInstalled();

expect(result).to.be.false;
expect(mockUtilities.execFile).not.to.have.been.called;
});
});
});