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
|
'use strict'
const common = require('../common-tap.js')
const getStream = require('get-stream')
const npm = require('../../')
const test = require('tap').test
const mkdirp = require('mkdirp')
const rimraf = require('rimraf')
const path = require('path')
const fs = require('fs')
const ms = require('mississippi')
const _createCacheWriteStream = require('../../lib/search/all-package-metadata.js')._createCacheWriteStream
const PKG_DIR = path.resolve(__dirname, 'create-cache-write-stream')
const CACHE_DIR = path.resolve(PKG_DIR, 'cache')
function setup () {
mkdirp.sync(CACHE_DIR)
}
function cleanup () {
rimraf.sync(PKG_DIR)
}
function fromArray (array) {
var idx = 0
return ms.from.obj(function (size, next) {
next(null, array[idx++] || null)
})
}
test('setup', function (t) {
// This is pretty much only used for `getCacheStat` in the implementation
npm.load({ cache: CACHE_DIR, registry: common.registry }, function (err) {
t.ifError(err, 'npm successfully loaded')
t.done()
})
})
test('createCacheEntryStream basic', function (t) {
setup()
var cachePath = path.join(CACHE_DIR, '.cache.json')
var latest = 12345
var src = [
{ name: 'bar', version: '1.0.0' },
{ name: 'foo', version: '1.0.0' }
]
var srcStream = fromArray(src)
return _createCacheWriteStream(cachePath, latest, {
cache: CACHE_DIR
}).then(stream => {
t.ok(stream, 'returned a stream')
stream = ms.pipeline.obj(srcStream, stream)
return getStream.array(stream)
}).then(results => {
t.deepEquals(results, [{
name: 'bar',
version: '1.0.0'
}, {
name: 'foo',
version: '1.0.0'
}])
var fileData = JSON.parse(fs.readFileSync(cachePath))
t.ok(fileData, 'cache contents written to the right file')
t.deepEquals(fileData, {
'_updated': latest,
bar: {
name: 'bar',
version: '1.0.0'
},
foo: {
name: 'foo',
version: '1.0.0'
}
}, 'cache contents based on what was written')
cleanup()
})
})
test('createCacheEntryStream no entries', function (t) {
setup()
const cachePath = path.join(CACHE_DIR, '.cache.json')
var latest = 12345
const src = []
const srcStream = fromArray(src)
return _createCacheWriteStream(cachePath, latest, {
cache: CACHE_DIR
}).then(stream => {
t.ok(stream, 'returned a stream')
stream = ms.pipeline.obj(srcStream, stream)
stream.resume()
return getStream(stream)
}).then(() => {
const fileData = JSON.parse(fs.readFileSync(cachePath))
t.ok(fileData, 'cache file exists and has stuff in it')
cleanup()
})
})
test('createCacheEntryStream missing cache dir', function (t) {
setup()
var cachePath = path.join(CACHE_DIR, '.cache.json')
var latest = 12345
var src = []
var srcStream = fromArray(src)
return _createCacheWriteStream(cachePath, latest, {
cache: CACHE_DIR
}).then(stream => {
t.ok(stream, 'returned a stream')
stream = ms.pipeline.obj(srcStream, stream)
return getStream.array(stream)
}).then(res => {
t.deepEqual(res, [], 'no data returned')
var fileData = JSON.parse(fs.readFileSync(cachePath))
t.ok(fileData, 'cache contents written to the right file')
t.deepEquals(fileData, {
'_updated': latest
}, 'cache still contains `_updated`')
cleanup()
})
})
|