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

Commit

Permalink
test(impl): Additional implementation tests and base input validation…
Browse files Browse the repository at this point in the history
… tests.

Added input validation tests for both implementations and new tests specifically for the filesystem
implementation.
  • Loading branch information
mikeal committed Jul 17, 2017
1 parent bb65f4c commit 3ef5e6b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 43 deletions.
32 changes: 2 additions & 30 deletions fs.js
Expand Up @@ -8,7 +8,6 @@ const isDirectory = dir => fs.statSync(dir).isDirectory()

class FileSystemContentAddressableStorage {
constructor (dir, algo = 'sha256', _createHasher = createHasher) {
console.log(dir)
if (!isDirectory(dir)) throw new Error('Not a directory.')
this.dir = dir
this._algo = algo
Expand All @@ -18,7 +17,7 @@ class FileSystemContentAddressableStorage {
if (Buffer.isBuffer(value)) {
return this._setBuffer(value, cb)
}
if (typeof value === 'object' && value.readable) {
if (value && typeof value === 'object' && value.readable) {
return this._setStream(value, cb)
}
process.nextTick(() => cb(new Error('value is a not a valid type')))
Expand All @@ -30,7 +29,7 @@ class FileSystemContentAddressableStorage {
hasher.end()
return
}
if (typeof value === 'object' && value.readable) {
if (value && typeof value === 'object' && value.readable) {
return value.pipe(hasher)
}
process.nextTick(() => cb(new Error('value is a not a valid type')))
Expand Down Expand Up @@ -70,33 +69,6 @@ class FileSystemContentAddressableStorage {
value.pipe(hasher)
}

// _setStream (value, cb) {
// cb = once(cb)
// let hash
// let hasher = util.promisify(this._createHasher)(this._algo)
// hasher.then(_hash => {
// console.log('hash', hash)
// hash = _hash
// fs.rename(tmpfile, path.join(this.dir, hash), err => {
// if (err) return cb(err)
// cb(null, hash)
// })
// })
// let tmpfile = path.join(this.dir, '.' + Date.now() + Math.random())
// let filepromise = new Promise((resolve, reject) => {
// let file = fs.createWriteStream(tmpfile)
// file.on('error', err => reject(err))
// file.on('close', () => resolve())
// value.pipe(file)
// })
// let all = Promise.all(hasher, filepromise)
// all.then(() => {
// fs.rename(tmpfile, path.join(this.dir, hash), err => {
// if (err) return cb(err)
// cb(null, hash)
// })
// })
// }
getBuffer (hash, cb) {
fs.readFile(path.join(this.dir, hash), cb)
}
Expand Down
15 changes: 10 additions & 5 deletions inmemory.js
Expand Up @@ -40,8 +40,10 @@ class InMemoryContentAddressableStorage {
hasher.end()
return
}
// TODO: validate value is a stream
value.pipe(hasher)
if (value && typeof value === 'object' && value.readable) {
return value.pipe(hasher)
}
process.nextTick(() => cb(new Error('value is a not a valid type')))
}

set (value, cb) {
Expand All @@ -58,9 +60,12 @@ class InMemoryContentAddressableStorage {
hasher.end()
return
}
// TODO: validate value is a stream.
value.pipe(hasher)
value.pipe(_value)
if (value && typeof value === 'object' && value.readable) {
value.pipe(hasher)
value.pipe(_value)
return
}
process.nextTick(() => cb(new Error('value is a not a valid type')))
}
}

Expand Down
18 changes: 18 additions & 0 deletions lib/test-basics.js
Expand Up @@ -107,5 +107,23 @@ module.exports = (name, store) => {
stream.pipe(b)
})
})

test(`${name}: set invalid values`, t => {
t.plan(4)
store.set({}, err => t.type(err, 'Error'))
store.set(null, err => t.type(err, 'Error'))
store.set('asdf', err => t.type(err, 'Error'))
store.set(1123454, err => t.type(err, 'Error'))
})

test(`${name}: hash invalid values`, t => {
t.plan(4)
store.hash({}, err => t.type(err, 'Error'))
store.hash(null, err => t.type(err, 'Error'))
store.hash('asdf', err => t.type(err, 'Error'))
store.hash(1123454, err => t.type(err, 'Error'))
})


}

43 changes: 35 additions & 8 deletions tests/test-fs.js
Expand Up @@ -5,16 +5,43 @@ const testdir = fs.mkdtempSync(path.join(__dirname, '.testtmp-'))

require('../lib/test-basics')('fs', fsStore(testdir))

let test = require('tap').test
const test = require('tap').test
const through = require('through2')

const failHasher = (algo, cb) => {
process.nextTick(() => cb(new Error('Test Error')))
return through(() => {})
}

test('fs(implementation): directory does not exist', t => {
t.plan(1)
try {
fsStore('falskdjfoaisdfjoadfjodi8f9823')
} catch (e) {
t.type(e, 'Error')
}
})

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

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

let rimraf = require('rimraf')

process.on('beforeExit', () => {
rimraf.sync(testdir)
})

// test('teardown', t => {
// t.plan(1)
// rimraf(testdir, err => {
// t.error(err)
// })
// })

0 comments on commit 3ef5e6b

Please sign in to comment.