Skip to content

Commit fd8d40c

Browse files
authored
Merge pull request #78 from matlab-actions/no-sudo
Don't add the sudo prefix if sudo is not on the path
2 parents 222b3da + 717c4e2 commit fd8d40c

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"@actions/cache": "^3.2.2",
2222
"@actions/core": "^1.10.0",
2323
"@actions/exec": "^1.1.0",
24-
"@actions/io": "^1.1.2",
24+
"@actions/io": "^1.1.3",
2525
"@actions/tool-cache": "^1.7.1"
2626
},
2727
"devDependencies": {

src/script.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// Copyright 2020-2022 The MathWorks, Inc.
1+
// Copyright 2020-2023 The MathWorks, Inc.
22

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

@@ -13,7 +14,7 @@ import path from "path";
1314
*/
1415
export async function downloadAndRunScript(platform: string, url: string, args?: string[]) {
1516
const scriptPath = await tc.downloadTool(url);
16-
const cmd = generateExecCommand(platform, scriptPath);
17+
const cmd = await generateExecCommand(platform, scriptPath);
1718

1819
const exitCode = await exec.exec(cmd, args);
1920

@@ -29,12 +30,15 @@ export async function downloadAndRunScript(platform: string, url: string, args?:
2930
* @param platform Operating system of the runner (e.g. "win32" or "linux").
3031
* @param scriptPath Path to the script (on runner's filesystem).
3132
*/
32-
export function generateExecCommand(platform: string, scriptPath: string): string {
33+
export async function generateExecCommand(platform: string, scriptPath: string): Promise<string> {
3334
// Run the install script using bash
3435
let installCmd = `bash ${scriptPath}`;
3536

3637
if (platform !== "win32") {
37-
installCmd = `sudo -E ${installCmd}`;
38+
const sudo = await io.which("sudo");
39+
if (sudo) {
40+
installCmd = `sudo -E ${installCmd}`;
41+
}
3842
}
3943

4044
return installCmd;
@@ -48,4 +52,4 @@ export function defaultInstallRoot(platform: string, programName: string): strin
4852
installRoot = path.join("/","opt", programName);
4953
}
5054
return installRoot;
51-
}
55+
}

src/script.unit.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
// Copyright 2020-2022 The MathWorks, Inc.
1+
// Copyright 2020-2023 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.mockResolvedValue("");
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)