Skip to content

Commit b50a758

Browse files
authored
Merge pull request #125 from matlab-actions/cache_revert
Remove MATLAB from the tool cache if mpm fails during installation.
2 parents 9918121 + b410805 commit b50a758

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/mpm.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

69-
const exitCode = await exec.exec(mpmPath, mpmArguments);
70+
const exitCode = await exec.exec(mpmPath, mpmArguments).catch(async e => {
71+
// Fully remove failed MATLAB installation for self-hosted runners
72+
await rmRF(destination);
73+
throw e;
74+
});
7075
if (exitCode !== 0) {
76+
await rmRF(destination);
7177
return Promise.reject(Error(`Script exited with non-zero code ${exitCode}`));
7278
}
7379
return

src/mpm.unit.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import * as exec from "@actions/exec";
44
import * as tc from "@actions/tool-cache";
5+
import * as io from "@actions/io";
56
import * as path from "path";
67
import * as mpm from "./mpm";
78
import * as script from "./script";
89

910
jest.mock("@actions/core");
1011
jest.mock("@actions/exec");
1112
jest.mock("@actions/tool-cache");
13+
jest.mock("@actions/io");
1214
jest.mock("./script");
1315

1416
afterEach(() => {
@@ -101,11 +103,13 @@ describe("setup mpm", () => {
101103

102104
describe("mpm install", () => {
103105
let execMock: jest.Mock;
106+
let rmRFMock: jest.Mock;
104107
const mpmPath = "mpm";
105108
const releaseInfo = {name: "r2022b", version: "9.13.0", update: "", isPrerelease: false};
106109
const mpmRelease = "r2022b";
107110
beforeEach(() => {
108111
execMock = exec.exec as jest.Mock;
112+
rmRFMock = io.rmRF as jest.Mock;
109113
});
110114

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

164-
it("rejects on failed install", async () => {
168+
it("rejects and cleans on mpm rejection", async () => {
169+
const destination = "/opt/matlab";
170+
const products = ["MATLAB", "Compiler"];
171+
execMock.mockRejectedValue(1);
172+
await expect(mpm.install(mpmPath, releaseInfo, products, destination)).rejects.toBeDefined();
173+
expect(rmRFMock).toHaveBeenCalledWith(destination);
174+
});
175+
176+
it("rejects and cleans on failed install", async () => {
165177
const destination = "/opt/matlab";
166178
const products = ["MATLAB", "Compiler"];
167179
execMock.mockResolvedValue(1);
168180
await expect(mpm.install(mpmPath, releaseInfo, products, destination)).rejects.toBeDefined();
181+
expect(rmRFMock).toHaveBeenCalledWith(destination);
169182
});
170183
});

0 commit comments

Comments
 (0)