Skip to content

Commit 4d8e316

Browse files
committed
Add tests
1 parent 7e7f2de commit 4d8e316

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/script.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ export async function generateExecCommand(platform: string, scriptPath: string):
3838
try {
3939
await io.which("sudo", true);
4040
installCmd = `sudo -E ${installCmd}`;
41+
return installCmd;
4142
} catch {
4243
// Sudo not available, do not prepend
44+
return installCmd;
4345
}
46+
} else {
47+
return installCmd;
4448
}
45-
46-
return installCmd;
4749
}
4850

4951
export function defaultInstallRoot(platform: string, programName: string): string {

src/script.unit.test.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// Copyright 2020-2022 The MathWorks, Inc.
22

33
import * as exec from "@actions/exec";
4+
import * as io from "@actions/io";
45
import * as toolCache from "@actions/tool-cache";
56
import * as script from "./script";
67

78
jest.mock("@actions/exec");
9+
jest.mock("@actions/io");
810
jest.mock("@actions/tool-cache");
911

1012
afterEach(() => {
@@ -56,6 +58,8 @@ describe("script downloader/runner", () => {
5658
});
5759

5860
describe("install command generator", () => {
61+
const whichMock = io.which as jest.Mock;
62+
5963
const scriptPath = "hello.sh";
6064

6165
beforeAll(() => {
@@ -64,13 +68,20 @@ describe("install command generator", () => {
6468

6569
it("does not change the command on Windows", () => {
6670
const cmd = script.generateExecCommand("win32", scriptPath);
67-
expect(cmd).toEqual(`bash ${scriptPath}`);
71+
expect(cmd).resolves.toEqual(`bash ${scriptPath}`);
6872
});
6973

7074
["darwin", "linux"].forEach((platform) => {
7175
it(`calls the command with sudo on ${platform}`, () => {
76+
whichMock.mockResolvedValue("path/to/sudo");
77+
const cmd = script.generateExecCommand(platform, scriptPath);
78+
expect(cmd).resolves.toEqual(`sudo -E bash ${scriptPath}`);
79+
});
80+
81+
it(`calls the command without sudo on ${platform}`, () => {
82+
whichMock.mockRejectedValue("No sudo!");
7283
const cmd = script.generateExecCommand(platform, scriptPath);
73-
expect(cmd).toEqual(`sudo -E bash ${scriptPath}`);
84+
expect(cmd).resolves.toEqual(`bash ${scriptPath}`);
7485
});
7586
});
7687
});
@@ -87,4 +98,4 @@ describe("default install root", () => {
8798
testCase("win32", 'Program Files');
8899
testCase("darwin", "opt");
89100
testCase("linux", "opt");
90-
})
101+
})

0 commit comments

Comments
 (0)