test: cleanup

This commit is contained in:
zhom
2025-08-10 04:14:43 +04:00
parent fbcec2cbc1
commit a9720676ae
13 changed files with 1526 additions and 178 deletions
+213 -53
View File
@@ -1444,93 +1444,211 @@ mod tests {
#[tokio::test]
async fn test_extract_zip_with_test_archive() {
let extractor = Extractor::instance();
let temp_dir = TempDir::new().unwrap();
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let dest_dir = temp_dir.path().join("extracted");
// Use the test ZIP archive
let zip_path = std::path::Path::new("test-assets/test.zip");
if !zip_path.exists() {
// Skip test if test archive doesn't exist
return;
// Create a test ZIP archive in memory
let zip_path = temp_dir.path().join("test.zip");
{
let file = std::fs::File::create(&zip_path).expect("Failed to create test zip file");
let mut zip = zip::ZipWriter::new(file);
let options =
zip::write::FileOptions::<()>::default().compression_method(zip::CompressionMethod::Stored);
zip
.start_file("test.txt", options)
.expect("Failed to start zip file");
zip
.write_all(b"Hello, World!")
.expect("Failed to write to zip");
zip.finish().expect("Failed to finish zip");
}
let _result = extractor.extract_zip(zip_path, &dest_dir).await;
let result = extractor.extract_zip(&zip_path, &dest_dir).await;
// The result might fail because we're looking for executables, but the extraction should work
// Let's just check if the file was extracted
// Let's check if the file was extracted regardless of the result
let extracted_file = dest_dir.join("test.txt");
if extracted_file.exists() {
let content = std::fs::read_to_string(&extracted_file).unwrap();
assert_eq!(content.trim(), "Hello, World!");
assert!(extracted_file.exists(), "Extracted file should exist");
let content = std::fs::read_to_string(&extracted_file).expect("Failed to read extracted file");
assert_eq!(
content.trim(),
"Hello, World!",
"Extracted content should match"
);
// If the result is an error, it should be because no executable was found, not extraction failure
if let Err(e) = result {
let error_msg = e.to_string();
assert!(
error_msg.contains("No executable found") || error_msg.contains("executable"),
"Error should be about missing executable, not extraction failure: {error_msg}"
);
}
}
#[tokio::test]
async fn test_extract_tar_gz_with_test_archive() {
let extractor = Extractor::instance();
let temp_dir = TempDir::new().unwrap();
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let dest_dir = temp_dir.path().join("extracted");
// Use the test tar.gz archive
let tar_gz_path = std::path::Path::new("test-assets/test.tar.gz");
if !tar_gz_path.exists() {
// Skip test if test archive doesn't exist
return;
// Create a test tar.gz archive in memory
let tar_gz_path = temp_dir.path().join("test.tar.gz");
{
let tar_gz_file =
std::fs::File::create(&tar_gz_path).expect("Failed to create test tar.gz file");
let enc = flate2::write::GzEncoder::new(tar_gz_file, flate2::Compression::default());
let mut tar = tar::Builder::new(enc);
let mut header = tar::Header::new_gnu();
header.set_path("test.txt").expect("Failed to set tar path");
header.set_size(13); // "Hello, World!" length
header.set_cksum();
tar
.append(&header, "Hello, World!".as_bytes())
.expect("Failed to append to tar");
tar.finish().expect("Failed to finish tar");
}
let _result = extractor.extract_tar_gz(tar_gz_path, &dest_dir).await;
let result = extractor.extract_tar_gz(&tar_gz_path, &dest_dir).await;
// Check if the file was extracted
let extracted_file = dest_dir.join("test.txt");
if extracted_file.exists() {
let content = std::fs::read_to_string(&extracted_file).unwrap();
assert_eq!(content.trim(), "Hello, World!");
assert!(extracted_file.exists(), "Extracted file should exist");
let content = std::fs::read_to_string(&extracted_file).expect("Failed to read extracted file");
assert_eq!(
content.trim(),
"Hello, World!",
"Extracted content should match"
);
// If the result is an error, it should be because no executable was found, not extraction failure
if let Err(e) = result {
let error_msg = e.to_string();
assert!(
error_msg.contains("No executable found")
|| error_msg.contains("executable")
|| error_msg.contains("No .app found")
|| error_msg.contains("app not found"),
"Error should be about missing executable/app, not extraction failure: {error_msg}"
);
}
}
#[tokio::test]
async fn test_extract_tar_bz2_with_test_archive() {
let extractor = Extractor::instance();
let temp_dir = TempDir::new().unwrap();
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let dest_dir = temp_dir.path().join("extracted");
// Use the test tar.bz2 archive
let tar_bz2_path = std::path::Path::new("test-assets/test.tar.bz2");
if !tar_bz2_path.exists() {
// Skip test if test archive doesn't exist
return;
// Create a test tar.bz2 archive in memory
let tar_bz2_path = temp_dir.path().join("test.tar.bz2");
{
let tar_bz2_file =
std::fs::File::create(&tar_bz2_path).expect("Failed to create test tar.bz2 file");
let enc = bzip2::write::BzEncoder::new(tar_bz2_file, bzip2::Compression::default());
let mut tar = tar::Builder::new(enc);
let mut header = tar::Header::new_gnu();
header.set_path("test.txt").expect("Failed to set tar path");
header.set_size(13); // "Hello, World!" length
header.set_cksum();
tar
.append(&header, "Hello, World!".as_bytes())
.expect("Failed to append to tar");
tar.finish().expect("Failed to finish tar");
}
let _result = extractor.extract_tar_bz2(tar_bz2_path, &dest_dir).await;
let result = extractor.extract_tar_bz2(&tar_bz2_path, &dest_dir).await;
// Check if the file was extracted
let extracted_file = dest_dir.join("test.txt");
if extracted_file.exists() {
let content = std::fs::read_to_string(&extracted_file).unwrap();
assert_eq!(content.trim(), "Hello, World!");
assert!(extracted_file.exists(), "Extracted file should exist");
let content = std::fs::read_to_string(&extracted_file).expect("Failed to read extracted file");
assert_eq!(
content.trim(),
"Hello, World!",
"Extracted content should match"
);
// If the result is an error, it should be because no executable was found, not extraction failure
if let Err(e) = result {
let error_msg = e.to_string();
assert!(
error_msg.contains("No executable found")
|| error_msg.contains("executable")
|| error_msg.contains("No .app found")
|| error_msg.contains("app not found"),
"Error should be about missing executable/app, not extraction failure: {error_msg}"
);
}
}
#[tokio::test]
async fn test_extract_tar_xz_with_test_archive() {
let extractor = Extractor::instance();
let temp_dir = TempDir::new().unwrap();
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let dest_dir = temp_dir.path().join("extracted");
// Use the test tar.xz archive
let tar_xz_path = std::path::Path::new("test-assets/test.tar.xz");
if !tar_xz_path.exists() {
// Skip test if test archive doesn't exist
return;
// Create a test tar.xz archive in memory
let tar_xz_path = temp_dir.path().join("test.tar.xz");
{
// First create a tar archive in memory
let mut tar_data = Vec::new();
{
let mut tar = tar::Builder::new(&mut tar_data);
let mut header = tar::Header::new_gnu();
header.set_path("test.txt").expect("Failed to set tar path");
header.set_size(13); // "Hello, World!" length
header.set_cksum();
tar
.append(&header, "Hello, World!".as_bytes())
.expect("Failed to append to tar");
tar.finish().expect("Failed to finish tar");
}
// Then compress with xz
let tar_xz_file =
std::fs::File::create(&tar_xz_path).expect("Failed to create test tar.xz file");
let mut compressed_data = Vec::new();
lzma_rs::xz_compress(&mut std::io::Cursor::new(tar_data), &mut compressed_data)
.expect("Failed to compress with xz");
std::io::Write::write_all(&mut std::io::BufWriter::new(tar_xz_file), &compressed_data)
.expect("Failed to write compressed data");
}
let _result = extractor.extract_tar_xz(tar_xz_path, &dest_dir).await;
let result = extractor.extract_tar_xz(&tar_xz_path, &dest_dir).await;
// Check if the file was extracted
let extracted_file = dest_dir.join("test.txt");
if extracted_file.exists() {
let content = std::fs::read_to_string(&extracted_file).unwrap();
assert_eq!(content.trim(), "Hello, World!");
assert!(extracted_file.exists(), "Extracted file should exist");
let content = std::fs::read_to_string(&extracted_file).expect("Failed to read extracted file");
assert_eq!(
content.trim(),
"Hello, World!",
"Extracted content should match"
);
// If the result is an error, it should be because no executable was found, not extraction failure
if let Err(e) = result {
let error_msg = e.to_string();
assert!(
error_msg.contains("No executable found")
|| error_msg.contains("executable")
|| error_msg.contains("No .app found")
|| error_msg.contains("app not found"),
"Error should be about missing executable/app, not extraction failure: {error_msg}"
);
}
}
@@ -1578,27 +1696,69 @@ mod tests {
assert!(found_app.exists());
}
#[cfg(target_os = "linux")]
#[test]
fn test_is_executable() {
#[allow(unused_variables)]
let extractor = Extractor::instance();
let temp_dir = TempDir::new().unwrap();
let temp_dir = TempDir::new().expect("Failed to create temp directory");
// Create a regular file
let regular_file = temp_dir.path().join("regular.txt");
File::create(&regular_file).unwrap();
File::create(&regular_file).expect("Failed to create test file");
// Should not be executable initially
assert!(!extractor.is_executable(&regular_file));
#[cfg(target_os = "linux")]
{
// Should not be executable initially
assert!(
!extractor.is_executable(&regular_file),
"File should not be executable initially"
);
// Make it executable
use std::os::unix::fs::PermissionsExt;
let mut permissions = regular_file.metadata().unwrap().permissions();
permissions.set_mode(0o755);
std::fs::set_permissions(&regular_file, permissions).unwrap();
// Make it executable
use std::os::unix::fs::PermissionsExt;
let mut permissions = regular_file
.metadata()
.expect("Failed to get file metadata")
.permissions();
permissions.set_mode(0o755);
std::fs::set_permissions(&regular_file, permissions).expect("Failed to set permissions");
// Should now be executable
assert!(extractor.is_executable(&regular_file));
// Should now be executable
assert!(
extractor.is_executable(&regular_file),
"File should be executable after setting permissions"
);
}
#[cfg(not(target_os = "linux"))]
{
// On non-Linux systems, the is_executable method is not available
// We'll just verify the file exists since executable permissions work differently on Windows/macOS
assert!(regular_file.exists(), "Test file should exist");
// On Unix systems (but not Linux), we can still test basic permission setting
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut permissions = regular_file
.metadata()
.expect("Failed to get file metadata")
.permissions();
permissions.set_mode(0o755);
std::fs::set_permissions(&regular_file, permissions).expect("Failed to set permissions");
// Verify the permissions were set
let new_permissions = regular_file
.metadata()
.expect("Failed to get updated metadata")
.permissions();
assert_eq!(
new_permissions.mode() & 0o777,
0o755,
"Permissions should be set to 755"
);
}
}
}
}