From 7a1d861c7398716bd01823dcc7995ab2d9389a66 Mon Sep 17 00:00:00 2001 From: David Francoeur Date: Tue, 9 Aug 2022 14:08:31 -0400 Subject: [PATCH] Better handling of platforms when downloading Check whether platform is supported before moving on Add some tests to ensure it's the case. Also that we support all said platform (file can actually be downloaded). This test suite is slow, but I think it's worth it Note: in future releases, windows support will need to be reworked. in some instances, the `exe` is gone and replaced with a zip that'll likely need to unpack --- src/coursier/coursier.ts | 6 +++- src/coursier/download-coursier.ts | 42 ++++++++++++++---------- tests/coursier/download-coursier.spec.ts | 26 +++++++++++++++ 3 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 tests/coursier/download-coursier.spec.ts diff --git a/src/coursier/coursier.ts b/src/coursier/coursier.ts index 1d3eea7..8a0c72e 100644 --- a/src/coursier/coursier.ts +++ b/src/coursier/coursier.ts @@ -6,7 +6,11 @@ export function getCoursierExecutable(extensionPath: string): Promise { if (paths.length > 0) { return paths[0]; } else { - return downloadCoursierIfRequired(extensionPath, "v2.0.6"); + return downloadCoursierIfRequired( + extensionPath, + process.platform, + "v2.0.13" + ); } }); } diff --git a/src/coursier/download-coursier.ts b/src/coursier/download-coursier.ts index 8e71d9b..7034866 100644 --- a/src/coursier/download-coursier.ts +++ b/src/coursier/download-coursier.ts @@ -6,6 +6,7 @@ import { access, mkdir } from "fs/promises"; export function downloadCoursierIfRequired( extensionPath: string, + platform: string, versionPath: string ): Promise { function binPath(filename: string) { @@ -20,25 +21,32 @@ export function downloadCoursierIfRequired( }); } - const urls = { - darwin: `https://github.com/coursier/coursier/releases/download/${versionPath}/cs-x86_64-apple-darwin`, - linux: `https://github.com/coursier/coursier/releases/download/${versionPath}/cs-x86_64-pc-linux`, - win32: `https://github.com/coursier/coursier/releases/download/${versionPath}/cs-x86_64-pc-win32.exe`, - }; - const targets = { - darwin: binPath("coursier"), - linux: binPath("coursier"), - win32: binPath("coursier.exe"), + const supportedTargets = { + darwin: { + url: `https://github.com/coursier/coursier/releases/download/${versionPath}/cs-x86_64-apple-darwin`, + bin: binPath("coursier"), + }, + linux: { + url: `https://github.com/coursier/coursier/releases/download/${versionPath}/cs-x86_64-pc-linux`, + bin: binPath("coursier"), + }, + win32: { + url: `https://github.com/coursier/coursier/releases/download/${versionPath}/cs-x86_64-pc-win32.exe`, + bin: binPath("coursier.exe"), + }, }; - const targetFile = targets[process.platform]; - return validBinFileExists(targetFile).then((valid) => { - return valid - ? targetFile - : createDir().then(() => - downloadFile(urls[process.platform], targetFile) - ); - }); + const target = supportedTargets[platform]; + if (target === undefined) { + return Promise.reject(`Unsupported platform ${platform}.`); + } else { + const targetFile = target.bin; + return validBinFileExists(targetFile).then((valid) => { + return valid + ? targetFile + : createDir().then(() => downloadFile(target.url, targetFile)); + }); + } } function validBinFileExists(file: string): Promise { diff --git a/tests/coursier/download-coursier.spec.ts b/tests/coursier/download-coursier.spec.ts new file mode 100644 index 0000000..68ff79c --- /dev/null +++ b/tests/coursier/download-coursier.spec.ts @@ -0,0 +1,26 @@ +import { downloadCoursierIfRequired } from "../../src/coursier/download-coursier"; +import { tmpdir } from "os"; +import { existsSync, mkdirSync, accessSync, constants, rmSync } from "fs"; +import { join } from "path"; + +["darwin", "linux", "win32"].forEach((p) => { + test( + `download on platform: ${p}`, + () => { + const dir = join(tmpdir(), p); + rmSync(dir, { recursive: true, force: true }); + mkdirSync(dir, { recursive: true }); + return downloadCoursierIfRequired(dir, p, "v2.0.13").then((x) => { + expect(existsSync(x)).toBeTruthy(); + accessSync(x, constants.X_OK); + }); + }, + 25 * 1000 + ); +}); + +test(`fails on unknown platform`, () => { + return expect( + downloadCoursierIfRequired(tmpdir(), "unsupported", "v2.0.13") + ).rejects.toEqual("Unsupported platform unsupported."); +});