Skip to content

Commit

Permalink
Handle copying when source and target destinations are the same
Browse files Browse the repository at this point in the history
Fixes #4812

- Fixes an issue when calling `copy` from the filesystem when
the `source` and `target` destinations are the same. Currently,
no check is performed to verify if the paths are different
which leads to `fs-extra` throwing an error.

Signed-off-by: Vincent Fugnitto <[email protected]>
  • Loading branch information
vince-fugnitto committed Apr 5, 2019
1 parent b1df8fc commit b08535a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/filesystem/src/node/node-filesystem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,14 @@ describe('NodeFileSystem', function () {
await expectThrowsAsync(fileSystem.copy(sourceUri.toString(), targetUri.toString()), Error);
});

it('Copy a file to existing location with the same file name. Should be rejected with an error.', async () => {
const sourceUri = root.resolve('foo');
fs.mkdirSync(FileUri.fsPath(sourceUri));
expect(fs.statSync(FileUri.fsPath(sourceUri)).isDirectory()).to.be.true;

await expectThrowsAsync(fileSystem.copy(sourceUri.toString(), sourceUri.toString()), Error);
});

it('Copy an empty directory to a non-existing location. Should return with the file stat representing the new file at the target location.', async () => {
const sourceUri = root.resolve('foo');
const targetUri = root.resolve('bar');
Expand Down
3 changes: 3 additions & 0 deletions packages/filesystem/src/node/node-filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ export class FileSystemNode implements FileSystem {
if (targetStat && !overwrite) {
throw FileSystemError.FileExists(targetUri, "Did you set the 'overwrite' flag to true?");
}
if (targetStat && targetStat.uri === sourceStat.uri) {
throw FileSystemError.FileExists(targetUri, 'Cannot perform copy, source and destination are the same.');
}
await fs.copy(FileUri.fsPath(_sourceUri), FileUri.fsPath(_targetUri), { overwrite, recursive });
const newStat = await this.doGetStat(_targetUri, 1);
if (newStat) {
Expand Down

0 comments on commit b08535a

Please sign in to comment.