Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue in CopyFiles task for directories with square brackets #9589 #11152

Merged
merged 3 commits into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"loc.friendlyName": "Copy files",
"loc.helpMarkDown": "[More Information](https://go.microsoft.com/fwlink/?LinkID=708389)",
"loc.helpMarkDown": "[Learn more about this task](https://go.microsoft.com/fwlink/?LinkID=708389)",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this change intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It happened automatically when I built. Seemed harmless so I left it.

"loc.description": "Copy files from a source folder to a target folder using patterns matching file paths (not folder paths)",
"loc.instanceNameFormat": "Copy Files to: $(TargetFolder)",
"loc.releaseNotes": "Match pattern consistency.",
Expand Down
40 changes: 40 additions & 0 deletions Tasks/CopyFilesV2/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,46 @@ describe('CopyFiles L0 Suite', function () {
done();
});

it('copy files from srcdir to destdir with brackets in src path', (done: MochaDone) => {
this.timeout(1000);

let testPath = path.join(__dirname, 'L0copyAllFilesWithBracketsInSrcPath.js');
let runner: mocktest.MockTestRunner = new mocktest.MockTestRunner(testPath);
runner.run();

assert(
runner.succeeded,
'should have succeeded');
assert(
runner.stdOutContained(`creating path: ${path.normalize('/destDir')}`),
'should have mkdirP destDir');
assert(
runner.stdOutContained(`creating path: ${path.normalize('/destDir/someOtherDir')}`),
'should have mkdirP someOtherDir');
assert(
runner.stdOutContained(`creating path: ${path.normalize('/destDir/someOtherDir2')}`),
'should have mkdirP someOtherDir2');
assert(
!runner.stdOutContained(`creating path: ${path.normalize('/destDir/someOtherDir3')}`),
'should not have mkdirP someOtherDir3');
assert(
runner.stdOutContained(`copying ${path.normalize('/srcDir [bracket]/someOtherDir/file1.file')} to ${path.normalize('/destDir/someOtherDir/file1.file')}`),
'should have copied dir1 file1');
assert(
runner.stdOutContained(`copying ${path.normalize('/srcDir [bracket]/someOtherDir/file2.file')} to ${path.normalize('/destDir/someOtherDir/file2.file')}`),
'should have copied dir1 file2');
assert(
runner.stdOutContained(`copying ${path.normalize('/srcDir [bracket]/someOtherDir2/file1.file')} to ${path.normalize('/destDir/someOtherDir2/file1.file')}`),
'should have copied dir2 file1');
assert(
runner.stdOutContained(`copying ${path.normalize('/srcDir [bracket]/someOtherDir2/file2.file')} to ${path.normalize('/destDir/someOtherDir2/file2.file')}`),
'should have copied dir2 file2');
assert(
runner.stdOutContained(`copying ${path.normalize('/srcDir [bracket]/someOtherDir2/file3.file')} to ${path.normalize('/destDir/someOtherDir2/file3.file')}`),
'should have copied dir2 file3');
done();
});

it('copy files and subtract based on exclude pattern', (done: MochaDone) => {
this.timeout(1000);

Expand Down
52 changes: 52 additions & 0 deletions Tasks/CopyFilesV2/Tests/L0copyAllFilesWithBracketsInSrcPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import fs = require('fs');
import mockanswer = require('vsts-task-lib/mock-answer');
import mockrun = require('vsts-task-lib/mock-run');
import path = require('path');

let taskPath = path.join(__dirname, '..', 'copyfiles.js');
let runner: mockrun.TaskMockRunner = new mockrun.TaskMockRunner(taskPath);
runner.setInput('Contents', '**');
runner.setInput('SourceFolder', path.normalize('/srcDir [bracket]'));
runner.setInput('TargetFolder', path.normalize('/destDir'));
runner.setInput('CleanTargetFolder', 'false');
runner.setInput('Overwrite', 'false');
let answers = <mockanswer.TaskLibAnswers> {
checkPath: { },
find: { },
};
answers.checkPath[path.normalize('/srcDir [bracket]')] = true;
answers.find[path.normalize('/srcDir [bracket]')] = [
path.normalize('/srcDir [bracket]'),
path.normalize('/srcDir [bracket]/someOtherDir'),
path.normalize('/srcDir [bracket]/someOtherDir/file1.file'),
path.normalize('/srcDir [bracket]/someOtherDir/file2.file'),
path.normalize('/srcDir [bracket]/someOtherDir2'),
path.normalize('/srcDir [bracket]/someOtherDir2/file1.file'),
path.normalize('/srcDir [bracket]/someOtherDir2/file2.file'),
path.normalize('/srcDir [bracket]/someOtherDir2/file3.file'),
path.normalize('/srcDir [bracket]/someOtherDir3'),
];
runner.setAnswers(answers);
runner.registerMockExport('stats', (itemPath: string) => {
console.log('##vso[task.debug]stats ' + itemPath);
switch (itemPath) {
case path.normalize('/srcDir [bracket]/someOtherDir'):
case path.normalize('/srcDir [bracket]/someOtherDir2'):
case path.normalize('/srcDir [bracket]/someOtherDir3'):
return { isDirectory: () => true };
case path.normalize('/srcDir [bracket]/someOtherDir/file1.file'):
case path.normalize('/srcDir [bracket]/someOtherDir/file2.file'):
case path.normalize('/srcDir [bracket]/someOtherDir2/file1.file'):
case path.normalize('/srcDir [bracket]/someOtherDir2/file2.file'):
case path.normalize('/srcDir [bracket]/someOtherDir2/file3.file'):
return { isDirectory: () => false };
default:
throw { code: 'ENOENT' };
}
});

// as a precaution, disable fs.chmodSync. it should not be called during this scenario.
fs.chmodSync = null;
runner.registerMock('fs', fs);

runner.run();
4 changes: 2 additions & 2 deletions Tasks/CopyFilesV2/copyfiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const preserveTimestamp: boolean = tl.getBoolInput('preserveTimestamp', false);
// normalize the source folder path. this is important for later in order to accurately
// determine the relative path of each found file (substring using sourceFolder.length).
sourceFolder = path.normalize(sourceFolder);

let allPaths: string[] = tl.find(sourceFolder); // default find options (follow sym links)
let matchedPaths: string[] = tl.match(allPaths, contents, sourceFolder); // default match options
let sourceFolderPattern = sourceFolder.replace('[','[[]'); // directories can have [] in them, and they have special meanings as a pattern, so escape them
let matchedPaths: string[] = tl.match(allPaths, contents, sourceFolderPattern); // default match options
let matchedFiles: string[] = matchedPaths.filter((itemPath: string) => !tl.stats(itemPath).isDirectory()); // filter-out directories

// copy the files to the target folder
Expand Down