2020-05-06 15:10:18 +00:00
|
|
|
import * as exec from '@actions/exec'
|
|
|
|
import * as io from '@actions/io'
|
|
|
|
import * as path from 'path'
|
|
|
|
import {CacheFilename, CompressionMethod} 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
|
2022-06-06 07:56:16 +00:00
|
|
|
import fs = require('fs')
|
2020-05-06 15:10:18 +00:00
|
|
|
|
|
|
|
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'
|
2020-05-06 15:10:18 +00:00
|
|
|
|
2021-02-02 19:09:10 +00:00
|
|
|
const defaultTarPath = process.platform === 'darwin' ? 'gtar' : 'tar'
|
|
|
|
|
2020-05-06 15:10:18 +00:00
|
|
|
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()
|
|
|
|
})
|
|
|
|
|
2020-05-06 15:10:18 +00:00
|
|
|
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-08 08:47:35 +00:00
|
|
|
const tarPath = IS_WINDOWS
|
|
|
|
? `${process.env['PROGRAMFILES']}\\Git\\usr\\bin\\tar.exe`
|
|
|
|
: defaultTarPath
|
2020-05-06 15:10:18 +00:00
|
|
|
|
|
|
|
await tar.extractTar(archivePath, CompressionMethod.Zstd)
|
|
|
|
|
|
|
|
expect(mkdirMock).toHaveBeenCalledWith(workspace)
|
|
|
|
expect(execMock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(execMock).toHaveBeenCalledWith(
|
2022-11-08 08:47:35 +00:00
|
|
|
`"${tarPath}"`,
|
2020-05-06 15:10:18 +00:00
|
|
|
[
|
|
|
|
'--use-compress-program',
|
2022-08-17 08:21:09 +00:00
|
|
|
IS_WINDOWS ? 'zstd -d --long=30' : 'unzstd --long=30',
|
2020-05-06 15:10:18 +00:00
|
|
|
'-xf',
|
|
|
|
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
|
|
|
|
'-P',
|
|
|
|
'-C',
|
|
|
|
IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace
|
2021-04-09 19:16:19 +00:00
|
|
|
]
|
|
|
|
.concat(IS_WINDOWS ? ['--force-local'] : [])
|
|
|
|
.concat(IS_MAC ? ['--delay-directory-restore'] : []),
|
2020-05-06 15:10:18 +00:00
|
|
|
{cwd: undefined}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
const tarPath = IS_WINDOWS
|
2022-11-08 08:47:35 +00:00
|
|
|
? `${process.env['PROGRAMFILES']}\\Git\\usr\\bin\\tar.exe`
|
2021-02-02 19:09:10 +00:00
|
|
|
: defaultTarPath
|
2020-05-06 15:10:18 +00:00
|
|
|
expect(execMock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(execMock).toHaveBeenCalledWith(
|
|
|
|
`"${tarPath}"`,
|
|
|
|
[
|
|
|
|
'-z',
|
|
|
|
'-xf',
|
|
|
|
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
|
|
|
|
'-P',
|
|
|
|
'-C',
|
|
|
|
IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace
|
2022-11-08 08:47:35 +00:00
|
|
|
]
|
|
|
|
.concat(IS_WINDOWS ? ['--force-local'] : [])
|
|
|
|
.concat(IS_MAC ? ['--delay-directory-restore'] : []),
|
2020-05-06 15:10:18 +00:00
|
|
|
{cwd: undefined}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-11-08 06:43:28 +00:00
|
|
|
test('gzip extract GNU tar on windows with GNUtar in path', async () => {
|
2020-05-06 15:10:18 +00:00
|
|
|
if (IS_WINDOWS) {
|
2022-11-08 06:43:28 +00:00
|
|
|
jest.spyOn(fs, 'existsSync').mockReturnValue(false)
|
2020-05-06 15:10:18 +00:00
|
|
|
|
|
|
|
const isGnuMock = jest
|
2020-05-18 20:35:13 +00:00
|
|
|
.spyOn(utils, 'isGnuTarInstalled')
|
2020-05-06 15:10:18 +00:00
|
|
|
.mockReturnValue(Promise.resolve(true))
|
|
|
|
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(isGnuMock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(execMock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(execMock).toHaveBeenCalledWith(
|
|
|
|
`"tar"`,
|
|
|
|
[
|
|
|
|
'-z',
|
|
|
|
'-xf',
|
|
|
|
archivePath.replace(/\\/g, '/'),
|
|
|
|
'-P',
|
|
|
|
'-C',
|
|
|
|
workspace?.replace(/\\/g, '/'),
|
|
|
|
'--force-local'
|
|
|
|
],
|
|
|
|
{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-08 08:47:35 +00:00
|
|
|
const tarPath = IS_WINDOWS
|
|
|
|
? `${process.env['PROGRAMFILES']}\\Git\\usr\\bin\\tar.exe`
|
|
|
|
: defaultTarPath
|
|
|
|
|
2020-05-06 15:10:18 +00:00
|
|
|
expect(execMock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(execMock).toHaveBeenCalledWith(
|
2022-11-08 08:47:35 +00:00
|
|
|
`"${tarPath}"`,
|
2020-05-06 15:10:18 +00:00
|
|
|
[
|
2020-08-05 15:17:23 +00:00
|
|
|
'--posix',
|
2020-05-06 15:10:18 +00:00
|
|
|
'--use-compress-program',
|
2022-08-17 08:21:09 +00:00
|
|
|
IS_WINDOWS ? 'zstd -T0 --long=30' : 'zstdmt --long=30',
|
2020-05-06 15:10:18 +00:00
|
|
|
'-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,
|
2020-05-06 15:10:18 +00:00
|
|
|
'-P',
|
|
|
|
'-C',
|
|
|
|
IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace,
|
|
|
|
'--files-from',
|
|
|
|
'manifest.txt'
|
2021-04-09 19:16:19 +00:00
|
|
|
]
|
|
|
|
.concat(IS_WINDOWS ? ['--force-local'] : [])
|
|
|
|
.concat(IS_MAC ? ['--delay-directory-restore'] : []),
|
2020-05-06 15:10:18 +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)
|
|
|
|
|
|
|
|
const tarPath = IS_WINDOWS
|
2022-11-08 08:47:35 +00:00
|
|
|
? `${process.env['PROGRAMFILES']}\\Git\\usr\\bin\\tar.exe`
|
2021-02-02 19:09:10 +00:00
|
|
|
: defaultTarPath
|
2020-05-06 15:10:18 +00:00
|
|
|
|
|
|
|
expect(execMock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(execMock).toHaveBeenCalledWith(
|
|
|
|
`"${tarPath}"`,
|
|
|
|
[
|
2020-08-05 15:17:23 +00:00
|
|
|
'--posix',
|
2020-05-06 15:10:18 +00:00
|
|
|
'-z',
|
|
|
|
'-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,
|
2020-05-06 15:10:18 +00:00
|
|
|
'-P',
|
|
|
|
'-C',
|
|
|
|
IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace,
|
|
|
|
'--files-from',
|
|
|
|
'manifest.txt'
|
2022-11-08 08:47:35 +00:00
|
|
|
]
|
|
|
|
.concat(IS_WINDOWS ? ['--force-local'] : [])
|
|
|
|
.concat(IS_MAC ? ['--delay-directory-restore'] : []),
|
2020-05-06 15:10:18 +00:00
|
|
|
{
|
|
|
|
cwd: archiveFolder
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-11-25 22:56:57 +00:00
|
|
|
|
|
|
|
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-08 08:47:35 +00:00
|
|
|
const tarPath = IS_WINDOWS
|
|
|
|
? `${process.env['PROGRAMFILES']}\\Git\\usr\\bin\\tar.exe`
|
|
|
|
: defaultTarPath
|
2020-11-25 22:56:57 +00:00
|
|
|
expect(execMock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(execMock).toHaveBeenCalledWith(
|
2022-11-08 08:47:35 +00:00
|
|
|
`"${tarPath}"`,
|
2020-11-25 22:56:57 +00:00
|
|
|
[
|
|
|
|
'--use-compress-program',
|
2022-08-17 08:21:09 +00:00
|
|
|
IS_WINDOWS ? 'zstd -d --long=30' : 'unzstd --long=30',
|
2020-11-25 22:56:57 +00:00
|
|
|
'-tf',
|
|
|
|
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
|
|
|
|
'-P'
|
2021-04-09 19:16:19 +00:00
|
|
|
]
|
|
|
|
.concat(IS_WINDOWS ? ['--force-local'] : [])
|
|
|
|
.concat(IS_MAC ? ['--delay-directory-restore'] : []),
|
2020-11-25 22:56:57 +00:00
|
|
|
{cwd: undefined}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
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-08 08:47:35 +00:00
|
|
|
const tarPath = IS_WINDOWS
|
|
|
|
? `${process.env['PROGRAMFILES']}\\Git\\usr\\bin\\tar.exe`
|
|
|
|
: defaultTarPath
|
2020-11-25 22:56:57 +00:00
|
|
|
expect(execMock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(execMock).toHaveBeenCalledWith(
|
2022-11-08 08:47:35 +00:00
|
|
|
`"${tarPath}"`,
|
2020-11-25 22:56:57 +00:00
|
|
|
[
|
|
|
|
'--use-compress-program',
|
2022-08-17 08:21:09 +00:00
|
|
|
IS_WINDOWS ? 'zstd -d' : 'unzstd',
|
2020-11-25 22:56:57 +00:00
|
|
|
'-tf',
|
|
|
|
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
|
|
|
|
'-P'
|
2021-04-09 19:16:19 +00:00
|
|
|
]
|
|
|
|
.concat(IS_WINDOWS ? ['--force-local'] : [])
|
|
|
|
.concat(IS_MAC ? ['--delay-directory-restore'] : []),
|
2020-11-25 22:56:57 +00:00
|
|
|
{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)
|
|
|
|
|
|
|
|
const tarPath = IS_WINDOWS
|
2022-11-08 08:47:35 +00:00
|
|
|
? `${process.env['PROGRAMFILES']}\\Git\\usr\\bin\\tar.exe`
|
2021-02-02 19:09:10 +00:00
|
|
|
: defaultTarPath
|
2020-11-25 22:56:57 +00:00
|
|
|
expect(execMock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(execMock).toHaveBeenCalledWith(
|
|
|
|
`"${tarPath}"`,
|
|
|
|
[
|
|
|
|
'-z',
|
|
|
|
'-tf',
|
|
|
|
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
|
|
|
|
'-P'
|
2022-11-08 08:47:35 +00:00
|
|
|
]
|
|
|
|
.concat(IS_WINDOWS ? ['--force-local'] : [])
|
|
|
|
.concat(IS_MAC ? ['--delay-directory-restore'] : []),
|
2020-11-25 22:56:57 +00:00
|
|
|
{cwd: undefined}
|
|
|
|
)
|
|
|
|
})
|