this repo has no description
1extern crate assert_cmd;
2extern crate assert_fs;
3extern crate predicates;
4
5use assert_cmd::prelude::*;
6use assert_fs::TempDir;
7use assert_fs::prelude::*;
8use predicates::prelude::*;
9use std::process::Command;
10
11// Test manual step-by-step tar.gz roundtrip
12#[test]
13fn test_tar_gz_manual_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
14 let temp_dir = TempDir::new()?;
15 let file = temp_dir.child("test.txt");
16 file.write_str("test content")?;
17
18 // Step 1: Create a tar file
19 let tar_file = temp_dir.child("test.tar");
20 Command::cargo_bin("cmprss")?
21 .arg("tar")
22 .arg(file.path())
23 .arg(tar_file.path())
24 .assert()
25 .success();
26
27 // Step 2: Compress the tar file with gzip
28 let tar_gz_file = temp_dir.child("test.tar.gz");
29 Command::cargo_bin("cmprss")?
30 .arg("gzip")
31 .arg(tar_file.path())
32 .arg(tar_gz_file.path())
33 .assert()
34 .success();
35
36 // Step 3: Extract the gzip layer
37 let extract_tar = temp_dir.child("extracted.tar");
38 Command::cargo_bin("cmprss")?
39 .arg("gzip")
40 .arg("--extract")
41 .arg(tar_gz_file.path())
42 .arg(extract_tar.path())
43 .assert()
44 .success();
45
46 // Step 4: Extract the tar layer to a directory
47 let extract_dir = temp_dir.child("extracted");
48 extract_dir.create_dir_all()?;
49 Command::cargo_bin("cmprss")?
50 .arg("tar")
51 .arg("--extract")
52 .arg(extract_tar.path())
53 .arg(extract_dir.path())
54 .assert()
55 .success();
56
57 // Verify the extracted content
58 let extracted_file = extract_dir.child("test.txt");
59 extracted_file.assert(predicate::path::exists());
60 extracted_file.assert(predicate::str::contains("test content"));
61
62 Ok(())
63}
64
65// Test pipeline compression using tar.gz format
66#[test]
67fn test_tar_gz_compress() -> Result<(), Box<dyn std::error::Error>> {
68 let temp_dir = TempDir::new()?;
69
70 // Create a file structure for testing
71 let source_dir = temp_dir.child("source");
72 source_dir.create_dir_all()?;
73
74 let test_file = source_dir.child("test_file.txt");
75 test_file.write_str("test content for tar.gz compression")?;
76
77 // Create a tar.gz archive directly in one step
78 let archive = temp_dir.child("direct.tar.gz");
79 Command::cargo_bin("cmprss")?
80 .arg("--compress") // explicitly specify compression
81 .arg(source_dir.path())
82 .arg(archive.path())
83 .assert()
84 .success();
85
86 // Verify the archive was created
87 archive.assert(predicate::path::exists());
88
89 Ok(())
90}
91
92// Test pipeline extraction using tar.gz format
93#[test]
94fn test_tar_gz_extract() -> Result<(), Box<dyn std::error::Error>> {
95 let temp_dir = TempDir::new()?;
96
97 // Create a file structure for testing
98 let source_dir = temp_dir.child("source");
99 source_dir.create_dir_all()?;
100
101 let test_file = source_dir.child("test_file.txt");
102 test_file.write_str("test content for tar.gz extraction")?;
103
104 // Create a tar file first
105 let tar_file = temp_dir.child("archive.tar");
106 Command::cargo_bin("cmprss")?
107 .arg("tar")
108 .arg(source_dir.path())
109 .arg(tar_file.path())
110 .assert()
111 .success();
112
113 // Compress the tar with gzip
114 let tar_gz_file = temp_dir.child("archive.tar.gz");
115 Command::cargo_bin("cmprss")?
116 .arg("gzip")
117 .arg(tar_file.path())
118 .arg(tar_gz_file.path())
119 .assert()
120 .success();
121
122 // Create an extraction directory
123 let extract_dir = temp_dir.child("extract");
124 extract_dir.create_dir_all()?;
125
126 // Extract the tar.gz archive
127 Command::cargo_bin("cmprss")?
128 .arg("--extract")
129 .arg(tar_gz_file.path())
130 .arg(extract_dir.path())
131 .assert()
132 .success();
133
134 // Verify the file was extracted correctly (tar preserves the directory structure)
135 let extracted_file = extract_dir.child("source").child("test_file.txt");
136 extracted_file.assert(predicate::path::exists());
137 extracted_file.assert(predicate::str::contains(
138 "test content for tar.gz extraction",
139 ));
140
141 Ok(())
142}
143
144/// Full roundtrip for a `tar.<codec>` compound extension using cmprss alone.
145/// Verifies that the extension resolver, compress pipeline, and extract
146/// pipeline agree for the given compound format.
147fn tar_pipeline_roundtrip(ext: &str) -> Result<(), Box<dyn std::error::Error>> {
148 let temp_dir = TempDir::new()?;
149
150 let source_dir = temp_dir.child("source");
151 source_dir.create_dir_all()?;
152 let test_file = source_dir.child("data.txt");
153 let content = format!("{ext} roundtrip content");
154 test_file.write_str(&content)?;
155
156 let archive = temp_dir.child(format!("archive.{ext}"));
157 Command::cargo_bin("cmprss")?
158 .arg("--compress")
159 .arg(source_dir.path())
160 .arg(archive.path())
161 .assert()
162 .success();
163
164 let extract_dir = temp_dir.child("extract");
165 extract_dir.create_dir_all()?;
166 Command::cargo_bin("cmprss")?
167 .arg("--extract")
168 .arg(archive.path())
169 .arg(extract_dir.path())
170 .assert()
171 .success();
172
173 let extracted_file = extract_dir.child("source").child("data.txt");
174 extracted_file.assert(predicate::path::exists());
175 extracted_file.assert(predicate::str::contains(content));
176
177 Ok(())
178}
179
180#[test]
181fn test_tar_xz_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
182 tar_pipeline_roundtrip("tar.xz")
183}
184
185#[test]
186fn test_tar_bz2_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
187 tar_pipeline_roundtrip("tar.bz2")
188}
189
190#[test]
191fn test_tar_zst_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
192 tar_pipeline_roundtrip("tar.zst")
193}
194
195#[test]
196fn test_tar_lzma_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
197 tar_pipeline_roundtrip("tar.lzma")
198}
199
200#[test]
201fn test_tar_br_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
202 tar_pipeline_roundtrip("tar.br")
203}
204
205#[test]
206fn test_tar_lz4_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
207 tar_pipeline_roundtrip("tar.lz4")
208}
209
210#[test]
211fn test_tar_sz_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
212 tar_pipeline_roundtrip("tar.sz")
213}
214
215// Test pipeline extraction using tar.gz with explicit compress then auto-detect extract
216#[test]
217fn test_tar_gz_explicit_then_extract() -> Result<(), Box<dyn std::error::Error>> {
218 let temp_dir = TempDir::new()?;
219
220 // Create a simple test file
221 let test_file = temp_dir.child("test.txt");
222 test_file.write_str("test content for tar.gz")?;
223
224 // Create a tar archive first (explicit command)
225 let tar_file = temp_dir.child("test.tar");
226 Command::cargo_bin("cmprss")?
227 .arg("tar")
228 .arg(test_file.path())
229 .arg(tar_file.path())
230 .assert()
231 .success();
232
233 // Compress the tar with gzip (explicit command)
234 let tar_gz_file = temp_dir.child("test.tar.gz");
235 Command::cargo_bin("cmprss")?
236 .arg("gzip")
237 .arg(tar_file.path())
238 .arg(tar_gz_file.path())
239 .assert()
240 .success();
241
242 // Create an extraction directory
243 let extract_dir = temp_dir.child("extract");
244 extract_dir.create_dir_all()?;
245
246 // Extract using the tar.gz auto-detection
247 Command::cargo_bin("cmprss")?
248 .arg("-e") // Use short form for extract
249 .arg(tar_gz_file.path())
250 .arg(extract_dir.path())
251 .assert()
252 .success();
253
254 // Verify the file was extracted correctly
255 let extracted_file = extract_dir.child("test.txt");
256 extracted_file.assert(predicate::path::exists());
257 extracted_file.assert(predicate::str::contains("test content for tar.gz"));
258
259 Ok(())
260}