Skip to content

Commit

Permalink
Fixed copy files for broken symlink case (#13759)
Browse files Browse the repository at this point in the history
* Fixed copy files for broken symlinks case

* Added unit tests

* Fixed typo

* Moved find options to separate object

* Skipping node downloading

* Reverted make.js test changes

* Added general timeout
  • Loading branch information
Anatoly Bolshakov authored Oct 29, 2020
1 parent c54a6bb commit dc07aba
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Tasks/CopyFilesV2/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as os from 'os';
import * as path from 'path';

describe('CopyFiles L0 Suite', function () {
this.timeout(parseInt(process.env.TASK_TEST_TIMEOUT) || 20000);

before(() => { });

after(() => { });
Expand Down
33 changes: 33 additions & 0 deletions Tasks/CopyFilesV2/Tests/L0BrokenSymlinksAllowed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs = require('fs');
import mockanswer = require('azure-pipelines-task-lib/mock-answer');
import mockrun = require('azure-pipelines-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'));
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')] = true;
runner.setAnswers(answers);


runner.registerMockExport('find', (itemPath: string, options: any) => {
if(!options.allowBrokenSymbolicLinks) {
throw new Error("");
}

return ['/srcDir/file1'];
});

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

runner.run();
12 changes: 10 additions & 2 deletions Tasks/CopyFilesV2/copyfiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import fs = require('fs');
import path = require('path');
import tl = require('azure-pipelines-task-lib/task');


// we allow broken symlinks - since there could be broken symlinks found in source folder, but filtered by contents pattern
const findOptions: tl.FindOptions = {
allowBrokenSymbolicLinks: true,
followSpecifiedSymbolicLink: true,
followSymbolicLinks: true
};

tl.setResourcePath(path.join(__dirname, 'task.json'));

// contents is a multiline input containing glob patterns
Expand All @@ -16,8 +24,8 @@ 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 sourceFolderPattern = sourceFolder.replace('[','[[]'); // directories can have [] in them, and they have special meanings as a pattern, so escape them
let allPaths: string[] = tl.find(sourceFolder, findOptions);
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

Expand Down
4 changes: 2 additions & 2 deletions Tasks/CopyFilesV2/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 2,
"Minor": 164,
"Patch": 2
"Minor": 178,
"Patch": 0
},
"releaseNotes": "Match pattern consistency.",
"demands": [],
Expand Down
4 changes: 2 additions & 2 deletions Tasks/CopyFilesV2/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 2,
"Minor": 164,
"Patch": 2
"Minor": 178,
"Patch": 0
},
"releaseNotes": "ms-resource:loc.releaseNotes",
"demands": [],
Expand Down

0 comments on commit dc07aba

Please sign in to comment.