|
3 | 3 | // For the full copyright and license information, please view the LICENSE |
4 | 4 | // file that was distributed with this source code. |
5 | 5 |
|
| 6 | +use std::io::Write; |
6 | 7 | use std::path::{self, PathBuf}; |
7 | 8 |
|
8 | 9 | use uutests::{at_and_ucmd, new_ucmd}; |
@@ -752,3 +753,79 @@ fn test_list_conflicts_with_extract() { |
752 | 753 | .code_is(2) |
753 | 754 | .stderr_contains("cannot be used with"); |
754 | 755 | } |
| 756 | + |
| 757 | +// Gzip-compressed archive tests |
| 758 | + |
| 759 | +#[test] |
| 760 | +fn test_extract_gzip_archive() { |
| 761 | + let (at, _ucmd) = at_and_ucmd!(); |
| 762 | + |
| 763 | + // Build a .tar.gz in memory: tar containing one file, then gzip-compress it |
| 764 | + let mut tar_bytes = Vec::new(); |
| 765 | + { |
| 766 | + let mut builder = tar_rs_crate::Builder::new(&mut tar_bytes); |
| 767 | + let content = b"hello from gzip"; |
| 768 | + let mut header = tar_rs_crate::Header::new_gnu(); |
| 769 | + header.set_path("gzfile.txt").unwrap(); |
| 770 | + header.set_size(content.len() as u64); |
| 771 | + header.set_mode(0o644); |
| 772 | + header.set_cksum(); |
| 773 | + builder.append(&header, &content[..]).unwrap(); |
| 774 | + builder.finish().unwrap(); |
| 775 | + } |
| 776 | + |
| 777 | + // Gzip-compress the tar bytes |
| 778 | + let mut gz_bytes = Vec::new(); |
| 779 | + { |
| 780 | + let mut encoder = |
| 781 | + flate2::write::GzEncoder::new(&mut gz_bytes, flate2::Compression::default()); |
| 782 | + encoder.write_all(&tar_bytes).unwrap(); |
| 783 | + encoder.finish().unwrap(); |
| 784 | + } |
| 785 | + |
| 786 | + at.write_bytes("archive.tar.gz", &gz_bytes); |
| 787 | + |
| 788 | + // Extract using our tar implementation |
| 789 | + new_ucmd!() |
| 790 | + .arg("-xf") |
| 791 | + .arg(at.plus("archive.tar.gz")) |
| 792 | + .current_dir(at.as_string()) |
| 793 | + .succeeds(); |
| 794 | + |
| 795 | + assert!(at.file_exists("gzfile.txt")); |
| 796 | + assert_eq!(at.read("gzfile.txt"), "hello from gzip"); |
| 797 | +} |
| 798 | + |
| 799 | +#[test] |
| 800 | +fn test_list_gzip_archive() { |
| 801 | + let (at, _ucmd) = at_and_ucmd!(); |
| 802 | + |
| 803 | + // Build a .tar.gz in memory |
| 804 | + let mut tar_bytes = Vec::new(); |
| 805 | + { |
| 806 | + let mut builder = tar_rs_crate::Builder::new(&mut tar_bytes); |
| 807 | + let content = b"list test content"; |
| 808 | + let mut header = tar_rs_crate::Header::new_gnu(); |
| 809 | + header.set_path("listed.txt").unwrap(); |
| 810 | + header.set_size(content.len() as u64); |
| 811 | + header.set_mode(0o644); |
| 812 | + header.set_cksum(); |
| 813 | + builder.append(&header, &content[..]).unwrap(); |
| 814 | + builder.finish().unwrap(); |
| 815 | + } |
| 816 | + |
| 817 | + let mut gz_bytes = Vec::new(); |
| 818 | + { |
| 819 | + let mut encoder = |
| 820 | + flate2::write::GzEncoder::new(&mut gz_bytes, flate2::Compression::default()); |
| 821 | + encoder.write_all(&tar_bytes).unwrap(); |
| 822 | + encoder.finish().unwrap(); |
| 823 | + } |
| 824 | + |
| 825 | + at.write_bytes("archive.tar.gz", &gz_bytes); |
| 826 | + |
| 827 | + new_ucmd!() |
| 828 | + .args(&["-tf", &at.plus_as_string("archive.tar.gz")]) |
| 829 | + .succeeds() |
| 830 | + .stdout_contains("listed.txt"); |
| 831 | +} |
0 commit comments