Skip to content

Commit 8362819

Browse files
authored
Merge pull request #193 from Tcode-Motion/fix/zip_dir_tests-9038664599961891736
🧪 [testing improvement] Add tests for zip_dir
2 parents 7466415 + 4feb9cd commit 8362819

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

‎stdlib/src/compress.rs‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,58 @@ mod tests {
308308

309309
std::fs::remove_dir_all(&temp_dir).unwrap();
310310
}
311+
312+
#[test]
313+
fn test_zip_dir_success() {
314+
let temp_dir = std::env::temp_dir().join("techscript_zip_test");
315+
std::fs::create_dir_all(&temp_dir).unwrap();
316+
317+
let file1_path = temp_dir.join("file1.txt");
318+
std::fs::write(&file1_path, b"hello world").unwrap();
319+
320+
let file2_path = temp_dir.join("file2.txt");
321+
std::fs::write(&file2_path, b"test content").unwrap();
322+
323+
let zip_path = std::env::temp_dir().join("output.zip");
324+
325+
// Use zip_dir to compress
326+
let result = zip_dir(temp_dir.to_str().unwrap(), zip_path.to_str().unwrap());
327+
assert!(result.is_ok());
328+
329+
// Verify the zip file
330+
let file = std::fs::File::open(&zip_path).unwrap();
331+
let mut archive = zip::ZipArchive::new(file).unwrap();
332+
333+
let mut files_in_zip: Vec<String> = (0..archive.len())
334+
.map(|i| archive.by_index(i).unwrap().name().to_string())
335+
.collect();
336+
files_in_zip.sort();
337+
338+
println!("Files in zip: {:?}", files_in_zip);
339+
340+
{
341+
let mut f1 = archive.by_name("file1.txt").unwrap();
342+
let mut buf1 = String::new();
343+
std::io::Read::read_to_string(&mut f1, &mut buf1).unwrap();
344+
assert_eq!(buf1, "hello world");
345+
}
346+
347+
{
348+
let mut f2 = archive.by_name("file2.txt").unwrap();
349+
let mut buf2 = String::new();
350+
std::io::Read::read_to_string(&mut f2, &mut buf2).unwrap();
351+
assert_eq!(buf2, "test content");
352+
}
353+
354+
std::fs::remove_dir_all(&temp_dir).unwrap();
355+
std::fs::remove_file(&zip_path).unwrap();
356+
}
357+
358+
#[test]
359+
fn test_zip_dir_invalid_src() {
360+
let zip_path = std::env::temp_dir().join("output_invalid_src.zip");
361+
let result = zip_dir("non_existent_dir_12345", zip_path.to_str().unwrap());
362+
assert!(result.is_err());
363+
let _ = std::fs::remove_file(&zip_path); // Cleanup just in case it was created
364+
}
311365
}

0 commit comments

Comments
 (0)