Skip to content

Commit

Permalink
fix: file#move do not delete origin file if same as destination (#874)
Browse files Browse the repository at this point in the history
* fix: file#move do not delete origin if same as destination

* lint: lint

* fix: system-test

* chore: remove commented code
  • Loading branch information
AVaksman authored and jkwlui committed Oct 8, 2019
1 parent 09b8fa4 commit dcaba8a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
13 changes: 10 additions & 3 deletions src/file.ts
Expand Up @@ -3042,9 +3042,16 @@ class File extends ServiceObject<File> {
return;
}

this.delete(options, (err, apiResponse) => {
callback!(err, destinationFile, apiResponse);
});
if (
this.name !== destinationFile!.name ||
this.bucket.name !== destinationFile!.bucket.name
) {
this.delete(options, (err, apiResponse) => {
callback!(err, destinationFile, apiResponse);
});
} else {
callback!(null, destinationFile, apiResponse);
}
});
}

Expand Down
31 changes: 28 additions & 3 deletions test/file.ts
Expand Up @@ -3450,8 +3450,9 @@ describe('File', () => {

describe('delete original file', () => {
it('should delete if copy is successful', done => {
const destinationFile = {bucket: {}};
file.copy = (destination: {}, options: {}, callback: Function) => {
callback(null);
callback(null, destinationFile);
};
Object.assign(file, {
delete() {
Expand All @@ -3476,11 +3477,34 @@ describe('File', () => {
});
});

it('should not delete the destination is same as origin', done => {
file.request = (config: {}, callback: Function) => {
callback(null, {});
};
const stub = sinon.stub(file, 'delete');
// destination is same bucket as object
file.move(BUCKET, (err: Error) => {
assert.ifError(err);
// destination is same file as object
file.move(file, (err: Error) => {
assert.ifError(err);
// destination is same file name as string
file.move(file.name, (err: Error) => {
assert.ifError(err);
assert.ok(stub.notCalled);
stub.reset();
done();
});
});
});
});

it('should pass options to delete', done => {
const options = {};
const destinationFile = {bucket: {}};

file.copy = (destination: {}, options: {}, callback: Function) => {
callback();
callback(null, destinationFile);
};

file.delete = (options_: {}) => {
Expand All @@ -3493,8 +3517,9 @@ describe('File', () => {

it('should fail if delete fails', done => {
const error = new Error('Error.');
const destinationFile = {bucket: {}};
file.copy = (destination: {}, options: {}, callback: Function) => {
callback();
callback(null, destinationFile);
};
file.delete = (options: {}, callback: Function) => {
callback(error);
Expand Down

0 comments on commit dcaba8a

Please sign in to comment.