Skip to content

Detect Swiftly Version File #1721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 21 additions & 2 deletions src/toolchain/swiftly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const ListResult = z.object({
toolchains: z.array(
z.object({
inUse: z.boolean(),
installed: z.boolean(),
isDefault: z.boolean(),
name: z.string(),
version: z.discriminatedUnion("type", [
z.object({
major: z.number().optional(),
Expand Down Expand Up @@ -125,7 +127,8 @@ export class Swiftly {
const response = ListResult.parse(JSON.parse(stdout));
return response.toolchains.map(t => t.version.name);
} catch (error) {
outputChannel?.appendLine(`Failed to retrieve Swiftly installations: ${error}`);
outputChannel?.appendLine(
`Failed to retrieve Swiftly installations : ${error}`);
return [];
}
}
Expand Down Expand Up @@ -155,7 +158,7 @@ export class Swiftly {
}
}

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

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

public static async isInstalled() {
if (!Swiftly.isSupported()) {
return false;
}

try {
await Swiftly.version();
return true;
} catch (error) {
if (error instanceof ExecFileError && "code" in error && error.code === "ENOENT") {
return false;
}
throw error;
}
}
}
2 changes: 1 addition & 1 deletion src/ui/ToolchainSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,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())) {
const platformName = process.platform === "linux" ? "Linux" : "macOS";
actionItems.push({
type: "action",
Expand Down
58 changes: 54 additions & 4 deletions test/unit-tests/toolchain/swiftly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ suite("Swiftly Unit Tests", () => {
toolchains: [
{
inUse: true,
installed: true,
isDefault: true,
name: "swift-5.9.0-RELEASE",
version: {
major: 5,
minor: 9,
Expand All @@ -49,7 +51,9 @@ suite("Swiftly Unit Tests", () => {
},
{
inUse: false,
installed: true,
isDefault: false,
name: "swift-5.8.0-RELEASE",
version: {
major: 5,
minor: 8,
Expand All @@ -60,7 +64,9 @@ suite("Swiftly Unit Tests", () => {
},
{
inUse: false,
installed: false,
isDefault: false,
name: "swift-DEVELOPMENT-SNAPSHOT-2023-10-15-a",
version: {
major: 5,
minor: 10,
Expand All @@ -73,10 +79,12 @@ suite("Swiftly Unit Tests", () => {
],
};

mockUtilities.execFile.withArgs("swiftly", ["list", "--format=json"]).resolves({
stdout: JSON.stringify(jsonOutput),
stderr: "",
});
mockUtilities.execFile
.withArgs("swiftly", ["list", "--format=json"])
.resolves({
stdout: JSON.stringify(jsonOutput),
stderr: "",
});

const result = await Swiftly.listAvailableToolchains();

Expand All @@ -102,4 +110,46 @@ suite("Swiftly Unit Tests", () => {
expect(mockUtilities.execFile).not.have.been.called;
});
});

suite("isInstalled", () => {
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.have.been.called;
});

test("should return true when swiftly is installed", async () => {
mockedPlatform.setValue("darwin");

mockUtilities.execFile.withArgs("swiftly", ["--version"]).resolves({
stdout: "1.1.0\n",
stderr: "",
});

const result = await Swiftly.isInstalled();

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

test("should throw error when swiftly command fails with non-ENOENT error", async () => {
mockedPlatform.setValue("darwin");

const otherError = new Error("Other error");

mockUtilities.execFile.withArgs("swiftly", ["--version"]).rejects(otherError);

try {
await Swiftly.isInstalled();
expect.fail("Should have thrown an error");
} catch (error) {
expect(error).to.equal(otherError);
}

expect(mockUtilities.execFile).to.have.been.calledWith("swiftly", ["--version"]);
});
});
});