@@ -20,7 +20,7 @@ describe('parser', () => {
20
20
21
21
test ( 'should read files and directories' , async ( ) => {
22
22
// Set the file names and their data
23
- const dirName = 'dir' ;
23
+ const dirName = 'dir/ ' ;
24
24
const fileName1 = 'file.txt' ;
25
25
const fileName2 = 'dir/file.txt' ;
26
26
const fileData = 'testing' ;
@@ -37,7 +37,7 @@ describe('parser', () => {
37
37
[ fileName1 , dirName , fileName2 ] ,
38
38
) ;
39
39
40
- const entries : Record < string , string | undefined > = { } ;
40
+ const entries : Record < string , string | null > = { } ;
41
41
42
42
// Read files and directories and add it to the entries record
43
43
const vtar = new VirtualTarParser ( {
@@ -50,7 +50,7 @@ describe('parser', () => {
50
50
entries [ header . path ] = fileContent ;
51
51
} ,
52
52
onDirectory : async ( header ) => {
53
- entries [ header . path ] = undefined ;
53
+ entries [ header . path ] = null ;
54
54
} ,
55
55
} ) ;
56
56
@@ -59,11 +59,121 @@ describe('parser', () => {
59
59
await vtar . write ( chunk ) ;
60
60
}
61
61
62
- // Make sure all the callbacks settle
63
- await vtar . settled ( ) ;
62
+ expect ( entries [ dirName ] ) . toBeNull ( ) ;
63
+ expect ( entries [ fileName1 ] ) . toEqual ( fileData ) ;
64
+ expect ( entries [ fileName2 ] ) . toEqual ( fileData ) ;
65
+ } ) ;
66
+
67
+ test ( 'should ignore files if callback is not provided' , async ( ) => {
68
+ // Set the file names and their data
69
+ const dirName = 'dir/' ;
70
+ const fileName1 = 'file.txt' ;
71
+ const fileName2 = 'dir/file.txt' ;
72
+ const fileData = 'testing' ;
73
+
74
+ await fs . promises . mkdir ( path . join ( tempDir , dirName ) ) ;
75
+ await fs . promises . writeFile ( path . join ( tempDir , fileName1 ) , fileData ) ;
76
+ await fs . promises . writeFile ( path . join ( tempDir , fileName2 ) , fileData ) ;
77
+
78
+ const archive = tar . create (
79
+ {
80
+ cwd : tempDir ,
81
+ preservePaths : true ,
82
+ } ,
83
+ [ fileName1 , dirName , fileName2 ] ,
84
+ ) ;
85
+
86
+ const entries : Record < string , string | null > = { } ;
87
+
88
+ // Read files and directories and add it to the entries record
89
+ const vtar = new VirtualTarParser ( {
90
+ onDirectory : async ( header ) => {
91
+ entries [ header . path ] = null ;
92
+ } ,
93
+ } ) ;
94
+
95
+ // Enqueue each generated chunk from the archive
96
+ for await ( const chunk of archive ) {
97
+ await vtar . write ( chunk ) ;
98
+ }
99
+
100
+ expect ( entries [ dirName ] ) . toBeNull ( ) ;
101
+ expect ( entries [ fileName1 ] ) . toBeUndefined ( ) ;
102
+ expect ( entries [ fileName2 ] ) . toBeUndefined ( ) ;
103
+ } ) ;
104
+
105
+ test ( 'should ignore directories if callback is not provided' , async ( ) => {
106
+ // Set the file names and their data
107
+ const dirName = 'dir/' ;
108
+ const fileName1 = 'file.txt' ;
109
+ const fileName2 = 'dir/file.txt' ;
110
+ const fileData = 'testing' ;
111
+
112
+ await fs . promises . mkdir ( path . join ( tempDir , dirName ) ) ;
113
+ await fs . promises . writeFile ( path . join ( tempDir , fileName1 ) , fileData ) ;
114
+ await fs . promises . writeFile ( path . join ( tempDir , fileName2 ) , fileData ) ;
115
+
116
+ const archive = tar . create (
117
+ {
118
+ cwd : tempDir ,
119
+ preservePaths : true ,
120
+ } ,
121
+ [ fileName1 , dirName , fileName2 ] ,
122
+ ) ;
123
+
124
+ const entries : Record < string , string | null > = { } ;
125
+
126
+ // Read files and directories and add it to the entries record
127
+ const vtar = new VirtualTarParser ( {
128
+ onFile : async ( header , data ) => {
129
+ const content : Array < Uint8Array > = [ ] ;
130
+ for await ( const chunk of data ( ) ) {
131
+ content . push ( chunk ) ;
132
+ }
133
+ const fileContent = Buffer . concat ( content ) . toString ( ) ;
134
+ entries [ header . path ] = fileContent ;
135
+ } ,
136
+ } ) ;
137
+
138
+ // Enqueue each generated chunk from the archive
139
+ for await ( const chunk of archive ) {
140
+ await vtar . write ( chunk ) ;
141
+ }
64
142
65
143
expect ( entries [ dirName ] ) . toBeUndefined ( ) ;
66
144
expect ( entries [ fileName1 ] ) . toEqual ( fileData ) ;
67
145
expect ( entries [ fileName2 ] ) . toEqual ( fileData ) ;
68
146
} ) ;
147
+
148
+ test ( 'should ignore everything if callback is not provided' , async ( ) => {
149
+ // Set the file names and their data
150
+ const dirName = 'dir/' ;
151
+ const fileName1 = 'file.txt' ;
152
+ const fileName2 = 'dir/file.txt' ;
153
+ const fileData = 'testing' ;
154
+
155
+ await fs . promises . mkdir ( path . join ( tempDir , dirName ) ) ;
156
+ await fs . promises . writeFile ( path . join ( tempDir , fileName1 ) , fileData ) ;
157
+ await fs . promises . writeFile ( path . join ( tempDir , fileName2 ) , fileData ) ;
158
+
159
+ const archive = tar . create (
160
+ {
161
+ cwd : tempDir ,
162
+ preservePaths : true ,
163
+ } ,
164
+ [ fileName1 , dirName , fileName2 ] ,
165
+ ) ;
166
+
167
+ const entries : Record < string , string | null > = { } ;
168
+
169
+ // Read files and directories and add it to the entries record
170
+ const vtar = new VirtualTarParser ( ) ;
171
+
172
+ // Enqueue each generated chunk from the archive
173
+ for await ( const chunk of archive ) {
174
+ await vtar . write ( chunk ) ;
175
+ }
176
+
177
+ expect ( Object . keys ( entries ) . length ) . toEqual ( 0 ) ;
178
+ } ) ;
69
179
} ) ;
0 commit comments