mirror of https://github.com/actions/cache.git
Use BSD tar on windows
parent
3d01b4eb53
commit
7352daed78
@ -0,0 +1,60 @@
|
|||||||
|
import * as exec from "@actions/exec";
|
||||||
|
import * as io from "@actions/io";
|
||||||
|
import * as tar from "../src/tar";
|
||||||
|
|
||||||
|
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");
|
||||||
|
|
||||||
|
const archivePath = "cache.tar";
|
||||||
|
const targetDirectory = "~/.npm/cache";
|
||||||
|
await tar.extractTar(archivePath, targetDirectory);
|
||||||
|
|
||||||
|
expect(mkdirMock).toHaveBeenCalledWith(targetDirectory);
|
||||||
|
|
||||||
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
|
const tarPath = IS_WINDOWS ? "C:\\System32\\tar.exe" : "tar";
|
||||||
|
expect(execMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [
|
||||||
|
"-xz",
|
||||||
|
"-f",
|
||||||
|
archivePath,
|
||||||
|
"-C",
|
||||||
|
targetDirectory
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("create tar", async () => {
|
||||||
|
const execMock = jest.spyOn(exec, "exec");
|
||||||
|
|
||||||
|
const archivePath = "cache.tar";
|
||||||
|
const sourceDirectory = "~/.npm/cache";
|
||||||
|
await tar.createTar(archivePath, sourceDirectory);
|
||||||
|
|
||||||
|
const IS_WINDOWS = process.platform === "win32";
|
||||||
|
const tarPath = IS_WINDOWS ? "C:\\System32\\tar.exe" : "tar";
|
||||||
|
expect(execMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [
|
||||||
|
"-cz",
|
||||||
|
"-f",
|
||||||
|
archivePath,
|
||||||
|
"-C",
|
||||||
|
sourceDirectory,
|
||||||
|
"."
|
||||||
|
]);
|
||||||
|
});
|
@ -0,0 +1,27 @@
|
|||||||
|
import { exec } from "@actions/exec";
|
||||||
|
import * as io from "@actions/io";
|
||||||
|
|
||||||
|
export async function extractTar(archivePath: string, targetDirectory: string) {
|
||||||
|
// Create directory to extract tar into
|
||||||
|
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];
|
||||||
|
await exec(`"${await getTarPath()}"`, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createTar(archivePath: string, sourceDirectory: string) {
|
||||||
|
// 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, "."];
|
||||||
|
await exec(`"${await getTarPath()}"`, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getTarPath(): Promise<string> {
|
||||||
|
// 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);
|
||||||
|
}
|
Loading…
Reference in New Issue