From 2c3e55b8c924a48530e7595e8e3c18b6e21536f4 Mon Sep 17 00:00:00 2001 From: Alif Rachmawadi Date: Wed, 14 Aug 2019 01:26:14 +0700 Subject: [PATCH] Add more supports for tar extensions (#48) * added test for extracting .tar.gz * added ability to extract .tar.xz * add flags to extract tar * make use of tempPath * check file contents and make different content for tar file --- .../tool-cache/__tests__/data/test.tar.gz | Bin 0 -> 281 bytes .../tool-cache/__tests__/data/test.tar.xz | Bin 0 -> 10240 bytes .../tool-cache/__tests__/tool-cache.test.ts | 56 ++++++++++++++++++ packages/tool-cache/src/tool-cache.ts | 9 ++- 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 packages/tool-cache/__tests__/data/test.tar.gz create mode 100644 packages/tool-cache/__tests__/data/test.tar.xz 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 0000000000000000000000000000000000000000..9ba362f6234fad6550b31467c4da52b32f3f149b GIT binary patch literal 281 zcmV+!0p|W6iwFP!000001MSw)ZG$ir1yGOT1t5O5AD)L2M?s1}!j9UFI!K1;8nxS` z^rHzsF>Ta)gv19(Sh_s>)*DfrN*r^(lVkdRzYt_t+jNn#LJ={skX(LgLL5VECn-fZ zK8>M|Rt=D<)7z2r~g(aeE8Eo=f7;=i}SDN`8PW6|AyFM{?~9xUv}dV{Sd=%F9=qZ z=6^hF{Zz6Be)PZ8@ox9O^ZdJf{%gk?{cjCl`42uszpYA~2Fv}=&HXR`Cu#R5i~WBM fKc|n=000000000000000;8DE;vO4YL04M+eM2v~K literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..5993e1fd9d694a620c13f2c95272eadf4369f9ca GIT binary patch literal 10240 zcmeIz+X{j}5C-7A?kV;OboMX`y^T^)AS`&G9>3kp5W(tZBI@6pMOPj9W`E6cB|a#Q zl3B&clx*g6T1rD1$JTSR+I?SsKFbdw z2=9T^;5Ym4%93LLtur3||HRfA`+wXwP00F7FH$n~zwmyZ`{_H7mWb>l|M&iT!=yBc zyv^dFf1dx8{#*CA`W)J3x6r}ij|*gZUA1D#AMsMepXQ(Z^#8ZaMg0Cda`FF<{L62? zI)sgU6;yx#1Rwwb2tWV=5P$##AOHafKmY;|fB*y_009U<00Izz00bZa0cn9N;>lkc literal 0 HcmV?d00001 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 }