Skip to content

Commit

Permalink
Fixes write batch in Dropbox
Browse files Browse the repository at this point in the history
* Batch correctly overwrite existing files
* Batch now rejects the promise if one writing action failed

Closes #131
  • Loading branch information
JbIPS committed Nov 26, 2017
1 parent b0e7180 commit f84fea4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Fixed
- [Dropbox] Batch correctly overwrite existing files
- [Dropbox] Batch now rejects the promise if one writing action failed

## [2.0.0] - 2017-11-25
### Changed
- GitHub batch fixes and optimization
Expand Down Expand Up @@ -51,6 +56,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
### Changed
- Total rework of the philosphy

[2.0.0]: https://github.com/silexlabs/unifile/compare/v1.2.0...v
[Unreleased]: https://github.com/silexlabs/unifile/compare/v2.0.0...HEAD
[2.0.0]: https://github.com/silexlabs/unifile/compare/v1.2.0...v2.0.0
[1.2.0]: https://github.com/silexlabs/unifile/compare/v1.1.0...v1.2.0
[1.1.0]: https://github.com/silexlabs/unifile/compare/v1.0.0...v1.1.0
15 changes: 14 additions & 1 deletion lib/unifile-dropbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ function checkBatchEnd(session, result, checkRoute, jobId) {
})
.then((result) => checkBatchEnd(session, result, checkRoute, newId));
case 'complete':
const failed = result.entries.reduce((memo, entry, index) => {
if(entry['.tag'] === 'failure') memo.push({entry, index});
return memo;
}, []);
if(failed.length > 0) {
const errors = failed.map(({entry, index}) => {
const failureTag = entry.failure['.tag'];
return `Could not complete action ${index}: ${failureTag + '/' + entry.failure[failureTag]['.tag']}`;
});
return Promise.reject(new UnifileError(
UnifileError.EIO, errors.join(',')));
}
return Promise.resolve();
}
}
Expand Down Expand Up @@ -408,6 +420,7 @@ class DropboxConnector {
}

batch(session, actions, message) {
const writeMode = this.writeMode;
let actionsChain = Promise.resolve();

let uploadEntries = [];
Expand Down Expand Up @@ -469,7 +482,7 @@ class DropboxConnector {
},
commit: {
path: makePathAbsolute(action.path),
mode: this.writeMode
mode: writeMode
}
};
});
Expand Down
33 changes: 33 additions & 0 deletions test/unifile-dropbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,20 @@ describe('DropboxConnector', function() {
.then(() => connector.rmdir(session, 'tmp'));
});

it('rejects the promise if an conflict happen and overwrite is not set', function() {
const path = 'tmp/indexFile';
const fileContent = 'html';
const noOverwriteConnector = new DropboxConnector(Object.assign({}, authConfig, {writeMode: 'add'}));
return noOverwriteConnector.writeFile(session, path, 'lorem')
.then(() => {
return noOverwriteConnector.batch(session, [{
name: 'writefile',
path: path,
content: fileContent
}]);
}).should.be.rejectedWith('Could not complete action 0: path/conflict');
});

it('executes action in order', function() {
return connector.batch(session, creation)
.then(() => {
Expand All @@ -797,6 +811,25 @@ describe('DropboxConnector', function() {
});
});

it('can overwrite existing files', function() {
const path = 'tmp/indexFile';
const fileContent = 'html';
return connector.writeFile(session, path, 'lorem')
.then(() => {
return connector.batch(session, [{
name: 'writefile',
path: path,
content: fileContent
}]);
})
.then(() => {
return connector.readFile(session, path);
})
.then((content) => {
return expect(content.toString()).to.equal(fileContent);
});
});

it('executes action in order and ignores unsupported ones', function() {
creation.unshift({name: 'createReadStream', path: 'unknown_file'});
creation.unshift({name: 'createReadStream', path: 'a/test/unknown_file'});
Expand Down

0 comments on commit f84fea4

Please sign in to comment.