1
0
Fork 0
toolkit/packages/cache/__tests__/tar.test.ts

447 lines
12 KiB
TypeScript
Raw Normal View History

import * as exec from '@actions/exec'
import * as io from '@actions/io'
import * as path from 'path'
2022-11-15 08:41:04 +00:00
import {
CacheFilename,
CompressionMethod,
2022-11-21 12:51:07 +00:00
GnuTarPathOnWindows,
2022-11-29 10:35:22 +00:00
ManifestFilename,
SystemTarPathOnWindows,
TarFilename
2022-11-15 08:41:04 +00:00
} from '../src/internal/constants'
import * as tar from '../src/internal/tar'
import * as utils from '../src/internal/cacheUtils'
// eslint-disable-next-line @typescript-eslint/no-require-imports
import fs = require('fs')
jest.mock('@actions/exec')
jest.mock('@actions/io')
const IS_WINDOWS = process.platform === 'win32'
2021-04-09 19:10:36 +00:00
const IS_MAC = process.platform === 'darwin'
2022-11-23 10:44:38 +00:00
const defaultTarPath = IS_MAC ? 'gtar' : 'tar'
function getTempDir(): string {
return path.join(__dirname, '_temp', 'tar')
}
beforeAll(async () => {
jest.spyOn(io, 'which').mockImplementation(async tool => {
return tool
})
process.env['GITHUB_WORKSPACE'] = process.cwd()
await jest.requireActual('@actions/io').rmRF(getTempDir())
})
2022-11-08 08:47:35 +00:00
beforeEach(async () => {
jest.restoreAllMocks()
})
afterAll(async () => {
delete process.env['GITHUB_WORKSPACE']
await jest.requireActual('@actions/io').rmRF(getTempDir())
})
test('zstd extract tar', async () => {
const mkdirMock = jest.spyOn(io, 'mkdirP')
const execMock = jest.spyOn(exec, 'exec')
const archivePath = IS_WINDOWS
? `${process.env['windir']}\\fakepath\\cache.tar`
: 'cache.tar'
const workspace = process.env['GITHUB_WORKSPACE']
2022-11-15 08:41:04 +00:00
const tarPath = IS_WINDOWS ? GnuTarPathOnWindows : defaultTarPath
await tar.extractTar(archivePath, CompressionMethod.Zstd)
expect(mkdirMock).toHaveBeenCalledWith(workspace)
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
[
2022-11-29 10:35:22 +00:00
`"${tarPath}"`,
'-xf',
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
'-P',
'-C',
2022-11-23 07:58:46 +00:00
IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace
2021-04-09 19:16:19 +00:00
]
.concat(IS_WINDOWS ? ['--force-local'] : [])
2022-11-23 07:58:46 +00:00
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
.concat([
'--use-compress-program',
2022-11-30 10:11:07 +00:00
IS_WINDOWS ? '"zstd -d --long=30"' : 'unzstd --long=30'
2022-11-29 10:35:22 +00:00
])
.join(' '),
undefined,
{cwd: undefined}
)
})
2022-11-23 10:44:38 +00:00
test('zstd extract tar with windows BSDtar', async () => {
if (IS_WINDOWS) {
const mkdirMock = jest.spyOn(io, 'mkdirP')
const execMock = jest.spyOn(exec, 'exec')
jest
.spyOn(utils, 'getGnuTarPathOnWindows')
.mockReturnValue(Promise.resolve(''))
const archivePath = `${process.env['windir']}\\fakepath\\cache.tar`
const workspace = process.env['GITHUB_WORKSPACE']
const tarPath = SystemTarPathOnWindows
await tar.extractTar(archivePath, CompressionMethod.Zstd)
expect(mkdirMock).toHaveBeenCalledWith(workspace)
expect(execMock).toHaveBeenCalledTimes(2)
expect(execMock).toHaveBeenNthCalledWith(
1,
2022-11-23 10:44:38 +00:00
[
2022-12-12 07:51:54 +00:00
'zstd -d --long=30 --force -o',
2022-11-29 10:35:22 +00:00
TarFilename.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/')
].join(' '),
undefined,
{cwd: undefined}
)
expect(execMock).toHaveBeenNthCalledWith(
2,
[
2022-11-30 06:05:34 +00:00
`"${tarPath}"`,
2022-11-23 10:44:38 +00:00
'-xf',
2022-11-29 10:35:22 +00:00
TarFilename.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
2022-11-23 10:44:38 +00:00
'-P',
'-C',
workspace?.replace(/\\/g, '/')
].join(' '),
undefined,
{cwd: undefined}
2022-11-23 10:44:38 +00:00
)
}
})
test('gzip extract tar', async () => {
const mkdirMock = jest.spyOn(io, 'mkdirP')
const execMock = jest.spyOn(exec, 'exec')
const archivePath = IS_WINDOWS
? `${process.env['windir']}\\fakepath\\cache.tar`
: 'cache.tar'
const workspace = process.env['GITHUB_WORKSPACE']
await tar.extractTar(archivePath, CompressionMethod.Gzip)
expect(mkdirMock).toHaveBeenCalledWith(workspace)
2022-11-15 08:41:04 +00:00
const tarPath = IS_WINDOWS ? GnuTarPathOnWindows : defaultTarPath
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
[
2022-11-29 10:35:22 +00:00
`"${tarPath}"`,
'-xf',
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
'-P',
'-C',
2022-11-23 07:58:46 +00:00
IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace
2022-11-08 08:47:35 +00:00
]
.concat(IS_WINDOWS ? ['--force-local'] : [])
2022-11-23 07:58:46 +00:00
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
2022-11-29 10:35:22 +00:00
.concat(['-z'])
.join(' '),
undefined,
{cwd: undefined}
)
})
2022-11-08 06:43:28 +00:00
test('gzip extract GNU tar on windows with GNUtar in path', async () => {
if (IS_WINDOWS) {
2022-11-15 08:41:04 +00:00
// GNU tar present in path but not at default location
2022-11-23 10:44:38 +00:00
jest
2022-11-15 08:41:04 +00:00
.spyOn(utils, 'getGnuTarPathOnWindows')
2022-11-15 08:48:29 +00:00
.mockReturnValue(Promise.resolve('tar'))
const execMock = jest.spyOn(exec, 'exec')
const archivePath = `${process.env['windir']}\\fakepath\\cache.tar`
const workspace = process.env['GITHUB_WORKSPACE']
await tar.extractTar(archivePath, CompressionMethod.Gzip)
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
[
2022-11-29 10:35:22 +00:00
`"tar"`,
'-xf',
archivePath.replace(/\\/g, '/'),
'-P',
'-C',
workspace?.replace(/\\/g, '/'),
2022-11-23 07:58:46 +00:00
'--force-local',
'-z'
].join(' '),
undefined,
{cwd: undefined}
)
}
})
test('zstd create tar', async () => {
const execMock = jest.spyOn(exec, 'exec')
const archiveFolder = getTempDir()
const workspace = process.env['GITHUB_WORKSPACE']
const sourceDirectories = ['~/.npm/cache', `${workspace}/dist`]
await fs.promises.mkdir(archiveFolder, {recursive: true})
await tar.createTar(archiveFolder, sourceDirectories, CompressionMethod.Zstd)
2022-11-15 08:41:04 +00:00
const tarPath = IS_WINDOWS ? GnuTarPathOnWindows : defaultTarPath
2022-11-08 08:47:35 +00:00
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
[
2022-11-29 10:35:22 +00:00
`"${tarPath}"`,
'--posix',
'-cf',
IS_WINDOWS ? CacheFilename.Zstd.replace(/\\/g, '/') : CacheFilename.Zstd,
2022-06-06 08:03:56 +00:00
'--exclude',
IS_WINDOWS ? CacheFilename.Zstd.replace(/\\/g, '/') : CacheFilename.Zstd,
'-P',
'-C',
IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace,
'--files-from',
2022-11-29 10:35:22 +00:00
ManifestFilename
2021-04-09 19:16:19 +00:00
]
.concat(IS_WINDOWS ? ['--force-local'] : [])
2022-11-23 07:58:46 +00:00
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
.concat([
'--use-compress-program',
2022-11-30 10:11:07 +00:00
IS_WINDOWS ? '"zstd -T0 --long=30"' : 'zstdmt --long=30'
2022-11-29 10:35:22 +00:00
])
.join(' '),
2022-11-29 12:16:17 +00:00
undefined, // args
{
cwd: archiveFolder
}
)
})
2022-11-21 12:19:44 +00:00
test('zstd create tar with windows BSDtar', async () => {
if (IS_WINDOWS) {
const execMock = jest.spyOn(exec, 'exec')
2022-11-23 10:44:38 +00:00
jest
2022-11-21 12:19:44 +00:00
.spyOn(utils, 'getGnuTarPathOnWindows')
.mockReturnValue(Promise.resolve(''))
const archiveFolder = getTempDir()
const workspace = process.env['GITHUB_WORKSPACE']
const sourceDirectories = ['~/.npm/cache', `${workspace}/dist`]
await fs.promises.mkdir(archiveFolder, {recursive: true})
2022-11-23 07:39:15 +00:00
await tar.createTar(
archiveFolder,
sourceDirectories,
CompressionMethod.Zstd
)
2022-11-21 12:19:44 +00:00
2022-11-21 12:51:07 +00:00
const tarPath = SystemTarPathOnWindows
2022-11-21 12:19:44 +00:00
expect(execMock).toHaveBeenCalledTimes(2)
expect(execMock).toHaveBeenNthCalledWith(
1,
2022-11-21 12:19:44 +00:00
[
2022-11-29 10:35:22 +00:00
`"${tarPath}"`,
2022-11-21 12:19:44 +00:00
'--posix',
'-cf',
2022-11-29 10:35:22 +00:00
TarFilename.replace(/\\/g, '/'),
2022-11-21 12:19:44 +00:00
'--exclude',
2022-11-29 10:35:22 +00:00
TarFilename.replace(/\\/g, '/'),
2022-11-21 12:19:44 +00:00
'-P',
'-C',
2022-11-21 12:51:07 +00:00
workspace?.replace(/\\/g, '/'),
2022-11-21 12:19:44 +00:00
'--files-from',
ManifestFilename
].join(' '),
undefined, // args
{
cwd: archiveFolder
}
)
expect(execMock).toHaveBeenNthCalledWith(
2,
[
2022-12-12 07:51:54 +00:00
'zstd -T0 --long=30 --force -o',
2022-11-23 07:39:15 +00:00
CacheFilename.Zstd.replace(/\\/g, '/'),
TarFilename.replace(/\\/g, '/')
2022-11-29 10:35:22 +00:00
].join(' '),
2022-11-29 12:16:17 +00:00
undefined, // args
2022-11-21 12:19:44 +00:00
{
cwd: archiveFolder
}
)
}
})
test('gzip create tar', async () => {
const execMock = jest.spyOn(exec, 'exec')
const archiveFolder = getTempDir()
const workspace = process.env['GITHUB_WORKSPACE']
const sourceDirectories = ['~/.npm/cache', `${workspace}/dist`]
await fs.promises.mkdir(archiveFolder, {recursive: true})
await tar.createTar(archiveFolder, sourceDirectories, CompressionMethod.Gzip)
2022-11-15 08:41:04 +00:00
const tarPath = IS_WINDOWS ? GnuTarPathOnWindows : defaultTarPath
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
[
2022-11-29 12:16:17 +00:00
`"${tarPath}"`,
'--posix',
'-cf',
IS_WINDOWS ? CacheFilename.Gzip.replace(/\\/g, '/') : CacheFilename.Gzip,
2022-06-06 08:03:56 +00:00
'--exclude',
IS_WINDOWS ? CacheFilename.Gzip.replace(/\\/g, '/') : CacheFilename.Gzip,
'-P',
'-C',
IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace,
'--files-from',
2022-11-29 10:35:22 +00:00
ManifestFilename
2022-11-08 08:47:35 +00:00
]
.concat(IS_WINDOWS ? ['--force-local'] : [])
2022-11-23 07:58:46 +00:00
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
2022-11-29 10:35:22 +00:00
.concat(['-z'])
.join(' '),
2022-11-29 12:16:17 +00:00
undefined, // args
{
cwd: archiveFolder
}
)
})
test('zstd list tar', async () => {
const execMock = jest.spyOn(exec, 'exec')
const archivePath = IS_WINDOWS
? `${process.env['windir']}\\fakepath\\cache.tar`
: 'cache.tar'
await tar.listTar(archivePath, CompressionMethod.Zstd)
2022-11-15 08:41:04 +00:00
const tarPath = IS_WINDOWS ? GnuTarPathOnWindows : defaultTarPath
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
2022-11-29 12:16:17 +00:00
[
`"${tarPath}"`,
'-tf',
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
'-P'
]
2021-04-09 19:16:19 +00:00
.concat(IS_WINDOWS ? ['--force-local'] : [])
2022-11-23 07:58:46 +00:00
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
.concat([
'--use-compress-program',
2022-11-30 10:11:07 +00:00
IS_WINDOWS ? '"zstd -d --long=30"' : 'unzstd --long=30'
2022-11-29 10:35:22 +00:00
])
.join(' '),
undefined,
{cwd: undefined}
)
})
2022-11-23 10:44:38 +00:00
test('zstd list tar with windows BSDtar', async () => {
if (IS_WINDOWS) {
const execMock = jest.spyOn(exec, 'exec')
jest
.spyOn(utils, 'getGnuTarPathOnWindows')
.mockReturnValue(Promise.resolve(''))
const archivePath = `${process.env['windir']}\\fakepath\\cache.tar`
await tar.listTar(archivePath, CompressionMethod.Zstd)
const tarPath = SystemTarPathOnWindows
2022-12-12 07:25:36 +00:00
expect(execMock).toHaveBeenCalledTimes(2)
expect(execMock).toHaveBeenNthCalledWith(
1,
2022-11-23 10:44:38 +00:00
[
2022-12-12 07:51:54 +00:00
'zstd -d --long=30 --force -o',
2022-11-29 10:35:22 +00:00
TarFilename.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/')
].join(' '),
undefined,
{cwd: undefined}
)
expect(execMock).toHaveBeenNthCalledWith(
2,
[
2022-11-30 06:05:34 +00:00
`"${tarPath}"`,
2022-11-23 10:44:38 +00:00
'-tf',
2022-11-29 10:35:22 +00:00
TarFilename.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
'-P'
].join(' '),
undefined,
{cwd: undefined}
2022-11-23 10:44:38 +00:00
)
}
})
test('zstdWithoutLong list tar', async () => {
const execMock = jest.spyOn(exec, 'exec')
const archivePath = IS_WINDOWS
? `${process.env['windir']}\\fakepath\\cache.tar`
: 'cache.tar'
await tar.listTar(archivePath, CompressionMethod.ZstdWithoutLong)
2022-11-15 08:41:04 +00:00
const tarPath = IS_WINDOWS ? GnuTarPathOnWindows : defaultTarPath
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
2022-11-29 12:16:17 +00:00
[
`"${tarPath}"`,
'-tf',
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
'-P'
]
2021-04-09 19:16:19 +00:00
.concat(IS_WINDOWS ? ['--force-local'] : [])
2022-11-23 07:58:46 +00:00
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
2022-11-30 10:11:07 +00:00
.concat(['--use-compress-program', IS_WINDOWS ? '"zstd -d"' : 'unzstd'])
.join(' '),
undefined,
{cwd: undefined}
)
})
test('gzip list tar', async () => {
const execMock = jest.spyOn(exec, 'exec')
const archivePath = IS_WINDOWS
? `${process.env['windir']}\\fakepath\\cache.tar`
: 'cache.tar'
await tar.listTar(archivePath, CompressionMethod.Gzip)
2022-11-15 08:41:04 +00:00
const tarPath = IS_WINDOWS ? GnuTarPathOnWindows : defaultTarPath
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
2022-11-29 12:16:17 +00:00
[
`"${tarPath}"`,
'-tf',
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
'-P'
]
2022-11-08 08:47:35 +00:00
.concat(IS_WINDOWS ? ['--force-local'] : [])
2022-11-23 07:58:46 +00:00
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
2022-11-29 10:35:22 +00:00
.concat(['-z'])
.join(' '),
undefined,
{cwd: undefined}
)
})