| 
									
										
										
										
											2019-12-13 17:24:37 -05:00
										 |  |  | import * as exec from "@actions/exec"; | 
					
						
							|  |  |  | import * as io from "@actions/io"; | 
					
						
							| 
									
										
										
										
											2020-03-20 13:02:11 -07:00
										 |  |  | import { promises as fs } from "fs"; | 
					
						
							|  |  |  | import * as path from "path"; | 
					
						
							| 
									
										
										
										
											2020-03-18 22:35:13 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 13:02:11 -07:00
										 |  |  | import { CacheFilename } from "../src/constants"; | 
					
						
							| 
									
										
										
										
											2019-12-13 17:24:37 -05:00
										 |  |  | import * as tar from "../src/tar"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | jest.mock("@actions/exec"); | 
					
						
							|  |  |  | jest.mock("@actions/io"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 13:02:11 -07:00
										 |  |  | function getTempDir(): string { | 
					
						
							|  |  |  |     return path.join(__dirname, "_temp", "tar"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | beforeAll(async () => { | 
					
						
							| 
									
										
										
										
											2019-12-13 17:24:37 -05:00
										 |  |  |     jest.spyOn(io, "which").mockImplementation(tool => { | 
					
						
							|  |  |  |         return Promise.resolve(tool); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-20 13:02:11 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     process.env["GITHUB_WORKSPACE"] = process.cwd(); | 
					
						
							|  |  |  |     await jest.requireActual("@actions/io").rmRF(getTempDir()); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | afterAll(async () => { | 
					
						
							|  |  |  |     delete process.env["GITHUB_WORKSPACE"]; | 
					
						
							|  |  |  |     await jest.requireActual("@actions/io").rmRF(getTempDir()); | 
					
						
							| 
									
										
										
										
											2019-12-13 17:24:37 -05:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("extract tar", async () => { | 
					
						
							|  |  |  |     const mkdirMock = jest.spyOn(io, "mkdirP"); | 
					
						
							|  |  |  |     const execMock = jest.spyOn(exec, "exec"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const archivePath = "cache.tar"; | 
					
						
							| 
									
										
										
										
											2020-03-20 13:02:11 -07:00
										 |  |  |     const workspace = process.env["GITHUB_WORKSPACE"]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await tar.extractTar(archivePath); | 
					
						
							| 
									
										
										
										
											2019-12-13 17:24:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 13:02:11 -07:00
										 |  |  |     expect(mkdirMock).toHaveBeenCalledWith(workspace); | 
					
						
							| 
									
										
										
										
											2019-12-13 17:24:37 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const IS_WINDOWS = process.platform === "win32"; | 
					
						
							|  |  |  |     const tarPath = IS_WINDOWS | 
					
						
							|  |  |  |         ? `${process.env["windir"]}\\System32\\tar.exe` | 
					
						
							|  |  |  |         : "tar"; | 
					
						
							|  |  |  |     expect(execMock).toHaveBeenCalledTimes(1); | 
					
						
							| 
									
										
										
										
											2020-03-20 13:02:11 -07:00
										 |  |  |     expect(execMock).toHaveBeenCalledWith( | 
					
						
							|  |  |  |         `"${tarPath}"`, | 
					
						
							|  |  |  |         ["-xz", "-f", archivePath, "-P", "-C", workspace], | 
					
						
							|  |  |  |         { cwd: undefined } | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2019-12-13 17:24:37 -05:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("create tar", async () => { | 
					
						
							|  |  |  |     const execMock = jest.spyOn(exec, "exec"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 13:02:11 -07:00
										 |  |  |     const archiveFolder = getTempDir(); | 
					
						
							|  |  |  |     const workspace = process.env["GITHUB_WORKSPACE"]; | 
					
						
							|  |  |  |     const sourceDirectories = ["~/.npm/cache", `${workspace}/dist`]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await fs.mkdir(archiveFolder, { recursive: true }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await tar.createTar(archiveFolder, sourceDirectories); | 
					
						
							| 
									
										
										
										
											2019-12-13 17:24:37 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const IS_WINDOWS = process.platform === "win32"; | 
					
						
							|  |  |  |     const tarPath = IS_WINDOWS | 
					
						
							|  |  |  |         ? `${process.env["windir"]}\\System32\\tar.exe` | 
					
						
							|  |  |  |         : "tar"; | 
					
						
							| 
									
										
										
										
											2020-03-20 13:02:11 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-13 17:24:37 -05:00
										 |  |  |     expect(execMock).toHaveBeenCalledTimes(1); | 
					
						
							| 
									
										
										
										
											2020-03-20 13:02:11 -07:00
										 |  |  |     expect(execMock).toHaveBeenCalledWith( | 
					
						
							|  |  |  |         `"${tarPath}"`, | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             "-cz", | 
					
						
							|  |  |  |             "-f", | 
					
						
							|  |  |  |             CacheFilename, | 
					
						
							|  |  |  |             "-C", | 
					
						
							|  |  |  |             workspace, | 
					
						
							|  |  |  |             "--files-from", | 
					
						
							|  |  |  |             "manifest.txt" | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             cwd: archiveFolder | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2019-12-13 17:24:37 -05:00
										 |  |  | }); |