diff --git a/Tasks/ArchiveFilesV2/Tests/L0.ts b/Tasks/ArchiveFilesV2/Tests/L0.ts index cfadf26f0c67..b7ce25911f1a 100644 --- a/Tasks/ArchiveFilesV2/Tests/L0.ts +++ b/Tasks/ArchiveFilesV2/Tests/L0.ts @@ -190,5 +190,32 @@ describe('ArchiveFiles L0 Suite', function () { assert(tr.stdout.indexOf('Creating archive') > -1, 'Should have tried to create archive'); assert(fs.existsSync(expectedArchivePath), `Should have successfully created the archive at ${expectedArchivePath}, instead directory contents are ${fs.readdirSync(path.dirname(expectedArchivePath))}`); }); + + it('Successfully adds a single file to an existing archive', async function () { + this.timeout(5000); + process.env['archiveType'] = 'zip'; + process.env['archiveFile'] = 'singleFileArchive.zip'; + process.env['includeRootFolder'] = 'false'; + process.env['replaceExistingArchive'] = 'false'; + expectedArchivePath = path.join(__dirname, 'test_output', 'singleFileArchive.zip'); + + // Create the initial archive + let tp1: string = path.join(__dirname, 'L0CreateArchive.js'); + let tr1: ttm.MockTestRunner = new ttm.MockTestRunner(tp1); + await tr1.runAsync(); + assert(fs.existsSync(expectedArchivePath), 'Should have created the initial archive'); + + // Add a single file to the existing archive + let tp2: string = path.join(__dirname, 'L0AddSingleFileToExisting.js'); + let tr2: ttm.MockTestRunner = new ttm.MockTestRunner(tp2); + await tr2.runAsync(); + + assert(tr2.succeeded, 'Task should have succeeded'); + assert(tr2.stdout.indexOf('Adding to existing archive') > -1 || + tr2.stdout.indexOf('Adding file to archive') > -1 || + tr2.stdout.indexOf('Archiving file:') > -1, + 'Should have added file to archive'); + assert(fs.existsSync(expectedArchivePath), 'Archive should still exist'); + }); } }); diff --git a/Tasks/ArchiveFilesV2/Tests/L0AddSingleFileToExisting.ts b/Tasks/ArchiveFilesV2/Tests/L0AddSingleFileToExisting.ts new file mode 100644 index 000000000000..baf308ec5b53 --- /dev/null +++ b/Tasks/ArchiveFilesV2/Tests/L0AddSingleFileToExisting.ts @@ -0,0 +1,17 @@ +import tmrm = require('azure-pipelines-task-lib/mock-run'); +import path = require('path'); + +let taskPath = path.join(__dirname, '..', 'archivefiles.js'); +let tmr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath); + +process.env['AGENT_TEMPDIRECTORY'] = path.join(__dirname, 'test_temp'); + +// First, set up to create an initial archive +tmr.setInput('rootFolderOrFile', path.join(__dirname, 'test_folder', 'a', 'abc.txt')); +tmr.setInput('includeRootFolder', process.env['includeRootFolder']); +tmr.setInput('archiveType', process.env['archiveType']); +tmr.setInput('archiveFile', path.join(__dirname, 'test_output', process.env['archiveFile'])); +tmr.setInput('replaceExistingArchive', process.env['replaceExistingArchive'] || 'false'); +tmr.setInput('tarCompression', 'gz'); + +tmr.run(true); \ No newline at end of file diff --git a/Tasks/ArchiveFilesV2/archivefiles.ts b/Tasks/ArchiveFilesV2/archivefiles.ts index 80002e17f8fd..6d10989fab53 100644 --- a/Tasks/ArchiveFilesV2/archivefiles.ts +++ b/Tasks/ArchiveFilesV2/archivefiles.ts @@ -45,12 +45,19 @@ function findFiles(): string[] { } return [path.basename(rootFolderOrFile)]; } else { - var fullPaths: string[] = tl.ls('-A', [rootFolderOrFile]); - var baseNames: string[] = []; - for (var i = 0; i < fullPaths.length; i++) { - baseNames[i] = path.basename(fullPaths[i]); + // Check if rootFolderOrFile is a file (only when feature flag is enabled) + if (tl.getPipelineFeature("DistributedTasks.Task.ArchiveOneFileWithRootFolderInput") && + fs.existsSync(rootFolderOrFile) && + fs.statSync(rootFolderOrFile).isFile()) { + return [path.basename(rootFolderOrFile)]; + } else { + var fullPaths: string[] = tl.ls('-A', [rootFolderOrFile]); + var baseNames: string[] = []; + for (var i = 0; i < fullPaths.length; i++) { + baseNames[i] = path.basename(fullPaths[i]); + } + return baseNames; } - return baseNames; } }