Skip to content

Commit dcb9875

Browse files
committed
Detect if swiftly is already installed
1 parent d58b133 commit dcb9875

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/toolchain/swiftly.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,18 @@ export class Swiftly {
174174
}
175175

176176
public static async isInstalled() {
177-
178-
if(!Swiftly.isSupported()) {
177+
if (!Swiftly.isSupported()) {
179178
return false;
180179
}
181180

182181
try {
183182
await Swiftly.version();
184183
return true;
185184
} catch (error) {
186-
if (error instanceof ExecFileError && 'code' in error && error.code === "ENOENT") {
185+
if (error instanceof ExecFileError && "code" in error && error.code === "ENOENT") {
187186
return false;
188187
}
189188
throw error;
190189
}
191-
192190
}
193191
}

test/unit-tests/toolchain/swiftly.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,46 @@ suite("Swiftly Unit Tests", () => {
103103
expect(mockUtilities.execFile).not.have.been.called;
104104
});
105105
});
106+
107+
suite("isInstalled", () => {
108+
test("should return false when platform is not supported", async () => {
109+
mockedPlatform.setValue("win32");
110+
111+
const result = await Swiftly.isInstalled();
112+
113+
expect(result).to.be.false;
114+
expect(mockUtilities.execFile).not.have.been.called;
115+
});
116+
117+
test("should return true when swiftly is installed", async () => {
118+
mockedPlatform.setValue("darwin");
119+
120+
mockUtilities.execFile.withArgs("swiftly", ["--version"]).resolves({
121+
stdout: "1.1.0\n",
122+
stderr: "",
123+
});
124+
125+
const result = await Swiftly.isInstalled();
126+
127+
expect(result).to.be.true;
128+
expect(mockUtilities.execFile).to.have.been.calledWith("swiftly", ["--version"]);
129+
});
130+
131+
test("should throw error when swiftly command fails with non-ENOENT error", async () => {
132+
mockedPlatform.setValue("darwin");
133+
134+
const otherError = new Error("Other error");
135+
136+
mockUtilities.execFile.withArgs("swiftly", ["--version"]).rejects(otherError);
137+
138+
try {
139+
await Swiftly.isInstalled();
140+
expect.fail("Should have thrown an error");
141+
} catch (error) {
142+
expect(error).to.equal(otherError);
143+
}
144+
145+
expect(mockUtilities.execFile).to.have.been.calledWith("swiftly", ["--version"]);
146+
});
147+
});
106148
});

0 commit comments

Comments
 (0)