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
2020-02-09 03:44:19 +01:00

59 lines
1.8 KiB
TypeScript

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(() => {
jest.spyOn(io, "which").mockImplementation(tool => {
return Promise.resolve(tool);
});
});
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
? `${process.env["windir"]}\\System32\\tar.exe`
: "tar";
let tarParams = ["-xz", "-f", archivePath, "-C", targetDirectory];
let tarExec = `${tarPath}`;
if (!IS_WINDOWS) {
tarExec = "sudo";
tarParams = [`${tarPath}`, ...tarParams];
}
expect(execMock).toHaveBeenCalledTimes(1);
expect(execMock).toHaveBeenCalledWith(`"${tarExec}"`, tarParams);
});
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
? `${process.env["windir"]}\\System32\\tar.exe`
: "tar";
let tarParams = ["-cz", "-f", archivePath, "-C", sourceDirectory, "."];
let tarExec = `${tarPath}`;
if (!IS_WINDOWS) {
tarExec = "sudo";
tarParams = [`${tarPath}`, ...tarParams];
}
expect(execMock).toHaveBeenCalledTimes(1);
expect(execMock).toHaveBeenCalledWith(`"${tarExec}"`, tarParams);
});