Skip to content

Commit

Permalink
Make the path be handled politely during path resolution
Browse files Browse the repository at this point in the history
Signed-off-by: Sora Morimoto <[email protected]>
  • Loading branch information
smorimoto committed Mar 10, 2021
1 parent 383ec9f commit a310ef0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 38 deletions.
36 changes: 18 additions & 18 deletions packages/cache/__tests__/tar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ test('zstd extract tar', async () => {
'--use-compress-program',
'zstd -d --long=30',
'-xf',
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
path.normalize(archivePath),
'-P',
'-C',
IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
path.normalize(workspace!)
].concat(IS_WINDOWS ? ['--force-local'] : []),
{cwd: undefined}
)
Expand All @@ -80,10 +81,11 @@ test('gzip extract tar', async () => {
[
'-z',
'-xf',
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
path.normalize(archivePath),
'-P',
'-C',
IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
path.normalize(workspace!)
],
{cwd: undefined}
)
Expand All @@ -109,10 +111,11 @@ test('gzip extract GNU tar on windows', async () => {
[
'-z',
'-xf',
archivePath.replace(/\\/g, '/'),
path.normalize(archivePath),
'-P',
'-C',
workspace?.replace(/\\/g, '/'),
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
path.normalize(workspace!),
'--force-local'
],
{cwd: undefined}
Expand All @@ -139,10 +142,11 @@ test('zstd create tar', async () => {
'--use-compress-program',
'zstd -T0 --long=30',
'-cf',
IS_WINDOWS ? CacheFilename.Zstd.replace(/\\/g, '/') : CacheFilename.Zstd,
path.normalize(CacheFilename.Zstd),
'-P',
'-C',
IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
path.normalize(workspace!),
'--files-from',
'manifest.txt'
].concat(IS_WINDOWS ? ['--force-local'] : []),
Expand Down Expand Up @@ -174,10 +178,11 @@ test('gzip create tar', async () => {
'--posix',
'-z',
'-cf',
IS_WINDOWS ? CacheFilename.Gzip.replace(/\\/g, '/') : CacheFilename.Gzip,
path.normalize(CacheFilename.Gzip),
'-P',
'-C',
IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
path.normalize(workspace!),
'--files-from',
'manifest.txt'
],
Expand All @@ -203,7 +208,7 @@ test('zstd list tar', async () => {
'--use-compress-program',
'zstd -d --long=30',
'-tf',
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
path.normalize(archivePath),
'-P'
].concat(IS_WINDOWS ? ['--force-local'] : []),
{cwd: undefined}
Expand All @@ -226,7 +231,7 @@ test('zstdWithoutLong list tar', async () => {
'--use-compress-program',
'zstd -d',
'-tf',
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
path.normalize(archivePath),
'-P'
].concat(IS_WINDOWS ? ['--force-local'] : []),
{cwd: undefined}
Expand All @@ -247,12 +252,7 @@ test('gzip list tar', async () => {
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
`"${tarPath}"`,
[
'-z',
'-tf',
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
'-P'
],
['-z', '-tf', path.normalize(archivePath), '-P'],
{cwd: undefined}
)
})
8 changes: 3 additions & 5 deletions packages/cache/src/internal/cacheUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,16 @@ export function getArchiveFileSizeIsBytes(filePath: string): number {

export async function resolvePaths(patterns: string[]): Promise<string[]> {
const paths: string[] = []
const workspace = process.env['GITHUB_WORKSPACE'] ?? process.cwd()
const globber = await glob.create(patterns.join('\n'), {
implicitDescendants: false
})

for await (const file of globber.globGenerator()) {
const relativeFile = path
.relative(workspace, file)
.replace(new RegExp(`\\${path.sep}`, 'g'), '/')
const cwd = process.cwd()
const relativeFile = path.normalize(path.relative(cwd, file))
core.debug(`Matched: ${relativeFile}`)
// Paths are made relative so the tar entries are all relative to the root of the workspace.
paths.push(`${relativeFile}`)
paths.push(relativeFile)
}

return paths
Expand Down
10 changes: 5 additions & 5 deletions packages/cache/src/internal/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ export async function extractTar(
const args = [
...getCompressionProgram(),
'-xf',
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
path.normalize(archivePath),
'-P',
'-C',
workingDirectory.replace(new RegExp(`\\${path.sep}`, 'g'), '/')
path.normalize(workingDirectory)
]
await execTar(args, compressionMethod)
}
Expand Down Expand Up @@ -115,10 +115,10 @@ export async function createTar(
'--posix',
...getCompressionProgram(),
'-cf',
cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
path.normalize(cacheFileName),
'-P',
'-C',
workingDirectory.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
path.normalize(workingDirectory),
'--files-from',
manifestFilename
]
Expand Down Expand Up @@ -146,7 +146,7 @@ export async function listTar(
const args = [
...getCompressionProgram(),
'-tf',
archivePath.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
path.normalize(archivePath),
'-P'
]
await execTar(args, compressionMethod)
Expand Down
12 changes: 2 additions & 10 deletions packages/tool-cache/src/tool-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,8 @@ export async function extractTar(
args.push('-v')
}

let destArg = dest
let fileArg = file
if (IS_WINDOWS && isGnuTar) {
args.push('--force-local')
destArg = dest.replace(/\\/g, '/')

// Technically only the dest needs to have `/` but for aesthetic consistency
// convert slashes in the file arg too.
fileArg = file.replace(/\\/g, '/')
}
const destArg = path.normalize(dest)
const fileArg = path.normalize(file)

if (isGnuTar) {
// Suppress warnings when using GNU tar to extract archives created by BSD tar
Expand Down

0 comments on commit a310ef0

Please sign in to comment.