Skip to content

Commit b410805

Browse files
author
Sam Eagen
committed
Remove potentially poisoned MATLAB intall when mpm fails
1 parent 80640a3 commit b410805

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/mpm.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ export async function install(mpmPath: string, release: matlab.Release, products
6767
}
6868
mpmArguments = mpmArguments.concat("--products", ...parsedProducts);
6969

70-
const exitCode = await exec.exec(mpmPath, mpmArguments);
71-
if (exitCode !== 0) {
70+
const exitCode = await exec.exec(mpmPath, mpmArguments).catch(async e => {
7271
// Fully remove failed MATLAB installation for self-hosted runners
7372
await rmRF(destination);
74-
73+
throw e;
74+
});
75+
if (exitCode !== 0) {
76+
await rmRF(destination);
7577
return Promise.reject(Error(`Script exited with non-zero code ${exitCode}`));
7678
}
7779
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)