2020-01-06 18:36:33 +00:00
|
|
|
import * as exec from "@actions/exec";
|
|
|
|
import * as io from "@actions/io";
|
2020-04-09 14:29:33 +00:00
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "path";
|
2020-01-06 18:36:33 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-09 14:29:33 +00:00
|
|
|
test("extract BSD tar", async () => {
|
2020-01-06 18:36:33 +00:00
|
|
|
const mkdirMock = jest.spyOn(io, "mkdirP");
|
|
|
|
const execMock = jest.spyOn(exec, "exec");
|
|
|
|
|
2020-04-09 14:29:33 +00:00
|
|
|
const IS_WINDOWS = process.platform === "win32";
|
|
|
|
const archivePath = IS_WINDOWS
|
|
|
|
? `${process.env["windir"]}\\fakepath\\cache.tar`
|
|
|
|
: "cache.tar";
|
2020-01-06 18:36:33 +00:00
|
|
|
const targetDirectory = "~/.npm/cache";
|
|
|
|
await tar.extractTar(archivePath, targetDirectory);
|
|
|
|
|
|
|
|
expect(mkdirMock).toHaveBeenCalledWith(targetDirectory);
|
|
|
|
|
|
|
|
const tarPath = IS_WINDOWS
|
|
|
|
? `${process.env["windir"]}\\System32\\tar.exe`
|
|
|
|
: "tar";
|
|
|
|
expect(execMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [
|
|
|
|
"-xz",
|
|
|
|
"-f",
|
2020-04-09 14:29:33 +00:00
|
|
|
archivePath?.replace(/\\/g, "/"),
|
2020-01-06 18:36:33 +00:00
|
|
|
"-C",
|
2020-04-09 14:29:33 +00:00
|
|
|
targetDirectory?.replace(/\\/g, "/"),
|
2020-01-06 18:36:33 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2020-04-09 14:29:33 +00:00
|
|
|
test("extract GNU tar", async () => {
|
|
|
|
const IS_WINDOWS = process.platform === "win32";
|
|
|
|
if (IS_WINDOWS) {
|
|
|
|
jest.mock("fs");
|
|
|
|
|
|
|
|
const execMock = jest.spyOn(exec, "exec");
|
|
|
|
const existsSyncMock = jest
|
|
|
|
.spyOn(fs, "existsSync")
|
|
|
|
.mockReturnValue(false);
|
|
|
|
const isGnuTarMock = jest
|
|
|
|
.spyOn(tar, "isGnuTar")
|
|
|
|
.mockReturnValue(Promise.resolve(true));
|
|
|
|
const archivePath = `${process.env["windir"]}\\fakepath\\cache.tar`;
|
|
|
|
const targetDirectory = "~/.npm/cache";
|
|
|
|
|
|
|
|
await tar.extractTar(archivePath, targetDirectory);
|
|
|
|
|
|
|
|
expect(existsSyncMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(isGnuTarMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(execMock).toHaveBeenCalledTimes(2);
|
|
|
|
expect(execMock).toHaveBeenLastCalledWith(
|
|
|
|
"tar",
|
|
|
|
[
|
|
|
|
"-xz",
|
|
|
|
"-f",
|
|
|
|
archivePath?.replace(/\\/g, "/"),
|
|
|
|
"-C",
|
|
|
|
targetDirectory?.replace(/\\/g, "/"),
|
|
|
|
"--force-local"
|
|
|
|
],
|
|
|
|
{ cwd: undefined }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test("create BSD tar", async () => {
|
2020-01-06 18:36:33 +00:00
|
|
|
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";
|
|
|
|
expect(execMock).toHaveBeenCalledTimes(1);
|
|
|
|
expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [
|
|
|
|
"-cz",
|
|
|
|
"-f",
|
2020-04-09 14:29:33 +00:00
|
|
|
archivePath?.replace(/\\/g, "/"),
|
2020-01-06 18:36:33 +00:00
|
|
|
"-C",
|
2020-04-09 14:29:33 +00:00
|
|
|
sourceDirectory?.replace(/\\/g, "/"),
|
2020-01-06 18:36:33 +00:00
|
|
|
"."
|
|
|
|
]);
|
|
|
|
});
|