-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathnode.js
122 lines (109 loc) · 2.88 KB
/
node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* eslint-env mocha */
'use strict'
const ncp = require('ncp').ncp
const rimraf = require('rimraf')
const fs = require('fs')
const path = require('path')
const promisify = require('util').promisify
const os = require('os')
const { LockExistsError } = require('../src/errors')
const chai = require('chai')
chai.use(require('dirty-chai'))
const asyncRimraf = promisify(rimraf)
const asyncNcp = promisify(ncp)
const fsstat = promisify(fs.stat)
const IPFSRepo = require('../src')
async function createTempRepo (options = {}) {
const date = Date.now().toString()
const repoPath = path.join(os.tmpdir(), 'test-repo-for-' + date)
await asyncNcp(path.join(__dirname, 'test-repo'), repoPath)
const repo = new IPFSRepo(repoPath, options)
await repo.open()
return repo
}
describe('IPFS Repo Tests onNode.js', () => {
require('./options-test')
require('./migrations-test')(createTempRepo)
const customLock = {
lockName: 'test.lock',
lock: async (dir) => {
const isLocked = await customLock.locked(dir)
if (isLocked) {
throw new LockExistsError('already locked')
}
const lockPath = path.join(dir, customLock.lockName)
fs.writeFileSync(lockPath, '')
return {
close: () => asyncRimraf(lockPath)
}
},
locked: async (dir) => {
try {
await fsstat(path.join(dir, customLock.lockName))
return true
} catch (err) {
return false
}
}
}
const repos = [
{
name: 'default inited',
opts: undefined,
init: true
},
{
name: 'memory',
opts: {
fs: require('interface-datastore').MemoryDatastore,
level: require('memdown'),
lock: 'memory'
},
init: true
},
{
name: 'custom locker',
opts: {
lock: customLock
},
init: true
},
{
name: 'default existing',
opts: undefined,
init: false
}
]
repos.forEach((r) => describe(r.name, () => {
const testRepoPath = path.join(__dirname, 'test-repo')
const date = Date.now().toString()
const repoPath = path.join(os.tmpdir(), 'test-repo-for-' + date)
const repo = new IPFSRepo(repoPath, r.opts)
before(async () => {
if (r.init) {
await repo.init({})
} else {
await asyncNcp(testRepoPath, repoPath)
}
await repo.open()
})
after(async () => {
await repo.close()
await asyncRimraf(repoPath)
})
require('./repo-test')(repo)
require('./blockstore-test')(repo)
require('./datastore-test')(repo)
require('./keystore-test')(repo)
require('./stat-test')(repo)
require('./lock-test')(repo)
require('./config-test')(repo)
require('./api-addr-test')(repo)
if (!r.init) {
require('./interop-test')(repo)
}
require('./pins-test')(repo)
require('./is-initialized')
}))
require('./blockstore-utils-test')()
})