Skip to content

Commit

Permalink
Merge pull request #23 from matlab-actions/windows-and-macos
Browse files Browse the repository at this point in the history
Add limited, undocumented, Windows and macOS support
  • Loading branch information
mcafaro authored Jan 5, 2022
2 parents 83047ff + 2e54d2d commit 8091c47
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/bat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ jobs:
- os: ubuntu-18.04
release: R2020a
command: assert(strcmp(version('-release'),'2020a'))
- os: macos-latest
release: latest
command: assert(~isempty(regexp(version('-release'), '\d{4}.')))
- os: windows-latest
release: latest
command: assert(~isempty(regexp(version('-release'), '\d{4}.')))
steps:
- uses: actions/download-artifact@v2
with:
Expand Down
18 changes: 18 additions & 0 deletions src/ematlab.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2022 The MathWorks, Inc.

import fs from "fs";
import path from "path";
import os from "os";
import * as core from "@actions/core";

export const rootFile = path.join(os.tmpdir(), "ephemeral_matlab_root");

export function addToPath() {
let root: string;
try {
root = fs.readFileSync(rootFile).toString();
} catch (err) {
throw new Error(`Unable to read file containing MATLAB root: ${err.message}`);
}
core.addPath(path.join(root, "bin"));
}
32 changes: 32 additions & 0 deletions src/ematlab.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2022 The MathWorks, Inc.

import * as core from "@actions/core";
import * as fs from "fs";
import * as ematlab from "./ematlab";

jest.mock("@actions/core");

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

describe("ephemeral matlab", () => {
let addPathMock = core.addPath as jest.Mock;

beforeEach(() => {
addPathMock = core.addPath as jest.Mock;
if (fs.existsSync(ematlab.rootFile)) {
fs.unlinkSync(ematlab.rootFile);
}
});

it("ideally works", async () => {
fs.writeFileSync(ematlab.rootFile, "path/to/matlab");
ematlab.addToPath();
expect(addPathMock).toBeCalledWith("path/to/matlab/bin");
});

it("rejects when root file does not exist", async () => {
expect(() => ematlab.addToPath()).toThrow();
});
});
23 changes: 14 additions & 9 deletions src/install.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright 2020 The MathWorks, Inc.
// Copyright 2020-2022 The MathWorks, Inc.

import * as core from "@actions/core";
import properties from "./properties.json";
import * as script from "./script";
import * as ematlab from "./ematlab";

export default install;

Expand All @@ -16,17 +17,21 @@ export default install;
* @param release Release of MATLAB to be set up (e.g., "latest" or "R2020a").
*/
export async function install(platform: string, release: string) {
// Install runtime system dependencies for MATLAB
await core.group("Preparing system for MATLAB", () =>
script.downloadAndRunScript(platform, properties.matlabDepsUrl, [release])
);
// Install runtime system dependencies for MATLAB on Linux
if (platform === "linux") {
await core.group("Preparing system for MATLAB", () =>
script.downloadAndRunScript(platform, properties.matlabDepsUrl, [release])
);
}

// Invoke ephemeral installer to setup a MATLAB on the runner
await core.group("Setting up MATLAB", () =>
script.downloadAndRunScript(platform, properties.ephemeralInstallerUrl, [
"--release",
release,
])
script
.downloadAndRunScript(platform, properties.ephemeralInstallerUrl, [
"--release",
release,
])
.then(ematlab.addToPath)
);

return;
Expand Down
27 changes: 27 additions & 0 deletions src/install.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
import * as core from "@actions/core";
import * as install from "./install";
import * as script from "./script";
import * as ematlab from "./ematlab";

jest.mock("@actions/core");
jest.mock("./script");
jest.mock("./ematlab");

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

describe("install procedure", () => {
let downloadAndRunScriptMock: jest.Mock<any, any>;
let addToPathMock: jest.Mock<any, any>;

// install() does not perform any logic itself on its parameters. Therefore
// they can be held static for these unit tests
Expand All @@ -22,6 +25,7 @@ describe("install procedure", () => {

beforeEach(() => {
downloadAndRunScriptMock = script.downloadAndRunScript as jest.Mock;
addToPathMock = ematlab.addToPath as jest.Mock;

// Mock core.group to simply return the output of the func it gets from
// the caller
Expand All @@ -32,9 +36,11 @@ describe("install procedure", () => {

it("ideally works", async () => {
downloadAndRunScriptMock.mockResolvedValue(undefined);
addToPathMock.mockResolvedValue(undefined);

await expect(doInstall()).resolves.toBeUndefined();
expect(downloadAndRunScriptMock).toHaveBeenCalledTimes(2);
expect(addToPathMock).toHaveBeenCalledTimes(1);
});

it("rejects when the download fails", async () => {
Expand All @@ -52,6 +58,27 @@ describe("install procedure", () => {

await expect(doInstall()).rejects.toBeDefined();
expect(downloadAndRunScriptMock).toHaveBeenCalledTimes(2);
expect(addToPathMock).toHaveBeenCalledTimes(0);
expect(core.group).toHaveBeenCalledTimes(2);
});

it("rejects when add to path fails", async () => {
downloadAndRunScriptMock.mockResolvedValue(undefined);
addToPathMock.mockRejectedValueOnce(Error("oof"));

await expect(doInstall()).rejects.toBeDefined();
expect(downloadAndRunScriptMock).toHaveBeenCalledTimes(2);
expect(addToPathMock).toHaveBeenCalledTimes(1);
});

["darwin", "win32"].forEach((os) => {
it(`does not run deps script on ${os}`, async () => {
downloadAndRunScriptMock.mockResolvedValue(undefined);
addToPathMock.mockResolvedValue(undefined);

await expect(install.install(os, release)).resolves.toBeUndefined();
expect(downloadAndRunScriptMock).toHaveBeenCalledTimes(1);
expect(addToPathMock).toHaveBeenCalledTimes(1);
});
});
});

0 comments on commit 8091c47

Please sign in to comment.