Permalink
Browse files

Using 'file.name' instead of just 'file', empty batch should run fini…

…sh immediately, added tests for getFiles()
  • Loading branch information...
1 parent 4af8b4a commit e571531126a29d9f08543af3be67611cc327c8b4 @indutny indutny committed Dec 1, 2010
Showing with 97 additions and 8 deletions.
  1. +1 −1 lib/cloudfiles/container.js
  2. +5 −3 lib/cloudfiles/core.js
  3. +8 −3 lib/cloudfiles/utils.js
  4. +83 −1 test/container-test.js
@@ -43,7 +43,7 @@ Container.prototype = {
this.client.getFiles(this.name, download, function (err, files) {
self.files = files;
- callback(null, files);
+ callback(err, files);
});
},
View
@@ -182,20 +182,22 @@ Cloudfiles.prototype.getFiles = function (container, download, callback) {
// Download all files
if (download !== true) {
files = files.filter(function (file) {
- return download.test(file);
+ return download.test(file.name);
});
}
// Create a batch
batch = files.map(function (file) {
return function(callback) {
- self.getFile(container, file, callback);
+ self.getFile(container, file.name, callback);
}
});
} else if (Array.isArray(download)) {
// Go through all files that we've asked to download
batch = download.map(function(file) {
- var exists = files.indexOf(file);
+ var exists = files.some(function(item) {
+ return item.name == file
+ });
// If file exists - get it
// If not report about error
View
@@ -206,8 +206,9 @@ utils.rackspaceCurl = function (options, callback, success) {
utils.runBatch = function(fns, finish) {
var finished = 0,
total = fns.length,
- errors = [],
- results = [];
+ errors = new Array(total),
+ was_error = false,
+ results = new Array(total);
fns.forEach(function(fn, i) {
var once = false;
@@ -219,16 +220,20 @@ utils.runBatch = function(fns, finish) {
// If have error - push them into errors
if (err) {
+ was_error = true;
errors[i] = err;
} else {
results[i] = value;
}
- if (++finished === total) finish(errors.length ? errors : null, results);
+ if (++finished === total) finish(was_error ? errors : null, results);
}
var value = fn(next);
// If function returns value - it won't be working in async mode
if (value !== undefined) next(null, value);
});
+
+ if (fns.length === 0) finish(null, []);
};
+
View
@@ -143,6 +143,88 @@ vows.describe('node-cloudfiles/containers').addBatch({
}).addBatch({
"The node-cloudfiles client": {
"an instance of a Container object": {
+ "the getFiles(true) method": {
+ topic: function () {
+ testData.container.getFiles(true, this.callback);
+ },
+ "should response with a list of files with content": function (err, files) {
+ assert.isArray(files);
+ assert.length(files, 1);
+ assert.isArray(testData.container.files);
+ assert.length(testData.container.files, 1);
+ assert.isNotNull(files[0].local);
+ }
+ }
+ }
+ }
+}).addBatch({
+ "The node-cloudfiles client": {
+ "an instance of a Container object": {
+ "the getFiles(new RegExp(...)) method": {
+ topic: function () {
+ testData.container.getFiles(/^file/, this.callback);
+ },
+ "should response with a list of files with content": function (err, files) {
+ assert.isArray(files);
+ assert.length(files, 1);
+ assert.isArray(testData.container.files);
+ assert.length(testData.container.files, 1);
+ assert.isTrue(/^file/.test(files[0].name));
+ }
+ }
+ }
+ }
+}).addBatch({
+ "The node-cloudfiles client": {
+ "an instance of a Container object": {
+ "the getFiles(new RegExp(...) with no matches) method": {
+ topic: function () {
+ testData.container.getFiles(/^no_matches/, this.callback);
+ },
+ "should response with a empty list": function (err, files) {
+ assert.isArray(files);
+ assert.length(files, 0);
+ assert.isArray(testData.container.files);
+ assert.length(testData.container.files, 0);
+ }
+ }
+ }
+ }
+}).addBatch({
+ "The node-cloudfiles client": {
+ "an instance of a Container object": {
+ "the getFiles([filenames]) method": {
+ topic: function () {
+ testData.container.getFiles(['file1.txt'], this.callback);
+ },
+ "should response with a list of files with content": function (err, files) {
+ assert.isArray(files);
+ assert.length(files, 1);
+ assert.isArray(testData.container.files);
+ assert.length(testData.container.files, 1);
+ assert.equal(files[0].name, 'file1.txt');
+ }
+ }
+ }
+ }
+}).addBatch({
+ "The node-cloudfiles client": {
+ "an instance of a Container object": {
+ "the getFiles([not-existing-filenames]) method": {
+ topic: function () {
+ testData.container.getFiles(['not-exists.txt'], this.callback);
+ },
+ "should response with a error": function (err, files) {
+ assert.isArray(err);
+ assert.length(err, 1);
+ assert.instanceOf(err[0], Error);
+ }
+ }
+ }
+ }
+}).addBatch({
+ "The node-cloudfiles client": {
+ "an instance of a Container object": {
"the removeFile() method": {
topic: function () {
testData.container.removeFile('file1.txt', this.callback);
@@ -153,4 +235,4 @@ vows.describe('node-cloudfiles/containers').addBatch({
}
}
}
-}).export(module);
+}).export(module);

0 comments on commit e571531

Please sign in to comment.