@@ -440,6 +440,7 @@ static EXTENSIONS: phf::Set<&'static str> = phf::phf_set! {
440440#[ cfg( test) ]
441441mod tests {
442442 use std:: {
443+ collections:: HashSet ,
443444 fs,
444445 io:: { self , Write } ,
445446 path:: { Path , PathBuf } ,
@@ -555,6 +556,123 @@ mod tests {
555556 Ok ( ( ) )
556557 }
557558
559+ #[ test]
560+ fn compressor_uses_default_and_custom_extension_filters ( ) {
561+ let default = Compressor :: new ( 1 , 1 , Quality :: default ( ) , Algorithms :: default ( ) , None , false ) ;
562+ let custom = Compressor :: new (
563+ 1 ,
564+ 1 ,
565+ Quality :: default ( ) ,
566+ Algorithms :: default ( ) ,
567+ Some ( HashSet :: from ( [ String :: from ( "bin" ) ] ) ) ,
568+ false ,
569+ ) ;
570+
571+ assert ! ( default . should_compress( Path :: new( "asset.js" ) ) ) ;
572+ assert ! ( !default . should_compress( Path :: new( "asset.bin" ) ) ) ;
573+ assert ! ( !default . should_compress( Path :: new( "LICENSE" ) ) ) ;
574+ assert ! ( custom. should_compress( Path :: new( "asset.bin" ) ) ) ;
575+ assert ! ( !custom. should_compress( Path :: new( "asset.js" ) ) ) ;
576+ }
577+
578+ #[ test]
579+ fn compressor_skips_small_and_filtered_out_files ( ) -> Result < ( ) > {
580+ let root = test_dir ( "skip-files" ) ;
581+ fs:: write ( root. join ( "small.js" ) , "tiny" ) ?;
582+ fs:: write ( root. join ( "note.txt" ) , "ignored extension" ) ?;
583+
584+ let compressor = Compressor :: new (
585+ 1 ,
586+ 32 ,
587+ Quality :: default ( ) ,
588+ Algorithms {
589+ brotli : false ,
590+ deflate : false ,
591+ gzip : true ,
592+ zstd : false ,
593+ } ,
594+ Some ( HashSet :: from ( [ String :: from ( "js" ) ] ) ) ,
595+ false ,
596+ ) ;
597+ compressor. precompress ( & root, & WalkOptions :: default ( ) ) ?;
598+ let stats = compressor. finish ( ) ;
599+
600+ assert_eq ! ( stats. num_source_files, 0 ) ;
601+ assert_eq ! ( stats. num_errors, 0 ) ;
602+ assert ! ( !root. join( "small.js.gz" ) . exists( ) ) ;
603+ assert ! ( !root. join( "note.txt.gz" ) . exists( ) ) ;
604+
605+ fs:: remove_dir_all ( root) ?;
606+ Ok ( ( ) )
607+ }
608+
609+ #[ test]
610+ fn compressor_overwrites_existing_outputs ( ) -> Result < ( ) > {
611+ let root = test_dir ( "overwrite-output" ) ;
612+ let src_path = root. join ( "asset.js" ) ;
613+ fs:: write ( & src_path, "const payload = 'hello world';\n " . repeat ( 256 ) ) ?;
614+ let dst_path = root. join ( "asset.js.gz" ) ;
615+ fs:: write ( & dst_path, b"stale artifact" ) ?;
616+
617+ let original = fs:: read ( & dst_path) ?;
618+ let compressor = Compressor :: new (
619+ 1 ,
620+ 1 ,
621+ Quality :: default ( ) ,
622+ Algorithms {
623+ brotli : false ,
624+ deflate : false ,
625+ gzip : true ,
626+ zstd : false ,
627+ } ,
628+ None ,
629+ false ,
630+ ) ;
631+ compressor. precompress ( & root, & WalkOptions :: default ( ) ) ?;
632+ let stats = compressor. finish ( ) ;
633+
634+ assert_eq ! ( stats. num_source_files, 1 ) ;
635+ assert_eq ! ( stats. num_errors, 0 ) ;
636+ assert_ne ! ( fs:: read( & dst_path) ?, original) ;
637+
638+ fs:: remove_dir_all ( root) ?;
639+ Ok ( ( ) )
640+ }
641+
642+ #[ test]
643+ fn compressor_cleans_up_temp_output_after_failed_replace ( ) -> Result < ( ) > {
644+ let root = test_dir ( "cleanup-failed-replace" ) ;
645+ let src_path = root. join ( "asset.js" ) ;
646+ fs:: write ( & src_path, "const payload = 'hello world';\n " . repeat ( 256 ) ) ?;
647+ let dst_path = root. join ( "asset.js.gz" ) ;
648+ let tmp_path = tmp_output_path ( & dst_path) ;
649+ fs:: create_dir ( & dst_path) ?;
650+
651+ let compressor = Compressor :: new (
652+ 1 ,
653+ 1 ,
654+ Quality :: default ( ) ,
655+ Algorithms {
656+ brotli : false ,
657+ deflate : false ,
658+ gzip : true ,
659+ zstd : false ,
660+ } ,
661+ None ,
662+ false ,
663+ ) ;
664+ compressor. precompress ( & root, & WalkOptions :: default ( ) ) ?;
665+ let stats = compressor. finish ( ) ;
666+
667+ assert_eq ! ( stats. num_source_files, 0 ) ;
668+ assert_eq ! ( stats. num_errors, 1 ) ;
669+ assert ! ( dst_path. is_dir( ) ) ;
670+ assert ! ( !tmp_path. exists( ) ) ;
671+
672+ fs:: remove_dir_all ( root) ?;
673+ Ok ( ( ) )
674+ }
675+
558676 fn walk_paths ( root : & Path , options : & WalkOptions ) -> Result < Vec < String > > {
559677 let mut paths = build_walk ( root, options) ?
560678 . filter_map ( |entry| entry. ok ( ) )
0 commit comments