1
- import { Archive , create_zip_archive } from 'app/server/lib/Archive' ;
1
+ import {
2
+ Archive ,
3
+ ArchiveEntry ,
4
+ create_tar_archive ,
5
+ create_zip_archive
6
+ } from 'app/server/lib/Archive' ;
2
7
import { MemoryWritableStream } from 'app/server/utils/MemoryWritableStream' ;
3
8
import decompress from 'decompress' ;
4
9
import { assert } from 'chai' ;
@@ -16,24 +21,24 @@ const testFiles = [
16
21
] ;
17
22
18
23
async function * generateTestArchiveContents ( ) {
19
- yield {
20
- name : testFiles [ 0 ] . name ,
21
- data : stream . Readable . from ( Buffer . from ( testFiles [ 0 ] . contents ) )
22
- } ;
23
- yield {
24
- name : testFiles [ 1 ] . name ,
25
- data : Buffer . from ( testFiles [ 1 ] . contents )
26
- } ;
24
+ for ( const file of testFiles ) {
25
+ const buffer = Buffer . from ( file . contents ) ;
26
+ yield {
27
+ name : file . name ,
28
+ size : buffer . length ,
29
+ data : stream . Readable . from ( buffer ) ,
30
+ } ;
31
+ }
27
32
}
28
33
29
34
async function assertArchiveCompletedSuccessfully ( archive : Archive ) {
30
35
await assert . isFulfilled ( archive . completed ) ;
31
- assert . isTrue ( archive . dataStream . closed , "archive data stream was not closed " ) ;
36
+ assert . isTrue ( archive . dataStream . destroyed , "archive data stream was not finished " ) ;
32
37
}
33
38
34
39
async function assertArchiveFailedWithError ( archive : Archive ) {
35
40
await assert . isRejected ( archive . completed ) ;
36
- assert . isTrue ( archive . dataStream . closed , "archive data stream was not closed " ) ;
41
+ assert . isTrue ( archive . dataStream . destroyed , "archive data stream was not finished " ) ;
37
42
}
38
43
39
44
async function assertArchiveContainsTestFiles ( archiveData : Buffer ) {
@@ -46,30 +51,41 @@ async function assertArchiveContainsTestFiles(archiveData: Buffer) {
46
51
assert . deepEqual ( zippedFile ?. data . toString ( ) , testFile . contents ) ;
47
52
}
48
53
}
49
- describe ( 'Archive' , function ( ) {
50
- describe ( 'create_zip_archive' , function ( ) {
51
- it ( 'should create a zip archive' , async function ( ) {
52
- const archive = await create_zip_archive ( { store : true } , generateTestArchiveContents ( ) ) ;
53
- const output = new MemoryWritableStream ( ) ;
54
- archive . dataStream . pipe ( output ) ;
55
- await assertArchiveCompletedSuccessfully ( archive ) ;
56
54
57
- const archiveData = output . getBuffer ( ) ;
55
+ type ArchiveCreator = ( entries : AsyncIterable < ArchiveEntry > ) => Promise < Archive > ;
58
56
59
- await assertArchiveContainsTestFiles ( archiveData ) ;
60
- } ) ;
57
+ function testArchive ( type : string , makeArchive : ArchiveCreator ) {
58
+ it ( `should create a ${ type } archive` , async function ( ) {
59
+ const archive = await makeArchive ( generateTestArchiveContents ( ) ) ;
60
+ const output = new MemoryWritableStream ( ) ;
61
+ archive . dataStream . pipe ( output ) ;
62
+ await assertArchiveCompletedSuccessfully ( archive ) ;
61
63
62
- it ( 'errors in archive.completed if the generator errors' , async function ( ) {
63
- async function * throwErrorGenerator ( ) {
64
- throw new Error ( "Test error" ) ;
65
- yield { name : testFiles [ 0 ] . name , data : Buffer . from ( testFiles [ 0 ] . contents ) } ;
66
- }
64
+ const archiveData = output . getBuffer ( ) ;
67
65
66
+ await assertArchiveContainsTestFiles ( archiveData ) ;
67
+ } ) ;
68
+
69
+ it ( 'errors in archive.completed if the generator errors' , async function ( ) {
70
+ async function * throwErrorGenerator ( ) {
71
+ throw new Error ( "Test error" ) ;
72
+ yield { name : 'Test' , size : 0 , data : Buffer . from ( [ ] ) } ;
73
+ }
68
74
69
- // Shouldn't error here - as this just starts the packing.
70
- const archive = await create_zip_archive ( { store : true } , throwErrorGenerator ( ) ) ;
71
- await assert . isRejected ( archive . completed , "Test error" ) ;
72
- await assertArchiveFailedWithError ( archive ) ;
73
- } ) ;
75
+ // Shouldn't error here - as this just starts the packing.
76
+ const archive = await makeArchive ( throwErrorGenerator ( ) ) ;
77
+ await assert . isRejected ( archive . completed , "Test error" ) ;
78
+ await assertArchiveFailedWithError ( archive ) ;
79
+ } ) ;
80
+ }
81
+
82
+ describe ( 'Archive' , function ( ) {
83
+ describe ( 'create_zip_archive' , function ( ) {
84
+ const creator : ArchiveCreator = ( entries ) => create_zip_archive ( { store : true } , entries ) ;
85
+ testArchive ( 'zip' , creator ) ;
86
+ } ) ;
87
+ describe ( 'create_tar_archive' , function ( ) {
88
+ const creator : ArchiveCreator = ( entries ) => create_tar_archive ( entries ) ;
89
+ testArchive ( 'tar' , creator ) ;
74
90
} ) ;
75
91
} ) ;
0 commit comments