diff --git a/__tests__/tar.test.ts b/__tests__/tar.test.ts index 0f7bd18..87cdc22 100644 --- a/__tests__/tar.test.ts +++ b/__tests__/tar.test.ts @@ -6,17 +6,11 @@ jest.mock("@actions/exec"); jest.mock("@actions/io"); beforeAll(() => { - process.env["windir"] = "C:"; - jest.spyOn(io, "which").mockImplementation(tool => { return Promise.resolve(tool); }); }); -afterAll(() => { - delete process.env["windir"]; -}); - test("extract tar", async () => { const mkdirMock = jest.spyOn(io, "mkdirP"); const execMock = jest.spyOn(exec, "exec"); @@ -28,7 +22,7 @@ test("extract tar", async () => { expect(mkdirMock).toHaveBeenCalledWith(targetDirectory); const IS_WINDOWS = process.platform === "win32"; - const tarPath = IS_WINDOWS ? "C:\\System32\\tar.exe" : "tar"; + const tarPath = IS_WINDOWS ? `${process.env["windir"]}\\System32\\tar.exe` : "tar"; expect(execMock).toHaveBeenCalledTimes(1); expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [ "-xz", @@ -47,7 +41,7 @@ test("create tar", async () => { await tar.createTar(archivePath, sourceDirectory); const IS_WINDOWS = process.platform === "win32"; - const tarPath = IS_WINDOWS ? "C:\\System32\\tar.exe" : "tar"; + const tarPath = IS_WINDOWS ? `${process.env["windir"]}\\System32\\tar.exe` : "tar"; expect(execMock).toHaveBeenCalledTimes(1); expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [ "-cz", diff --git a/src/tar.ts b/src/tar.ts index 6014920..1f572d1 100644 --- a/src/tar.ts +++ b/src/tar.ts @@ -1,12 +1,31 @@ import { exec } from "@actions/exec"; import * as io from "@actions/io"; +import { existsSync } from "fs"; async function getTarPath(): Promise { // Explicitly use BSD Tar on Windows const IS_WINDOWS = process.platform === "win32"; - return IS_WINDOWS - ? `${process.env["windir"]}\\System32\\tar.exe` - : await io.which("tar", true); + if (IS_WINDOWS) { + const systemTar = `${process.env["windir"]}\\System32\\tar.exe`; + if (existsSync(systemTar)) { + return systemTar; + } + } + return await io.which("tar", true); +} + +async function execTar(args: string[]): Promise { + try { + await exec(`"${await getTarPath()}"`, args); + } catch (error) { + const IS_WINDOWS = process.platform === "win32"; + if (IS_WINDOWS) { + throw new Error( + `Tar failed with error: ${error?.message}. Ensure BSD tar is installed and on the PATH.` + ); + } + throw new Error(`Tar failed with error: ${error?.message}`); + } } export async function extractTar( @@ -15,19 +34,14 @@ export async function extractTar( ): Promise { // Create directory to extract tar into await io.mkdirP(targetDirectory); - - // http://man7.org/linux/man-pages/man1/tar.1.html - // tar [-options] [files or directories which to add into archive] const args = ["-xz", "-f", archivePath, "-C", targetDirectory]; - await exec(`"${await getTarPath()}"`, args); + await execTar(args); } export async function createTar( archivePath: string, sourceDirectory: string ): Promise { - // http://man7.org/linux/man-pages/man1/tar.1.html - // tar [-options] [files or directories which to add into archive] const args = ["-cz", "-f", archivePath, "-C", sourceDirectory, "."]; - await exec(`"${await getTarPath()}"`, args); + await execTar(args); }