-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathls.js
151 lines (144 loc) · 3.85 KB
/
ls.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict'
const CacheIndex = require('./fixtures/cache-index')
const contentPath = require('../lib/content/path')
const index = require('../lib/entry-index.js')
const t = require('tap')
const { ls } = require('..')
t.test('basic listing', async t => {
const contents = {
whatever: {
key: 'whatever',
integrity: 'sha512-deadbeef',
time: 12345,
metadata: 'omgsometa',
size: 234234,
},
whatnot: {
key: 'whatnot',
integrity: 'sha512-bada55',
time: 54321,
metadata: null,
size: 425345345,
},
}
const CACHE = t.testdir(CacheIndex(contents))
contents.whatever.path = contentPath(CACHE, contents.whatever.integrity)
contents.whatnot.path = contentPath(CACHE, contents.whatnot.integrity)
const listing = await ls(CACHE)
t.same(listing, contents, 'index contents correct')
const newListing = {}
const stream = ls.stream(CACHE)
stream.on('data', (entry) => {
newListing[entry.key] = entry
})
await stream.promise()
t.same(newListing, contents, 'ls is streamable')
})
t.test('separate keys in conflicting buckets', async t => {
const contents = {
whatever: {
key: 'whatever',
integrity: 'sha512-deadbeef',
time: 12345,
metadata: 'omgsometa',
size: 5,
},
whatev: {
key: 'whatev',
integrity: 'sha512-bada55',
time: 54321,
metadata: null,
size: 99234234,
},
}
const CACHE = t.testdir(
CacheIndex({
// put both in the same bucket
whatever: [contents.whatever, contents.whatev],
})
)
contents.whatever.path = contentPath(CACHE, contents.whatever.integrity)
contents.whatev.path = contentPath(CACHE, contents.whatev.integrity)
const listing = await ls(CACHE)
t.same(listing, contents, 'index contents correct')
})
t.test('works fine on an empty/missing cache', async t => {
const CACHE = t.testdir()
const listing = await ls(CACHE)
t.same(listing, {}, 'returned an empty listing')
})
t.test('ignores non-dir files', async t => {
const index = CacheIndex({
whatever: {
key: 'whatever',
integrity: 'sha512-deadbeef',
time: 12345,
metadata: 'omgsometa',
size: 234234,
},
})
index.garbage = 'hello world'
const CACHE = t.testdir(index)
const listing = await ls(CACHE)
t.equal(Object.keys(listing).length, 1, 'only 1 item in listing')
t.equal(listing.whatever.key, 'whatever', 'only the correct entry listed')
})
t.test('correctly ignores deleted entries', (t) => {
const contents = {
whatever: {
key: 'whatever',
integrity: 'sha512-deadbeef',
time: 12345,
metadata: 'omgsometa',
size: 234234,
},
whatnot: {
key: 'whatnot',
integrity: 'sha512-bada55',
time: 54321,
metadata: null,
size: 425345345,
},
whatwhere: {
key: 'whatwhere',
integrity: 'sha512-bada55e5',
time: 54321,
metadata: null,
size: 425345345,
},
}
const CACHE = t.testdir(CacheIndex(contents))
contents.whatever.path = contentPath(CACHE, contents.whatever.integrity)
contents.whatnot.path = contentPath(CACHE, contents.whatnot.integrity)
contents.whatwhere.path = contentPath(CACHE, contents.whatwhere.integrity)
return index
.delete(CACHE, 'whatnot')
.then(() => ls(CACHE))
.then((listing) =>
t.same(
listing,
{
whatever: contents.whatever,
whatwhere: contents.whatwhere,
},
'index contents correct'
)
)
.then(() => {
const listing = {}
const stream = ls.stream(CACHE)
stream.on('data', (entry) => {
listing[entry.key] = entry
})
return stream.promise().then(() =>
t.same(
listing,
{
whatever: contents.whatever,
whatwhere: contents.whatwhere,
},
'ls is streamable'
)
)
})
})