diff --git a/packages/tool-cache/__tests__/data/test.tar.gz b/packages/tool-cache/__tests__/data/test.tar.gz new file mode 100644 index 00000000..9ba362f6 Binary files /dev/null and b/packages/tool-cache/__tests__/data/test.tar.gz differ diff --git a/packages/tool-cache/__tests__/data/test.tar.xz b/packages/tool-cache/__tests__/data/test.tar.xz new file mode 100644 index 00000000..5993e1fd Binary files /dev/null and b/packages/tool-cache/__tests__/data/test.tar.xz differ diff --git a/packages/tool-cache/__tests__/tool-cache.test.ts b/packages/tool-cache/__tests__/tool-cache.test.ts index 59315e5c..35f90be1 100644 --- a/packages/tool-cache/__tests__/tool-cache.test.ts +++ b/packages/tool-cache/__tests__/tool-cache.test.ts @@ -197,6 +197,62 @@ describe('@actions/tool-cache', function() { await io.rmRF(tempDir) } }) + } else { + it('extract .tar.gz', async () => { + const tempDir = path.join(tempPath, 'test-install-tar.gz') + + await io.mkdirP(tempDir) + + // copy the .tar.gz file to the test dir + const _tgzFile: string = path.join(tempDir, 'test.tar.gz') + await io.cp(path.join(__dirname, 'data', 'test.tar.gz'), _tgzFile) + + // extract/cache + const extPath: string = await tc.extractTar(_tgzFile) + await tc.cacheDir(extPath, 'my-tgz-contents', '1.1.0') + const toolPath: string = tc.find('my-tgz-contents', '1.1.0') + + expect(fs.existsSync(toolPath)).toBeTruthy() + expect(fs.existsSync(`${toolPath}.complete`)).toBeTruthy() + expect(fs.existsSync(path.join(toolPath, 'file.txt'))).toBeTruthy() + expect( + fs.existsSync(path.join(toolPath, 'file-with-รง-character.txt')) + ).toBeTruthy() + expect( + fs.existsSync(path.join(toolPath, 'folder', 'nested-file.txt')) + ).toBeTruthy() + expect( + fs.readFileSync( + path.join(toolPath, 'folder', 'nested-file.txt'), + 'utf8' + ) + ).toBe('folder/nested-file.txt contents') + }) + + it('extract .tar.xz', async () => { + const tempDir = path.join(tempPath, 'test-install-tar.xz') + + await io.mkdirP(tempDir) + + // copy the .tar.gz file to the test dir + const _txzFile: string = path.join(tempDir, 'test.tar.xz') + await io.cp(path.join(__dirname, 'data', 'test.tar.xz'), _txzFile) + + // extract/cache + const extPath: string = await tc.extractTar(_txzFile, undefined, 'x') + await tc.cacheDir(extPath, 'my-txz-contents', '1.1.0') + const toolPath: string = tc.find('my-txz-contents', '1.1.0') + + expect(fs.existsSync(toolPath)).toBeTruthy() + expect(fs.existsSync(`${toolPath}.complete`)).toBeTruthy() + expect(fs.existsSync(path.join(toolPath, 'bar.txt'))).toBeTruthy() + expect( + fs.existsSync(path.join(toolPath, 'foo', 'hello.txt')) + ).toBeTruthy() + expect( + fs.readFileSync(path.join(toolPath, 'foo', 'hello.txt'), 'utf8') + ).toBe('foo/hello: world') + }) } it('installs a zip and finds it', async () => { diff --git a/packages/tool-cache/src/tool-cache.ts b/packages/tool-cache/src/tool-cache.ts index c4c71f1c..0dd556c3 100644 --- a/packages/tool-cache/src/tool-cache.ts +++ b/packages/tool-cache/src/tool-cache.ts @@ -187,16 +187,21 @@ export async function extract7z( * * @param file path to the tar * @param dest destination directory. Optional. + * @param flags flags for the tar. Optional. * @returns path to the destination directory */ -export async function extractTar(file: string, dest?: string): Promise { +export async function extractTar( + file: string, + dest?: string, + flags: string = 'xz' +): Promise { if (!file) { throw new Error("parameter 'file' is required") } dest = dest || (await _createExtractFolder(dest)) const tarPath: string = await io.which('tar', true) - await exec(`"${tarPath}"`, ['xzC', dest, '-f', file]) + await exec(`"${tarPath}"`, [flags, '-C', dest, '-f', file]) return dest }