1
1
'use strict'
2
2
3
3
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
11
4
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' )
13
11
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' )
16
18
17
19
function setup ( ) {
18
20
mkdirp . sync ( CACHE_DIR )
@@ -22,10 +24,10 @@ function cleanup () {
22
24
rimraf . sync ( PKG_DIR )
23
25
}
24
26
25
- test ( 'createCacheEntryStream basic' , function ( t ) {
27
+ test ( 'createCacheEntryStream basic' , t => {
26
28
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 ( {
29
31
'_updated' : 1234 ,
30
32
bar : {
31
33
name : 'bar' ,
@@ -37,16 +39,13 @@ test('createCacheEntryStream basic', function (t) {
37
39
}
38
40
} ) )
39
41
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
+ } ) => {
42
46
t . equals ( latest , 1234 , '`latest` correctly extracted' )
43
47
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 => {
50
49
t . deepEquals ( results , [ {
51
50
name : 'bar' ,
52
51
version : '1.0.0'
@@ -55,82 +54,54 @@ test('createCacheEntryStream basic', function (t) {
55
54
version : '1.0.0'
56
55
} ] )
57
56
cleanup ( )
58
- t . done ( )
59
57
} )
60
58
} )
61
59
} )
62
60
63
- test ( 'createCacheEntryStream empty cache' , function ( t ) {
61
+ test ( 'createCacheEntryStream empty cache' , t => {
64
62
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 ( { } ) )
67
65
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 , / E m p t y o r i n v a l i d s t r e a m / , '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 , / E m p t y o r i n v a l i d s t r e a m / , 'useful error message ')
71
+ cleanup ( )
72
+ }
73
+ )
76
74
} )
77
75
78
- test ( 'createCacheEntryStream no entry cache' , function ( t ) {
76
+ test ( 'createCacheEntryStream no entry cache' , t => {
79
77
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 ( {
82
80
'_updated' : 1234
83
81
} ) )
84
82
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
+ } ) => {
87
87
t . equals ( latest , 1234 , '`latest` correctly extracted' )
88
88
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 => {
95
90
t . deepEquals ( results , [ ] , 'no results' )
96
91
cleanup ( )
97
- t . done ( )
98
92
} )
99
93
} )
100
94
} )
101
95
102
- test ( 'createCacheEntryStream missing cache' , function ( t ) {
96
+ test ( 'createCacheEntryStream missing cache' , t => {
103
97
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 , / I n v a l i d J S O N / 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' )
132
104
cleanup ( )
133
- t . done ( )
134
- } )
135
- } )
105
+ }
106
+ )
136
107
} )
0 commit comments