Add end to end test for cache using bsd on windows

and address review comments
This commit is contained in:
Sampark Sharma
2022-12-12 06:53:11 +00:00
committed by GitHub
parent 0690c10515
commit d175a181a0
3 changed files with 182 additions and 59 deletions

View File

@@ -73,7 +73,9 @@ test('zstd extract tar', async () => {
'--use-compress-program',
IS_WINDOWS ? '"zstd -d --long=30"' : 'unzstd --long=30'
])
.join(' ')
.join(' '),
undefined,
{cwd: undefined}
)
})
@@ -92,22 +94,31 @@ test('zstd extract tar with windows BSDtar', async () => {
await tar.extractTar(archivePath, CompressionMethod.Zstd)
expect(mkdirMock).toHaveBeenCalledWith(workspace)
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
expect(execMock).toHaveBeenCalledTimes(2)
expect(execMock).toHaveBeenNthCalledWith(
1,
[
'cmd /c "',
'zstd -d --long=30 -o',
TarFilename.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
'&&',
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/')
].join(' '),
undefined,
{cwd: undefined}
)
expect(execMock).toHaveBeenNthCalledWith(
2,
[
`"${tarPath}"`,
'-xf',
TarFilename.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
'-P',
'-C',
workspace?.replace(/\\/g, '/'),
'"' // end cmd /c
].join(' ')
workspace?.replace(/\\/g, '/')
].join(' '),
undefined,
{cwd: undefined}
)
}
})
@@ -137,7 +148,9 @@ test('gzip extract tar', async () => {
.concat(IS_WINDOWS ? ['--force-local'] : [])
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
.concat(['-z'])
.join(' ')
.join(' '),
undefined,
{cwd: undefined}
)
})
@@ -164,7 +177,9 @@ test('gzip extract GNU tar on windows with GNUtar in path', async () => {
workspace?.replace(/\\/g, '/'),
'--force-local',
'-z'
].join(' ')
].join(' '),
undefined,
{cwd: undefined}
)
}
})
@@ -232,10 +247,11 @@ test('zstd create tar with windows BSDtar', async () => {
const tarPath = SystemTarPathOnWindows
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
expect(execMock).toHaveBeenCalledTimes(2)
expect(execMock).toHaveBeenNthCalledWith(
1,
[
'cmd /c "',
`"${tarPath}"`,
'--posix',
'-cf',
@@ -246,12 +262,20 @@ test('zstd create tar with windows BSDtar', async () => {
'-C',
workspace?.replace(/\\/g, '/'),
'--files-from',
ManifestFilename,
'&&',
ManifestFilename
].join(' '),
undefined, // args
{
cwd: archiveFolder
}
)
expect(execMock).toHaveBeenNthCalledWith(
2,
[
'zstd -T0 --long=30 -o',
CacheFilename.Zstd.replace(/\\/g, '/'),
TarFilename.replace(/\\/g, '/'),
'"' // end cmd /c
TarFilename.replace(/\\/g, '/')
].join(' '),
undefined, // args
{
@@ -324,7 +348,9 @@ test('zstd list tar', async () => {
'--use-compress-program',
IS_WINDOWS ? '"zstd -d --long=30"' : 'unzstd --long=30'
])
.join(' ')
.join(' '),
undefined,
{cwd: undefined}
)
})
@@ -340,19 +366,28 @@ test('zstd list tar with windows BSDtar', async () => {
const tarPath = SystemTarPathOnWindows
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
expect(execMock).toHaveBeenNthCalledWith(
1,
[
'cmd /c "',
'zstd -d --long=30 -o',
TarFilename.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
'&&',
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/')
].join(' '),
undefined,
{cwd: undefined}
)
expect(execMock).toHaveBeenNthCalledWith(
2,
[
`"${tarPath}"`,
'-tf',
TarFilename.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
'-P',
'"' // end cmd /c
].join(' ')
'-P'
].join(' '),
undefined,
{cwd: undefined}
)
}
})
@@ -378,7 +413,9 @@ test('zstdWithoutLong list tar', async () => {
.concat(IS_WINDOWS ? ['--force-local'] : [])
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
.concat(['--use-compress-program', IS_WINDOWS ? '"zstd -d"' : 'unzstd'])
.join(' ')
.join(' '),
undefined,
{cwd: undefined}
)
})
@@ -402,6 +439,8 @@ test('gzip list tar', async () => {
.concat(IS_WINDOWS ? ['--force-local'] : [])
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
.concat(['-z'])
.join(' ')
.join(' '),
undefined,
{cwd: undefined}
)
})

View File

@@ -122,12 +122,12 @@ async function getTarArgs(
return args
}
async function getArgs(
async function getCommands(
compressionMethod: CompressionMethod,
type: string,
archivePath = ''
): Promise<string> {
let args: string
): Promise<string[]> {
let args
const tarPath = await getTarPath()
const tarArgs = await getTarArgs(
@@ -146,16 +146,16 @@ async function getArgs(
IS_WINDOWS
if (BSD_TAR_ZSTD && type !== 'create') {
args = [...compressionArgs, ...tarArgs].join(' ')
args = [...compressionArgs, ...tarArgs]
} else {
args = [...tarArgs, ...compressionArgs].join(' ')
args = [...tarArgs, ...compressionArgs]
}
if (BSD_TAR_ZSTD) {
args = ['cmd /c "', args, '"'].join(' ')
return args
}
return args
return [args.join(' ')]
}
function getWorkingDirectory(): string {
@@ -182,8 +182,7 @@ async function getDecompressionProgram(
? [
'zstd -d --long=30 -o',
TarFilename,
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
'&&'
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/')
]
: [
'--use-compress-program',
@@ -194,8 +193,7 @@ async function getDecompressionProgram(
? [
'zstd -d -o',
TarFilename,
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
'&&'
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/')
]
: ['--use-compress-program', IS_WINDOWS ? '"zstd -d"' : 'unzstd']
default:
@@ -221,7 +219,6 @@ async function getCompressionProgram(
case CompressionMethod.Zstd:
return BSD_TAR_ZSTD
? [
'&&',
'zstd -T0 --long=30 -o',
cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
TarFilename
@@ -233,7 +230,6 @@ async function getCompressionProgram(
case CompressionMethod.ZstdWithoutLong:
return BSD_TAR_ZSTD
? [
'&&',
'zstd -T0 -o',
cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
TarFilename
@@ -244,16 +240,22 @@ async function getCompressionProgram(
}
}
async function execCommands(commands: string[], cwd?: string): Promise<void> {
for (const command of commands) {
try {
await exec(command, undefined, {cwd})
} catch (error) {
throw new Error(`${command[0]} failed with error: ${error?.message}`)
}
}
}
export async function listTar(
archivePath: string,
compressionMethod: CompressionMethod
): Promise<void> {
const args = await getArgs(compressionMethod, 'list', archivePath)
try {
await exec(args)
} catch (error) {
throw new Error(`Tar failed with error: ${error?.message}`)
}
const commands = await getCommands(compressionMethod, 'list', archivePath)
await execCommands(commands)
}
export async function extractTar(
@@ -263,12 +265,8 @@ export async function extractTar(
// Create directory to extract tar into
const workingDirectory = getWorkingDirectory()
await io.mkdirP(workingDirectory)
const args = await getArgs(compressionMethod, 'extract', archivePath)
try {
await exec(args)
} catch (error) {
throw new Error(`Tar failed with error: ${error?.message}`)
}
const commands = await getCommands(compressionMethod, 'extract', archivePath)
await execCommands(commands)
}
export async function createTar(
@@ -281,10 +279,6 @@ export async function createTar(
path.join(archiveFolder, ManifestFilename),
sourceDirectories.join('\n')
)
const args = await getArgs(compressionMethod, 'create')
try {
await exec(args, undefined, {cwd: archiveFolder})
} catch (error) {
throw new Error(`Tar failed with error: ${error?.message}`)
}
const commands = await getCommands(compressionMethod, 'create')
await execCommands(commands, archiveFolder)
}