From 9b5159f21af14c35adc8312e5732ca98a7da0b96 Mon Sep 17 00:00:00 2001 From: brainsandspace Date: Sun, 19 Mar 2017 20:19:57 -0400 Subject: [PATCH 1/2] added hash-files.promise --- README.md | 9 ++++- lib/hash.js | 16 ++++++++ test/hash.tests.js | 84 +++++++++++++++++++++++++++++++++++++++++ test/jshint/config.json | 4 +- 4 files changed, 110 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 849f403..c83a44d 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ a29089cc5e3f8bf6ae15ea6b9cd5eaefb14bbb12e3baa2c56ee5c21422250c75 ### hashFiles([options], callback) -Performs a hash of the contents of the given files ansynchronously. +Performs a hash of the contents of the given files asynchronously. * `options` - (Object) see below for more details * `callback` - (Function) called when an error occurs or the hash is completed. Should expect the following arguments: @@ -63,6 +63,13 @@ Performs a hash of the contents of the given files synchronously. * `options` - (Object) see below for more details * returns `hash` - (String) the value of the computed hash +### hashFiles.promise([options]) + +Performs a hash of the contents of the given files asynchronously and returns a promise. + +* `options` - (Object) see below for more details +* returns `Promise` - (Promise) resolves to the value of the computed hash, or rejects if an error occurred + ### Options * `files` - (optional) A collection of file paths to hash the contents of. Defaults to `['./**']` (all the files in the current working directory) diff --git a/lib/hash.js b/lib/hash.js index 3799845..3cd7cd2 100644 --- a/lib/hash.js +++ b/lib/hash.js @@ -105,6 +105,22 @@ function hashFilesSync(options) { return hash.digest('hex'); } +function hashFilesPromise(options, other) { + options = options || {}; + + if (other) { + throw new Error('hashFilesPromise only takes 1 parameter. Consider using hashFiles if you would like to use a callback instead of a promise.'); + } + + return new Promise(function(resolve, reject) { + hashFiles(options, function(err, hash) { + if (err) { reject(err); } + else { resolve(hash); } + }); + }); +} + hashFiles.sync = hashFilesSync; +hashFiles.promise = hashFilesPromise; module.exports = hashFiles; \ No newline at end of file diff --git a/test/hash.tests.js b/test/hash.tests.js index 130bc31..88be9ad 100644 --- a/test/hash.tests.js +++ b/test/hash.tests.js @@ -205,4 +205,88 @@ describe('hash-files Unit Tests', function() { }); + /* + 888 888 .d888 d8b 888 d8b + 888 888 d88P" Y8P 888 Y8P + 888 888 888 888 + 88888b. 8888b. .d8888b 88888b. 888888 888 888 .d88b. .d8888b 88888b. 888d888 .d88b. 88888b.d88b. 888 .d8888b .d88b. + 888 "88b "88b 88K 888 "88b 888 888 888 d8P Y8b 88K 888 "88b 888P" d88""88b 888 "888 "88b 888 88K d8P Y8b + 888 888 .d888888 "Y8888b. 888 888 888888 888 888 888 88888888 "Y8888b. 888888 888 888 888 888 888 888 888 888 888 "Y8888b. 88888888 + 888 888 888 888 X88 888 888 888 888 888 Y8b. X88 888 d88P 888 Y88..88P 888 888 888 888 X88 Y8b. + 888 888 "Y888888 88888P' 888 888 888 888 888 "Y8888 88888P' 88888P" 888 "Y88P" 888 888 888 888 88888P' "Y8888 + 888 + 888 + 888 + */ + describe('hash-files-promise', function() { + + it('should return a promise that resolves to hash contents of directory with defaults', function(done) { + var hash = hashFiles.promise(); + hash.then(function(hash) { + assert(hash === expectedContentsSha1); + done(); + }) + }); + + it('should return a promise that resolves to hash contents of files with md5 algorithm', function(done) { + var hash = hashFiles.promise({ files: ['/some/root/*'], algorithm: 'md5' }); + hash.then(function(hash) { + assert(hash === expectedContentsMd5); + done(); + }); + }); + + it('should return a promise that resolves to hash contents of files without globbing', function(done) { + var hash = hashFiles.promise({ files: ['z_file1.txt', 'some/dir/file2.txt'], noGlob: true }); + hash.then(function(hash) { + assert(hash === expectedContentsSha1); + done(); + }); + }); + + it('should reject proimse if unknown algorithm', function(done) { + // throw new Error + var hash = hashFiles.promise({ algorithm: 'fnord' }); + hash.catch(function(err) { + assert.throws(function(err) { + throw new Error; + }); + }); + done(); + }); + + it('should reject promise if globbing fails', function(done) { + + failGlobbing = true; + + var hash = hashFiles.promise(); + hash.catch(function(err) { + assert.throws(function(err) { + throw new Err; + }); + }); + done(); + }); + + it('should reject promise if readFiles fails', function(done) { + + failReadFile = true; + + var hash = hashFiles.promise(); + hash.catch(function(err) { + assert.throws(function(err) { + throw new Err; + }); + }); + done(); + }); + + it('should throw an error if more than one parameter is entered', function(done) { + assert.throws(function() { + hashFiles.promise({ files: ['/some/root/*'] }, 1) + }); + done(); + }); + }); + }); \ No newline at end of file diff --git a/test/jshint/config.json b/test/jshint/config.json index ef4cb71..645581e 100644 --- a/test/jshint/config.json +++ b/test/jshint/config.json @@ -27,7 +27,7 @@ "boss": true, "debug": false, "eqnull": true, - "esnext": false, + "esnext": true, "evil": false, "expr": false, "funcscope": false, @@ -46,4 +46,4 @@ "validthis": false, "node": true -} \ No newline at end of file + } \ No newline at end of file From 941e6005a243f4b016170e5483135c6ee5b5a3f2 Mon Sep 17 00:00:00 2001 From: brainsandspace Date: Sun, 19 Mar 2017 20:22:58 -0400 Subject: [PATCH 2/2] clean up --- test/hash.tests.js | 6 +++--- test/jshint/config.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/hash.tests.js b/test/hash.tests.js index 88be9ad..4fa0b0b 100644 --- a/test/hash.tests.js +++ b/test/hash.tests.js @@ -214,9 +214,9 @@ describe('hash-files Unit Tests', function() { 888 888 .d888888 "Y8888b. 888 888 888888 888 888 888 88888888 "Y8888b. 888888 888 888 888 888 888 888 888 888 888 "Y8888b. 88888888 888 888 888 888 X88 888 888 888 888 888 Y8b. X88 888 d88P 888 Y88..88P 888 888 888 888 X88 Y8b. 888 888 "Y888888 88888P' 888 888 888 888 888 "Y8888 88888P' 88888P" 888 "Y88P" 888 888 888 888 88888P' "Y8888 - 888 - 888 - 888 + 888 + 888 + 888 */ describe('hash-files-promise', function() { diff --git a/test/jshint/config.json b/test/jshint/config.json index 645581e..35a2715 100644 --- a/test/jshint/config.json +++ b/test/jshint/config.json @@ -46,4 +46,4 @@ "validthis": false, "node": true - } \ No newline at end of file +} \ No newline at end of file