Skip to content

Commit f813948

Browse files
committed
wip: fixing up search tests
1 parent 814a10c commit f813948

File tree

3 files changed

+98
-133
lines changed

3 files changed

+98
-133
lines changed

lib/search/all-package-metadata.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ function createEntryUpdateStream (staleness, latest, opts) {
152152
}
153153
return npmFetch(uri, opts).then(res => {
154154
log.silly('all-package-metadata', 'request stream opened, code:', res.statusCode)
155-
// NOTE - The stream returned by `request` seems to be very persnickety
156-
// and this is almost a magic incantation to get it to work.
157-
// Modify how `res` is used here at your own risk.
158155
let entryStream = ms.pipeline.obj(
159156
res.body,
160157
JSONStream.parse('*', (pkg, key) => {
@@ -199,15 +196,12 @@ function extractUpdated (entryStream, label, opts) {
199196
log.silly('all-package-metadata', 'got first stream entry for', label, latest)
200197
entryStream.removeListener('error', onErr)
201198
entryStream.removeListener('end', onEnd)
202-
// Because `.once()` unpauses the stream, we re-pause it after the first
203-
// entry so we don't vomit entries into the void.
204-
entryStream.pause()
205199
if (typeof latest === 'number') {
206200
// The extra pipeline is to return a stream that will implicitly unpause
207201
// after having an `.on('data')` listener attached, since using this
208202
// `data` event broke its initial state.
209203
resolve({
210-
updateStream: entryStream,
204+
updateStream: entryStream.pipe(ms.through.obj()),
211205
updatedLatest: latest
212206
})
213207
} else {
Lines changed: 48 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
'use strict'
22

33
require('../common-tap.js')
4-
var test = require('tap').test
5-
var mkdirp = require('mkdirp')
6-
var rimraf = require('rimraf')
7-
var path = require('path')
8-
var ms = require('mississippi')
9-
var Tacks = require('tacks')
10-
var File = Tacks.File
114

12-
var _createCacheEntryStream = require('../../lib/search/all-package-metadata.js')._createCacheEntryStream
5+
const getStream = require('get-stream')
6+
const mkdirp = require('mkdirp')
7+
const path = require('path')
8+
const rimraf = require('rimraf')
9+
const Tacks = require('tacks')
10+
const {test} = require('tap')
1311

14-
var PKG_DIR = path.resolve(__dirname, 'create-cache-entry-stream')
15-
var CACHE_DIR = path.resolve(PKG_DIR, 'cache')
12+
const {File} = Tacks
13+
14+
const _createCacheEntryStream = require('../../lib/search/all-package-metadata.js')._createCacheEntryStream
15+
16+
const PKG_DIR = path.resolve(__dirname, 'create-cache-entry-stream')
17+
const CACHE_DIR = path.resolve(PKG_DIR, 'cache')
1618

1719
function setup () {
1820
mkdirp.sync(CACHE_DIR)
@@ -22,10 +24,10 @@ function cleanup () {
2224
rimraf.sync(PKG_DIR)
2325
}
2426

25-
test('createCacheEntryStream basic', function (t) {
27+
test('createCacheEntryStream basic', t => {
2628
setup()
27-
var cachePath = path.join(CACHE_DIR, '.cache.json')
28-
var fixture = new Tacks(File({
29+
const cachePath = path.join(CACHE_DIR, '.cache.json')
30+
const fixture = new Tacks(File({
2931
'_updated': 1234,
3032
bar: {
3133
name: 'bar',
@@ -37,16 +39,13 @@ test('createCacheEntryStream basic', function (t) {
3739
}
3840
}))
3941
fixture.create(cachePath)
40-
_createCacheEntryStream(cachePath, function (err, stream, latest) {
41-
if (err) throw err
42+
return _createCacheEntryStream(cachePath, {}).then(({
43+
updateStream: stream,
44+
updatedLatest: latest
45+
}) => {
4246
t.equals(latest, 1234, '`latest` correctly extracted')
4347
t.ok(stream, 'returned a stream')
44-
var results = []
45-
stream.on('data', function (pkg) {
46-
results.push(pkg)
47-
})
48-
ms.finished(stream, function (err) {
49-
if (err) throw err
48+
return getStream.array(stream).then(results => {
5049
t.deepEquals(results, [{
5150
name: 'bar',
5251
version: '1.0.0'
@@ -55,82 +54,54 @@ test('createCacheEntryStream basic', function (t) {
5554
version: '1.0.0'
5655
}])
5756
cleanup()
58-
t.done()
5957
})
6058
})
6159
})
6260

63-
test('createCacheEntryStream empty cache', function (t) {
61+
test('createCacheEntryStream empty cache', t => {
6462
setup()
65-
var cachePath = path.join(CACHE_DIR, '.cache.json')
66-
var fixture = new Tacks(File({}))
63+
const cachePath = path.join(CACHE_DIR, '.cache.json')
64+
const fixture = new Tacks(File({}))
6765
fixture.create(cachePath)
68-
_createCacheEntryStream(cachePath, function (err, stream, latest) {
69-
t.ok(err, 'returned an error because there was no _updated')
70-
t.match(err.message, /Empty or invalid stream/, 'useful error message')
71-
t.notOk(stream, 'no stream returned')
72-
t.notOk(latest, 'no latest returned')
73-
cleanup()
74-
t.done()
75-
})
66+
return _createCacheEntryStream(cachePath, {}).then(
67+
() => { throw new Error('should not succeed') },
68+
err => {
69+
t.ok(err, 'returned an error because there was no _updated')
70+
t.match(err.message, /Empty or invalid stream/, 'useful error message')
71+
cleanup()
72+
}
73+
)
7674
})
7775

78-
test('createCacheEntryStream no entry cache', function (t) {
76+
test('createCacheEntryStream no entry cache', t => {
7977
setup()
80-
var cachePath = path.join(CACHE_DIR, '.cache.json')
81-
var fixture = new Tacks(File({
78+
const cachePath = path.join(CACHE_DIR, '.cache.json')
79+
const fixture = new Tacks(File({
8280
'_updated': 1234
8381
}))
8482
fixture.create(cachePath)
85-
_createCacheEntryStream(cachePath, function (err, stream, latest) {
86-
if (err) throw err
83+
return _createCacheEntryStream(cachePath, {}).then(({
84+
updateStream: stream,
85+
updatedLatest: latest
86+
}) => {
8787
t.equals(latest, 1234, '`latest` correctly extracted')
8888
t.ok(stream, 'returned a stream')
89-
var results = []
90-
stream.on('data', function (pkg) {
91-
results.push(pkg)
92-
})
93-
ms.finished(stream, function (err) {
94-
if (err) throw err
89+
return getStream.array(stream).then(results => {
9590
t.deepEquals(results, [], 'no results')
9691
cleanup()
97-
t.done()
9892
})
9993
})
10094
})
10195

102-
test('createCacheEntryStream missing cache', function (t) {
96+
test('createCacheEntryStream missing cache', t => {
10397
setup()
104-
var cachePath = path.join(CACHE_DIR, '.cache.json')
105-
_createCacheEntryStream(cachePath, function (err, stream, latest) {
106-
t.ok(err, 'returned an error because there was no cache')
107-
t.equals(err.code, 'ENOENT', 'useful error message')
108-
t.notOk(stream, 'no stream returned')
109-
t.notOk(latest, 'no latest returned')
110-
cleanup()
111-
t.done()
112-
})
113-
})
114-
115-
test('createCacheEntryStream bad syntax', function (t) {
116-
setup()
117-
var cachePath = path.join(CACHE_DIR, '.cache.json')
118-
var fixture = new Tacks(File('{"_updated": 1234, uh oh'))
119-
fixture.create(cachePath)
120-
_createCacheEntryStream(cachePath, function (err, stream, latest) {
121-
if (err) throw err
122-
t.equals(latest, 1234, '`latest` correctly extracted')
123-
t.ok(stream, 'returned a stream')
124-
var results = []
125-
stream.on('data', function (pkg) {
126-
results.push(pkg)
127-
})
128-
ms.finished(stream, function (err) {
129-
t.ok(err, 'stream errored')
130-
t.match(err.message, /Invalid JSON/i, 'explains there\'s a syntax error')
131-
t.deepEquals(results, [], 'no results')
98+
const cachePath = path.join(CACHE_DIR, '.cache.json')
99+
return _createCacheEntryStream(cachePath, {}).then(
100+
() => { throw new Error('should not succeed') },
101+
err => {
102+
t.ok(err, 'returned an error because there was no cache')
103+
t.equals(err.code, 'ENOENT', 'useful error message')
132104
cleanup()
133-
t.done()
134-
})
135-
})
105+
}
106+
)
136107
})

test/tap/all-package-metadata-entry-stream-unit.js

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
'use strict'
22

3-
var common = require('../common-tap.js')
4-
var npm = require('../../')
5-
var test = require('tap').test
6-
var mkdirp = require('mkdirp')
7-
var rimraf = require('rimraf')
8-
var path = require('path')
9-
var mr = require('npm-registry-mock')
10-
var ms = require('mississippi')
11-
var Tacks = require('tacks')
12-
var File = Tacks.File
3+
const common = require('../common-tap.js')
4+
const getStream = require('get-stream')
5+
const mkdirp = require('mkdirp')
6+
const mr = require('npm-registry-mock')
7+
const ms = require('mississippi')
8+
const npm = require('../../')
9+
const path = require('path')
10+
const rimraf = require('rimraf')
11+
const Tacks = require('tacks')
12+
const test = require('tap').test
1313

14-
var _createEntryStream = require('../../lib/search/all-package-metadata.js')._createEntryStream
14+
const {File} = Tacks
1515

16-
var ALL = common.registry + '/-/all'
17-
var PKG_DIR = path.resolve(__dirname, 'create-entry-update-stream')
18-
var CACHE_DIR = path.resolve(PKG_DIR, 'cache')
16+
const _createEntryStream = require('../../lib/search/all-package-metadata.js')._createEntryStream
1917

20-
var server
18+
const ALL = common.registry + '/-/all'
19+
const PKG_DIR = path.resolve(__dirname, 'create-entry-update-stream')
20+
const CACHE_DIR = path.resolve(PKG_DIR, 'cache')
21+
22+
let server
2123

2224
function setup () {
2325
mkdirp.sync(CACHE_DIR)
@@ -27,10 +29,11 @@ function cleanup () {
2729
rimraf.sync(PKG_DIR)
2830
}
2931

30-
test('setup', function (t) {
31-
mr({port: common.port, throwOnUnmatched: true}, function (err, s) {
32+
test('setup', t => {
33+
cleanup()
34+
mr({port: common.port, throwOnUnmatched: true}, (err, s) => {
3235
t.ifError(err, 'registry mocked successfully')
33-
npm.load({ cache: CACHE_DIR, registry: common.registry }, function (err) {
36+
npm.load({ cache: CACHE_DIR, registry: common.registry }, err => {
3437
t.ifError(err, 'npm loaded successfully')
3538
server = s
3639
t.pass('all set up')
@@ -39,48 +42,45 @@ test('setup', function (t) {
3942
})
4043
})
4144

42-
test('createEntryStream full request', function (t) {
45+
test('createEntryStream full request', t => {
4346
setup()
44-
var cachePath = path.join(CACHE_DIR, '.cache.json')
45-
var dataTime = +(new Date())
47+
const cachePath = path.join(CACHE_DIR, '.cache.json')
48+
const dataTime = +(new Date())
4649
server.get('/-/all').once().reply(200, {
4750
'_updated': dataTime,
4851
'bar': { name: 'bar', version: '1.0.0' },
4952
'foo': { name: 'foo', version: '1.0.0' }
5053
}, {
5154
date: 1234 // should never be used.
5255
})
53-
_createEntryStream(cachePath, ALL, {}, 600, function (err, stream, latest, newEntries) {
54-
if (err) throw err
56+
return _createEntryStream(cachePath, 600, {}).then(({
57+
entryStream: stream,
58+
latest,
59+
newEntries
60+
}) => {
5561
t.equals(latest, dataTime, '`latest` correctly extracted')
5662
t.ok(newEntries, 'new entries need to be written to cache')
5763
t.ok(stream, 'returned a stream')
58-
var results = []
59-
stream.on('data', function (pkg) {
60-
results.push(pkg)
61-
})
62-
ms.finished(stream, function (err) {
63-
if (err) throw err
64-
t.deepEquals(results, [{
65-
name: 'bar',
66-
version: '1.0.0'
67-
}, {
68-
name: 'foo',
69-
version: '1.0.0'
70-
}])
71-
server.done()
72-
cleanup()
73-
t.end()
74-
})
64+
return getStream.array(stream)
65+
}).then(results => {
66+
t.deepEquals(results, [{
67+
name: 'bar',
68+
version: '1.0.0'
69+
}, {
70+
name: 'foo',
71+
version: '1.0.0'
72+
}])
73+
server.done()
74+
cleanup()
7575
})
7676
})
7777

7878
test('createEntryStream cache only', function (t) {
7979
setup()
80-
var now = Date.now()
81-
var cacheTime = now - 100000
82-
var cachePath = path.join(CACHE_DIR, '.cache.json')
83-
var fixture = new Tacks(File({
80+
const now = Date.now()
81+
const cacheTime = now - 100000
82+
const cachePath = path.join(CACHE_DIR, '.cache.json')
83+
const fixture = new Tacks(File({
8484
'_updated': cacheTime,
8585
bar: { name: 'bar', version: '1.0.0' },
8686
cool: { name: 'cool', version: '1.0.0' },
@@ -113,17 +113,17 @@ test('createEntryStream cache only', function (t) {
113113

114114
test('createEntryStream merged stream', function (t) {
115115
setup()
116-
var now = Date.now()
117-
var cacheTime = now - 6000000
116+
const now = Date.now()
117+
const cacheTime = now - 6000000
118118
server.get('/-/all/since?stale=update_after&startkey=' + cacheTime).once().reply(200, {
119119
'bar': { name: 'bar', version: '2.0.0' },
120120
'car': { name: 'car', version: '1.0.0' },
121121
'foo': { name: 'foo', version: '1.0.0' }
122122
}, {
123123
date: (new Date(now)).toISOString()
124124
})
125-
var cachePath = path.join(CACHE_DIR, '.cache.json')
126-
var fixture = new Tacks(File({
125+
const cachePath = path.join(CACHE_DIR, '.cache.json')
126+
const fixture = new Tacks(File({
127127
'_updated': cacheTime,
128128
bar: { name: 'bar', version: '1.0.0' },
129129
cool: { name: 'cool', version: '1.0.0' },
@@ -164,7 +164,7 @@ test('createEntryStream merged stream', function (t) {
164164

165165
test('createEntryStream no sources', function (t) {
166166
setup()
167-
var cachePath = path.join(CACHE_DIR, '.cache.json')
167+
const cachePath = path.join(CACHE_DIR, '.cache.json')
168168
server.get('/-/all').once().reply(404, {})
169169
_createEntryStream(cachePath, ALL, {}, 600, function (err, stream, latest, newEntries) {
170170
t.ok(err, 'no sources, got an error')

0 commit comments

Comments
 (0)