Skip to content

Cache when install successful #148

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020-2024 The MathWorks, Inc.
# Copyright 2020-2025 The MathWorks, Inc.

name: Setup MATLAB
description: >-
Expand Down Expand Up @@ -27,4 +27,4 @@ runs:
using: node20
main: dist/setup/index.js
post: dist/cache-save/index.js
post-if: success()
post-if: always()
5 changes: 5 additions & 0 deletions src/install-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2025 The MathWorks, Inc.

export enum State {
InstallSuccessful = 'MATLAB_INSTALL_SUCCESSFUL',
}
4 changes: 3 additions & 1 deletion src/install.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright 2020-2024 The MathWorks, Inc.
// Copyright 2020-2025 The MathWorks, Inc.

import * as core from "@actions/core";
import * as matlab from "./matlab";
import * as mpm from "./mpm";
import * as path from "path";
import * as cache from "./cache-restore";
import { State } from './install-state';

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

core.addPath(path.join(destination, "bin"));
Expand Down
8 changes: 7 additions & 1 deletion src/install.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright 2020-2024 The MathWorks, Inc.
// Copyright 2020-2025 The MathWorks, Inc.

import * as core from "@actions/core";
import * as cache from "./cache-restore";
import * as install from "./install";
import * as matlab from "./matlab";
import * as mpm from "./mpm";
import { State } from './install-state';

jest.mock("@actions/core");
jest.mock("./matlab");
Expand All @@ -22,6 +23,7 @@ describe("install procedure", () => {
let matlabSetupBatchMock: jest.Mock;
let mpmSetupMock: jest.Mock;
let mpmInstallMock: jest.Mock;
let saveStateMock: jest.Mock;
let addPathMock: jest.Mock;
let setOutputMock: jest.Mock;
let restoreMATLABMock: jest.Mock;
Expand Down Expand Up @@ -49,6 +51,7 @@ describe("install procedure", () => {
matlabSetupBatchMock = matlab.setupBatch as jest.Mock;
mpmSetupMock = mpm.setup as jest.Mock;
mpmInstallMock = mpm.install as jest.Mock;
saveStateMock = core.saveState as jest.Mock;
addPathMock = core.addPath as jest.Mock;
setOutputMock = core.setOutput as jest.Mock;
restoreMATLABMock = cache.restoreMATLAB as jest.Mock;
Expand All @@ -71,6 +74,7 @@ describe("install procedure", () => {
expect(matlabSetupBatchMock).toHaveBeenCalledTimes(1);
expect(mpmSetupMock).toHaveBeenCalledTimes(1);
expect(mpmInstallMock).toHaveBeenCalledTimes(1);
expect(saveStateMock).toHaveBeenCalledWith(State.InstallSuccessful, 'true');
expect(addPathMock).toHaveBeenCalledTimes(1);
expect(setOutputMock).toHaveBeenCalledTimes(1);
});
Expand All @@ -79,6 +83,7 @@ describe("install procedure", () => {
matlabGetToolcacheDirMock.mockResolvedValue(["/opt/hostedtoolcache/MATLAB/9.13.0/x64", true]);
await expect(doInstall()).resolves.toBeUndefined();
expect(mpmInstallMock).toHaveBeenCalledTimes(0);
expect(saveStateMock).toHaveBeenCalledTimes(0);
expect(addPathMock).toHaveBeenCalledTimes(1);
expect(setOutputMock).toHaveBeenCalledTimes(1);
});
Expand Down Expand Up @@ -116,6 +121,7 @@ describe("install procedure", () => {
it("rejects when the mpm install fails", async () => {
mpmInstallMock.mockRejectedValue(Error("oof"));
await expect(doInstall()).rejects.toBeDefined();
expect(saveStateMock).toHaveBeenCalledTimes(0);
});

it("rejects when the matlab-batch install fails", async () => {
Expand Down
6 changes: 4 additions & 2 deletions src/post.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright 2023 The MathWorks, Inc.
// Copyright 2023-2025 The MathWorks, Inc.

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

export async function run() {
const cache = core.getBooleanInput('cache');
if (cache) {
const installSuccessful = core.getState(State.InstallSuccessful);
if (cache && installSuccessful === 'true') {
await cacheMATLAB();
}
}
Expand Down
44 changes: 44 additions & 0 deletions src/post.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2025 The MathWorks, Inc.

import * as core from '@actions/core';
import { cacheMATLAB } from "./cache-save";
import { run } from "./post";

jest.mock("@actions/core");
jest.mock("./cache-save");

afterEach(() => {
jest.resetAllMocks();
});

describe("post", () => {
let getBooleanInputMock: jest.Mock;
let getStateMock: jest.Mock;
let cacheMATLABMock: jest.Mock;

beforeEach(() => {
getBooleanInputMock = core.getBooleanInput as jest.Mock;
getStateMock = core.getState as jest.Mock;
cacheMATLABMock = cacheMATLAB as jest.Mock;
});

it("caches MATLAB when cache true and install successful", async () => {
getBooleanInputMock.mockReturnValueOnce(true);
getStateMock.mockReturnValueOnce('true');
await expect(run()).resolves.toBeUndefined();
expect(cacheMATLABMock).toHaveBeenCalledTimes(1);
});

it("does not cache MATLAB when cache false", async () => {
getBooleanInputMock.mockReturnValueOnce(false);
getStateMock.mockReturnValueOnce('true');
await expect(run()).resolves.toBeUndefined();
expect(cacheMATLABMock).toHaveBeenCalledTimes(0);
});

it("does not cache MATLAB when install not successful", async () => {
getBooleanInputMock.mockReturnValueOnce(true);
await expect(run()).resolves.toBeUndefined();
expect(cacheMATLABMock).toHaveBeenCalledTimes(0);
});
});
Loading