From 9960330d4ae2b197072117531d642b7371338353 Mon Sep 17 00:00:00 2001 From: Coen Hyde Date: Wed, 14 Aug 2013 19:50:18 -0700 Subject: [PATCH 1/3] give knox style option in tests so it communitates with fakes3 by specifying the bucket with as a path instead of as a virtualhost --- Gruntfile.js | 9 +++++++-- package.json | 8 +++++--- tasks/lib/s3.js | 44 ++++++++++++++++++++++++-------------------- test/download.js | 4 ++-- test/upload.js | 20 ++++++++------------ 5 files changed, 46 insertions(+), 39 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 37e24c8..af52899 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -7,6 +7,7 @@ module.exports = function (grunt) { nodeunit: { all: ['test/upload.js', 'test/download.js', 'test/s3Task.js'] }, + clean: [ 's3/'], s3: { options: { key: 'abc', @@ -15,7 +16,8 @@ module.exports = function (grunt) { endpoint: '127.0.0.1', port: 1337, secure: false, - access: 'public-read' + access: 'public-read', + style: 'path' }, test: { options: {} @@ -39,6 +41,9 @@ module.exports = function (grunt) { grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-nodeunit'); - grunt.registerTask('test', ['jshint', 'nodeunit']); + grunt.loadNpmTasks('grunt-contrib-clean'); + + grunt.registerTask('test', ['clean', 'jshint', 'nodeunit']); + grunt.loadTasks(__dirname + '/tasks'); }; diff --git a/package.json b/package.json index 3481545..0db7f9b 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,4 @@ - { +{ "name": "grunt-s3", "description": "A grunt task to automate moving files to/from Amazon S3.", "version": "0.2.0-alpha.2", @@ -20,7 +20,8 @@ "main": "tasks/s3", "bin": "bin/grunt-s3", "scripts": { - "test": "grunt test" + "test": "grunt test", + "fakes3": "fakes3 -r s3 -p 1337" }, "engines": { "node": ">= 0.8.0" @@ -41,6 +42,7 @@ "nodeunit": "~0.7.4", "grunt-contrib-jshint": "~0.1.0", "grunt-contrib-nodeunit": "~0.1.1", - "libyaml": "~0.2.1" + "libyaml": "~0.2.1", + "grunt-contrib-clean": "~0.5.0" } } diff --git a/tasks/lib/s3.js b/tasks/lib/s3.js index a57b71a..ec55e62 100644 --- a/tasks/lib/s3.js +++ b/tasks/lib/s3.js @@ -70,6 +70,20 @@ exports.init = function (grunt) { return new Error(msg); }; + var makeOptions = exports.makeOptions = function(opts) { + var options = _.clone(opts || {}, true); + + options = _.pick(options, [ + 'region', 'endpoint', 'port', 'key', 'secret', 'access', 'bucket', 'secure', 'headers', 'debug' + ]); + + _.defaults(options, { + 'style': 'path' + }); + + return options; + }; + /** * Publishes the local file at src to the s3 dest. * @@ -84,7 +98,7 @@ exports.init = function (grunt) { */ exports.put = exports.upload = function (src, dest, opts) { var dfd = new _.Deferred(); - var options = _.clone(opts, true); + var options = makeOptions(opts); // Make sure the local file exists. if (!existsSync(src)) { @@ -98,9 +112,7 @@ exports.init = function (grunt) { } // Pick out the configuration options we need for the client. - var client = knox.createClient(_(options).pick([ - 'region', 'endpoint', 'port', 'key', 'secret', 'access', 'bucket', 'secure' - ])); + var client = knox.createClient(options); if (options.debug) { return dfd.resolve(util.format(MSG_UPLOAD_DEBUG, path.relative(process.cwd(), src), client.bucket, dest)).promise(); @@ -216,12 +228,10 @@ exports.init = function (grunt) { */ exports.pull = exports.download = function (src, dest, opts) { var dfd = new _.Deferred(); - var options = _.clone(opts); + var options = makeOptions(opts); // Pick out the configuration options we need for the client. - var client = knox.createClient(_(options).pick([ - 'region', 'endpoint', 'port', 'key', 'secret', 'access', 'bucket' - ])); + var client = knox.createClient(options); if (options.debug) { return dfd.resolve(util.format(MSG_DOWNLOAD_DEBUG, client.bucket, src, path.relative(process.cwd(), dest))).promise(); @@ -288,12 +298,10 @@ exports.init = function (grunt) { */ exports.copy = function (src, dest, opts) { var dfd = new _.Deferred(); - var options = _.clone(opts); + var options = makeOptions(opts); // Pick out the configuration options we need for the client. - var client = knox.createClient(_(options).pick([ - 'region', 'endpoint', 'port', 'key', 'secret', 'access', 'bucket' - ])); + var client = knox.createClient(options); if (options.debug) { return dfd.resolve(util.format(MSG_COPY_DEBUG, src, client.bucket, dest)).promise(); @@ -334,12 +342,10 @@ exports.init = function (grunt) { */ exports.del = function (src, opts) { var dfd = new _.Deferred(); - var options = _.clone(opts); + var options = makeOptions(opts); // Pick out the configuration options we need for the client. - var client = knox.createClient(_(options).pick([ - 'region', 'endpoint', 'port', 'key', 'secret', 'access', 'bucket' - ])); + var client = knox.createClient(options); if (options.debug) { return dfd.resolve(util.format(MSG_DELETE_DEBUG, client.bucket, src)).promise(); @@ -374,12 +380,10 @@ exports.init = function (grunt) { */ exports.sync = function (src, dest, opts) { var dfd = new _.Deferred(); - var options = _.clone(opts); + var options = makeOptions(opts); // Pick out the configuration options we need for the client. - var client = knox.createClient(_(options).pick([ - 'region', 'endpoint', 'port', 'key', 'secret', 'access', 'bucket' - ])); + var client = knox.createClient(options); if (options.debug) { return dfd.resolve(util.format(MSG_SKIP_DEBUG, client.bucket, src)).promise(); diff --git a/test/download.js b/test/download.js index e2c6bb3..a790667 100644 --- a/test/download.js +++ b/test/download.js @@ -13,7 +13,7 @@ module.exports = { test.expect(1); var dest = __dirname + '/files/a.txt'; - var src = __dirname + '/../s3/127/a.txt/.fakes3_metadataFFF/content'; + var src = __dirname + '/../s3/127/test/a.txt/.fakes3_metadataFFF/content'; s3.download('a.txt', dest, config) .done(function () { @@ -28,7 +28,7 @@ module.exports = { test.expect(1); var dest = __dirname + '/files/b.txt.debug'; - var src = __dirname + '/../s3/127/b.txt/.fakes3_metadataFFF/content'; + var src = __dirname + '/../s3/127/test/b.txt/.fakes3_metadataFFF/content'; var debugConfig = _.defaults({}, config, {debug: true}); diff --git a/test/upload.js b/test/upload.js index 49f6240..184cdaa 100644 --- a/test/upload.js +++ b/test/upload.js @@ -14,10 +14,10 @@ module.exports = { testUpload : function (test) { test.expect(2); - async.waterfall([ + async.series([ function (cb) { var src = __dirname + '/files/a.txt'; - var dest = __dirname + '/../s3/127/a.txt/.fakes3_metadataFFF/content'; + var dest = __dirname + '/../s3/127/test/a.txt/.fakes3_metadataFFF/content'; s3.upload(src, 'a.txt', config) .done(function () { @@ -36,38 +36,34 @@ module.exports = { cb(null); }); } - ], function () { - test.done(); - }); + ], test.done); }, testUploadWithHeaders : function (test) { test.expect(1); - async.waterfall([ + async.series([ function (cb) { var src = __dirname + '/files/b.txt'; - var dest = __dirname + '/../s3/127/b.txt/.fakes3_metadataFFF/metadata'; + var dest = __dirname + '/../s3/127/test/b.txt/.fakes3_metadataFFF/metadata'; var headerConfig = _.defaults({}, config, { headers : {'Content-Type' : '<3'} }); s3.upload(src, 'b.txt', headerConfig) .always(function () { - var meta = yaml.parse(grunt.file.read(dest)) + var meta = yaml.parse(grunt.file.read(dest)); test.ok(meta[0][':content_type'] === new Buffer('<3').toString('base64'), 'Headers are preserved.'); cb(null); }); } - ], function () { - test.done(); - }); + ], test.done); }, testUploadDebug : function (test) { test.expect(1); var src = __dirname + '/files/c.txt'; - var dest = __dirname + '/../s3/127/c.txt/.fakes3_metadataFFF/content'; + var dest = __dirname + '/../s3/127/test/c.txt/.fakes3_metadataFFF/content'; var debugConfig = _.defaults({}, config, { debug: true }); From be70b891011b41a9826a2cbcd3c73c8e4ef9b3c5 Mon Sep 17 00:00:00 2001 From: Coen Hyde Date: Wed, 14 Aug 2013 20:05:24 -0700 Subject: [PATCH 2/3] removed style default --- tasks/lib/s3.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tasks/lib/s3.js b/tasks/lib/s3.js index ec55e62..42b7181 100644 --- a/tasks/lib/s3.js +++ b/tasks/lib/s3.js @@ -74,13 +74,9 @@ exports.init = function (grunt) { var options = _.clone(opts || {}, true); options = _.pick(options, [ - 'region', 'endpoint', 'port', 'key', 'secret', 'access', 'bucket', 'secure', 'headers', 'debug' + 'region', 'endpoint', 'port', 'key', 'secret', 'access', 'bucket', 'secure', 'headers', 'debug', 'style' ]); - - _.defaults(options, { - 'style': 'path' - }); - + return options; }; From 33630fb77c6cd8df4690135379b464dc88b6c261 Mon Sep 17 00:00:00 2001 From: Coen Hyde Date: Wed, 14 Aug 2013 20:15:49 -0700 Subject: [PATCH 3/3] only filter options when passing to knox --- tasks/lib/s3.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/tasks/lib/s3.js b/tasks/lib/s3.js index 42b7181..da357c3 100644 --- a/tasks/lib/s3.js +++ b/tasks/lib/s3.js @@ -73,13 +73,15 @@ exports.init = function (grunt) { var makeOptions = exports.makeOptions = function(opts) { var options = _.clone(opts || {}, true); - options = _.pick(options, [ - 'region', 'endpoint', 'port', 'key', 'secret', 'access', 'bucket', 'secure', 'headers', 'debug', 'style' - ]); - return options; }; + var makeClient = function(options) { + return knox.createClient(_.pick(options, [ + 'region', 'endpoint', 'port', 'key', 'secret', 'access', 'bucket', 'secure', 'headers', 'style' + ])); + }; + /** * Publishes the local file at src to the s3 dest. * @@ -108,7 +110,7 @@ exports.init = function (grunt) { } // Pick out the configuration options we need for the client. - var client = knox.createClient(options); + var client = makeClient(options); if (options.debug) { return dfd.resolve(util.format(MSG_UPLOAD_DEBUG, path.relative(process.cwd(), src), client.bucket, dest)).promise(); @@ -227,7 +229,7 @@ exports.init = function (grunt) { var options = makeOptions(opts); // Pick out the configuration options we need for the client. - var client = knox.createClient(options); + var client = makeClient(options); if (options.debug) { return dfd.resolve(util.format(MSG_DOWNLOAD_DEBUG, client.bucket, src, path.relative(process.cwd(), dest))).promise(); @@ -297,7 +299,7 @@ exports.init = function (grunt) { var options = makeOptions(opts); // Pick out the configuration options we need for the client. - var client = knox.createClient(options); + var client = makeClient(options); if (options.debug) { return dfd.resolve(util.format(MSG_COPY_DEBUG, src, client.bucket, dest)).promise(); @@ -341,7 +343,7 @@ exports.init = function (grunt) { var options = makeOptions(opts); // Pick out the configuration options we need for the client. - var client = knox.createClient(options); + var client = makeClient(options); if (options.debug) { return dfd.resolve(util.format(MSG_DELETE_DEBUG, client.bucket, src)).promise(); @@ -379,7 +381,7 @@ exports.init = function (grunt) { var options = makeOptions(opts); // Pick out the configuration options we need for the client. - var client = knox.createClient(options); + var client = makeClient(options); if (options.debug) { return dfd.resolve(util.format(MSG_SKIP_DEBUG, client.bucket, src)).promise();