2019-12-13 22:24:37 +00:00
|
|
|
import * as exec from "@actions/exec";
|
|
|
|
import * as io from "@actions/io";
|
2020-03-20 20:02:11 +00:00
|
|
|
import * as path from "path";
|
2020-03-18 13:35:13 +00:00
|
|
|
|
2020-04-22 20:36:34 +00:00
|
|
|
import { CacheFilename, CompressionMethod } from "../src/constants";
|
2019-12-13 22:24:37 +00:00
|
|
|
import * as tar from "../src/tar";
|
2020-04-22 20:36:34 +00:00
|
|
|
import * as utils from "../src/utils/actionUtils";
|
2019-12-13 22:24:37 +00:00
|
|
|
|
2020-04-10 19:26:15 +00:00
|
|
|
import fs = require("fs");
|
|
|
|
|
2019-12-13 22:24:37 +00:00
|
|
|
jest.mock("@actions/exec");
|
|
|
|
jest.mock("@actions/io");
|
|
|
|
|
2020-04-22 20:36:34 +00:00
|
|
|
const IS_WINDOWS = process.platform === "win32";
|
|
|
|
|
2020-03-20 20:02:11 +00:00
|
|
|
function getTempDir(): string {
|
|
|
|
return path.join(__dirname, "_temp", "tar");
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2019-12-13 22:24:37 +00:00
|
|
|
jest.spyOn(io, "which").mockImplementation(tool => {
|
|
|
|
return Promise.resolve(tool);
|
|
|
|
});
|
2020-03-20 20:02:11 +00:00
|
|
|
|
|
|
|
process.env["GITHUB_WORKSPACE"] = process.cwd();
|
|
|
|
await jest.requireActual("@actions/io").rmRF(getTempDir());
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
delete process.env["GITHUB_WORKSPACE"];
|
|
|
|
await jest.requireActual("@actions/io").rmRF(getTempDir());
|
2019-12-13 22:24:37 +00:00
|
|
|
});
|
|
|
|
|
2020-04-22 20:36:34 +00:00
|
|
|
test("zstd extract tar", async () => {
|
2019-12-13 22:24:37 +00:00
|
|
|
const mkdirMock = jest.spyOn(io, "mkdirP");
|
|
|
|
const execMock = jest.spyOn(exec, "exec");
|
|
|
|
|
2020-04-09 14:29:33 +00:00
|
|
|
const archivePath = IS_WINDOWS
|
|
|
|
? `${process.env["windir"]}\\fakepath\\cache.tar`
|
|
|
|
: "cache.tar";
|
2020-03-20 20:02:11 +00:00
|
|
|
const workspace = process.env["GITHUB_WORKSPACE"];
|
|
|
|
|
2020-04-22 20:36:34 +00:00
|
|
|
await tar.extractTar(archivePath, CompressionMethod.Zstd);
|
2019-12-13 22:24:37 +00:00
|
|
|
|
2020-03-20 20:02:11 +00:00
|
|
|
expect(mkdirMock).toHaveBeenCalledWith(workspace);
|
2020-04-22 20:36:34 +00:00
|
|
|
const tarPath = IS_WINDOWS
|
|
|
|
? `${process.env["windir"]}\\System32\\tar.exe`
|
|
|
|
: "tar";
|
|
|
|
expect(execMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(execMock).toHaveBeenCalledWith(
|
|
|
|
`${tarPath}`,
|
|
|
|
[
|
|
|
|
"--use-compress-program",
|
|
|
|
"zstd -d",
|
|
|
|
"-xf",
|
|
|
|
IS_WINDOWS ? archivePath.replace(/\\/g, "/") : archivePath,
|
|
|
|
"-P",
|
|
|
|
"-C",
|
|
|
|
IS_WINDOWS ? workspace?.replace(/\\/g, "/") : workspace
|
|
|
|
],
|
|
|
|
{ cwd: undefined }
|
|
|
|
);
|
|
|
|
});
|
2019-12-13 22:24:37 +00:00
|
|
|
|
2020-04-22 20:36:34 +00:00
|
|
|
test("gzip extract tar", async () => {
|
|
|
|
const mkdirMock = jest.spyOn(io, "mkdirP");
|
|
|
|
const execMock = jest.spyOn(exec, "exec");
|
|
|
|
const archivePath = IS_WINDOWS
|
|
|
|
? `${process.env["windir"]}\\fakepath\\cache.tar`
|
|
|
|
: "cache.tar";
|
|
|
|
const workspace = process.env["GITHUB_WORKSPACE"];
|
|
|
|
|
|
|
|
await tar.extractTar(archivePath, CompressionMethod.Gzip);
|
|
|
|
|
|
|
|
expect(mkdirMock).toHaveBeenCalledWith(workspace);
|
2019-12-13 22:24:37 +00:00
|
|
|
const tarPath = IS_WINDOWS
|
|
|
|
? `${process.env["windir"]}\\System32\\tar.exe`
|
|
|
|
: "tar";
|
|
|
|
expect(execMock).toHaveBeenCalledTimes(1);
|
2020-03-20 20:02:11 +00:00
|
|
|
expect(execMock).toHaveBeenCalledWith(
|
2020-04-22 20:36:34 +00:00
|
|
|
`${tarPath}`,
|
2020-04-09 14:29:33 +00:00
|
|
|
[
|
2020-04-22 20:36:34 +00:00
|
|
|
"-z",
|
|
|
|
"-xf",
|
2020-04-13 16:20:27 +00:00
|
|
|
IS_WINDOWS ? archivePath.replace(/\\/g, "/") : archivePath,
|
2020-04-09 14:29:33 +00:00
|
|
|
"-P",
|
|
|
|
"-C",
|
2020-04-13 16:20:27 +00:00
|
|
|
IS_WINDOWS ? workspace?.replace(/\\/g, "/") : workspace
|
2020-04-09 14:29:33 +00:00
|
|
|
],
|
2020-03-20 20:02:11 +00:00
|
|
|
{ cwd: undefined }
|
|
|
|
);
|
2019-12-13 22:24:37 +00:00
|
|
|
});
|
|
|
|
|
2020-04-22 20:36:34 +00:00
|
|
|
test("gzip extract GNU tar on windows", async () => {
|
2020-04-09 14:29:33 +00:00
|
|
|
if (IS_WINDOWS) {
|
2020-04-10 19:26:15 +00:00
|
|
|
jest.spyOn(fs, "existsSync").mockReturnValueOnce(false);
|
2020-04-09 14:29:33 +00:00
|
|
|
|
2020-04-22 20:36:34 +00:00
|
|
|
const isGnuMock = jest
|
|
|
|
.spyOn(utils, "useGnuTar")
|
|
|
|
.mockReturnValue(Promise.resolve(true));
|
2020-04-09 14:29:33 +00:00
|
|
|
const execMock = jest.spyOn(exec, "exec");
|
|
|
|
const archivePath = `${process.env["windir"]}\\fakepath\\cache.tar`;
|
|
|
|
const workspace = process.env["GITHUB_WORKSPACE"];
|
|
|
|
|
2020-04-22 20:36:34 +00:00
|
|
|
await tar.extractTar(archivePath, CompressionMethod.Gzip);
|
2020-04-09 14:29:33 +00:00
|
|
|
|
2020-04-22 20:36:34 +00:00
|
|
|
expect(isGnuMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(execMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(execMock).toHaveBeenCalledWith(
|
|
|
|
`tar`,
|
2020-04-09 14:29:33 +00:00
|
|
|
[
|
2020-04-22 20:36:34 +00:00
|
|
|
"-z",
|
|
|
|
"-xf",
|
2020-04-13 16:20:27 +00:00
|
|
|
archivePath.replace(/\\/g, "/"),
|
2020-04-09 14:29:33 +00:00
|
|
|
"-P",
|
|
|
|
"-C",
|
|
|
|
workspace?.replace(/\\/g, "/"),
|
|
|
|
"--force-local"
|
|
|
|
],
|
|
|
|
{ cwd: undefined }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-22 20:36:34 +00:00
|
|
|
test("zstd create tar", async () => {
|
|
|
|
const execMock = jest.spyOn(exec, "exec");
|
|
|
|
|
|
|
|
const archiveFolder = getTempDir();
|
|
|
|
const workspace = process.env["GITHUB_WORKSPACE"];
|
|
|
|
const sourceDirectories = ["~/.npm/cache", `${workspace}/dist`];
|
|
|
|
|
|
|
|
await fs.promises.mkdir(archiveFolder, { recursive: true });
|
|
|
|
|
|
|
|
await tar.createTar(
|
|
|
|
archiveFolder,
|
|
|
|
sourceDirectories,
|
|
|
|
CompressionMethod.Zstd
|
|
|
|
);
|
|
|
|
|
|
|
|
const tarPath = IS_WINDOWS
|
|
|
|
? `${process.env["windir"]}\\System32\\tar.exe`
|
|
|
|
: "tar";
|
|
|
|
|
|
|
|
expect(execMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(execMock).toHaveBeenCalledWith(
|
|
|
|
`${tarPath}`,
|
|
|
|
[
|
|
|
|
"--use-compress-program",
|
|
|
|
"zstd -T0",
|
|
|
|
"-cf",
|
|
|
|
IS_WINDOWS
|
|
|
|
? CacheFilename.Zstd.replace(/\\/g, "/")
|
|
|
|
: CacheFilename.Zstd,
|
|
|
|
"-P",
|
|
|
|
"-C",
|
|
|
|
IS_WINDOWS ? workspace?.replace(/\\/g, "/") : workspace,
|
|
|
|
"--files-from",
|
|
|
|
"manifest.txt"
|
|
|
|
],
|
|
|
|
{
|
|
|
|
cwd: archiveFolder
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("gzip create tar", async () => {
|
2019-12-13 22:24:37 +00:00
|
|
|
const execMock = jest.spyOn(exec, "exec");
|
|
|
|
|
2020-03-20 20:02:11 +00:00
|
|
|
const archiveFolder = getTempDir();
|
|
|
|
const workspace = process.env["GITHUB_WORKSPACE"];
|
|
|
|
const sourceDirectories = ["~/.npm/cache", `${workspace}/dist`];
|
|
|
|
|
2020-04-10 19:50:35 +00:00
|
|
|
await fs.promises.mkdir(archiveFolder, { recursive: true });
|
2020-03-20 20:02:11 +00:00
|
|
|
|
2020-04-22 20:36:34 +00:00
|
|
|
await tar.createTar(
|
|
|
|
archiveFolder,
|
|
|
|
sourceDirectories,
|
|
|
|
CompressionMethod.Gzip
|
|
|
|
);
|
2019-12-13 22:24:37 +00:00
|
|
|
|
|
|
|
const tarPath = IS_WINDOWS
|
|
|
|
? `${process.env["windir"]}\\System32\\tar.exe`
|
|
|
|
: "tar";
|
2020-03-20 20:02:11 +00:00
|
|
|
|
2019-12-13 22:24:37 +00:00
|
|
|
expect(execMock).toHaveBeenCalledTimes(1);
|
2020-03-20 20:02:11 +00:00
|
|
|
expect(execMock).toHaveBeenCalledWith(
|
2020-04-22 20:36:34 +00:00
|
|
|
`${tarPath}`,
|
2020-03-20 20:02:11 +00:00
|
|
|
[
|
2020-04-22 20:36:34 +00:00
|
|
|
"-z",
|
|
|
|
"-cf",
|
|
|
|
IS_WINDOWS
|
|
|
|
? CacheFilename.Gzip.replace(/\\/g, "/")
|
|
|
|
: CacheFilename.Gzip,
|
2020-04-08 14:58:38 +00:00
|
|
|
"-P",
|
2020-03-20 20:02:11 +00:00
|
|
|
"-C",
|
2020-04-13 16:20:27 +00:00
|
|
|
IS_WINDOWS ? workspace?.replace(/\\/g, "/") : workspace,
|
2020-03-20 20:02:11 +00:00
|
|
|
"--files-from",
|
|
|
|
"manifest.txt"
|
|
|
|
],
|
|
|
|
{
|
|
|
|
cwd: archiveFolder
|
|
|
|
}
|
|
|
|
);
|
2019-12-13 22:24:37 +00:00
|
|
|
});
|