diff --git a/package-lock.json b/package-lock.json index 4efc3df..8601fb0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,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": { @@ -113,9 +113,9 @@ } }, "node_modules/@actions/io": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/@actions/io/-/io-1.1.2.tgz", - "integrity": "sha512-d+RwPlMp+2qmBfeLYPLXuSRykDIFEwdTA0MMxzS9kh4kvP1ftrc/9fzy6pX6qAjthdXruHQ6/6kjT/DNo5ALuw==" + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/@actions/io/-/io-1.1.3.tgz", + "integrity": "sha512-wi9JjgKLYS7U/z8PPbco+PvTb/nRWjeoFlJ1Qer83k/3C5PHQi28hiVdeE2kHXmIL99mQFawx8qt/JPjZilJ8Q==" }, "node_modules/@actions/tool-cache": { "version": "1.7.2", @@ -5343,9 +5343,9 @@ } }, "@actions/io": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/@actions/io/-/io-1.1.2.tgz", - "integrity": "sha512-d+RwPlMp+2qmBfeLYPLXuSRykDIFEwdTA0MMxzS9kh4kvP1ftrc/9fzy6pX6qAjthdXruHQ6/6kjT/DNo5ALuw==" + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/@actions/io/-/io-1.1.3.tgz", + "integrity": "sha512-wi9JjgKLYS7U/z8PPbco+PvTb/nRWjeoFlJ1Qer83k/3C5PHQi28hiVdeE2kHXmIL99mQFawx8qt/JPjZilJ8Q==" }, "@actions/tool-cache": { "version": "1.7.2", diff --git a/package.json b/package.json index ad67a2d..6d142b0 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/script.ts b/src/script.ts index 6aefbff..c1f8809 100644 --- a/src/script.ts +++ b/src/script.ts @@ -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"; @@ -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); @@ -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 { // 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; @@ -48,4 +52,4 @@ export function defaultInstallRoot(platform: string, programName: string): strin installRoot = path.join("/","opt", programName); } return installRoot; -} \ No newline at end of file +} diff --git a/src/script.unit.test.ts b/src/script.unit.test.ts index 937b1a5..86639f9 100644 --- a/src/script.unit.test.ts +++ b/src/script.unit.test.ts @@ -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(() => { @@ -56,6 +58,8 @@ describe("script downloader/runner", () => { }); describe("install command generator", () => { + const whichMock = io.which as jest.Mock; + const scriptPath = "hello.sh"; beforeAll(() => { @@ -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}`); }); }); }); @@ -87,4 +98,4 @@ describe("default install root", () => { testCase("win32", 'Program Files'); testCase("darwin", "opt"); testCase("linux", "opt"); -}) \ No newline at end of file +})