mirror of
https://github.com/actions/cache.git
synced 2025-04-22 03:56:45 +00:00
Fallback to which tar if no system tar
This commit is contained in:
parent
6199b9ad63
commit
1569e37d6f
2 changed files with 26 additions and 18 deletions
|
@ -6,17 +6,11 @@ jest.mock("@actions/exec");
|
||||||
jest.mock("@actions/io");
|
jest.mock("@actions/io");
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
process.env["windir"] = "C:";
|
|
||||||
|
|
||||||
jest.spyOn(io, "which").mockImplementation(tool => {
|
jest.spyOn(io, "which").mockImplementation(tool => {
|
||||||
return Promise.resolve(tool);
|
return Promise.resolve(tool);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(() => {
|
|
||||||
delete process.env["windir"];
|
|
||||||
});
|
|
||||||
|
|
||||||
test("extract tar", async () => {
|
test("extract tar", async () => {
|
||||||
const mkdirMock = jest.spyOn(io, "mkdirP");
|
const mkdirMock = jest.spyOn(io, "mkdirP");
|
||||||
const execMock = jest.spyOn(exec, "exec");
|
const execMock = jest.spyOn(exec, "exec");
|
||||||
|
@ -28,7 +22,7 @@ test("extract tar", async () => {
|
||||||
expect(mkdirMock).toHaveBeenCalledWith(targetDirectory);
|
expect(mkdirMock).toHaveBeenCalledWith(targetDirectory);
|
||||||
|
|
||||||
const IS_WINDOWS = process.platform === "win32";
|
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).toHaveBeenCalledTimes(1);
|
||||||
expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [
|
expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [
|
||||||
"-xz",
|
"-xz",
|
||||||
|
@ -47,7 +41,7 @@ test("create tar", async () => {
|
||||||
await tar.createTar(archivePath, sourceDirectory);
|
await tar.createTar(archivePath, sourceDirectory);
|
||||||
|
|
||||||
const IS_WINDOWS = process.platform === "win32";
|
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).toHaveBeenCalledTimes(1);
|
||||||
expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [
|
expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [
|
||||||
"-cz",
|
"-cz",
|
||||||
|
|
34
src/tar.ts
34
src/tar.ts
|
@ -1,12 +1,31 @@
|
||||||
import { exec } from "@actions/exec";
|
import { exec } from "@actions/exec";
|
||||||
import * as io from "@actions/io";
|
import * as io from "@actions/io";
|
||||||
|
import { existsSync } from "fs";
|
||||||
|
|
||||||
async function getTarPath(): Promise<string> {
|
async function getTarPath(): Promise<string> {
|
||||||
// Explicitly use BSD Tar on Windows
|
// Explicitly use BSD Tar on Windows
|
||||||
const IS_WINDOWS = process.platform === "win32";
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
return IS_WINDOWS
|
if (IS_WINDOWS) {
|
||||||
? `${process.env["windir"]}\\System32\\tar.exe`
|
const systemTar = `${process.env["windir"]}\\System32\\tar.exe`;
|
||||||
: await io.which("tar", true);
|
if (existsSync(systemTar)) {
|
||||||
|
return systemTar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return await io.which("tar", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function execTar(args: string[]): Promise<void> {
|
||||||
|
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(
|
export async function extractTar(
|
||||||
|
@ -15,19 +34,14 @@ export async function extractTar(
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
// Create directory to extract tar into
|
// Create directory to extract tar into
|
||||||
await io.mkdirP(targetDirectory);
|
await io.mkdirP(targetDirectory);
|
||||||
|
|
||||||
// http://man7.org/linux/man-pages/man1/tar.1.html
|
|
||||||
// tar [-options] <name of the tar archive> [files or directories which to add into archive]
|
|
||||||
const args = ["-xz", "-f", archivePath, "-C", targetDirectory];
|
const args = ["-xz", "-f", archivePath, "-C", targetDirectory];
|
||||||
await exec(`"${await getTarPath()}"`, args);
|
await execTar(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createTar(
|
export async function createTar(
|
||||||
archivePath: string,
|
archivePath: string,
|
||||||
sourceDirectory: string
|
sourceDirectory: string
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
// http://man7.org/linux/man-pages/man1/tar.1.html
|
|
||||||
// tar [-options] <name of the tar archive> [files or directories which to add into archive]
|
|
||||||
const args = ["-cz", "-f", archivePath, "-C", sourceDirectory, "."];
|
const args = ["-cz", "-f", archivePath, "-C", sourceDirectory, "."];
|
||||||
await exec(`"${await getTarPath()}"`, args);
|
await execTar(args);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue