Skip to content
This repository has been archived by the owner on Jul 14, 2019. It is now read-only.

Commit

Permalink
test(impl): Improved hasher tests.
Browse files Browse the repository at this point in the history
Tests now use argument instead of resetting internals. Also added standard to test command and
disabled coverage on a line that is tested but not being picked up by coverage.
  • Loading branch information
mikeal committed Jul 17, 2017
1 parent 6565fee commit c54d351
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
4 changes: 3 additions & 1 deletion fs.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const fs = require('fs')
const path = require('path')
const once = require('once')
const util = require('util')
const createHasher = require('hashes-stream')

const isDirectory = dir => fs.statSync(dir).isDirectory()

class FileSystemContentAddressableStorage {
constructor (dir, algo = 'sha256', _createHasher = createHasher) {
/* This statement is tested but because it gets wrapped in a try/catch
the coverage report doesn't notice. */
/* istanbul ignore if */
if (!isDirectory(dir)) throw new Error('Not a directory.')
this.dir = dir
this._algo = algo
Expand Down
3 changes: 0 additions & 3 deletions lib/test-basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,4 @@ module.exports = (name, store) => {
store.hash('asdf', err => t.type(err, 'Error'))
store.hash(1123454, err => t.type(err, 'Error'))
})


}

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "tests"
},
"scripts": {
"test": "tap tests/*.js",
"test": "tap tests/*.js && standard",
"cover": "tap tests/*.js --cov --coverage-report=lcov",
"precommit": "npm test",
"prepush": "npm test",
Expand All @@ -33,6 +33,7 @@
"husky": "^0.14.3",
"rimraf": "^2.6.1",
"semantic-release": "^6.3.6",
"standard": "^10.0.2",
"validate-commit-msg": "^2.12.2"
},
"config": {
Expand Down
21 changes: 17 additions & 4 deletions tests/test-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ test('fs(implementation): directory does not exist', t => {

test('fs(implementation): hash error in hash()', t => {
t.plan(1)
let store = fsStore(testdir)
store._createHasher = failHasher
let store = fsStore(testdir, 'noop', failHasher)
store.hash(Buffer.from('asdf'), err => {
t.type(err, 'Error')
})
})

test('fs(implementation): hash error in set()', t => {
t.plan(2)
let store = fsStore(testdir)
store._createHasher = failHasher
let store = fsStore(testdir, 'noop', failHasher)
store.set(Buffer.from('asdf'), err => {
t.type(err, 'Error')
})
Expand Down Expand Up @@ -70,6 +68,21 @@ test('fs(implementation): filesystem errors, fs.writeFile()', t => {
})
})

test('fs(implementation): slow hasher', t => {
t.plan(2)
const slowHasher = (algo, cb) => {
return through(() => setTimeout(() => cb(null, 'asdf'), 100))
}
let store = fsStore(testdir, 'noop', slowHasher)
let stream = bl()
store.set(stream, (err, hash) => {
t.error(err)
t.equal(hash, 'asdf')
})
stream.write(Buffer.from('asdf'))
stream.end()
})

let rimraf = require('rimraf')

process.on('beforeExit', () => {
Expand Down
6 changes: 2 additions & 4 deletions tests/test-inmemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ const failHasher = (algo, cb) => {

test('inmemory: (implementation) hash error in hash()', t => {
t.plan(1)
let store = inmemory()
store._createHasher = failHasher
let store = inmemory('noop', failHasher)
store.hash(Buffer.from('asdf'), err => {
t.type(err, 'Error')
})
})

test('inmemory: (implementation) hash error in set()', t => {
t.plan(1)
let store = inmemory()
store._createHasher = failHasher
let store = inmemory('noop', failHasher)
store.set(Buffer.from('asdf'), err => {
t.type(err, 'Error')
})
Expand Down

0 comments on commit c54d351

Please sign in to comment.