mirror of
				https://github.com/actions/cache.git
				synced 2025-10-23 08:56:26 +08:00 
			
		
		
		
	Fallback to GNU tar if BSD tar is unavailable
This commit is contained in:
		| @@ -1,6 +1,6 @@ | ||||
| import * as exec from "@actions/exec"; | ||||
| import * as io from "@actions/io"; | ||||
| import { promises as fs } from "fs"; | ||||
| import * as fs from "fs"; | ||||
| import * as path from "path"; | ||||
|  | ||||
| import { CacheFilename } from "../src/constants"; | ||||
| @@ -27,37 +27,82 @@ afterAll(async () => { | ||||
|     await jest.requireActual("@actions/io").rmRF(getTempDir()); | ||||
| }); | ||||
|  | ||||
| test("extract tar", async () => { | ||||
| test("extract BSD tar", async () => { | ||||
|     const mkdirMock = jest.spyOn(io, "mkdirP"); | ||||
|     const execMock = jest.spyOn(exec, "exec"); | ||||
|  | ||||
|     const archivePath = "cache.tar"; | ||||
|     const IS_WINDOWS = process.platform === "win32"; | ||||
|     const archivePath = IS_WINDOWS | ||||
|         ? `${process.env["windir"]}\\fakepath\\cache.tar` | ||||
|         : "cache.tar"; | ||||
|     const workspace = process.env["GITHUB_WORKSPACE"]; | ||||
|  | ||||
|     await tar.extractTar(archivePath); | ||||
|  | ||||
|     expect(mkdirMock).toHaveBeenCalledWith(workspace); | ||||
|  | ||||
|     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}"`, | ||||
|         ["-xz", "-f", archivePath, "-P", "-C", workspace], | ||||
|         [ | ||||
|             "-xz", | ||||
|             "-f", | ||||
|             archivePath?.replace(/\\/g, "/"), | ||||
|             "-P", | ||||
|             "-C", | ||||
|             workspace?.replace(/\\/g, "/") | ||||
|         ], | ||||
|         { cwd: undefined } | ||||
|     ); | ||||
| }); | ||||
|  | ||||
| test("create tar", async () => { | ||||
| 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 workspace = process.env["GITHUB_WORKSPACE"]; | ||||
|  | ||||
|         await tar.extractTar(archivePath); | ||||
|  | ||||
|         expect(existsSyncMock).toHaveBeenCalledTimes(1); | ||||
|         expect(isGnuTarMock).toHaveBeenCalledTimes(1); | ||||
|         expect(execMock).toHaveBeenCalledTimes(2); | ||||
|         expect(execMock).toHaveBeenLastCalledWith( | ||||
|             "tar", | ||||
|             [ | ||||
|                 "-xz", | ||||
|                 "-f", | ||||
|                 archivePath?.replace(/\\/g, "/"), | ||||
|                 "-P", | ||||
|                 "-C", | ||||
|                 workspace?.replace(/\\/g, "/"), | ||||
|                 "--force-local" | ||||
|             ], | ||||
|             { cwd: undefined } | ||||
|         ); | ||||
|     } | ||||
| }); | ||||
|  | ||||
| test("create BSD tar", async () => { | ||||
|     const execMock = jest.spyOn(exec, "exec"); | ||||
|  | ||||
|     const archiveFolder = getTempDir(); | ||||
|     const workspace = process.env["GITHUB_WORKSPACE"]; | ||||
|     const sourceDirectories = ["~/.npm/cache", `${workspace}/dist`]; | ||||
|  | ||||
|     await fs.mkdir(archiveFolder, { recursive: true }); | ||||
|     await fs.mkdir(archiveFolder, () => void { recursive: true }); | ||||
|  | ||||
|     await tar.createTar(archiveFolder, sourceDirectories); | ||||
|  | ||||
| @@ -72,10 +117,10 @@ test("create tar", async () => { | ||||
|         [ | ||||
|             "-cz", | ||||
|             "-f", | ||||
|             CacheFilename, | ||||
|             CacheFilename?.replace(/\\/g, "/"), | ||||
|             "-P", | ||||
|             "-C", | ||||
|             workspace, | ||||
|             workspace?.replace(/\\/g, "/"), | ||||
|             "--files-from", | ||||
|             "manifest.txt" | ||||
|         ], | ||||
|   | ||||
		Reference in New Issue
	
	Block a user