1
1
'use strict'
2
2
3
3
const t = require ( 'tap' )
4
+ const nock = require ( 'nock' )
4
5
const x = require ( '../lib/extract.js' )
5
6
const path = require ( 'path' )
6
7
const fs = require ( 'fs' )
@@ -11,7 +12,17 @@ const { promisify } = require('util')
11
12
const rimraf = promisify ( require ( 'rimraf' ) )
12
13
const mutateFS = require ( 'mutate-fs' )
13
14
const pipeline = promisify ( require ( 'stream' ) . pipeline )
14
- const https = require ( 'https' )
15
+ const http = require ( 'http' )
16
+
17
+ const tnock = ( t , host , opts ) => {
18
+ nock . disableNetConnect ( )
19
+ const server = nock ( host , opts )
20
+ t . teardown ( function ( ) {
21
+ nock . enableNetConnect ( )
22
+ server . done ( )
23
+ } )
24
+ return server
25
+ }
15
26
16
27
t . teardown ( _ => rimraf ( extractdir ) )
17
28
@@ -57,6 +68,9 @@ t.test('basic extracting', t => {
57
68
} )
58
69
59
70
t . test ( 'ensure an open stream is not prematuraly closed' , t => {
71
+ t . plan ( 1 )
72
+
73
+ const file = path . resolve ( tars , 'long-paths.tar' )
60
74
const dir = path . resolve ( extractdir , 'basic-with-stream' )
61
75
62
76
t . beforeEach ( async ( ) => {
@@ -65,16 +79,51 @@ t.test('ensure an open stream is not prematuraly closed', t => {
65
79
} )
66
80
67
81
const check = async t => {
68
- fs . lstatSync ( dir + '/node-tar-main/LICENSE' )
82
+ t . ok ( fs . lstatSync ( dir + '/long-path' ) )
83
+ await rimraf ( dir )
84
+ t . end ( )
85
+ }
86
+
87
+ t . test ( 'async promisey' , t => {
88
+ const stream = fs . createReadStream ( file , {
89
+ highWaterMark : 1 ,
90
+ } )
91
+ pipeline (
92
+ stream ,
93
+ x ( { cwd : dir } )
94
+ ) . then ( _ => check ( t ) )
95
+ } )
96
+
97
+ t . end ( )
98
+ } )
99
+
100
+ t . test ( 'ensure an open stream is not prematuraly closed http' , t => {
101
+ t . plan ( 1 )
102
+
103
+ const file = path . resolve ( tars , 'long-paths.tar' )
104
+ const dir = path . resolve ( extractdir , 'basic-with-stream-http' )
105
+
106
+ t . beforeEach ( async ( ) => {
107
+ await rimraf ( dir )
108
+ await mkdirp ( dir )
109
+ } )
110
+
111
+ const check = async t => {
112
+ t . ok ( fs . lstatSync ( dir + '/long-path' ) )
69
113
await rimraf ( dir )
70
114
t . end ( )
71
115
}
72
116
73
117
t . test ( 'async promisey' , t => {
74
- https . get ( 'https://codeload.github.com/npm/node-tar/tar.gz/main' , ( stream ) => {
118
+ tnock ( t , 'http://codeload.github.com/' )
119
+ . get ( '/npm/node-tar/tar.gz/main' )
120
+ . delay ( 250 )
121
+ . reply ( 200 , ( ) => fs . createReadStream ( file ) )
122
+
123
+ http . get ( 'http://codeload.github.com/npm/node-tar/tar.gz/main' , ( stream ) => {
75
124
return pipeline (
76
125
stream ,
77
- x ( { cwd : dir } , [ 'node-tar-main/LICENSE' ] )
126
+ x ( { cwd : dir } )
78
127
) . then ( _ => check ( t ) )
79
128
} )
80
129
} )
0 commit comments