Skip to content

Don't add the sudo prefix if sudo is not on the path #78

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 18, 2023
Merged
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
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@actions/cache": "^3.2.2",
"@actions/core": "^1.10.0",
"@actions/exec": "^1.1.0",
"@actions/io": "^1.1.2",
"@actions/io": "^1.1.3",
"@actions/tool-cache": "^1.7.1"
},
"devDependencies": {
Expand Down
14 changes: 9 additions & 5 deletions src/script.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020-2022 The MathWorks, Inc.
// Copyright 2020-2023 The MathWorks, Inc.

import * as exec from "@actions/exec";
import * as io from "@actions/io";
import * as tc from "@actions/tool-cache";
import path from "path";

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

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

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

if (platform !== "win32") {
installCmd = `sudo -E ${installCmd}`;
const sudo = await io.which("sudo");
if (sudo) {
installCmd = `sudo -E ${installCmd}`;
}
}

return installCmd;
Expand All @@ -48,4 +52,4 @@ export function defaultInstallRoot(platform: string, programName: string): strin
installRoot = path.join("/","opt", programName);
}
return installRoot;
}
}
19 changes: 15 additions & 4 deletions src/script.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Copyright 2020-2022 The MathWorks, Inc.
// Copyright 2020-2023 The MathWorks, Inc.

import * as exec from "@actions/exec";
import * as io from "@actions/io";
import * as toolCache from "@actions/tool-cache";
import * as script from "./script";

jest.mock("@actions/exec");
jest.mock("@actions/io");
jest.mock("@actions/tool-cache");

afterEach(() => {
Expand Down Expand Up @@ -56,6 +58,8 @@ describe("script downloader/runner", () => {
});

describe("install command generator", () => {
const whichMock = io.which as jest.Mock;

const scriptPath = "hello.sh";

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

it("does not change the command on Windows", () => {
const cmd = script.generateExecCommand("win32", scriptPath);
expect(cmd).toEqual(`bash ${scriptPath}`);
expect(cmd).resolves.toEqual(`bash ${scriptPath}`);
});

["darwin", "linux"].forEach((platform) => {
it(`calls the command with sudo on ${platform}`, () => {
whichMock.mockResolvedValue("path/to/sudo");
const cmd = script.generateExecCommand(platform, scriptPath);
expect(cmd).resolves.toEqual(`sudo -E bash ${scriptPath}`);
});

it(`calls the command without sudo on ${platform}`, () => {
whichMock.mockResolvedValue("");
const cmd = script.generateExecCommand(platform, scriptPath);
expect(cmd).toEqual(`sudo -E bash ${scriptPath}`);
expect(cmd).resolves.toEqual(`bash ${scriptPath}`);
});
});
});
Expand All @@ -87,4 +98,4 @@ describe("default install root", () => {
testCase("win32", 'Program Files');
testCase("darwin", "opt");
testCase("linux", "opt");
})
})