@@ -28,17 +28,16 @@ const getReadStatFailure = (t, err) => getRead(t, {
28
28
} ) ,
29
29
} )
30
30
31
- t . test ( 'read: returns a Promise with cache content data' , function ( t ) {
31
+ t . test ( 'read: returns a Promise with cache content data' , async t => {
32
32
const CONTENT = Buffer . from ( 'foobarbaz' )
33
33
const INTEGRITY = ssri . fromData ( CONTENT )
34
34
const CACHE = t . testdir (
35
35
CacheContent ( {
36
36
[ INTEGRITY ] : CONTENT ,
37
37
} )
38
38
)
39
- return read ( CACHE , INTEGRITY ) . then ( ( data ) => {
40
- t . same ( data , CONTENT , 'cache contents read correctly' )
41
- } )
39
+ const data = await read ( CACHE , INTEGRITY )
40
+ t . same ( data , CONTENT , 'cache contents read correctly' )
42
41
} )
43
42
44
43
t . test ( 'read.sync: reads synchronously' , ( t ) => {
@@ -54,7 +53,7 @@ t.test('read.sync: reads synchronously', (t) => {
54
53
t . end ( )
55
54
} )
56
55
57
- t . test ( 'read.stream: returns a stream with cache content data' , function ( t ) {
56
+ t . test ( 'read.stream: returns a stream with cache content data' , async t => {
58
57
const CONTENT = Buffer . from ( 'foobarbaz' )
59
58
const INTEGRITY = ssri . fromData ( CONTENT )
60
59
const CACHE = t . testdir (
@@ -63,16 +62,15 @@ t.test('read.stream: returns a stream with cache content data', function (t) {
63
62
} )
64
63
)
65
64
const stream = read . stream ( CACHE , INTEGRITY )
66
- return Promise . all ( [
65
+ const [ fromStream , fromBulk ] = await Promise . all ( [
67
66
stream . concat ( ) ,
68
67
read ( CACHE , INTEGRITY , { size : CONTENT . length } ) ,
69
- ] ) . then ( ( [ fromStream , fromBulk ] ) => {
70
- t . same ( fromStream , CONTENT , 'stream data checks out' )
71
- t . same ( fromBulk , CONTENT , 'promise data checks out' )
72
- } )
68
+ ] )
69
+ t . same ( fromStream , CONTENT , 'stream data checks out' )
70
+ t . same ( fromBulk , CONTENT , 'promise data checks out' )
73
71
} )
74
72
75
- t . test ( 'read: allows hashAlgorithm configuration' , function ( t ) {
73
+ t . test ( 'read: allows hashAlgorithm configuration' , async t => {
76
74
const CONTENT = Buffer . from ( 'foobarbaz' )
77
75
const HASH = 'sha384'
78
76
const INTEGRITY = ssri . fromData ( CONTENT , { algorithms : [ HASH ] } )
@@ -82,16 +80,15 @@ t.test('read: allows hashAlgorithm configuration', function (t) {
82
80
} )
83
81
)
84
82
const stream = read . stream ( CACHE , INTEGRITY )
85
- return Promise . all ( [
83
+ const [ fromStream , fromBulk ] = await Promise . all ( [
86
84
stream . concat ( ) ,
87
85
read ( CACHE , INTEGRITY ) ,
88
- ] ) . then ( ( [ fromStream , fromBulk ] ) => {
89
- t . same ( fromStream , CONTENT , 'stream used algorithm' )
90
- t . same ( fromBulk , CONTENT , 'promise used algorithm' )
91
- } )
86
+ ] )
87
+ t . same ( fromStream , CONTENT , 'stream used algorithm' )
88
+ t . same ( fromBulk , CONTENT , 'promise used algorithm' )
92
89
} )
93
90
94
- t . test ( 'read: errors if content missing' , function ( t ) {
91
+ t . test ( 'read: errors if content missing' , async t => {
95
92
const CACHE = t . testdir ( { } )
96
93
const stream = read . stream ( CACHE , 'sha512-whatnot' )
97
94
stream . on ( 'data' , function ( data ) {
@@ -100,28 +97,19 @@ t.test('read: errors if content missing', function (t) {
100
97
stream . on ( 'end' , function ( ) {
101
98
throw new Error ( 'end was emitted even though stream errored' )
102
99
} )
103
- return Promise . all ( [
104
- stream . promise ( ) . catch ( ( err ) => {
105
- if ( err . code === 'ENOENT' ) {
106
- return err
107
- }
108
-
109
- throw err
110
- } ) ,
111
- read ( CACHE , 'sha512-whatnot' ) . catch ( ( err ) => {
112
- if ( err . code === 'ENOENT' ) {
113
- return err
114
- }
115
-
116
- throw err
117
- } ) ,
118
- ] ) . then ( ( [ streamErr , bulkErr ] ) => {
119
- t . match ( streamErr , { code : 'ENOENT' } , 'stream got the right error' )
120
- t . match ( bulkErr , { code : 'ENOENT' } , 'bulk got the right error' )
121
- } )
100
+ await t . rejects (
101
+ stream . promise ( ) ,
102
+ { code : 'ENOENT' } ,
103
+ 'stream got the right error'
104
+ )
105
+ await t . rejects (
106
+ read ( CACHE , 'sha512-whatnot' ) ,
107
+ { code : 'ENOENT' } ,
108
+ 'bulk got the right error'
109
+ )
122
110
} )
123
111
124
- t . test ( 'read: errors if content fails checksum' , function ( t ) {
112
+ t . test ( 'read: errors if content fails checksum' , async t => {
125
113
const CONTENT = Buffer . from ( 'foobarbaz' )
126
114
const INTEGRITY = ssri . fromData ( CONTENT )
127
115
const CACHE = t . testdir (
@@ -133,28 +121,19 @@ t.test('read: errors if content fails checksum', function (t) {
133
121
stream . on ( 'end' , function ( ) {
134
122
throw new Error ( 'end was emitted even though stream errored' )
135
123
} )
136
- return Promise . all ( [
137
- stream . promise ( ) . catch ( ( err ) => {
138
- if ( err . code === 'EINTEGRITY' ) {
139
- return err
140
- }
141
-
142
- throw err
143
- } ) ,
144
- read ( CACHE , INTEGRITY ) . catch ( ( err ) => {
145
- if ( err . code === 'EINTEGRITY' ) {
146
- return err
147
- }
148
-
149
- throw err
150
- } ) ,
151
- ] ) . then ( ( [ streamErr , bulkErr ] ) => {
152
- t . match ( streamErr , { code : 'EINTEGRITY' } , 'stream got the right error' )
153
- t . match ( bulkErr , { code : 'EINTEGRITY' } , 'bulk got the right error' )
154
- } )
124
+ await t . rejects (
125
+ stream . promise ( ) ,
126
+ { code : 'EINTEGRITY' } ,
127
+ 'stream got the right error'
128
+ )
129
+ await t . rejects (
130
+ read ( CACHE , INTEGRITY ) ,
131
+ { code : 'EINTEGRITY' } ,
132
+ 'bulk got the right error'
133
+ )
155
134
} )
156
135
157
- t . test ( 'read: errors if content size does not match size option' , function ( t ) {
136
+ t . test ( 'read: errors if content size does not match size option' , async t => {
158
137
const CONTENT = Buffer . from ( 'foobarbaz' )
159
138
const INTEGRITY = ssri . fromData ( CONTENT )
160
139
const CACHE = t . testdir (
@@ -166,27 +145,16 @@ t.test('read: errors if content size does not match size option', function (t) {
166
145
stream . on ( 'end' , function ( ) {
167
146
throw new Error ( 'end was called even though stream errored' )
168
147
} )
169
- return Promise . all ( [
170
- stream . promise ( ) . catch ( ( err ) => {
171
- if ( err . code === 'EBADSIZE' ) {
172
- return err
173
- }
174
-
175
- throw err
176
- } ) ,
177
- read ( CACHE , INTEGRITY , {
178
- size : CONTENT . length ,
179
- } ) . catch ( ( err ) => {
180
- if ( err . code === 'EBADSIZE' ) {
181
- return err
182
- }
183
-
184
- throw err
185
- } ) ,
186
- ] ) . then ( ( [ streamErr , bulkErr ] ) => {
187
- t . match ( streamErr , { code : 'EBADSIZE' } , 'stream got the right error' )
188
- t . match ( bulkErr , { code : 'EBADSIZE' } , 'bulk got the right error' )
189
- } )
148
+ await t . rejects (
149
+ stream . promise ( ) ,
150
+ { code : 'EBADSIZE' } ,
151
+ 'stream got the right error'
152
+ )
153
+ await t . rejects (
154
+ read ( CACHE , INTEGRITY , { size : CONTENT . length } ) ,
155
+ { code : 'EBADSIZE' } ,
156
+ 'bulk got the right error'
157
+ )
190
158
} )
191
159
192
160
t . test ( 'read: error while parsing provided integrity data' , function ( t ) {
@@ -344,27 +312,26 @@ t.test('read.sync: content size value does not match option', (t) => {
344
312
t . end ( )
345
313
} )
346
314
347
- t . test ( 'hasContent: tests content existence' , ( t ) => {
315
+ t . test ( 'hasContent: tests content existence' , async t => {
348
316
const CACHE = t . testdir (
349
317
CacheContent ( {
350
318
'sha1-deadbeef' : '' ,
351
319
} )
352
320
)
353
- return Promise . all ( [
354
- read . hasContent ( CACHE , 'sha1-deadbeef' ) . then ( ( content ) => {
355
- t . ok ( content . sri , 'returned sri for this content' )
356
- t . equal ( content . size , 0 , 'returned the right size for this content' )
357
- t . ok ( content . stat . isFile ( ) , 'returned actual stat object' )
358
- } ) ,
359
- read . hasContent ( CACHE , 'sha1-not-there' ) . then ( ( content ) => {
360
- t . equal ( content , false , 'returned false for missing content' )
361
- } ) ,
362
- read
363
- . hasContent ( CACHE , 'sha1-not-here sha1-also-not-here' )
364
- . then ( ( content ) => {
365
- t . equal ( content , false , 'multi-content hash failures work ok' )
366
- } ) ,
367
- ] )
321
+ const content = await read . hasContent ( CACHE , 'sha1-deadbeef' )
322
+ t . ok ( content . sri , 'returned sri for this content' )
323
+ t . equal ( content . size , 0 , 'returned the right size for this content' )
324
+ t . ok ( content . stat . isFile ( ) , 'returned actual stat object' )
325
+ await t . resolveMatch (
326
+ read . hasContent ( CACHE , 'sha1-not-there' ) ,
327
+ false ,
328
+ 'returned false for missing content'
329
+ )
330
+ await t . resolveMatch (
331
+ read . hasContent ( CACHE , 'sha1-not-here sha1-also-not-here' ) ,
332
+ false ,
333
+ 'multi-content hash failures work ok'
334
+ )
368
335
} )
369
336
370
337
t . test ( 'hasContent: permission error' , ( t ) => {
@@ -457,7 +424,7 @@ t.test('hasContent.sync: no integrity provided', (t) => {
457
424
t . end ( )
458
425
} )
459
426
460
- t . test ( 'copy: copies content to a destination path' , ( t ) => {
427
+ t . test ( 'copy: copies content to a destination path' , async t => {
461
428
const CONTENT = Buffer . from ( 'foobarbaz' )
462
429
const INTEGRITY = ssri . fromData ( CONTENT )
463
430
const CACHE = t . testdir (
@@ -466,14 +433,9 @@ t.test('copy: copies content to a destination path', (t) => {
466
433
} )
467
434
)
468
435
const DEST = path . join ( CACHE , 'foobar-file' )
469
- return read
470
- . copy ( CACHE , INTEGRITY , DEST )
471
- . then ( ( ) => {
472
- return fs . readFile ( DEST )
473
- } )
474
- . then ( ( data ) => {
475
- t . same ( data , CONTENT , 'file successfully copied' )
476
- } )
436
+ await read . copy ( CACHE , INTEGRITY , DEST )
437
+ const data = await fs . readFile ( DEST )
438
+ t . same ( data , CONTENT , 'file successfully copied' )
477
439
} )
478
440
479
441
t . test ( 'copy.sync: copies content to a destination path synchronously' , ( t ) => {
0 commit comments