mirror of
https://code.forgejo.org/actions/cache.git
synced 2025-05-02 00:09:54 +02:00
unit test coverage for caching multiple dirs
This commit is contained in:
parent
e0d1942524
commit
057d9de723
11 changed files with 161 additions and 80 deletions
|
@ -1,20 +1,29 @@
|
|||
import * as exec from "@actions/exec";
|
||||
import * as io from "@actions/io";
|
||||
import { promises as fs } from "fs";
|
||||
import * as path from "path";
|
||||
import * as tar from "../src/tar";
|
||||
import { CacheFilename } from "../src/constants";
|
||||
|
||||
jest.mock("@actions/exec");
|
||||
jest.mock("@actions/io");
|
||||
|
||||
beforeAll(() => {
|
||||
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(() => {
|
||||
afterAll(async () => {
|
||||
delete process.env["GITHUB_WORKSPACE"];
|
||||
await jest.requireActual("@actions/io").rmRF(getTempDir());
|
||||
});
|
||||
|
||||
test("extract tar", async () => {
|
||||
|
@ -33,36 +42,43 @@ test("extract tar", async () => {
|
|||
? `${process.env["windir"]}\\System32\\tar.exe`
|
||||
: "tar";
|
||||
expect(execMock).toHaveBeenCalledTimes(1);
|
||||
expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [
|
||||
"-xz",
|
||||
"-f",
|
||||
archivePath,
|
||||
"-P",
|
||||
"-C",
|
||||
workspace
|
||||
]);
|
||||
expect(execMock).toHaveBeenCalledWith(
|
||||
`"${tarPath}"`,
|
||||
["-xz", "-f", archivePath, "-P", "-C", workspace],
|
||||
{ cwd: undefined }
|
||||
);
|
||||
});
|
||||
|
||||
test("create tar", async () => {
|
||||
const execMock = jest.spyOn(exec, "exec");
|
||||
|
||||
const archivePath = "cache.tar";
|
||||
const archiveFolder = getTempDir();
|
||||
const workspace = process.env["GITHUB_WORKSPACE"];
|
||||
const sourceDirectories = ["~/.npm/cache", `${workspace}/dist`];
|
||||
|
||||
await tar.createTar(archivePath, sourceDirectories);
|
||||
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}"`, [
|
||||
"-cz",
|
||||
"-f",
|
||||
archivePath,
|
||||
"-C",
|
||||
workspace,
|
||||
sourceDirectories.join(" ")
|
||||
]);
|
||||
expect(execMock).toHaveBeenCalledWith(
|
||||
`"${tarPath}"`,
|
||||
[
|
||||
"-cz",
|
||||
"-f",
|
||||
CacheFilename,
|
||||
"-C",
|
||||
workspace,
|
||||
"--files-from",
|
||||
"manifest.txt"
|
||||
],
|
||||
{
|
||||
cwd: archiveFolder
|
||||
}
|
||||
);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue