Skip to content

Commit f2f60d6

Browse files
committed
Detect if swiftly is already installed
1 parent c531c62 commit f2f60d6

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
@@ -196,20 +196,18 @@ export class Swiftly {
196196
}
197197

198198
public static async isInstalled() {
199-
200-
if(!Swiftly.isSupported()) {
199+
if (!Swiftly.isSupported()) {
201200
return false;
202201
}
203202

204203
try {
205204
await Swiftly.version();
206205
return true;
207206
} catch (error) {
208-
if (error instanceof ExecFileError && 'code' in error && error.code === "ENOENT") {
207+
if (error instanceof ExecFileError && "code" in error && error.code === "ENOENT") {
209208
return false;
210209
}
211210
throw error;
212211
}
213-
214212
}
215213
}

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

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

0 commit comments

Comments
 (0)