1 | const path = require('path')
|
2 | const fs = require('fs')
|
3 | const bl = require('bl')
|
4 | const fsStore = require('../fs')
|
5 | const testdir = fs.mkdtempSync(path.join(__dirname, '.testtmp-'))
|
6 |
|
7 | require('../lib/test-basics')('fs', fsStore(testdir))
|
8 |
|
9 | const test = require('tap').test
|
10 | const through = require('through2')
|
11 |
|
12 | const failHasher = cb => {
|
13 | process.nextTick(() => cb(new Error('Test Error')))
|
14 | return through(() => {})
|
15 | }
|
16 |
|
17 | test('fs(implementation): directory does not exist', t => {
|
18 | t.plan(1)
|
19 | t.throws(() => {
|
20 | fsStore('falskdjfoaisdfjoadfjodi8f9823')
|
21 | })
|
22 | })
|
23 |
|
24 | test('fs(implementation): hash error in hash()', t => {
|
25 | t.plan(1)
|
26 | let store = fsStore(testdir, failHasher)
|
27 | store.hash(Buffer.from('asdf'), err => {
|
28 | t.type(err, 'Error')
|
29 | })
|
30 | })
|
31 |
|
32 | test('fs(implementation): hash error in set()', t => {
|
33 | t.plan(2)
|
34 | let store = fsStore(testdir, failHasher)
|
35 | store.set(Buffer.from('asdf'), err => {
|
36 | t.type(err, 'Error')
|
37 | })
|
38 | let stream = bl()
|
39 | store.set(stream, err => {
|
40 | t.type(err, 'Error')
|
41 | })
|
42 | stream.write(Buffer.from('asdf'))
|
43 | stream.end()
|
44 | })
|
45 |
|
46 | test('fs(implementation): filesystem errors, fs.rename()', t => {
|
47 | t.plan(1)
|
48 | let _rename = fs.rename
|
49 | fs.rename = (x, y, cb) => cb(new Error('Test Error.'))
|
50 | let store = fsStore(testdir)
|
51 | let stream = bl()
|
52 | store.set(stream, err => {
|
53 | fs.rename = _rename
|
54 | t.type(err, 'Error')
|
55 | })
|
56 | stream.write(Buffer.from('asdf'))
|
57 | stream.end()
|
58 | })
|
59 |
|
60 | test('fs(implementation): filesystem errors, fs.writeFile()', t => {
|
61 | t.plan(1)
|
62 | let _writeFile = fs.writeFile
|
63 | fs.writeFile = (path, buff, cb) => cb(new Error('Test Error.'))
|
64 | let store = fsStore(testdir)
|
65 | store.set(Buffer.from('asdf'), err => {
|
66 | fs.writeFile = _writeFile
|
67 | t.type(err, 'Error')
|
68 | })
|
69 | })
|
70 |
|
71 | test('fs(implementation): slow hasher', t => {
|
72 | t.plan(2)
|
73 | const slowHasher = cb => {
|
74 | return through(() => setTimeout(() => cb(null, 'asdf'), 100))
|
75 | }
|
76 | let store = fsStore(testdir, slowHasher)
|
77 | let stream = bl()
|
78 | store.set(stream, (err, hash) => {
|
79 | t.error(err)
|
80 | t.equal(hash, 'asdf')
|
81 | })
|
82 | stream.write(Buffer.from('asdf'))
|
83 | stream.end()
|
84 | })
|
85 |
|
86 | test('fs(implementation): hasher args', t => {
|
87 | t.plan(4)
|
88 | const argHasher = (one, two, three, cb) => {
|
89 | t.same([one, two, three], [1, 2, 3])
|
90 | return through(() => setTimeout(() => cb(null, 'asdf'), 100))
|
91 | }
|
92 | let store = fsStore(testdir, argHasher)
|
93 | store.set(Buffer.from('asdf'), 1, 2, 3, (err, hash) => {
|
94 | t.error(err)
|
95 | })
|
96 | store.hash(Buffer.from('asdf'), 1, 2, 3, (err, hash) => {
|
97 | t.error(err)
|
98 | })
|
99 | })
|
100 |
|
101 | let rimraf = require('rimraf')
|
102 |
|
103 | process.on('beforeExit', () => {
|
104 | rimraf.sync(testdir)
|
105 | })
|