@@ -13,7 +13,7 @@ import {
13
13
import { expect , assert } from 'chai' ;
14
14
import { createSandbox } from 'sinon' ;
15
15
import * as fs from 'fs' ;
16
- import { join } from 'path' ;
16
+ import { join , normalize } from 'path' ;
17
17
import { LibraryError } from '../../src/errors' ;
18
18
import { nls } from '../../src/i18n' ;
19
19
import { VirtualDirectory } from '../../src' ;
@@ -110,6 +110,8 @@ describe('Tree Containers', () => {
110
110
let tree : ZipTreeContainer ;
111
111
let zipBuffer : Buffer ;
112
112
113
+ const filesRoot = join ( '.' , 'main' , 'default' ) ;
114
+
113
115
before ( async ( ) => {
114
116
const pipeline = promisify ( cbPipeline ) ;
115
117
const archive = createArchive ( 'zip' , { zlib : { level : 3 } } ) ;
@@ -120,9 +122,13 @@ describe('Tree Containers', () => {
120
122
cb ( ) ;
121
123
} ;
122
124
pipeline ( archive , bufferWritable ) ;
123
- archive . append ( 'test text' , { name : 'test.txt' } ) ;
124
- archive . append ( 'test text 2' , { name : 'files/test2.txt' } ) ;
125
- archive . append ( 'test text 3' , { name : 'files/test3.txt' } ) ;
125
+
126
+ archive . append ( null , { name : 'main/' } ) ;
127
+ archive . append ( null , { name : 'main/default/' } ) ;
128
+ archive . append ( 'test text' , { name : 'main/default/test.txt' } ) ;
129
+ archive . append ( 'test text 2' , { name : 'main/default/test2.txt' } ) ;
130
+ archive . append ( 'test text 3' , { name : 'main/default/morefiles/test3.txt' } ) ;
131
+
126
132
await archive . finalize ( ) ;
127
133
128
134
zipBuffer = Buffer . concat ( buffers ) ;
@@ -131,35 +137,34 @@ describe('Tree Containers', () => {
131
137
132
138
describe ( 'exists' , ( ) => {
133
139
it ( 'should return true for file that exists' , ( ) => {
134
- const path = join ( '.' , 'test.txt' ) ;
140
+ const path = join ( filesRoot , 'test.txt' ) ;
135
141
expect ( tree . exists ( path ) ) . to . be . true ;
136
142
} ) ;
137
143
138
144
it ( 'should return true for directory that exists' , ( ) => {
139
- const path = join ( '.' , 'files ' ) ;
145
+ const path = join ( filesRoot , 'morefiles ' ) ;
140
146
expect ( tree . exists ( path ) ) . to . be . true ;
141
147
} ) ;
142
148
143
149
it ( 'should return false for file that does not exist' , ( ) => {
144
- const path = join ( '.' , 'files' , 'test4.txt' ) ;
150
+ const path = join ( filesRoot , 'test4.txt' ) ;
145
151
expect ( tree . exists ( path ) ) . to . be . false ;
146
152
} ) ;
147
153
148
154
it ( 'should return false for directory that does not exist' , ( ) => {
149
- const path = join ( '.' , 'otherfiles ' ) ;
155
+ const path = join ( '.' , 'dne ' ) ;
150
156
expect ( tree . exists ( path ) ) . to . be . false ;
151
157
} ) ;
152
158
} ) ;
153
159
154
160
describe ( 'isDirectory' , ( ) => {
155
161
it ( 'should return false for isDirectory' , ( ) => {
156
- const path = join ( '.' , 'test.txt' ) ;
162
+ const path = join ( filesRoot , 'test.txt' ) ;
157
163
expect ( tree . isDirectory ( path ) ) . to . be . false ;
158
164
} ) ;
159
165
160
166
it ( 'should return true for isDirectory' , ( ) => {
161
- const path = join ( '.' , 'files' ) ;
162
- expect ( tree . isDirectory ( path ) ) . to . be . true ;
167
+ expect ( tree . isDirectory ( filesRoot ) ) . to . be . true ;
163
168
} ) ;
164
169
165
170
it ( 'should throw an error if path does not exist' , ( ) => {
@@ -173,12 +178,21 @@ describe('Tree Containers', () => {
173
178
} ) ;
174
179
175
180
describe ( 'readDirectory' , ( ) => {
176
- it ( 'should return directory entries for readDirectory' , ( ) => {
177
- expect ( tree . readDirectory ( '.' ) ) . to . deep . equal ( [ 'test.txt' , 'files' ] ) ;
181
+ it ( 'should return correct directory entries for directory with files and directories' , ( ) => {
182
+ expect ( tree . readDirectory ( filesRoot ) ) . to . deep . equal ( [ 'test.txt' , 'test2.txt' , 'morefiles' ] ) ;
183
+ } ) ;
184
+
185
+ it ( 'should return correct directory entries for directory only files' , ( ) => {
186
+ const path = join ( filesRoot , 'morefiles' ) ;
187
+ expect ( tree . readDirectory ( path ) ) . to . deep . equal ( [ 'test3.txt' ] ) ;
188
+ } ) ;
189
+
190
+ it ( 'should return correct directory entries for directory with only directories' , ( ) => {
191
+ expect ( tree . readDirectory ( '.' ) ) . to . deep . equal ( [ 'main' ] ) ;
178
192
} ) ;
179
193
180
194
it ( 'should throw an error if path is not a directory' , ( ) => {
181
- const path = join ( '.' , 'files' , 'test2.txt' ) ;
195
+ const path = join ( filesRoot , 'test2.txt' ) ;
182
196
assert . throws (
183
197
( ) => tree . readDirectory ( path ) ,
184
198
LibraryError ,
@@ -189,35 +203,33 @@ describe('Tree Containers', () => {
189
203
190
204
describe ( 'readFile' , ( ) => {
191
205
it ( 'should read contents of zip entry into buffer' , async ( ) => {
192
- const path = join ( '.' , 'test.txt' ) ;
206
+ const path = join ( filesRoot , 'test.txt' ) ;
193
207
const contents = ( await tree . readFile ( path ) ) . toString ( ) ;
194
208
expect ( contents ) . to . equal ( 'test text' ) ;
195
209
} ) ;
196
210
197
211
it ( 'should throw an error if path is to directory' , ( ) => {
198
- const path = join ( '.' , 'files' ) ;
199
212
assert . throws (
200
- ( ) => tree . readFile ( path ) ,
213
+ ( ) => tree . readFile ( filesRoot ) ,
201
214
LibraryError ,
202
- nls . localize ( 'error_expected_file_path' , path )
215
+ nls . localize ( 'error_expected_file_path' , filesRoot )
203
216
) ;
204
217
} ) ;
205
218
} ) ;
206
219
207
220
describe ( 'stream' , ( ) => {
208
221
it ( 'should return a readable stream' , async ( ) => {
209
- const path = join ( '.' , 'test.txt' ) ;
222
+ const path = join ( filesRoot , 'test.txt' ) ;
210
223
const zipDir = await unzipper . Open . buffer ( zipBuffer ) ;
211
- const expectedStream = zipDir . files . find ( ( f ) => f . path === path ) ?. stream ( ) ;
224
+ const expectedStream = zipDir . files . find ( ( f ) => normalize ( f . path ) === path ) ?. stream ( ) ;
212
225
const actual = tree . stream ( path ) ;
213
226
expect ( actual instanceof Readable ) . to . be . true ;
214
227
expect ( ( actual as unzipper . Entry ) . path ) . to . equal ( expectedStream . path ) ;
215
228
} ) ;
216
229
217
230
it ( 'should throw an error if given path is to a directory' , ( ) => {
218
- const path = join ( '.' , 'files' ) ;
219
231
assert . throws (
220
- ( ) => tree . stream ( path ) ,
232
+ ( ) => tree . stream ( filesRoot ) ,
221
233
LibraryError ,
222
234
nls . localize ( 'error_no_directory_stream' , tree . constructor . name )
223
235
) ;
0 commit comments