Skip to content

Commit

Permalink
Merge pull request #125 from matlab-actions/cache_revert
Browse files Browse the repository at this point in the history
Remove MATLAB from the tool cache if mpm fails during installation.
  • Loading branch information
sameagen-MW authored Sep 5, 2024
2 parents 9918121 + b410805 commit b50a758
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/mpm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import * as exec from "@actions/exec";
import * as tc from "@actions/tool-cache";
import {rmRF} from "@actions/io";
import * as path from "path";
import * as matlab from "./matlab";
import properties from "./properties.json";
Expand Down Expand Up @@ -66,8 +67,13 @@ export async function install(mpmPath: string, release: matlab.Release, products
}
mpmArguments = mpmArguments.concat("--products", ...parsedProducts);

const exitCode = await exec.exec(mpmPath, mpmArguments);
const exitCode = await exec.exec(mpmPath, mpmArguments).catch(async e => {
// Fully remove failed MATLAB installation for self-hosted runners
await rmRF(destination);
throw e;
});
if (exitCode !== 0) {
await rmRF(destination);
return Promise.reject(Error(`Script exited with non-zero code ${exitCode}`));
}
return
Expand Down
15 changes: 14 additions & 1 deletion src/mpm.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import * as exec from "@actions/exec";
import * as tc from "@actions/tool-cache";
import * as io from "@actions/io";
import * as path from "path";
import * as mpm from "./mpm";
import * as script from "./script";

jest.mock("@actions/core");
jest.mock("@actions/exec");
jest.mock("@actions/tool-cache");
jest.mock("@actions/io");
jest.mock("./script");

afterEach(() => {
Expand Down Expand Up @@ -101,11 +103,13 @@ describe("setup mpm", () => {

describe("mpm install", () => {
let execMock: jest.Mock;
let rmRFMock: jest.Mock;
const mpmPath = "mpm";
const releaseInfo = {name: "r2022b", version: "9.13.0", update: "", isPrerelease: false};
const mpmRelease = "r2022b";
beforeEach(() => {
execMock = exec.exec as jest.Mock;
rmRFMock = io.rmRF as jest.Mock;
});

it("works with multiline products list", async () => {
Expand Down Expand Up @@ -161,10 +165,19 @@ describe("mpm install", () => {
expect(execMock.mock.calls[0][1]).toMatchObject(expectedMpmArgs);
});

it("rejects on failed install", async () => {
it("rejects and cleans on mpm rejection", async () => {
const destination = "/opt/matlab";
const products = ["MATLAB", "Compiler"];
execMock.mockRejectedValue(1);
await expect(mpm.install(mpmPath, releaseInfo, products, destination)).rejects.toBeDefined();
expect(rmRFMock).toHaveBeenCalledWith(destination);
});

it("rejects and cleans on failed install", async () => {
const destination = "/opt/matlab";
const products = ["MATLAB", "Compiler"];
execMock.mockResolvedValue(1);
await expect(mpm.install(mpmPath, releaseInfo, products, destination)).rejects.toBeDefined();
expect(rmRFMock).toHaveBeenCalledWith(destination);
});
});

0 comments on commit b50a758

Please sign in to comment.