1use crate::ExtractionError;
7use crate::ProgressCallback;
8use crate::Result;
9use crate::creation::config::CreationConfig;
10use crate::creation::filters;
11use crate::creation::report::CreationReport;
12use crate::creation::walker::EntryType;
13use crate::creation::walker::FilteredWalker;
14use crate::creation::walker::collect_entries;
15use std::fs::File;
16use std::io::Read;
17use std::io::Seek;
18use std::io::Write;
19use std::path::Path;
20use zip::CompressionMethod;
21use zip::ZipWriter;
22use zip::write::SimpleFileOptions;
23
24#[allow(dead_code)] pub fn create_zip<P: AsRef<Path>, Q: AsRef<Path>>(
47 output: P,
48 sources: &[Q],
49 config: &CreationConfig,
50) -> Result<CreationReport> {
51 let file = File::create(output.as_ref())?;
52 create_zip_internal(file, sources, config)
53}
54
55#[allow(dead_code)] pub fn create_zip_with_progress<P: AsRef<Path>, Q: AsRef<Path>>(
130 output: P,
131 sources: &[Q],
132 config: &CreationConfig,
133 progress: &mut dyn ProgressCallback,
134) -> Result<CreationReport> {
135 let file = File::create(output.as_ref())?;
136 create_zip_internal_with_progress(file, sources, config, progress)
137}
138
139fn create_zip_internal_with_progress<W: Write + Seek, P: AsRef<Path>>(
141 writer: W,
142 sources: &[P],
143 config: &CreationConfig,
144 progress: &mut dyn ProgressCallback,
145) -> Result<CreationReport> {
146 let mut zip = ZipWriter::new(writer);
147 let mut report = CreationReport::default();
148 let start = std::time::Instant::now();
149
150 let options = if config.compression_level == Some(0) {
152 SimpleFileOptions::default().compression_method(CompressionMethod::Stored)
153 } else {
154 let level = config.compression_level.unwrap_or(6);
155 SimpleFileOptions::default()
156 .compression_method(CompressionMethod::Deflated)
157 .compression_level(Some(i64::from(level)))
158 };
159
160 let entries = collect_entries(sources, config)?;
162 let total_entries = entries.len();
163
164 let mut buffer = vec![0u8; 64 * 1024]; for (idx, entry) in entries.iter().enumerate() {
168 let current_entry = idx + 1;
169
170 match &entry.entry_type {
171 EntryType::File => {
172 progress.on_entry_start(&entry.archive_path, total_entries, current_entry);
173 add_file_to_zip_with_progress_and_buffer(
174 &mut zip,
175 &entry.path,
176 &entry.archive_path,
177 config,
178 &mut report,
179 &options,
180 progress,
181 &mut buffer,
182 )?;
183 progress.on_entry_complete(&entry.archive_path);
184 }
185 EntryType::Directory => {
186 progress.on_entry_start(&entry.archive_path, total_entries, current_entry);
187 if !entry.archive_path.as_os_str().is_empty() {
189 let dir_path = format!("{}/", normalize_zip_path(&entry.archive_path)?);
190 zip.add_directory(&dir_path, options).map_err(|e| {
191 std::io::Error::other(format!("failed to add directory: {e}"))
192 })?;
193 report.directories_added += 1;
194 }
195 progress.on_entry_complete(&entry.archive_path);
196 }
197 EntryType::Symlink { .. } => {
198 progress.on_entry_start(&entry.archive_path, total_entries, current_entry);
199 if !config.follow_symlinks {
200 report.files_skipped += 1;
201 report.add_warning(format!("Skipped symlink: {}", entry.path.display()));
202 }
203 progress.on_entry_complete(&entry.archive_path);
204 }
205 }
206 }
207
208 zip.finish()
210 .map_err(|e| std::io::Error::other(format!("failed to finish ZIP archive: {e}")))?;
211
212 report.duration = start.elapsed();
213
214 progress.on_complete();
215
216 Ok(report)
217}
218
219fn create_zip_internal<W: Write + Seek, P: AsRef<Path>>(
223 writer: W,
224 sources: &[P],
225 config: &CreationConfig,
226) -> Result<CreationReport> {
227 let mut zip = ZipWriter::new(writer);
228 let mut report = CreationReport::default();
229 let start = std::time::Instant::now();
230
231 let options = if config.compression_level == Some(0) {
233 SimpleFileOptions::default().compression_method(CompressionMethod::Stored)
234 } else {
235 let level = config.compression_level.unwrap_or(6);
237 SimpleFileOptions::default()
238 .compression_method(CompressionMethod::Deflated)
239 .compression_level(Some(i64::from(level)))
240 };
241
242 for source in sources {
243 let path = source.as_ref();
244
245 if !path.exists() {
247 return Err(ExtractionError::SourceNotFound {
248 path: path.to_path_buf(),
249 });
250 }
251
252 if path.is_dir() {
254 add_directory_to_zip(&mut zip, path, config, &mut report, &options)?;
255 } else {
256 let archive_path =
258 filters::compute_archive_path(path, path.parent().unwrap_or(path), config)?;
259 add_file_to_zip(&mut zip, path, &archive_path, config, &mut report, &options)?;
260 }
261 }
262
263 zip.finish()
265 .map_err(|e| std::io::Error::other(format!("failed to finish ZIP archive: {e}")))?;
266
267 report.duration = start.elapsed();
268
269 Ok(report)
270}
271
272fn add_directory_to_zip<W: Write + Seek>(
274 zip: &mut ZipWriter<W>,
275 dir: &Path,
276 config: &CreationConfig,
277 report: &mut CreationReport,
278 options: &SimpleFileOptions,
279) -> Result<()> {
280 let walker = FilteredWalker::new(dir, config);
281
282 for entry in walker.walk() {
283 let entry = entry?;
284
285 match entry.entry_type {
286 EntryType::File => {
287 add_file_to_zip(
288 zip,
289 &entry.path,
290 &entry.archive_path,
291 config,
292 report,
293 options,
294 )?;
295 }
296 EntryType::Directory => {
297 let dir_path = format!("{}/", normalize_zip_path(&entry.archive_path)?);
299 zip.add_directory(&dir_path, *options)
300 .map_err(|e| std::io::Error::other(format!("failed to add directory: {e}")))?;
301 report.directories_added += 1;
302 }
303 EntryType::Symlink { .. } => {
304 if !config.follow_symlinks {
305 report.files_skipped += 1;
308 report.add_warning(format!("Skipped symlink: {}", entry.path.display()));
309 }
310 }
311 }
312 }
313
314 Ok(())
315}
316
317fn add_file_to_zip<W: Write + Seek>(
319 zip: &mut ZipWriter<W>,
320 file_path: &Path,
321 archive_path: &Path,
322 config: &CreationConfig,
323 report: &mut CreationReport,
324 options: &SimpleFileOptions,
325) -> Result<()> {
326 let mut file = File::open(file_path)?;
327 let metadata = file.metadata()?;
328 let size = metadata.len();
329
330 if let Some(max_size) = config.max_file_size
332 && size > max_size
333 {
334 report.files_skipped += 1;
335 report.add_warning(format!(
336 "Skipped file (too large): {} ({} bytes)",
337 file_path.display(),
338 size
339 ));
340 return Ok(());
341 }
342
343 let file_options = if config.preserve_permissions {
345 #[cfg(unix)]
346 {
347 use std::os::unix::fs::PermissionsExt;
348 options.unix_permissions(metadata.permissions().mode())
349 }
350 #[cfg(not(unix))]
351 {
352 *options
353 }
354 } else {
355 *options
356 };
357
358 let archive_name = normalize_zip_path(archive_path)?;
360
361 zip.start_file(&archive_name, file_options)
362 .map_err(|e| std::io::Error::other(format!("failed to start file in ZIP: {e}")))?;
363
364 let mut buffer = vec![0u8; 64 * 1024]; let mut bytes_written = 0u64;
367 loop {
368 let bytes_read = file.read(&mut buffer)?;
369 if bytes_read == 0 {
370 break;
371 }
372 zip.write_all(&buffer[..bytes_read])?;
373 bytes_written += bytes_read as u64;
374 }
375
376 report.files_added += 1;
377 report.bytes_written += bytes_written;
378
379 Ok(())
380}
381
382#[allow(clippy::too_many_arguments)]
385fn add_file_to_zip_with_progress_and_buffer<W: Write + Seek>(
386 zip: &mut ZipWriter<W>,
387 file_path: &Path,
388 archive_path: &Path,
389 config: &CreationConfig,
390 report: &mut CreationReport,
391 options: &SimpleFileOptions,
392 progress: &mut dyn ProgressCallback,
393 buffer: &mut [u8],
394) -> Result<()> {
395 let mut file = File::open(file_path)?;
396 let metadata = file.metadata()?;
397 let size = metadata.len();
398
399 if let Some(max_size) = config.max_file_size
401 && size > max_size
402 {
403 report.files_skipped += 1;
404 report.add_warning(format!(
405 "Skipped file (too large): {} ({} bytes)",
406 file_path.display(),
407 size
408 ));
409 return Ok(());
410 }
411
412 let file_options = if config.preserve_permissions {
414 #[cfg(unix)]
415 {
416 use std::os::unix::fs::PermissionsExt;
417 options.unix_permissions(metadata.permissions().mode())
418 }
419 #[cfg(not(unix))]
420 {
421 *options
422 }
423 } else {
424 *options
425 };
426
427 let archive_name = normalize_zip_path(archive_path)?;
428
429 zip.start_file(&archive_name, file_options)
430 .map_err(|e| std::io::Error::other(format!("failed to start file in ZIP: {e}")))?;
431
432 let mut bytes_written = 0u64;
434 loop {
435 let bytes_read = file.read(buffer)?;
436 if bytes_read == 0 {
437 break;
438 }
439 zip.write_all(&buffer[..bytes_read])?;
440 bytes_written += bytes_read as u64;
441 progress.on_bytes_written(bytes_read as u64);
442 }
443
444 report.files_added += 1;
445 report.bytes_written += bytes_written;
446
447 Ok(())
448}
449
450fn normalize_zip_path(path: &Path) -> Result<String> {
455 let path_str = path.to_str().ok_or_else(|| {
457 ExtractionError::Io(std::io::Error::other(format!(
458 "path is not valid UTF-8: {}",
459 path.display()
460 )))
461 })?;
462
463 #[cfg(windows)]
465 let normalized = path_str.replace('\\', "/");
466
467 #[cfg(not(windows))]
468 let normalized = path_str.to_string();
469
470 Ok(normalized)
471}
472
473pub struct ZipCreator;
475
476impl crate::formats::traits::FormatCreator for ZipCreator {
477 fn create(
478 &self,
479 output: &std::path::Path,
480 sources: &[&std::path::Path],
481 config: &CreationConfig,
482 progress: &mut dyn ProgressCallback,
483 ) -> crate::Result<crate::creation::CreationReport> {
484 create_zip_with_progress(output, sources, config, progress)
485 }
486
487 fn format_name(&self) -> &'static str {
488 "zip"
489 }
490}
491
492#[cfg(test)]
493#[allow(clippy::unwrap_used)] mod tests {
495 use super::*;
496 use std::fs;
497 use tempfile::TempDir;
498
499 #[test]
500 fn test_create_zip_single_file() {
501 let temp = TempDir::new().unwrap();
502 let output = temp.path().join("output.zip");
503
504 let source_dir = TempDir::new().unwrap();
506 fs::write(source_dir.path().join("test.txt"), "Hello ZIP").unwrap();
507
508 let config = CreationConfig::default()
509 .with_exclude_patterns(vec![])
510 .with_include_hidden(true);
511
512 let report = create_zip(&output, &[source_dir.path().join("test.txt")], &config).unwrap();
513
514 assert_eq!(report.files_added, 1);
515 assert!(report.bytes_written > 0);
516 assert!(output.exists());
517 }
518
519 #[test]
520 fn test_create_zip_directory() {
521 let temp = TempDir::new().unwrap();
522 let output = temp.path().join("output.zip");
523
524 let source_dir = TempDir::new().unwrap();
526 fs::write(source_dir.path().join("file1.txt"), "content1").unwrap();
527 fs::write(source_dir.path().join("file2.txt"), "content2").unwrap();
528 fs::create_dir(source_dir.path().join("subdir")).unwrap();
529 fs::write(source_dir.path().join("subdir/file3.txt"), "content3").unwrap();
530
531 let config = CreationConfig::default()
532 .with_exclude_patterns(vec![])
533 .with_include_hidden(true);
534
535 let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
536
537 assert_eq!(report.files_added, 3);
539 assert_eq!(report.directories_added, 2);
541 assert!(output.exists());
542 }
543
544 #[test]
545 fn test_create_zip_compression() {
546 let temp = TempDir::new().unwrap();
547 let output = temp.path().join("output.zip");
548
549 let source_dir = TempDir::new().unwrap();
551 fs::write(source_dir.path().join("test.txt"), "a".repeat(1000)).unwrap();
552
553 let config = CreationConfig::default()
554 .with_exclude_patterns(vec![])
555 .with_compression_level(9);
556
557 let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
558
559 assert_eq!(report.files_added, 1);
560 assert!(output.exists());
561
562 let data = fs::read(&output).unwrap();
564 assert_eq!(&data[0..4], b"PK\x03\x04"); }
566
567 #[test]
568 fn test_create_zip_compression_levels() {
569 let temp = TempDir::new().unwrap();
570
571 let source_dir = TempDir::new().unwrap();
573 fs::write(source_dir.path().join("test.txt"), "a".repeat(10000)).unwrap();
574
575 for level in [1, 6, 9] {
577 let output = temp.path().join(format!("output_{level}.zip"));
578 let config = CreationConfig::default()
579 .with_exclude_patterns(vec![])
580 .with_compression_level(level);
581
582 let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
583 assert_eq!(report.files_added, 1);
584 assert!(output.exists());
585 }
586 }
587
588 #[test]
589 fn test_create_zip_explicit_directories() {
590 let temp = TempDir::new().unwrap();
591 let output = temp.path().join("output.zip");
592
593 let source_dir = TempDir::new().unwrap();
595 fs::create_dir(source_dir.path().join("dir1")).unwrap();
596 fs::create_dir(source_dir.path().join("dir1/dir2")).unwrap();
597 fs::write(source_dir.path().join("dir1/dir2/file.txt"), "content").unwrap();
598
599 let config = CreationConfig::default()
600 .with_exclude_patterns(vec![])
601 .with_include_hidden(true);
602
603 let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
604
605 assert!(report.directories_added >= 2); assert!(output.exists());
607
608 let file = File::open(&output).unwrap();
610 let mut archive = zip::ZipArchive::new(file).unwrap();
611
612 let mut dir_entries = 0;
613 for i in 0..archive.len() {
614 let entry = archive.by_index(i).unwrap();
615 if entry.is_dir() {
616 dir_entries += 1;
617 assert!(
618 entry.name().unwrap().ends_with('/'),
619 "Directory entry should end with /"
620 );
621 }
622 }
623 assert!(dir_entries >= 2, "Expected at least 2 directory entries");
624 }
625
626 #[cfg(unix)]
627 #[test]
628 fn test_create_zip_preserves_permissions() {
629 use std::os::unix::fs::PermissionsExt;
630
631 let temp = TempDir::new().unwrap();
632 let output = temp.path().join("output.zip");
633
634 let source_dir = TempDir::new().unwrap();
636 let file_path = source_dir.path().join("test.txt");
637 fs::write(&file_path, "content").unwrap();
638 fs::set_permissions(&file_path, fs::Permissions::from_mode(0o755)).unwrap();
639
640 let config = CreationConfig::default()
641 .with_exclude_patterns(vec![])
642 .with_preserve_permissions(true);
643
644 let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
645 assert_eq!(report.files_added, 1);
646
647 let file = File::open(&output).unwrap();
649 let mut archive = zip::ZipArchive::new(file).unwrap();
650
651 for i in 0..archive.len() {
652 let entry = archive.by_index(i).unwrap();
653 if entry.name().unwrap().contains("test.txt")
654 && let Some(mode) = entry.unix_mode()
655 {
656 assert_eq!(mode & 0o777, 0o755, "Permissions should be preserved");
657 }
658 }
659 }
660
661 #[test]
662 fn test_create_zip_report_statistics() {
663 let temp = TempDir::new().unwrap();
664 let output = temp.path().join("output.zip");
665
666 let source_dir = TempDir::new().unwrap();
668 fs::write(source_dir.path().join("file1.txt"), "content1").unwrap();
669 fs::write(source_dir.path().join("file2.txt"), "content2").unwrap();
670 fs::create_dir(source_dir.path().join("subdir")).unwrap();
671 fs::write(source_dir.path().join("subdir/file3.txt"), "content3").unwrap();
672
673 let config = CreationConfig::default()
674 .with_exclude_patterns(vec![])
675 .with_include_hidden(true);
676
677 let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
678
679 assert_eq!(report.files_added, 3);
680 assert!(report.directories_added >= 1);
681 assert_eq!(report.files_skipped, 0);
682 assert!(!report.has_warnings());
683 assert!(report.duration.as_nanos() > 0);
684 }
685
686 #[test]
687 fn test_create_zip_roundtrip() {
688 let temp = TempDir::new().unwrap();
689 let output = temp.path().join("output.zip");
690
691 let source_dir = TempDir::new().unwrap();
693 fs::write(source_dir.path().join("file1.txt"), "content1").unwrap();
694 fs::create_dir(source_dir.path().join("subdir")).unwrap();
695 fs::write(source_dir.path().join("subdir/file2.txt"), "content2").unwrap();
696
697 let config = CreationConfig::default()
698 .with_exclude_patterns(vec![])
699 .with_include_hidden(true);
700
701 let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
703 assert!(report.files_added >= 2);
704
705 let file = File::open(&output).unwrap();
707 let mut archive = zip::ZipArchive::new(file).unwrap();
708
709 let extract_dir = TempDir::new().unwrap();
710
711 for i in 0..archive.len() {
712 let mut entry = archive.by_index(i).unwrap();
713 let outpath = extract_dir.path().join(entry.name().unwrap().as_ref());
714
715 if entry.is_dir() {
716 fs::create_dir_all(&outpath).unwrap();
717 } else {
718 if let Some(parent) = outpath.parent() {
719 fs::create_dir_all(parent).unwrap();
720 }
721 let mut outfile = File::create(&outpath).unwrap();
722 std::io::copy(&mut entry, &mut outfile).unwrap();
723 }
724 }
725
726 let extracted1 = fs::read_to_string(extract_dir.path().join("file1.txt")).unwrap();
728 assert_eq!(extracted1, "content1");
729
730 let extracted2 = fs::read_to_string(extract_dir.path().join("subdir/file2.txt")).unwrap();
731 assert_eq!(extracted2, "content2");
732 }
733
734 #[test]
735 fn test_create_zip_forward_slashes() {
736 let temp = TempDir::new().unwrap();
737 let output = temp.path().join("output.zip");
738
739 let source_dir = TempDir::new().unwrap();
741 fs::create_dir(source_dir.path().join("dir1")).unwrap();
742 fs::write(source_dir.path().join("dir1/file.txt"), "content").unwrap();
743
744 let config = CreationConfig::default()
745 .with_exclude_patterns(vec![])
746 .with_include_hidden(true);
747
748 create_zip(&output, &[source_dir.path()], &config).unwrap();
749
750 let file = File::open(&output).unwrap();
752 let mut archive = zip::ZipArchive::new(file).unwrap();
753
754 for i in 0..archive.len() {
755 let entry = archive.by_index(i).unwrap();
756 let name = entry.name().unwrap();
757 assert!(
759 !name.contains('\\'),
760 "ZIP path should use forward slashes: {name}"
761 );
762 if name.contains("dir1") && name.contains("file") {
764 assert!(name.contains("dir1/file"), "Expected forward slash in path");
765 }
766 }
767 }
768
769 #[test]
770 fn test_create_zip_source_not_found() {
771 let temp = TempDir::new().unwrap();
772 let output = temp.path().join("output.zip");
773
774 let config = CreationConfig::default();
775 let result = create_zip(&output, &[Path::new("/nonexistent/path")], &config);
776
777 assert!(result.is_err());
778 assert!(matches!(
779 result.unwrap_err(),
780 ExtractionError::SourceNotFound { .. }
781 ));
782 }
783
784 #[test]
785 fn test_normalize_zip_path() {
786 let path = Path::new("dir/file.txt");
788 let normalized = normalize_zip_path(path).unwrap();
789 assert_eq!(normalized, "dir/file.txt");
790
791 let path = Path::new("file.txt");
793 let normalized = normalize_zip_path(path).unwrap();
794 assert_eq!(normalized, "file.txt");
795
796 let path = Path::new("a/b/c/file.txt");
798 let normalized = normalize_zip_path(path).unwrap();
799 assert_eq!(normalized, "a/b/c/file.txt");
800 }
801
802 #[cfg(windows)]
803 #[test]
804 fn test_normalize_zip_path_windows() {
805 let path = Path::new("dir\\file.txt");
807 let normalized = normalize_zip_path(path).unwrap();
808 assert_eq!(normalized, "dir/file.txt");
809
810 let path = Path::new("a\\b\\c\\file.txt");
812 let normalized = normalize_zip_path(path).unwrap();
813 assert_eq!(normalized, "a/b/c/file.txt");
814 }
815
816 #[test]
817 fn test_create_zip_max_file_size() {
818 let temp = TempDir::new().unwrap();
819 let output = temp.path().join("output.zip");
820
821 let source_dir = TempDir::new().unwrap();
823 fs::write(source_dir.path().join("small.txt"), "tiny").unwrap(); fs::write(source_dir.path().join("large.txt"), "a".repeat(1000)).unwrap(); let config = CreationConfig::default()
828 .with_exclude_patterns(vec![])
829 .with_max_file_size(Some(100));
830
831 let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
832
833 assert_eq!(report.files_added, 1);
836 assert_eq!(report.files_skipped, 0);
837 }
838
839 #[cfg(unix)]
840 #[test]
841 fn test_create_zip_skips_symlinks() {
842 let temp = TempDir::new().unwrap();
843 let output = temp.path().join("output.zip");
844
845 let source_dir = TempDir::new().unwrap();
847 fs::write(source_dir.path().join("target.txt"), "content").unwrap();
848 std::os::unix::fs::symlink(
849 source_dir.path().join("target.txt"),
850 source_dir.path().join("link.txt"),
851 )
852 .unwrap();
853
854 let config = CreationConfig::default()
856 .with_exclude_patterns(vec![])
857 .with_include_hidden(true);
858
859 let report = create_zip(&output, &[source_dir.path()], &config).unwrap();
860
861 assert_eq!(report.files_added, 1);
863 assert_eq!(report.files_skipped, 1);
864 assert!(report.has_warnings());
865
866 let warning = &report.warnings[0];
867 assert!(warning.contains("Skipped symlink"));
868 }
869
870 #[test]
871 fn test_create_zip_with_progress_callback() {
872 #[derive(Debug, Default, Clone)]
873 struct TestProgress {
874 entries_started: Vec<String>,
875 entries_completed: Vec<String>,
876 bytes_written: u64,
877 completed: bool,
878 }
879
880 impl ProgressCallback for TestProgress {
881 fn on_entry_start(&mut self, path: &Path, _total: usize, _current: usize) {
882 self.entries_started
883 .push(path.to_string_lossy().to_string());
884 }
885
886 fn on_bytes_written(&mut self, bytes: u64) {
887 self.bytes_written += bytes;
888 }
889
890 fn on_entry_complete(&mut self, path: &Path) {
891 self.entries_completed
892 .push(path.to_string_lossy().to_string());
893 }
894
895 fn on_complete(&mut self) {
896 self.completed = true;
897 }
898 }
899
900 let temp = TempDir::new().unwrap();
901 let output = temp.path().join("output.zip");
902
903 let source_dir = TempDir::new().unwrap();
905 fs::write(source_dir.path().join("file1.txt"), "content1").unwrap();
906 fs::write(source_dir.path().join("file2.txt"), "content2").unwrap();
907 fs::create_dir(source_dir.path().join("subdir")).unwrap();
908 fs::write(source_dir.path().join("subdir/file3.txt"), "content3").unwrap();
909
910 let config = CreationConfig::default()
911 .with_exclude_patterns(vec![])
912 .with_include_hidden(true);
913
914 let mut progress = TestProgress::default();
915
916 let report =
917 create_zip_with_progress(&output, &[source_dir.path()], &config, &mut progress)
918 .unwrap();
919
920 assert_eq!(report.files_added, 3);
922 assert!(report.directories_added >= 1);
923
924 assert!(
926 progress.entries_started.len() >= 3,
927 "Expected at least 3 entry starts, got {}",
928 progress.entries_started.len()
929 );
930 assert!(
931 progress.entries_completed.len() >= 3,
932 "Expected at least 3 entry completions, got {}",
933 progress.entries_completed.len()
934 );
935 assert!(
936 progress.bytes_written > 0,
937 "Expected bytes written > 0, got {}",
938 progress.bytes_written
939 );
940 assert!(progress.completed, "Expected on_complete to be called");
941
942 let has_file1 = progress
944 .entries_started
945 .iter()
946 .any(|p| p.contains("file1.txt"));
947 let has_file2 = progress
948 .entries_started
949 .iter()
950 .any(|p| p.contains("file2.txt"));
951 let has_file3 = progress
952 .entries_started
953 .iter()
954 .any(|p| p.contains("file3.txt"));
955
956 assert!(has_file1, "Expected file1.txt in progress callbacks");
957 assert!(has_file2, "Expected file2.txt in progress callbacks");
958 assert!(has_file3, "Expected file3.txt in progress callbacks");
959 }
960}