Organize code

This commit is contained in:
BSKY 2020-03-21 11:42:29 +09:00
parent 36ce21c4a4
commit 8d6737458a
3 changed files with 14 additions and 15 deletions

View file

@ -3,7 +3,7 @@ import * as io from "@actions/io";
import { promises as fs } from "fs"; import { promises as fs } from "fs";
import * as path from "path"; import * as path from "path";
import { CacheFilename } from "../src/constants"; import { CacheFilename, ManifestFilename } from "../src/constants";
import * as tar from "../src/tar"; import * as tar from "../src/tar";
jest.mock("@actions/exec"); jest.mock("@actions/exec");
@ -32,11 +32,11 @@ test("extract tar", async () => {
const execMock = jest.spyOn(exec, "exec"); const execMock = jest.spyOn(exec, "exec");
const archivePath = "cache.tar"; const archivePath = "cache.tar";
const workspace = process.env["GITHUB_WORKSPACE"]; const workingDirectory = process.env["GITHUB_WORKSPACE"];
await tar.extractTar(archivePath); await tar.extractTar(archivePath);
expect(mkdirMock).toHaveBeenCalledWith(workspace); expect(mkdirMock).toHaveBeenCalledWith(workingDirectory);
const IS_WINDOWS = process.platform === "win32"; const IS_WINDOWS = process.platform === "win32";
const tarPath = IS_WINDOWS const tarPath = IS_WINDOWS
@ -52,7 +52,7 @@ test("extract tar", async () => {
archivePath, archivePath,
"-P", "-P",
"-C", "-C",
workspace workingDirectory
], ],
{ cwd: undefined } { cwd: undefined }
); );
@ -62,8 +62,8 @@ test("create tar", async () => {
const execMock = jest.spyOn(exec, "exec"); const execMock = jest.spyOn(exec, "exec");
const archiveFolder = getTempDir(); const archiveFolder = getTempDir();
const workspace = process.env["GITHUB_WORKSPACE"]; const workingDirectory = process.env["GITHUB_WORKSPACE"];
const sourceDirectories = ["~/.npm/cache", `${workspace}/dist`]; const sourceDirectories = ["~/.npm/cache", `${workingDirectory}/dist`];
await fs.mkdir(archiveFolder, { recursive: true }); await fs.mkdir(archiveFolder, { recursive: true });
@ -83,12 +83,10 @@ test("create tar", async () => {
"-cf", "-cf",
CacheFilename, CacheFilename,
"-C", "-C",
workspace, workingDirectory,
"--files-from", "--files-from",
"manifest.txt" ManifestFilename
], ],
{ { cwd: archiveFolder }
cwd: archiveFolder
}
); );
}); });

View file

@ -20,3 +20,5 @@ export enum Events {
} }
export const CacheFilename = "cache.tar.zst"; export const CacheFilename = "cache.tar.zst";
export const ManifestFilename = "manifest.txt";

View file

@ -3,7 +3,7 @@ import * as io from "@actions/io";
import { existsSync, writeFileSync } from "fs"; import { existsSync, writeFileSync } from "fs";
import * as path from "path"; import * as path from "path";
import { CacheFilename } from "./constants"; import { CacheFilename, ManifestFilename } from "./constants";
async function getTarPath(): Promise<string> { async function getTarPath(): Promise<string> {
// Explicitly use BSD Tar on Windows // Explicitly use BSD Tar on Windows
@ -56,9 +56,8 @@ export async function createTar(
sourceDirectories: string[] sourceDirectories: string[]
): Promise<void> { ): Promise<void> {
// Write source directories to manifest.txt to avoid command length limits // Write source directories to manifest.txt to avoid command length limits
const manifestFilename = "manifest.txt";
writeFileSync( writeFileSync(
path.join(archiveFolder, manifestFilename), path.join(archiveFolder, ManifestFilename),
sourceDirectories.join("\n") sourceDirectories.join("\n")
); );
@ -71,7 +70,7 @@ export async function createTar(
"-C", "-C",
workingDirectory, workingDirectory,
"--files-from", "--files-from",
manifestFilename ManifestFilename
]; ];
await execTar(args, archiveFolder); await execTar(args, archiveFolder);
} }