mirror of
				https://github.com/actions/cache.git
				synced 2025-11-04 07:38:37 +08:00 
			
		
		
		
	Run tar with sudo [actions/cache#133]
This commit is contained in:
		
							parent
							
								
									78a4b2143b
								
							
						
					
					
						commit
						1e5552f09c
					
				| 
						 | 
					@ -25,14 +25,15 @@ test("extract tar", async () => {
 | 
				
			||||||
    const tarPath = IS_WINDOWS
 | 
					    const tarPath = IS_WINDOWS
 | 
				
			||||||
        ? `${process.env["windir"]}\\System32\\tar.exe`
 | 
					        ? `${process.env["windir"]}\\System32\\tar.exe`
 | 
				
			||||||
        : "tar";
 | 
					        : "tar";
 | 
				
			||||||
 | 
					    let tarParams = ["-xz", "-f", archivePath, "-C", targetDirectory];
 | 
				
			||||||
 | 
					    let tarExec = `${tarPath}`;
 | 
				
			||||||
 | 
					    if (!IS_WINDOWS) {
 | 
				
			||||||
 | 
					        tarExec = "sudo";
 | 
				
			||||||
 | 
					        tarParams = [`${tarPath}`, ...tarParams];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    expect(execMock).toHaveBeenCalledTimes(1);
 | 
					    expect(execMock).toHaveBeenCalledTimes(1);
 | 
				
			||||||
    expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [
 | 
					    expect(execMock).toHaveBeenCalledWith(`"${tarExec}"`, tarParams);
 | 
				
			||||||
        "-xz",
 | 
					 | 
				
			||||||
        "-f",
 | 
					 | 
				
			||||||
        archivePath,
 | 
					 | 
				
			||||||
        "-C",
 | 
					 | 
				
			||||||
        targetDirectory
 | 
					 | 
				
			||||||
    ]);
 | 
					 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test("create tar", async () => {
 | 
					test("create tar", async () => {
 | 
				
			||||||
| 
						 | 
					@ -46,13 +47,13 @@ test("create tar", async () => {
 | 
				
			||||||
    const tarPath = IS_WINDOWS
 | 
					    const tarPath = IS_WINDOWS
 | 
				
			||||||
        ? `${process.env["windir"]}\\System32\\tar.exe`
 | 
					        ? `${process.env["windir"]}\\System32\\tar.exe`
 | 
				
			||||||
        : "tar";
 | 
					        : "tar";
 | 
				
			||||||
 | 
					    let tarParams = ["-cz", "-f", archivePath, "-C", sourceDirectory, "."];
 | 
				
			||||||
 | 
					    let tarExec = `${tarPath}`;
 | 
				
			||||||
 | 
					    if (!IS_WINDOWS) {
 | 
				
			||||||
 | 
					        tarExec = "sudo";
 | 
				
			||||||
 | 
					        tarParams = [`${tarPath}`, ...tarParams];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    expect(execMock).toHaveBeenCalledTimes(1);
 | 
					    expect(execMock).toHaveBeenCalledTimes(1);
 | 
				
			||||||
    expect(execMock).toHaveBeenCalledWith(`"${tarPath}"`, [
 | 
					    expect(execMock).toHaveBeenCalledWith(`"${tarExec}"`, tarParams);
 | 
				
			||||||
        "-cz",
 | 
					 | 
				
			||||||
        "-f",
 | 
					 | 
				
			||||||
        archivePath,
 | 
					 | 
				
			||||||
        "-C",
 | 
					 | 
				
			||||||
        sourceDirectory,
 | 
					 | 
				
			||||||
        "."
 | 
					 | 
				
			||||||
    ]);
 | 
					 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										12
									
								
								src/tar.ts
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								src/tar.ts
									
									
									
									
									
								
							| 
						 | 
					@ -15,10 +15,16 @@ async function getTarPath(): Promise<string> {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function execTar(args: string[]): Promise<void> {
 | 
					async function execTar(args: string[]): Promise<void> {
 | 
				
			||||||
    try {
 | 
					 | 
				
			||||||
        await exec(`"${await getTarPath()}"`, args);
 | 
					 | 
				
			||||||
    } catch (error) {
 | 
					 | 
				
			||||||
    const IS_WINDOWS = process.platform === "win32";
 | 
					    const IS_WINDOWS = process.platform === "win32";
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					        const tarPath = await getTarPath();
 | 
				
			||||||
 | 
					        let tarExec = tarPath;
 | 
				
			||||||
 | 
					        if (!IS_WINDOWS) {
 | 
				
			||||||
 | 
					            tarExec = "sudo";
 | 
				
			||||||
 | 
					            args = [`${tarPath}`, ...args];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        await exec(`"${tarExec}"`, args);
 | 
				
			||||||
 | 
					    } catch (error) {
 | 
				
			||||||
        if (IS_WINDOWS) {
 | 
					        if (IS_WINDOWS) {
 | 
				
			||||||
            throw new Error(
 | 
					            throw new Error(
 | 
				
			||||||
                `Tar failed with error: ${error?.message}. Ensure BSD tar is installed and on the PATH.`
 | 
					                `Tar failed with error: ${error?.message}. Ensure BSD tar is installed and on the PATH.`
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user