Skip to content

Commit 1dcf7e8

Browse files
authored
Cache when install successful (#148)
* Cache when install successful Cache when install successful * Fix test expectation to be string rather than boolean
1 parent bd5e318 commit 1dcf7e8

File tree

6 files changed

+65
-6
lines changed

6 files changed

+65
-6
lines changed

action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020-2024 The MathWorks, Inc.
1+
# Copyright 2020-2025 The MathWorks, Inc.
22

33
name: Setup MATLAB
44
description: >-
@@ -27,4 +27,4 @@ runs:
2727
using: node20
2828
main: dist/setup/index.js
2929
post: dist/cache-save/index.js
30-
post-if: success()
30+
post-if: always()

src/install-state.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright 2025 The MathWorks, Inc.
2+
3+
export enum State {
4+
InstallSuccessful = 'MATLAB_INSTALL_SUCCESSFUL',
5+
}

src/install.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// Copyright 2020-2024 The MathWorks, Inc.
1+
// Copyright 2020-2025 The MathWorks, Inc.
22

33
import * as core from "@actions/core";
44
import * as matlab from "./matlab";
55
import * as mpm from "./mpm";
66
import * as path from "path";
77
import * as cache from "./cache-restore";
8+
import { State } from './install-state';
89

910
/**
1011
* Set up an instance of MATLAB on the runner.
@@ -48,6 +49,7 @@ export async function install(platform: string, architecture: string, release: s
4849
if (!alreadyExists && !cacheHit) {
4950
const mpmPath: string = await mpm.setup(platform, matlabArch);
5051
await mpm.install(mpmPath, releaseInfo, products, destination);
52+
core.saveState(State.InstallSuccessful, 'true');
5153
}
5254

5355
core.addPath(path.join(destination, "bin"));

src/install.unit.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// Copyright 2020-2024 The MathWorks, Inc.
1+
// Copyright 2020-2025 The MathWorks, Inc.
22

33
import * as core from "@actions/core";
44
import * as cache from "./cache-restore";
55
import * as install from "./install";
66
import * as matlab from "./matlab";
77
import * as mpm from "./mpm";
8+
import { State } from './install-state';
89

910
jest.mock("@actions/core");
1011
jest.mock("./matlab");
@@ -22,6 +23,7 @@ describe("install procedure", () => {
2223
let matlabSetupBatchMock: jest.Mock;
2324
let mpmSetupMock: jest.Mock;
2425
let mpmInstallMock: jest.Mock;
26+
let saveStateMock: jest.Mock;
2527
let addPathMock: jest.Mock;
2628
let setOutputMock: jest.Mock;
2729
let restoreMATLABMock: jest.Mock;
@@ -49,6 +51,7 @@ describe("install procedure", () => {
4951
matlabSetupBatchMock = matlab.setupBatch as jest.Mock;
5052
mpmSetupMock = mpm.setup as jest.Mock;
5153
mpmInstallMock = mpm.install as jest.Mock;
54+
saveStateMock = core.saveState as jest.Mock;
5255
addPathMock = core.addPath as jest.Mock;
5356
setOutputMock = core.setOutput as jest.Mock;
5457
restoreMATLABMock = cache.restoreMATLAB as jest.Mock;
@@ -71,6 +74,7 @@ describe("install procedure", () => {
7174
expect(matlabSetupBatchMock).toHaveBeenCalledTimes(1);
7275
expect(mpmSetupMock).toHaveBeenCalledTimes(1);
7376
expect(mpmInstallMock).toHaveBeenCalledTimes(1);
77+
expect(saveStateMock).toHaveBeenCalledWith(State.InstallSuccessful, 'true');
7478
expect(addPathMock).toHaveBeenCalledTimes(1);
7579
expect(setOutputMock).toHaveBeenCalledTimes(1);
7680
});
@@ -79,6 +83,7 @@ describe("install procedure", () => {
7983
matlabGetToolcacheDirMock.mockResolvedValue(["/opt/hostedtoolcache/MATLAB/9.13.0/x64", true]);
8084
await expect(doInstall()).resolves.toBeUndefined();
8185
expect(mpmInstallMock).toHaveBeenCalledTimes(0);
86+
expect(saveStateMock).toHaveBeenCalledTimes(0);
8287
expect(addPathMock).toHaveBeenCalledTimes(1);
8388
expect(setOutputMock).toHaveBeenCalledTimes(1);
8489
});
@@ -116,6 +121,7 @@ describe("install procedure", () => {
116121
it("rejects when the mpm install fails", async () => {
117122
mpmInstallMock.mockRejectedValue(Error("oof"));
118123
await expect(doInstall()).rejects.toBeDefined();
124+
expect(saveStateMock).toHaveBeenCalledTimes(0);
119125
});
120126

121127
it("rejects when the matlab-batch install fails", async () => {

src/post.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
// Copyright 2023 The MathWorks, Inc.
1+
// Copyright 2023-2025 The MathWorks, Inc.
22

33
import * as core from "@actions/core";
44
import { cacheMATLAB } from "./cache-save";
5+
import { State } from './install-state';
56

67
export async function run() {
78
const cache = core.getBooleanInput('cache');
8-
if (cache) {
9+
const installSuccessful = core.getState(State.InstallSuccessful);
10+
if (cache && installSuccessful === 'true') {
911
await cacheMATLAB();
1012
}
1113
}

src/post.unit.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2025 The MathWorks, Inc.
2+
3+
import * as core from '@actions/core';
4+
import { cacheMATLAB } from "./cache-save";
5+
import { run } from "./post";
6+
7+
jest.mock("@actions/core");
8+
jest.mock("./cache-save");
9+
10+
afterEach(() => {
11+
jest.resetAllMocks();
12+
});
13+
14+
describe("post", () => {
15+
let getBooleanInputMock: jest.Mock;
16+
let getStateMock: jest.Mock;
17+
let cacheMATLABMock: jest.Mock;
18+
19+
beforeEach(() => {
20+
getBooleanInputMock = core.getBooleanInput as jest.Mock;
21+
getStateMock = core.getState as jest.Mock;
22+
cacheMATLABMock = cacheMATLAB as jest.Mock;
23+
});
24+
25+
it("caches MATLAB when cache true and install successful", async () => {
26+
getBooleanInputMock.mockReturnValueOnce(true);
27+
getStateMock.mockReturnValueOnce('true');
28+
await expect(run()).resolves.toBeUndefined();
29+
expect(cacheMATLABMock).toHaveBeenCalledTimes(1);
30+
});
31+
32+
it("does not cache MATLAB when cache false", async () => {
33+
getBooleanInputMock.mockReturnValueOnce(false);
34+
getStateMock.mockReturnValueOnce('true');
35+
await expect(run()).resolves.toBeUndefined();
36+
expect(cacheMATLABMock).toHaveBeenCalledTimes(0);
37+
});
38+
39+
it("does not cache MATLAB when install not successful", async () => {
40+
getBooleanInputMock.mockReturnValueOnce(true);
41+
await expect(run()).resolves.toBeUndefined();
42+
expect(cacheMATLABMock).toHaveBeenCalledTimes(0);
43+
});
44+
});

0 commit comments

Comments
 (0)