@@ -36,14 +36,15 @@ module.exports = (repo) => {
36
36
describe ( 'exporter' , ( ) => {
37
37
let ipld
38
38
39
- function addTestFile ( { file, strategy = 'balanced' , path = '/foo' , maxChunkSize} , cb ) {
39
+ function addTestFile ( { file, strategy = 'balanced' , path = '/foo' , maxChunkSize, rawLeaves } , cb ) {
40
40
pull (
41
41
pull . values ( [ {
42
42
path,
43
43
content : file
44
44
} ] ) ,
45
45
importer ( ipld , {
46
46
strategy,
47
+ rawLeaves,
47
48
chunkerOptions : {
48
49
maxChunkSize
49
50
}
@@ -54,8 +55,8 @@ module.exports = (repo) => {
54
55
)
55
56
}
56
57
57
- function addAndReadTestFile ( { file, offset, length, strategy = 'balanced' , path = '/foo' , maxChunkSize} , cb ) {
58
- addTestFile ( { file, strategy, path, maxChunkSize} , ( error , multihash ) => {
58
+ function addAndReadTestFile ( { file, offset, length, strategy = 'balanced' , path = '/foo' , maxChunkSize, rawLeaves } , cb ) {
59
+ addTestFile ( { file, strategy, path, maxChunkSize, rawLeaves } , ( error , multihash ) => {
59
60
if ( error ) {
60
61
return cb ( error )
61
62
}
@@ -322,6 +323,21 @@ module.exports = (repo) => {
322
323
)
323
324
} )
324
325
326
+ it ( 'exports a zero length chunk of a large file' , function ( done ) {
327
+ this . timeout ( 30 * 1000 )
328
+
329
+ addAndReadTestFile ( {
330
+ file : bigFile ,
331
+ path : '1.2MiB.txt' ,
332
+ rawLeaves : true ,
333
+ length : 0
334
+ } , ( err , data ) => {
335
+ expect ( err ) . to . not . exist ( )
336
+ expect ( data ) . to . eql ( Buffer . alloc ( 0 ) )
337
+ done ( )
338
+ } )
339
+ } )
340
+
325
341
it ( 'exports a chunk of a large file > 5mb made from multiple blocks' , function ( done ) {
326
342
this . timeout ( 30 * 1000 )
327
343
const hash = 'QmRQgufjp9vLE8XK2LGKZSsPCFCF6e4iynCQtNB5X2HBKE'
@@ -424,31 +440,119 @@ module.exports = (repo) => {
424
440
it ( 'exports a small file imported with raw leaves' , function ( done ) {
425
441
this . timeout ( 30 * 1000 )
426
442
427
- pull (
428
- pull . values ( [ {
429
- path : '200Bytes.txt' ,
430
- content : pull . values ( [ smallFile ] )
431
- } ] ) ,
432
- importer ( ipld , {
433
- rawLeaves : true
434
- } ) ,
435
- pull . collect ( collected )
436
- )
443
+ addAndReadTestFile ( {
444
+ file : smallFile ,
445
+ path : '200Bytes.txt' ,
446
+ rawLeaves : true
447
+ } , ( err , data ) => {
448
+ expect ( err ) . to . not . exist ( )
449
+ expect ( data ) . to . eql ( smallFile )
450
+ done ( )
451
+ } )
452
+ } )
437
453
438
- function collected ( err , files ) {
454
+ it ( 'exports a chunk of a small file imported with raw leaves' , function ( done ) {
455
+ this . timeout ( 30 * 1000 )
456
+
457
+ const length = 100
458
+
459
+ addAndReadTestFile ( {
460
+ file : smallFile ,
461
+ path : '200Bytes.txt' ,
462
+ rawLeaves : true ,
463
+ length
464
+ } , ( err , data ) => {
439
465
expect ( err ) . to . not . exist ( )
440
- expect ( files . length ) . to . equal ( 1 )
466
+ expect ( data ) . to . eql ( smallFile . slice ( 0 , length ) )
467
+ done ( )
468
+ } )
469
+ } )
441
470
442
- pull (
443
- exporter ( files [ 0 ] . multihash , ipld ) ,
444
- pull . collect ( ( err , files ) => {
445
- expect ( err ) . to . not . exist ( )
446
- expect ( new CID ( files [ 0 ] . hash ) . toBaseEncodedString ( ) ) . to . equal ( 'zb2rhXrz1gkCv8p4nUDZRohY6MzBE9C3HVTVDP72g6Du3SD9Q' )
471
+ it ( 'exports a chunk of a small file imported with raw leaves with length' , function ( done ) {
472
+ this . timeout ( 30 * 1000 )
447
473
448
- fileEql ( files [ 0 ] , smallFile , done )
449
- } )
450
- )
451
- }
474
+ const offset = 100
475
+ const length = 200
476
+
477
+ addAndReadTestFile ( {
478
+ file : smallFile ,
479
+ path : '200Bytes.txt' ,
480
+ rawLeaves : true ,
481
+ offset,
482
+ length
483
+ } , ( err , data ) => {
484
+ expect ( err ) . to . not . exist ( )
485
+ expect ( data ) . to . eql ( smallFile . slice ( offset ) )
486
+ done ( )
487
+ } )
488
+ } )
489
+
490
+ it ( 'exports a zero length chunk of a small file imported with raw leaves' , function ( done ) {
491
+ this . timeout ( 30 * 1000 )
492
+
493
+ const length = 0
494
+
495
+ addAndReadTestFile ( {
496
+ file : smallFile ,
497
+ path : '200Bytes.txt' ,
498
+ rawLeaves : true ,
499
+ length
500
+ } , ( err , data ) => {
501
+ expect ( err ) . to . not . exist ( )
502
+ expect ( data ) . to . eql ( Buffer . alloc ( 0 ) )
503
+ done ( )
504
+ } )
505
+ } )
506
+
507
+ it ( 'errors when exporting a chunk of a small file imported with raw leaves and negative length' , function ( done ) {
508
+ this . timeout ( 30 * 1000 )
509
+
510
+ const length = - 100
511
+
512
+ addAndReadTestFile ( {
513
+ file : smallFile ,
514
+ path : '200Bytes.txt' ,
515
+ rawLeaves : true ,
516
+ length
517
+ } , ( err , data ) => {
518
+ expect ( err ) . to . exist ( )
519
+ expect ( err . message ) . to . equal ( 'Length must be greater than or equal to 0' )
520
+ done ( )
521
+ } )
522
+ } )
523
+
524
+ it ( 'errors when exporting a chunk of a small file imported with raw leaves and negative offset' , function ( done ) {
525
+ this . timeout ( 30 * 1000 )
526
+
527
+ const offset = - 100
528
+
529
+ addAndReadTestFile ( {
530
+ file : smallFile ,
531
+ path : '200Bytes.txt' ,
532
+ rawLeaves : true ,
533
+ offset
534
+ } , ( err , data ) => {
535
+ expect ( err ) . to . exist ( )
536
+ expect ( err . message ) . to . equal ( 'Offset must be greater than or equal to 0' )
537
+ done ( )
538
+ } )
539
+ } )
540
+
541
+ it ( 'errors when exporting a chunk of a small file imported with raw leaves and offset greater than file size' , function ( done ) {
542
+ this . timeout ( 30 * 1000 )
543
+
544
+ const offset = 201
545
+
546
+ addAndReadTestFile ( {
547
+ file : smallFile ,
548
+ path : '200Bytes.txt' ,
549
+ rawLeaves : true ,
550
+ offset
551
+ } , ( err , data ) => {
552
+ expect ( err ) . to . exist ( )
553
+ expect ( err . message ) . to . equal ( 'Offset must be less than the file size' )
554
+ done ( )
555
+ } )
452
556
} )
453
557
454
558
it ( 'exports a large file > 1mb imported with raw leaves' , function ( done ) {
@@ -512,7 +616,7 @@ module.exports = (repo) => {
512
616
offset : - 1
513
617
} , ( error , data ) => {
514
618
expect ( error ) . to . be . ok ( )
515
- expect ( error . message ) . to . contain ( 'Offset must be greater than 0' )
619
+ expect ( error . message ) . to . contain ( 'Offset must be greater than or equal to 0' )
516
620
517
621
done ( )
518
622
} )
0 commit comments