1
0
Fork 0
mirror of https://code.forgejo.org/actions/cache.git synced 2025-04-20 03:46:17 +02:00
cache/__tests__/tar.test.ts

93 lines
2.5 KiB
TypeScript
Raw Normal View History

import * as exec from "@actions/exec";
import * as io from "@actions/io";
import { promises as fs } from "fs";
import * as path from "path";
2020-03-21 11:42:29 +09:00
import { CacheFilename, ManifestFilename } from "../src/constants";
import * as tar from "../src/tar";
jest.mock("@actions/exec");
jest.mock("@actions/io");
function getTempDir(): string {
return path.join(__dirname, "_temp", "tar");
}
beforeAll(async () => {
jest.spyOn(io, "which").mockImplementation(tool => {
return Promise.resolve(tool);
});
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());
});
test("extract tar", async () => {
const mkdirMock = jest.spyOn(io, "mkdirP");
const execMock = jest.spyOn(exec, "exec");
const archivePath = "cache.tar";
2020-03-21 11:42:29 +09:00
const workingDirectory = process.env["GITHUB_WORKSPACE"];
await tar.extractTar(archivePath);
2020-03-21 11:42:29 +09:00
expect(mkdirMock).toHaveBeenCalledWith(workingDirectory);
const IS_WINDOWS = process.platform === "win32";
const tarPath = IS_WINDOWS
? `${process.env["windir"]}\\System32\\tar.exe`
: "tar";
expect(execMock).toHaveBeenCalledTimes(1);
expect(execMock).toHaveBeenCalledWith(
`"${tarPath}"`,
2020-03-21 10:57:48 +09:00
[
"--use-compress-program",
"zstd --long=31 -d",
"-xf",
archivePath,
"-P",
"-C",
2020-03-21 11:42:29 +09:00
workingDirectory
2020-03-21 10:57:48 +09:00
],
{ cwd: undefined }
);
});
test("create tar", async () => {
const execMock = jest.spyOn(exec, "exec");
const archiveFolder = getTempDir();
2020-03-21 11:42:29 +09:00
const workingDirectory = process.env["GITHUB_WORKSPACE"];
const sourceDirectories = ["~/.npm/cache", `${workingDirectory}/dist`];
await fs.mkdir(archiveFolder, { recursive: true });
await tar.createTar(archiveFolder, sourceDirectories);
const IS_WINDOWS = process.platform === "win32";
const tarPath = IS_WINDOWS
? `${process.env["windir"]}\\System32\\tar.exe`
: "tar";
expect(execMock).toHaveBeenCalledTimes(1);
expect(execMock).toHaveBeenCalledWith(
`"${tarPath}"`,
[
2020-03-21 10:57:48 +09:00
"--use-compress-program",
"zstd -T0 --long=31",
"-cf",
CacheFilename,
"-C",
2020-03-21 11:42:29 +09:00
workingDirectory,
"--files-from",
2020-03-21 11:42:29 +09:00
ManifestFilename
],
2020-03-21 11:42:29 +09:00
{ cwd: archiveFolder }
);
});