Skip to content

Commit fba7353

Browse files
committed
Detect if swiftly is already installed
1 parent 81c8a22 commit fba7353

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
@@ -247,20 +247,18 @@ export class Swiftly {
247247
}
248248

249249
public static async isInstalled() {
250-
251-
if(!Swiftly.isSupported()) {
250+
if (!Swiftly.isSupported()) {
252251
return false;
253252
}
254253

255254
try {
256255
await Swiftly.version();
257256
return true;
258257
} catch (error) {
259-
if (error instanceof ExecFileError && 'code' in error && error.code === "ENOENT") {
258+
if (error instanceof ExecFileError && "code" in error && error.code === "ENOENT") {
260259
return false;
261260
}
262261
throw error;
263262
}
264-
265263
}
266264
}

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

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

0 commit comments

Comments
 (0)