this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

test: add unit tests for pipeline extension scanning and trait methods

+104
+104
src/main.rs
··· 558 558 Ok(()) 559 559 } 560 560 561 + #[cfg(test)] 562 + mod tests { 563 + use super::*; 564 + use std::path::Path; 565 + 566 + fn compressor_name(path: &str) -> Option<String> { 567 + get_compressor_from_filename(Path::new(path)).map(|c| c.name().to_string()) 568 + } 569 + 570 + fn compressor_extension(path: &str) -> Option<String> { 571 + get_compressor_from_filename(Path::new(path)).map(|c| c.extension().to_string()) 572 + } 573 + 574 + #[test] 575 + fn test_single_extension() { 576 + assert_eq!(compressor_name("file.gz"), Some("gzip".into())); 577 + assert_eq!(compressor_name("file.xz"), Some("xz".into())); 578 + assert_eq!(compressor_name("file.bz2"), Some("bzip2".into())); 579 + assert_eq!(compressor_name("file.zst"), Some("zstd".into())); 580 + assert_eq!(compressor_name("file.lz4"), Some("lz4".into())); 581 + assert_eq!(compressor_name("file.tar"), Some("tar".into())); 582 + assert_eq!(compressor_name("file.zip"), Some("zip".into())); 583 + } 584 + 585 + #[test] 586 + fn test_multi_extension() { 587 + assert_eq!(compressor_name("archive.tar.gz"), Some("gzip".into())); 588 + assert_eq!(compressor_name("archive.tar.xz"), Some("xz".into())); 589 + assert_eq!(compressor_name("archive.tar.bz2"), Some("bzip2".into())); 590 + assert_eq!(compressor_name("archive.tar.zst"), Some("zstd".into())); 591 + } 592 + 593 + #[test] 594 + fn test_unknown_middle_extension() { 595 + // "b" is not a compressor, so only tar.gz should be detected 596 + assert_eq!(compressor_name("a.b.tar.gz"), Some("gzip".into())); 597 + assert_eq!(compressor_name("report.2024.tar.gz"), Some("gzip".into())); 598 + } 599 + 600 + #[test] 601 + fn test_no_recognized_extension() { 602 + assert_eq!(compressor_name("file.txt"), None); 603 + assert_eq!(compressor_name("file.pdf"), None); 604 + assert_eq!(compressor_name("file"), None); 605 + } 606 + 607 + #[test] 608 + fn test_default_filenames_single_pipeline() { 609 + let c = get_compressor_from_filename(Path::new("file.gz")).unwrap(); 610 + assert_eq!( 611 + c.default_compressed_filename(Path::new("data.txt")), 612 + "data.txt.gz" 613 + ); 614 + assert_eq!(c.default_extracted_filename(Path::new("data.gz")), "data"); 615 + } 616 + 617 + #[test] 618 + fn test_default_filenames_multi_pipeline() { 619 + let c = get_compressor_from_filename(Path::new("archive.tar.gz")).unwrap(); 620 + assert_eq!( 621 + c.default_compressed_filename(Path::new("data")), 622 + "data.tar.gz" 623 + ); 624 + // tar.gz extracts to a directory, so extracted filename is "." 625 + assert_eq!(c.default_extracted_filename(Path::new("data.tar.gz")), "."); 626 + } 627 + 628 + #[test] 629 + fn test_is_archive_single_pipeline() { 630 + let c = get_compressor_from_filename(Path::new("file.gz")).unwrap(); 631 + assert!(c.is_archive(Path::new("test.gz"))); 632 + assert!(!c.is_archive(Path::new("test.xz"))); 633 + } 634 + 635 + #[test] 636 + fn test_is_archive_multi_pipeline() { 637 + let c = get_compressor_from_filename(Path::new("archive.tar.gz")).unwrap(); 638 + assert!(c.is_archive(Path::new("foo.tar.gz"))); 639 + assert!(!c.is_archive(Path::new("foo.gz"))); 640 + } 641 + 642 + #[test] 643 + fn test_extracted_target_single_pipeline() { 644 + let gz = get_compressor_from_filename(Path::new("file.gz")).unwrap(); 645 + assert_eq!(gz.default_extracted_target(), ExtractedTarget::FILE); 646 + 647 + let tar = get_compressor_from_filename(Path::new("file.tar")).unwrap(); 648 + assert_eq!(tar.default_extracted_target(), ExtractedTarget::DIRECTORY); 649 + } 650 + 651 + #[test] 652 + fn test_extracted_target_multi_pipeline() { 653 + // tar.gz: innermost is tar, which extracts to directory 654 + let c = get_compressor_from_filename(Path::new("archive.tar.gz")).unwrap(); 655 + assert_eq!(c.default_extracted_target(), ExtractedTarget::DIRECTORY); 656 + } 657 + 658 + #[test] 659 + fn test_single_extension_returns_correct_extension() { 660 + assert_eq!(compressor_extension("file.gz"), Some("gz".into())); 661 + assert_eq!(compressor_extension("file.tar"), Some("tar".into())); 662 + } 663 + } 664 + 561 665 fn main() { 562 666 let args = CmprssArgs::parse(); 563 667 match args.format {