-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
testCommon.js
73 lines (60 loc) · 1.57 KB
/
testCommon.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
var path = require('path')
var fs = !process.browser && require('fs')
var rimraf = !process.browser && require('rimraf')
var dbidx = 0
var location = function () {
return path.join(__dirname, '_leveldown_test_db_' + dbidx++)
}
var lastLocation = function () {
return path.join(__dirname, '_leveldown_test_db_' + dbidx)
}
var cleanup = function (callback) {
if (process.browser) { return process.nextTick(callback) }
fs.readdir(__dirname, function (err, list) {
if (err) return callback(err)
list = list.filter(function (f) {
return (/^_leveldown_test_db_/).test(f)
})
if (!list.length) { return callback() }
var ret = 0
list.forEach(function (f) {
rimraf(path.join(__dirname, f), function (err) {
if (err) return callback(err)
if (++ret === list.length) { callback() }
})
})
})
}
var setUp = function (t) {
cleanup(function (err) {
t.error(err, 'cleanup returned an error')
t.end()
})
}
var tearDown = function (t) {
setUp(t) // same cleanup!
}
var collectEntries = function (iterator, callback) {
var data = []
var next = function () {
iterator.next(function (err, key, value) {
if (err) return callback(err)
if (!arguments.length) {
return iterator.end(function (err) {
callback(err, data)
})
}
data.push({ key: key, value: value })
setTimeout(next, 0)
})
}
next()
}
module.exports = {
location: location,
cleanup: cleanup,
lastLocation: lastLocation,
setUp: setUp,
tearDown: tearDown,
collectEntries: collectEntries
}