1
0
Fork 0

Use GNU tar on macOS if available (#701)

* use GNU tar on macOS

* remove unnecessary code

* formatting fix

* fix tests

* fix more tests

* typo fix

* fix test
pull/705/head
Benedek Kozma 2021-02-02 20:09:10 +01:00 committed by GitHub
parent 825204968b
commit ccd1dd298f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 22 deletions

View File

@ -12,6 +12,8 @@ jest.mock('@actions/io')
const IS_WINDOWS = process.platform === 'win32' const IS_WINDOWS = process.platform === 'win32'
const defaultTarPath = process.platform === 'darwin' ? 'gtar' : 'tar'
function getTempDir(): string { function getTempDir(): string {
return path.join(__dirname, '_temp', 'tar') return path.join(__dirname, '_temp', 'tar')
} }
@ -38,14 +40,13 @@ test('zstd extract tar', async () => {
? `${process.env['windir']}\\fakepath\\cache.tar` ? `${process.env['windir']}\\fakepath\\cache.tar`
: 'cache.tar' : 'cache.tar'
const workspace = process.env['GITHUB_WORKSPACE'] const workspace = process.env['GITHUB_WORKSPACE']
const tarPath = 'tar'
await tar.extractTar(archivePath, CompressionMethod.Zstd) await tar.extractTar(archivePath, CompressionMethod.Zstd)
expect(mkdirMock).toHaveBeenCalledWith(workspace) expect(mkdirMock).toHaveBeenCalledWith(workspace)
expect(execMock).toHaveBeenCalledTimes(1) expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith( expect(execMock).toHaveBeenCalledWith(
`"${tarPath}"`, `"${defaultTarPath}"`,
[ [
'--use-compress-program', '--use-compress-program',
'zstd -d --long=30', 'zstd -d --long=30',
@ -72,7 +73,7 @@ test('gzip extract tar', async () => {
expect(mkdirMock).toHaveBeenCalledWith(workspace) expect(mkdirMock).toHaveBeenCalledWith(workspace)
const tarPath = IS_WINDOWS const tarPath = IS_WINDOWS
? `${process.env['windir']}\\System32\\tar.exe` ? `${process.env['windir']}\\System32\\tar.exe`
: 'tar' : defaultTarPath
expect(execMock).toHaveBeenCalledTimes(1) expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith( expect(execMock).toHaveBeenCalledWith(
`"${tarPath}"`, `"${tarPath}"`,
@ -125,7 +126,6 @@ test('zstd create tar', async () => {
const archiveFolder = getTempDir() const archiveFolder = getTempDir()
const workspace = process.env['GITHUB_WORKSPACE'] const workspace = process.env['GITHUB_WORKSPACE']
const sourceDirectories = ['~/.npm/cache', `${workspace}/dist`] const sourceDirectories = ['~/.npm/cache', `${workspace}/dist`]
const tarPath = 'tar'
await fs.promises.mkdir(archiveFolder, {recursive: true}) await fs.promises.mkdir(archiveFolder, {recursive: true})
@ -133,7 +133,7 @@ test('zstd create tar', async () => {
expect(execMock).toHaveBeenCalledTimes(1) expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith( expect(execMock).toHaveBeenCalledWith(
`"${tarPath}"`, `"${defaultTarPath}"`,
[ [
'--posix', '--posix',
'--use-compress-program', '--use-compress-program',
@ -165,7 +165,7 @@ test('gzip create tar', async () => {
const tarPath = IS_WINDOWS const tarPath = IS_WINDOWS
? `${process.env['windir']}\\System32\\tar.exe` ? `${process.env['windir']}\\System32\\tar.exe`
: 'tar' : defaultTarPath
expect(execMock).toHaveBeenCalledTimes(1) expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith( expect(execMock).toHaveBeenCalledWith(
@ -193,13 +193,12 @@ test('zstd list tar', async () => {
const archivePath = IS_WINDOWS const archivePath = IS_WINDOWS
? `${process.env['windir']}\\fakepath\\cache.tar` ? `${process.env['windir']}\\fakepath\\cache.tar`
: 'cache.tar' : 'cache.tar'
const tarPath = 'tar'
await tar.listTar(archivePath, CompressionMethod.Zstd) await tar.listTar(archivePath, CompressionMethod.Zstd)
expect(execMock).toHaveBeenCalledTimes(1) expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith( expect(execMock).toHaveBeenCalledWith(
`"${tarPath}"`, `"${defaultTarPath}"`,
[ [
'--use-compress-program', '--use-compress-program',
'zstd -d --long=30', 'zstd -d --long=30',
@ -217,13 +216,12 @@ test('zstdWithoutLong list tar', async () => {
const archivePath = IS_WINDOWS const archivePath = IS_WINDOWS
? `${process.env['windir']}\\fakepath\\cache.tar` ? `${process.env['windir']}\\fakepath\\cache.tar`
: 'cache.tar' : 'cache.tar'
const tarPath = 'tar'
await tar.listTar(archivePath, CompressionMethod.ZstdWithoutLong) await tar.listTar(archivePath, CompressionMethod.ZstdWithoutLong)
expect(execMock).toHaveBeenCalledTimes(1) expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith( expect(execMock).toHaveBeenCalledWith(
`"${tarPath}"`, `"${defaultTarPath}"`,
[ [
'--use-compress-program', '--use-compress-program',
'zstd -d', 'zstd -d',
@ -245,7 +243,7 @@ test('gzip list tar', async () => {
const tarPath = IS_WINDOWS const tarPath = IS_WINDOWS
? `${process.env['windir']}\\System32\\tar.exe` ? `${process.env['windir']}\\System32\\tar.exe`
: 'tar' : defaultTarPath
expect(execMock).toHaveBeenCalledTimes(1) expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith( expect(execMock).toHaveBeenCalledWith(
`"${tarPath}"`, `"${tarPath}"`,

View File

@ -9,18 +9,29 @@ async function getTarPath(
args: string[], args: string[],
compressionMethod: CompressionMethod compressionMethod: CompressionMethod
): Promise<string> { ): Promise<string> {
const IS_WINDOWS = process.platform === 'win32' switch (process.platform) {
if (IS_WINDOWS) { case 'win32': {
const systemTar = `${process.env['windir']}\\System32\\tar.exe` const systemTar = `${process.env['windir']}\\System32\\tar.exe`
if (compressionMethod !== CompressionMethod.Gzip) { if (compressionMethod !== CompressionMethod.Gzip) {
// We only use zstandard compression on windows when gnu tar is installed due to // We only use zstandard compression on windows when gnu tar is installed due to
// a bug with compressing large files with bsdtar + zstd // a bug with compressing large files with bsdtar + zstd
args.push('--force-local') args.push('--force-local')
} else if (existsSync(systemTar)) { } else if (existsSync(systemTar)) {
return systemTar return systemTar
} else if (await utils.isGnuTarInstalled()) { } else if (await utils.isGnuTarInstalled()) {
args.push('--force-local') args.push('--force-local')
}
break
} }
case 'darwin': {
const gnuTar = await io.which('gtar', false)
if (gnuTar) {
return gnuTar
}
break
}
default:
break
} }
return await io.which('tar', true) return await io.which('tar', true)
} }