diff --git a/src/cache-save.ts b/src/cache-save.ts index 548cdac..3525034 100644 --- a/src/cache-save.ts +++ b/src/cache-save.ts @@ -16,6 +16,10 @@ export async function cacheMATLAB() { return; } - await cache.saveCache([matlabPath, supportPackagesPath], primaryKey); - core.info(`Cache saved with the key: ${primaryKey}`); + try { + await cache.saveCache([matlabPath, supportPackagesPath], primaryKey); + core.info(`Cache saved with the key: ${primaryKey}`); + } catch (e) { + core.warning(`Failed to save MATLAB to cache: ${e}`); + } } diff --git a/src/install.ts b/src/install.ts index 384abe5..6d4b0df 100644 --- a/src/install.ts +++ b/src/install.ts @@ -29,7 +29,7 @@ export async function install(platform: string, architecture: string, release: s ); await core.group("Setting up MATLAB", async () => { - let [destination, alreadyExists]: [string, boolean] = await matlab.makeToolcacheDir(releaseInfo, platform); + let [destination, alreadyExists]: [string, boolean] = await matlab.getToolcacheDir(platform, releaseInfo); let cacheHit = false; if (useCache) { diff --git a/src/install.unit.test.ts b/src/install.unit.test.ts index cd01fdf..bd9c566 100644 --- a/src/install.unit.test.ts +++ b/src/install.unit.test.ts @@ -42,7 +42,7 @@ describe("install procedure", () => { beforeEach(() => { matlabInstallSystemDependenciesMock = matlab.installSystemDependencies as jest.Mock; matlabGetReleaseInfoMock = matlab.getReleaseInfo as jest.Mock; - matlabMakeToolcacheDirMock = matlab.makeToolcacheDir as jest.Mock; + matlabMakeToolcacheDirMock = matlab.getToolcacheDir as jest.Mock; matlabSetupBatchMock = matlab.setupBatch as jest.Mock; mpmSetupMock = mpm.setup as jest.Mock; mpmInstallMock = mpm.install as jest.Mock; diff --git a/src/matlab.ts b/src/matlab.ts index 6413f63..ca0e517 100644 --- a/src/matlab.ts +++ b/src/matlab.ts @@ -18,25 +18,36 @@ export interface Release { isPrerelease: boolean; } -export async function makeToolcacheDir(release: Release, platform: string): Promise<[string, boolean]> { +export async function getToolcacheDir(platform: string, release: Release): Promise<[string, boolean]> { let toolpath: string = tc.find("MATLAB", release.version); let alreadyExists = false; if (toolpath) { core.info(`Found MATLAB ${release.name} in cache at ${toolpath}.`); alreadyExists = true; } else { - if (platform === "win32") { - toolpath = await windowsHostedToolpath(release).catch(async () => { - return await defaultToolpath(release, platform); - }); - } else { - toolpath = await defaultToolpath(release, platform); - } + toolpath = await makeToolcacheDir(platform, release); + } + if (platform == "darwin") { + toolpath = toolpath + "/MATLAB.app"; } return [toolpath, alreadyExists] } -async function windowsHostedToolpath(release: Release): Promise { +async function makeToolcacheDir(platform: string, release: Release): Promise { + let toolcacheDir: string; + if (platform === "win32") { + toolcacheDir = await makeWindowsHostedToolpath(release) + .catch(async (e) => { + console.log(e) + return await makeDefaultToolpath(release) + }); + } else { + toolcacheDir = await makeDefaultToolpath(release); + } + return toolcacheDir; +} + +async function makeWindowsHostedToolpath(release: Release): Promise { // bail early if not on a github hosted runner if (process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted' && process.env['AGENT_ISSELFHOSTED'] === '1') { return Promise.reject(); @@ -72,13 +83,10 @@ async function windowsHostedToolpath(release: Release): Promise { return actualToolCacheDir; } -async function defaultToolpath(release: Release, platform: string): Promise { +async function makeDefaultToolpath(release: Release): Promise { fs.writeFileSync(".keep", ""); let toolpath = await tc.cacheFile(".keep", ".keep", "MATLAB", release.version); io.rmRF(".keep"); - if (platform == "darwin") { - toolpath = toolpath + "/MATLAB.app"; - } return toolpath } diff --git a/src/matlab.unit.test.ts b/src/matlab.unit.test.ts index b51df75..3b262b4 100644 --- a/src/matlab.unit.test.ts +++ b/src/matlab.unit.test.ts @@ -46,20 +46,20 @@ describe("matlab tests", () => { it("returns toolpath if in toolcache", async () => { findMock.mockReturnValue("/opt/hostedtoolcache/matlab/r2022b"); - await expect(matlab.makeToolcacheDir(release, platform)).resolves.toMatchObject(["/opt/hostedtoolcache/matlab/r2022b", true]); + await expect(matlab.getToolcacheDir(platform, release)).resolves.toMatchObject(["/opt/hostedtoolcache/matlab/r2022b", true]); expect(infoMock).toHaveBeenCalledTimes(1); }); it("creates cache and returns default path for linux", async () => { findMock.mockReturnValue(""); cacheFileMock.mockReturnValue("/opt/hostedtoolcache/matlab/r2022b"); - await expect(matlab.makeToolcacheDir(release, platform)).resolves.toMatchObject(["/opt/hostedtoolcache/matlab/r2022b", false]); + await expect(matlab.getToolcacheDir(platform, release)).resolves.toMatchObject(["/opt/hostedtoolcache/matlab/r2022b", false]); }); it("creates cache and returns default path for mac", async () => { findMock.mockReturnValue(""); cacheFileMock.mockReturnValue("/opt/hostedtoolcache/matlab/r2022b"); - await expect(matlab.makeToolcacheDir(release, "darwin")).resolves.toMatchObject(["/opt/hostedtoolcache/matlab/r2022b/MATLAB.app", false]); + await expect(matlab.getToolcacheDir("darwin", release)).resolves.toMatchObject(["/opt/hostedtoolcache/matlab/r2022b/MATLAB.app", false]); }); describe("windows performance workaround", () => { @@ -94,7 +94,7 @@ describe("matlab tests", () => { let mkdirSyncSpy = jest.spyOn(fs, "mkdirSync").mockImplementation(() => ""); let symlinkSyncSpy = jest.spyOn(fs, "symlinkSync").mockImplementation(() => {}); - await expect(matlab.makeToolcacheDir(release, "win32")).resolves.toMatchObject([expectedToolcacheDir, false]); + await expect(matlab.getToolcacheDir("win32", release)).resolves.toMatchObject([expectedToolcacheDir, false]); expect(existsSyncSpy).toHaveBeenCalledTimes(2); expect(mkdirSyncSpy).toHaveBeenCalledTimes(1); expect(symlinkSyncSpy).toHaveBeenCalledTimes(2); @@ -104,26 +104,26 @@ describe("matlab tests", () => { let expectedToolcacheDir = "C:\\hostedtoolcache\\windows\\matlab\\r2022b"; process.env["AGENT_ISSELFHOSTED"] = "1"; process.env["RUNNER_ENVIRONMENT"] = "self-hosted"; - await expect(matlab.makeToolcacheDir(release, "win32")).resolves.toMatchObject([expectedToolcacheDir, false]); + await expect(matlab.getToolcacheDir("win32", release)).resolves.toMatchObject([expectedToolcacheDir, false]); }); it("uses default toolcache directory toolcache directory is not defined", async () => { let expectedToolcacheDir = "C:\\hostedtoolcache\\windows\\matlab\\r2022b"; process.env["RUNNER_TOOL_CACHE"] = ''; cacheFileMock.mockReturnValue(expectedToolcacheDir); - await expect(matlab.makeToolcacheDir(release, "win32")).resolves.toMatchObject([expectedToolcacheDir, false]); + await expect(matlab.getToolcacheDir("win32", release)).resolves.toMatchObject([expectedToolcacheDir, false]); }); it("uses default toolcache directory if d: drive doesn't exist", async () => { jest.spyOn(fs, "existsSync").mockReturnValue(false); let expectedToolcacheDir = "C:\\hostedtoolcache\\windows\\matlab\\r2022b"; - await expect(matlab.makeToolcacheDir(release, "win32")).resolves.toMatchObject([expectedToolcacheDir, false]); + await expect(matlab.getToolcacheDir("win32", release)).resolves.toMatchObject([expectedToolcacheDir, false]); }); it("uses default toolcache directory if c: drive doesn't exist", async () => { jest.spyOn(fs, "existsSync").mockReturnValueOnce(true).mockReturnValue(false); let expectedToolcacheDir = "C:\\hostedtoolcache\\windows\\matlab\\r2022b"; - await expect(matlab.makeToolcacheDir(release, "win32")).resolves.toMatchObject([expectedToolcacheDir, false]); + await expect(matlab.getToolcacheDir("win32", release)).resolves.toMatchObject([expectedToolcacheDir, false]); }); });