use std::fs::{self, File};
use std::io::{Read, Write};
use tempfile::tempdir;
use crate::{*, archive::{Archive, ArchiveOperation}, zip_manager::*};
use ::zip::read::ZipArchive;
#[test]
fn test_archive_util_archive_without_deletion() {
let temp_dir = tempdir().expect("Failed to create temp directory");
let temp_dir_path = temp_dir.path();
let test_file_path = temp_dir_path.join("test_file.txt");
{
let mut test_file = File::create(&test_file_path).expect("Failed to create test file");
writeln!(test_file, "This is a test file for archiving.").expect("Failed to write to test file");
}
assert!(test_file_path.exists());
archive_util!(temp_dir_path, false, Archive);
let archive_path = temp_dir_path.with_extension("tar.xz");
assert!(archive_path.exists(), "Archive file was not created.");
assert!(temp_dir_path.exists(), "Original directory was deleted despite delete_dir=false.");
fs::remove_file(archive_path).expect("Failed to remove archive file.");
}
#[test]
fn test_archive_util_archive_with_deletion() {
let temp_dir = tempdir().expect("Failed to create temp directory");
let temp_dir_path = temp_dir.path().to_path_buf();
let test_file_path = temp_dir_path.join("test_file.txt");
{
let mut test_file = File::create(&test_file_path).expect("Failed to create test file");
writeln!(test_file, "This is a test file for archiving with deletion.").expect("Failed to write to test file");
}
assert!(test_file_path.exists());
archive_util!(temp_dir_path.clone(), true, Archive);
let archive_path = temp_dir_path.with_extension("tar.xz");
assert!(archive_path.exists(), "Archive file was not created.");
assert!(!temp_dir_path.exists(), "Original directory was not deleted despite delete_dir=true.");
fs::remove_file(archive_path).expect("Failed to remove archive file.");
}
#[test]
fn test_archive_util_extract_without_deletion() {
let archive_temp_dir = tempdir().expect("Failed to create archive temp directory");
let archive_temp_dir_path = archive_temp_dir.path();
let test_file_path = archive_temp_dir_path.join("test_file.txt");
{
let mut test_file = File::create(&test_file_path).expect("Failed to create test file");
writeln!(test_file, "This is a test file for extraction.").expect("Failed to write to test file");
}
archive_util!(archive_temp_dir_path, false, Archive);
let archive_path = archive_temp_dir_path.with_extension("tar.xz");
assert!(archive_path.exists(), "Archive file was not created.");
archive_util!(archive_path.clone(), false, Extract);
let extraction_dir = archive_temp_dir_path;
assert!(extraction_dir.exists(), "Extraction directory does not exist.");
let extracted_file_path = extraction_dir.join("test_file.txt");
assert!(extracted_file_path.exists(), "Extracted file does not exist.");
let mut contents = String::new();
let mut extracted_file = File::open(&extracted_file_path).expect("Failed to open extracted file.");
extracted_file.read_to_string(&mut contents).expect("Failed to read extracted file.");
assert_eq!(contents.trim(), "This is a test file for extraction.", "Extracted file contents do not match.");
assert!(archive_path.exists(), "Archive file was deleted despite delete_archive=false.");
fs::remove_file(archive_path).expect("Failed to remove archive file.");
}
#[test]
fn test_archive_util_extract_with_deletion() {
let archive_temp_dir = tempdir().expect("Failed to create archive temp directory");
let archive_temp_dir_path = archive_temp_dir.path().to_path_buf();
let test_file_path = archive_temp_dir_path.join("test_file.txt");
{
let mut test_file = File::create(&test_file_path).expect("Failed to create test file");
writeln!(test_file, "This is a test file for extraction with deletion.").expect("Failed to write to test file");
}
archive_util!(archive_temp_dir_path.clone(), true, Archive);
let archive_path = archive_temp_dir_path.with_extension("tar.xz");
assert!(archive_path.exists(), "Archive file was not created.");
archive_util!(archive_path.clone(), true, Extract);
let extraction_dir = archive_temp_dir_path.clone();
assert!(extraction_dir.exists(), "Extraction directory does not exist.");
let extracted_file_path = extraction_dir.join("test_file.txt");
assert!(extracted_file_path.exists(), "Extracted file does not exist.");
let mut contents = String::new();
let mut extracted_file = File::open(&extracted_file_path).expect("Failed to open extracted file.");
extracted_file.read_to_string(&mut contents).expect("Failed to read extracted file.");
assert_eq!(contents.trim(), "This is a test file for extraction with deletion.", "Extracted file contents do not match.");
assert!(!archive_path.exists(), "Archive file was not deleted despite delete_archive=true.");
}
#[test]
fn test_archive_and_extract() {
let temp_dir = tempdir().expect("Failed to create temp directory");
let temp_dir_path = temp_dir.path();
let temp_file_path = temp_dir_path.join("test_file.txt");
{
let mut temp_file = File::create(&temp_file_path).expect("Failed to create temp file");
writeln!(temp_file, "This is a test file.").expect("Failed to write to temp file");
}
assert!(temp_file_path.exists());
let archive = Archive::new(temp_dir_path.to_path_buf(), ArchiveOperation::Archive);
archive.execute(false).expect("Archiving failed");
let archive_path = temp_dir_path.with_extension("tar.xz");
assert!(archive_path.exists());
let unarchive = Archive::new(archive_path.clone(), ArchiveOperation::Unarchive);
unarchive.execute(false).expect("Extraction failed");
let extracted_dir = temp_dir_path.to_path_buf();
assert!(extracted_dir.exists());
let extracted_file_path = extracted_dir.join("test_file.txt");
assert!(extracted_file_path.exists());
let mut contents = String::new();
let mut extracted_file = File::open(&extracted_file_path).expect("Failed to open extracted file");
extracted_file
.read_to_string(&mut contents)
.expect("Failed to read extracted file");
assert_eq!(contents.trim(), "This is a test file.");
}
#[test]
fn test_archive_subdirectory() {
let temp_dir = tempdir().expect("Failed to create temp directory");
let temp_dir_path = temp_dir.path();
let sub_dir_path = temp_dir_path.join("sub_dir");
fs::create_dir(&sub_dir_path).expect("Failed to create subdirectory");
let file_in_subdir_path = sub_dir_path.join("test_file.txt");
{
let mut temp_file = File::create(&file_in_subdir_path).expect("Failed to create temp file");
writeln!(temp_file, "This is a test file in a subdirectory.")
.expect("Failed to write to temp file");
}
assert!(file_in_subdir_path.exists());
let archive = Archive::new(sub_dir_path.clone(), ArchiveOperation::Archive);
archive.execute(false).expect("Archiving failed");
let archive_path = sub_dir_path.with_extension("tar.xz");
assert!(archive_path.exists());
let extract_archive = Archive::new(archive_path.clone(), ArchiveOperation::Unarchive);
extract_archive.execute(false).expect("Extraction failed");
let extracted_dir = archive_path
.with_extension("") .with_extension("");
assert!(extracted_dir.exists());
let extracted_file_path = extracted_dir.join("test_file.txt");
assert!(extracted_file_path.exists());
let mut contents = String::new();
let mut extracted_file =
File::open(&extracted_file_path).expect("Failed to open extracted file");
extracted_file
.read_to_string(&mut contents)
.expect("Failed to read extracted file");
assert_eq!(contents.trim(), "This is a test file in a subdirectory.");
}
#[test]
fn test_archive_and_extract_with_delete_dir() {
let temp_dir = tempdir().expect("Failed to create temp directory");
let temp_dir_path = temp_dir.path().to_path_buf();
let temp_file_path = temp_dir_path.join("test_file_delete.txt");
{
let mut temp_file = File::create(&temp_file_path)
.expect("Failed to create temp file for delete_dir=true test");
writeln!(temp_file, "This file will be archived and then the directory deleted.")
.expect("Failed to write to temp file");
}
assert!(temp_file_path.exists());
let archive = Archive::new(temp_dir_path.clone(), ArchiveOperation::Archive);
archive.execute(true).expect("Archiving with delete_dir=true failed");
let archive_path = temp_dir_path.with_extension("tar.xz");
assert!(archive_path.exists());
assert!(!temp_dir_path.exists(), "Source directory was not deleted after archiving with delete_dir=true");
let unarchive = Archive::new(archive_path.clone(), ArchiveOperation::Unarchive);
unarchive.execute(true).expect("Unarchiving with delete_dir=true failed");
let extracted_dir = temp_dir_path.clone();
assert!(extracted_dir.exists(), "Extracted directory does not exist after unarchiving with delete_dir=true");
assert!(!archive_path.exists(), "Archive file was not deleted after unarchiving with delete_dir=true");
let extracted_file_path = extracted_dir.join("test_file_delete.txt");
assert!(extracted_file_path.exists(), "Extracted file does not exist");
let mut contents = String::new();
let mut extracted_file = File::open(&extracted_file_path)
.expect("Failed to open extracted file after unarchiving with delete_dir=true");
extracted_file
.read_to_string(&mut contents)
.expect("Failed to read extracted file after unarchiving with delete_dir=true");
assert_eq!(
contents.trim(),
"This file will be archived and then the directory deleted.",
"Extracted file contents do not match expected value"
);
}
#[test]
fn test_archive_macro_without_delete() {
let temp_dir = tempdir().expect("Failed to create temp directory");
let temp_dir_path = temp_dir.path();
let temp_file_path = temp_dir_path.join("test_file.txt");
{
let mut temp_file = File::create(&temp_file_path).expect("Failed to create temp file");
writeln!(temp_file, "Test content").expect("Failed to write to temp file");
}
assert!(temp_file_path.exists());
archive!(temp_dir_path, false);
let archive_path = temp_dir_path.with_extension("tar.xz");
assert!(archive_path.exists());
assert!(temp_dir_path.exists());
fs::remove_file(archive_path).expect("Failed to remove archive file");
}
#[test]
fn test_extract_macro_with_delete() {
let temp_dir = tempdir().expect("Failed to create temp directory");
let temp_dir_path = temp_dir.path();
let temp_file_path = temp_dir_path.join("test_file.txt");
{
let mut temp_file = File::create(&temp_file_path).expect("Failed to create temp file");
writeln!(temp_file, "Test content").expect("Failed to write to temp file");
}
archive!(temp_dir_path, true);
let archive_path = temp_dir_path.with_extension("tar.xz");
assert!(archive_path.exists());
assert!(!temp_dir_path.exists(), "Source directory was not deleted after archiving");
extract!(archive_path, true);
assert!(temp_dir_path.exists(), "Extraction failed: Directory does not exist");
assert!(!archive_path.exists(), "Archive file was not deleted after extraction");
let extracted_file_path = temp_dir_path.join("test_file.txt");
assert!(extracted_file_path.exists());
let mut contents = String::new();
let mut extracted_file = File::open(&extracted_file_path).expect("Failed to open extracted file");
extracted_file
.read_to_string(&mut contents)
.expect("Failed to read extracted file");
assert_eq!(contents.trim(), "Test content");
}
#[test]
fn test_zip_creation() {
let temp_dir = tempdir().expect("Failed to create temp directory");
let temp_dir_path = temp_dir.path();
let test_file_path = temp_dir_path.join("test_file.txt");
{
let mut test_file = File::create(&test_file_path).expect("Failed to create test file");
writeln!(test_file, "This is a test file.").expect("Failed to write to test file");
}
assert!(test_file_path.exists(), "Test file does not exist");
let archive_path = temp_dir_path.join("test_archive.zip");
let mut manager = ZipManager::new(archive_path.to_str().unwrap());
manager.add_file(test_file_path.to_str().unwrap());
manager.create_zip(Compression::deflated()).expect("Failed to create ZIP archive");
assert!(archive_path.exists(), "ZIP archive was not created");
let file = File::open(&archive_path).expect("Failed to open ZIP archive");
let mut zip_archive = ZipArchive::new(file).expect("Failed to read ZIP archive");
println!("Files in ZIP archive:");
for i in 0..zip_archive.len() {
let file = zip_archive.by_index(i).expect("Failed to access file in ZIP archive");
println!("Found file in ZIP: {}", file.name());
}
let file_name_in_zip = "test_file.txt";
let mut extracted_file = zip_archive
.by_name(file_name_in_zip)
.unwrap();
let mut contents = String::new();
extracted_file.read_to_string(&mut contents).expect("Failed to read extracted file");
assert_eq!(contents.trim(), "This is a test file.", "Extracted file contents do not match");
fs::remove_file(&archive_path).expect("Failed to remove ZIP archive");
}
#[test]
fn test_zip_folder() {
let temp_dir = tempdir().expect("Failed to create temp directory");
let temp_dir_path = temp_dir.path();
let sub_dir_path = temp_dir_path.join("sub_dir");
let nested_sub_dir_path = sub_dir_path.join("nested");
fs::create_dir_all(&nested_sub_dir_path).expect("Failed to create nested subdirectory");
let test_file1_path = temp_dir_path.join("file1.txt");
let test_file2_path = sub_dir_path.join("file2.txt");
let test_file3_path = nested_sub_dir_path.join("file3.txt");
{
let mut test_file1 = File::create(&test_file1_path).expect("Failed to create test file1");
writeln!(test_file1, "Content of file1").expect("Failed to write to test file1");
let mut test_file2 = File::create(&test_file2_path).expect("Failed to create test file2");
writeln!(test_file2, "Content of file2").expect("Failed to write to test file2");
let mut test_file3 = File::create(&test_file3_path).expect("Failed to create test file3");
writeln!(test_file3, "Content of file3").expect("Failed to write to test file3");
}
assert!(test_file1_path.exists(), "Test file1 does not exist");
assert!(test_file2_path.exists(), "Test file2 does not exist");
assert!(test_file3_path.exists(), "Test file3 does not exist");
let archive_path = temp_dir_path.join("test_folder_archive.zip");
let mut manager = ZipManager::new(archive_path.to_str().unwrap());
manager.add_directory(temp_dir_path.to_str().unwrap());
manager.create_zip(Compression::Deflated).expect("Failed to create ZIP archive");
assert!(archive_path.exists(), "ZIP archive was not created");
let file = File::open(&archive_path).expect("Failed to open ZIP archive");
let mut zip_archive = ZipArchive::new(file).expect("Failed to read ZIP archive");
println!("Files in ZIP archive:");
let mut found_files = Vec::new();
for i in 0..zip_archive.len() {
let file = zip_archive.by_index(i).expect("Failed to access file in ZIP archive");
println!("Found file in ZIP: {}", file.name());
found_files.push(file.name().to_string());
}
let sub_dir_entry = found_files
.iter()
.find(|path| path.ends_with("sub_dir/"))
.expect("Failed to find sub_dir/ in ZIP archive");
let expected_file2_path = format!("{}/file2.txt", sub_dir_entry.trim_end_matches('/'));
let expected_file3_path = format!("{}/nested/file3.txt", sub_dir_entry.trim_end_matches('/'));
{
let mut extracted_file2 = zip_archive
.by_name(&expected_file2_path)
.expect("Failed to find sub_dir/file2.txt in ZIP archive");
let mut contents2 = String::new();
extracted_file2.read_to_string(&mut contents2).expect("Failed to read extracted file2");
assert_eq!(contents2.trim(), "Content of file2", "Extracted file2 content mismatch");
}
{
let mut extracted_file3 = zip_archive
.by_name(&expected_file3_path)
.expect("Failed to find sub_dir/nested/file3.txt in ZIP archive");
let mut contents3 = String::new();
extracted_file3.read_to_string(&mut contents3).expect("Failed to read extracted file3");
assert_eq!(contents3.trim(), "Content of file3", "Extracted file3 content mismatch");
}
fs::remove_file(&archive_path).expect("Failed to remove ZIP archive");
}