1
0
Fork 0

add 'None' compression type and add tests

pull/1877/head
Andrew Marshall 2024-11-24 18:38:20 +00:00
parent b7a00a3203
commit 85a278dd2f
4 changed files with 130 additions and 9 deletions

View File

@ -46,6 +46,40 @@ afterAll(async () => {
await jest.requireActual('@actions/io').rmRF(getTempDir())
})
test('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']
const tarPath = IS_WINDOWS ? GnuTarPathOnWindows : defaultTarPath
await tar.extractTar(archivePath, CompressionMethod.None)
expect(mkdirMock).toHaveBeenCalledWith(workspace)
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
[
`"${tarPath}"`,
'-xf',
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
'-P',
'-C',
IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace
]
.concat(IS_WINDOWS ? ['--force-local'] : [])
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
.join(' '),
undefined,
{
cwd: undefined,
env: expect.objectContaining(defaultEnv)
}
)
})
test('zstd extract tar', async () => {
const mkdirMock = jest.spyOn(io, 'mkdirP')
const execMock = jest.spyOn(exec, 'exec')
@ -201,6 +235,45 @@ test('gzip extract GNU tar on windows with GNUtar in path', async () => {
}
})
test('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.None)
const tarPath = IS_WINDOWS ? GnuTarPathOnWindows : defaultTarPath
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
[
`"${tarPath}"`,
'--posix',
'-cf',
IS_WINDOWS ? CacheFilename.None.replace(/\\/g, '/') : CacheFilename.None,
'--exclude',
IS_WINDOWS ? CacheFilename.None.replace(/\\/g, '/') : CacheFilename.None,
'-P',
'-C',
IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace,
'--files-from',
ManifestFilename
]
.concat(IS_WINDOWS ? ['--force-local'] : [])
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
.join(' '),
undefined, // args
{
cwd: archiveFolder,
env: expect.objectContaining(defaultEnv)
}
)
})
test('zstd create tar', async () => {
const execMock = jest.spyOn(exec, 'exec')
@ -345,6 +418,35 @@ test('gzip create tar', async () => {
)
})
test('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.None)
const tarPath = IS_WINDOWS ? GnuTarPathOnWindows : defaultTarPath
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
[
`"${tarPath}"`,
'-tf',
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
'-P'
]
.concat(IS_WINDOWS ? ['--force-local'] : [])
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
.join(' '),
undefined,
{
cwd: undefined,
env: expect.objectContaining(defaultEnv)
}
)
})
test('zstd list tar', async () => {
const execMock = jest.spyOn(exec, 'exec')

View File

@ -112,9 +112,15 @@ export async function getCompressionMethod(): Promise<CompressionMethod> {
}
export function getCacheFileName(compressionMethod: CompressionMethod): string {
return compressionMethod === CompressionMethod.Gzip
? CacheFilename.Gzip
: CacheFilename.Zstd
switch (compressionMethod) {
default:
case CompressionMethod.Gzip:
return CacheFilename.Gzip
case CompressionMethod.Zstd:
return CacheFilename.Zstd
case CompressionMethod.None:
return CacheFilename.None
}
}
export async function getGnuTarPathOnWindows(): Promise<string> {

View File

@ -1,6 +1,7 @@
export enum CacheFilename {
Gzip = 'cache.tgz',
Zstd = 'cache.tzst'
Zstd = 'cache.tzst',
None = 'cache.tar'
}
export enum CompressionMethod {
@ -8,7 +9,8 @@ export enum CompressionMethod {
// Long range mode was added to zstd in v1.3.2.
// This enum is for earlier version of zstd that does not have --long support
ZstdWithoutLong = 'zstd-without-long',
Zstd = 'zstd'
Zstd = 'zstd',
None = 'none'
}
export enum ArchiveToolType {

View File

@ -5,11 +5,11 @@ import * as path from 'path'
import * as utils from './cacheUtils'
import {ArchiveTool} from './contracts'
import {
CompressionMethod,
SystemTarPathOnWindows,
ArchiveToolType,
TarFilename,
ManifestFilename
CompressionMethod,
ManifestFilename,
SystemTarPathOnWindows,
TarFilename
} from './constants'
const IS_WINDOWS = process.platform === 'win32'
@ -65,6 +65,7 @@ async function getTarArgs(
const BSD_TAR_ZSTD =
tarPath.type === ArchiveToolType.BSD &&
compressionMethod !== CompressionMethod.Gzip &&
compressionMethod !== CompressionMethod.None &&
IS_WINDOWS
// Method specific args
@ -139,10 +140,16 @@ async function getCommands(
type,
archivePath
)
if (compressionMethod === CompressionMethod.None) {
return [tarArgs.join(' ')]
}
const compressionArgs =
type !== 'create'
? await getDecompressionProgram(tarPath, compressionMethod, archivePath)
: await getCompressionProgram(tarPath, compressionMethod)
const BSD_TAR_ZSTD =
tarPath.type === ArchiveToolType.BSD &&
compressionMethod !== CompressionMethod.Gzip &&
@ -178,6 +185,7 @@ async function getDecompressionProgram(
const BSD_TAR_ZSTD =
tarPath.type === ArchiveToolType.BSD &&
compressionMethod !== CompressionMethod.Gzip &&
compressionMethod !== CompressionMethod.None &&
IS_WINDOWS
switch (compressionMethod) {
case CompressionMethod.Zstd:
@ -199,7 +207,10 @@ async function getDecompressionProgram(
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/')
]
: ['--use-compress-program', IS_WINDOWS ? '"zstd -d"' : 'unzstd']
case CompressionMethod.None:
return []
default:
case CompressionMethod.Gzip:
return ['-z']
}
}