diff --git a/storage/buckets.js b/storage/buckets.js index 511ccb1d9d8..4d809e9faef 100644 --- a/storage/buckets.js +++ b/storage/buckets.js @@ -101,28 +101,31 @@ function printUsage () { } // [END usage] -// Run the command-line program -function main (args, cb) { - var command = args.shift(); - if (command === 'create') { - createBucket(args[0], cb); - } else if (command === 'list') { - listBuckets(cb); - } else if (command === 'delete') { - deleteBucket(args[0], cb); - } else { - printUsage(); - cb(); +// The command-line program +var program = { + createBucket: createBucket, + listBuckets: listBuckets, + deleteBucket: deleteBucket, + printUsage: printUsage, + + // Executed when this program is run from the command-line + main: function (args, cb) { + var command = args.shift(); + if (command === 'create') { + this.createBucket(args[0], cb); + } else if (command === 'list') { + this.listBuckets(cb); + } else if (command === 'delete') { + this.deleteBucket(args[0], cb); + } else { + this.printUsage(); + } } -} +}; if (module === require.main) { - main(process.argv.slice(2), console.log); + program.main(process.argv.slice(2), console.log); } // [END all] -exports.createBucket = createBucket; -exports.listBuckets = listBuckets; -exports.deleteBucket = deleteBucket; -exports.printUsage = printUsage; -exports.main = main; +module.exports = program; diff --git a/storage/files.js b/storage/files.js index cb55ef34afa..c248c06eef4 100644 --- a/storage/files.js +++ b/storage/files.js @@ -338,46 +338,49 @@ function printUsage () { } // [END usage] -// Run the command-line program -function main (args, cb) { - var command = args.shift(); - if (command === 'list') { - listFiles(args[0], cb); - } else if (command === 'listByPrefix') { - listFilesWithPrefix(args[0], args[1], args[2], cb); - } else if (command === 'upload') { - uploadFile(args[0], args[1], cb); - } else if (command === 'download') { - downloadFile(args[0], args[1], args[2], cb); - } else if (command === 'delete') { - deleteFile(args[0], args[1], cb); - } else if (command === 'getMetadata') { - getMetadata(args[0], args[1], cb); - } else if (command === 'makePublic') { - makePublic(args[0], args[1], cb); - } else if (command === 'move') { - moveFile(args[0], args[1], args[2], cb); - } else if (command === 'copy') { - copyFile(args[0], args[1], args[2], args[3], cb); - } else { - printUsage(); - cb(); +// The command-line program +var program = { + listFiles: listFiles, + listFilesWithPrefix: listFilesWithPrefix, + uploadFile: uploadFile, + downloadFile: downloadFile, + deleteFile: deleteFile, + getMetadata: getMetadata, + makePublic: makePublic, + moveFile: moveFile, + copyFile: copyFile, + printUsage: printUsage, + + // Executed when this program is run from the command-line + main: function (args, cb) { + var command = args.shift(); + if (command === 'list') { + this.listFiles(args[0], cb); + } else if (command === 'listByPrefix') { + this.listFilesWithPrefix(args[0], args[1], args[2], cb); + } else if (command === 'upload') { + this.uploadFile(args[0], args[1], cb); + } else if (command === 'download') { + this.downloadFile(args[0], args[1], args[2], cb); + } else if (command === 'delete') { + this.deleteFile(args[0], args[1], cb); + } else if (command === 'getMetadata') { + this.getMetadata(args[0], args[1], cb); + } else if (command === 'makePublic') { + this.makePublic(args[0], args[1], cb); + } else if (command === 'move') { + this.moveFile(args[0], args[1], args[2], cb); + } else if (command === 'copy') { + this.copyFile(args[0], args[1], args[2], args[3], cb); + } else { + this.printUsage(); + } } -} +}; if (module === require.main) { - main(process.argv.slice(2), console.log); + program.main(process.argv.slice(2), console.log); } // [END all] -exports.listFiles = listFiles; -exports.listFilesWithPrefix = listFilesWithPrefix; -exports.uploadFile = uploadFile; -exports.downloadFile = downloadFile; -exports.deleteFile = deleteFile; -exports.getMetadata = getMetadata; -exports.makePublic = makePublic; -exports.moveFile = moveFile; -exports.copyFile = copyFile; -exports.printUsage = printUsage; -exports.main = main; +module.exports = program; diff --git a/storage/foo.raw b/storage/foo.raw deleted file mode 100644 index a01dfc45a59..00000000000 Binary files a/storage/foo.raw and /dev/null differ diff --git a/storage/test/buckets.test.js b/storage/test/buckets.test.js index b8b6e078a2a..2e041bdfd8c 100644 --- a/storage/test/buckets.test.js +++ b/storage/test/buckets.test.js @@ -129,34 +129,6 @@ describe('storage:buckets', function () { }); }); }); - describe('main', function () { - it('should call the right commands', function () { - var bucketsSample = getSample(); - - bucketsSample.sample.main(['create', bucketName], function (err, bucket) { - assert.ifError(err); - assert.strictEqual(bucket, bucketsSample.mocks.buckets[0]); - assert(console.log.calledWith('Created bucket: %s', bucketName)); - }); - bucketsSample.sample.main(['list'], function (err, buckets) { - assert.ifError(err); - assert.strictEqual(buckets, bucketsSample.mocks.buckets); - assert(console.log.calledWith('Found %d buckets!', bucketsSample.mocks.buckets.length)); - }); - bucketsSample.sample.main(['delete', bucketName], function (err, apiResponse) { - assert.ifError(err); - assert.equal(bucketsSample.mocks.storage.bucket.firstCall.args[0], bucketName); - assert(console.log.calledWith('Deleted bucket: %s', bucketName)); - }); - bucketsSample.sample.main(['foo'], function () { - assert(console.log.calledWith('Usage: node buckets [COMMAND] [ARGS...]')); - assert(console.log.calledWith('\nCommands:\n')); - assert(console.log.calledWith('\tcreate [BUCKET_NAME]')); - assert(console.log.calledWith('\tlist')); - assert(console.log.calledWith('\tdelete [BUCKET_NAME]')); - }); - }); - }); describe('printUsage', function () { it('should print usage', function () { var bucketsSample = getSample(); @@ -170,4 +142,25 @@ describe('storage:buckets', function () { assert(console.log.calledWith('\tdelete [BUCKET_NAME]')); }); }); + describe('main', function () { + it('should call the right commands', function () { + var program = getSample().sample; + + sinon.stub(program, 'createBucket') + program.main(['create']); + assert(program.createBucket.calledOnce); + + sinon.stub(program, 'listBucket') + program.main(['list']); + assert(program.listBucket.calledOnce); + + sinon.stub(program, 'deleteBucket') + program.main(['delete']); + assert(program.deleteBucket.calledOnce); + + sinon.stub(program, 'printUsage') + program.main(['--help']); + assert(program.printUsage.calledOnce); + }); + }); }); diff --git a/storage/test/files.test.js b/storage/test/files.test.js index 68131c88875..180c211a956 100644 --- a/storage/test/files.test.js +++ b/storage/test/files.test.js @@ -415,66 +415,6 @@ describe('storage:files', function () { }); }); }); - describe('main', function () { - it('should call the right commands', function () { - var filesSample = getSample(); - var fileName = 'text.txt'; - - filesSample.sample.main(['list', bucketName], function (err, files) { - assert.ifError(err); - assert.strictEqual(files, filesSample.mocks.files); - assert(console.log.calledWith('Found %d files!', filesSample.mocks.files.length)); - }); - filesSample.mocks.bucket.getFiles = sinon.stub().callsArgWith(1, null, filesSample.mocks.files); - filesSample.sample.main(['listByPrefix', bucketName, '/a'], function (err, files) { - assert.ifError(err); - assert.strictEqual(files, filesSample.mocks.files); - assert(console.log.calledWith('Found %d files!', filesSample.mocks.files.length)); - }); - filesSample.sample.main(['upload', bucketName, fileName], function (err, file) { - assert.ifError(err); - assert.strictEqual(file, filesSample.mocks.files[0]); - assert(console.log.calledWith('Uploaded file: %s', fileName)); - }); - filesSample.sample.main(['download', bucketName, fileName, fileName], function (err) { - assert.ifError(err); - assert(console.log.calledWith('Downloaded %s to %s', fileName, fileName)); - }); - filesSample.sample.main(['delete', bucketName, fileName], function (err) { - assert.ifError(err); - assert(console.log.calledWith('Deleted file: %s', fileName)); - }); - filesSample.sample.main(['getMetadata', bucketName, fileName], function (err) { - assert.ifError(err); - assert(console.log.calledWith('Got metadata for file: %s', fileName)); - }); - filesSample.sample.main(['makePublic', bucketName, fileName], function (err) { - assert.ifError(err); - assert(console.log.calledWith('Made %s public!', fileName)); - }); - filesSample.sample.main(['move', bucketName, fileName, fileName], function (err) { - assert.ifError(err); - assert(console.log.calledWith('%s moved to %s', fileName, fileName)); - }); - filesSample.sample.main(['copy', bucketName, fileName, bucketName, fileName], function (err) { - assert.ifError(err); - assert(console.log.calledWith('%s moved to %s in %s', fileName, fileName, bucketName)); - }); - filesSample.sample.main(['foo'], function () { - assert(console.log.calledWith('Usage: node files [COMMAND] [ARGS...]')); - assert(console.log.calledWith('\nCommands:\n')); - assert(console.log.calledWith('\tlist [BUCKET_NAME]')); - assert(console.log.calledWith('\tlistByPrefix [BUCKET_NAME] [PREFIX] [DELIMITER]')); - assert(console.log.calledWith('\tupload [BUCKET_NAME] [FILE_NAME]')); - assert(console.log.calledWith('\tdownload [BUCKET_NAME] [SRC_FILE_NAME] [DEST_FILE_NAME]')); - assert(console.log.calledWith('\tdelete [BUCKET_NAME] [FILE_NAME]')); - assert(console.log.calledWith('\tgetMetadata [BUCKET_NAME] [FILE_NAME]')); - assert(console.log.calledWith('\tmakePublic [BUCKET_NAME] [FILE_NAME]')); - assert(console.log.calledWith('\tmove [BUCKET_NAME] [SRC_FILE_NAME] [DEST_FILE_NAME]')); - assert(console.log.calledWith('\tcopy [BUCKET_NAME] [SRC_FILE_NAME] [DEST_BUCKET_NAME] [DEST_FILE_NAME]')); - }); - }); - }); describe('printUsage', function () { it('should print usage', function () { var filesSample = getSample(); @@ -494,4 +434,49 @@ describe('storage:files', function () { assert(console.log.calledWith('\tcopy [BUCKET_NAME] [SRC_FILE_NAME] [DEST_BUCKET_NAME] [DEST_FILE_NAME]')); }); }); + describe('main', function () { + it('should call the right commands', function () { + var program = getSample().sample; + + sinon.stub(program, 'listFiles') + program.main(['list']); + assert(program.listFiles.calledOnce); + + sinon.stub(program, 'listFilesWithPrefix') + program.main(['listByPrefix']); + assert(program.listFilesWithPrefix.calledOnce); + + sinon.stub(program, 'uploadFile') + program.main(['upload']); + assert(program.uploadFile.calledOnce); + + sinon.stub(program, 'downloadFile') + program.main(['download']); + assert(program.downloadFile.calledOnce); + + sinon.stub(program, 'deleteFile') + program.main(['delete']); + assert(program.deleteFile.calledOnce); + + sinon.stub(program, 'getMetadata') + program.main(['getMetadata']); + assert(program.getMetadata.calledOnce); + + sinon.stub(program, 'makePublic') + program.main(['makePublic']); + assert(program.makePublic.calledOnce); + + sinon.stub(program, 'moveFile') + program.main(['move']); + assert(program.moveFile.calledOnce); + + sinon.stub(program, 'copyFile') + program.main(['copy']); + assert(program.copyFile.calledOnce); + + sinon.stub(program, 'printUsage') + program.main(['--help']); + assert(program.printUsage.calledOnce); + }); + }); });