From ae6949fb5286f275d346eb3df9e7d778cfd03f14 Mon Sep 17 00:00:00 2001 From: Dominic Scheirlinck Date: Wed, 12 Jan 2022 13:58:59 +1300 Subject: [PATCH] fix: sort file arguments to tar file-list To fix https://github.com/folbricht/desync/issues/210 --- src/artifacts/matcher.ts | 2 +- src/commands/upload.ts | 16 ++++------------ test/commands/upload.test.ts | 31 ++++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/artifacts/matcher.ts b/src/artifacts/matcher.ts index 11bf8aa3..ed0b2066 100644 --- a/src/artifacts/matcher.ts +++ b/src/artifacts/matcher.ts @@ -66,5 +66,5 @@ export async function filesToUpload({ } await Promise.all(matching); - return matched.matched; + return matched.matched.sort(); } diff --git a/src/commands/upload.ts b/src/commands/upload.ts index 0e0588f6..0c8a33ae 100644 --- a/src/commands/upload.ts +++ b/src/commands/upload.ts @@ -1,6 +1,5 @@ import { flags as f } from '@oclif/command'; import debug from 'debug'; -import execa from 'execa'; import { upload } from '../artifacts/api'; import { deflateCmd } from '../artifacts/compression'; import { filesToUpload } from '../artifacts/matcher'; @@ -102,22 +101,15 @@ locally cached return; } - if (flags.verbose) { - log('Files to upload', files); - } - - log(`Uploading ${count(files, 'path')} as ${args.output}`); + log(`Uploading ${count(files, 'path')} as ${args.output}`, files); const tarBin = await tar(); const tarArgs = ['-c', '--null', '--files-from', '-']; - const allArgs: string[] = ['-o', 'pipefail', ';', tarBin, ...tarArgs, '|', ...(await deflateCmd(artifact))]; + const input = `${files.join('\x00')}\x00`; - log(`Going to deflate, using set ${allArgs.join(' ')} < files-to-upload`); - - await execa('set', allArgs, { - input: stream.Readable.from(files.join('\x00')), - shell: 'bash', + await exec('set', allArgs, { + input, }); log(`Archive deflated at ${args.output}`); diff --git a/test/commands/upload.test.ts b/test/commands/upload.test.ts index 6bf63d8c..a4eb946e 100644 --- a/test/commands/upload.test.ts +++ b/test/commands/upload.test.ts @@ -1,5 +1,6 @@ import * as fs from 'fs'; import { promisify } from 'util'; +import mkdirp from 'mkdirp'; import tempy from 'tempy'; import { upload } from '../../src/artifacts/api'; import Upload from '../../src/commands/upload'; @@ -31,7 +32,7 @@ describe('cmd upload', () => { await writeFile(`${dir}/foo.txt`, 'bar'); await writeFile(`${dir}/bar.txt`, 'baz'); - await writeFile(`${dir}/file-list.null.txt`, 'foo.txt\x00bar.txt'); + await writeFile(`${dir}/file-list.null.txt`, 'foo.txt\x00bar.txt\x00'); const { stderr } = await testRun(Upload, [ '--null', @@ -44,6 +45,34 @@ describe('cmd upload', () => { }); }); + it('can upload a list of directories in the right order, null separated', async () => { + await tempy.directory.task(async (dir) => { + process.chdir(dir); + + await mkdirp(`${dir}/foo/bar`); + await mkdirp(`${dir}/foo/baz`); + +(await writeFile(`${dir}/foo/bar/a.txt`, 'a')); + await writeFile(`${dir}/foo/bar/b.txt`, 'b'); + await writeFile(`${dir}/foo/baz/a.txt`, 'a'); + await writeFile(`${dir}/foo/baz/b.txt`, 'b'); + await writeFile(`${dir}/foo/a.txt`, 'a'); + await writeFile(`${dir}/foo/b.txt`, 'b'); + + // This file list is in the wrong order! https://github.com/folbricht/desync/issues/210 + await writeFile(`${dir}/file-list.null.txt`, './foo/bar\x00./foo/\x00./foo/baz/\x00'); + + const { stderr } = await testRun(Upload, [ + '--null', + '--files-from', + `${dir}/file-list.null.txt`, + 'some-upload.tar.gz', + ]); + + expect(stderr).toContain("Uploading 3 paths as some-upload.tar.gz [ './foo/', './foo/bar', './foo/baz/' ]"); + expect(stderr).toContain('Successfully uploaded some-upload'); + }); + }); + it('can upload a with globs', async () => { await tempy.directory.task(async (dir: string) => { process.chdir(dir);