From 068eea55e0612184151461e64633b3dc18e53490 Mon Sep 17 00:00:00 2001 From: Namilskyy Date: Mon, 1 Dec 2025 14:06:10 +0300 Subject: Implemented test suite, fixed some issues and added more modular structure into .woodpecker.yaml --- tests/pkgtoolkit_funcs.rs | 193 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 tests/pkgtoolkit_funcs.rs (limited to 'tests/pkgtoolkit_funcs.rs') diff --git a/tests/pkgtoolkit_funcs.rs b/tests/pkgtoolkit_funcs.rs new file mode 100644 index 0000000..ae0c4d7 --- /dev/null +++ b/tests/pkgtoolkit_funcs.rs @@ -0,0 +1,193 @@ +use mesk::pkgtoolkit::pkgtools::{Package, Archs}; + + +mod shared; +use shared::create_test_config; + +use tempfile::TempDir; +use tokio; + + +// Pkg toolkit tests +#[cfg(test)] +mod package_tests { + use super::*; + use std::fs; + use std::io::Write; + + #[test] + fn test_archs_as_str() { + assert_eq!(Archs::X86_64.as_str(), "x86_64"); + assert_eq!(Archs::Aarch64.as_str(), "aarch64"); + assert_eq!(Archs::X86.as_str(), "x86"); + assert_eq!(Archs::ArmV7.as_str(), "armv7"); + assert_eq!(Archs::ArmV8.as_str(), "armv8"); + } + + #[tokio::test] + async fn test_extract_archive() -> Result<(), Box> { + let temp_dir = TempDir::new()?; + let _config = create_test_config(temp_dir.path().to_str().unwrap()); + + + let test_file_content = "test content"; + let test_file_path = temp_dir.path().join("test_file.txt"); + fs::write(&test_file_path, test_file_content)?; + + let archive_path = temp_dir.path().join("test_archive.tar.gz"); + let file = fs::File::create(&archive_path)?; + let gz_encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default()); + let mut tar_builder = tar::Builder::new(gz_encoder); + tar_builder.append_path_with_name(&test_file_path, "extracted_test_file.txt")?; + tar_builder.into_inner()?.finish()?; + + + Package::extract_archive(archive_path.to_str().unwrap())?; + + + let extracted_file_path = temp_dir.path().join("extracted_test_file.txt"); + assert!(extracted_file_path.exists()); + let content = fs::read_to_string(extracted_file_path)?; + assert_eq!(content, test_file_content); + + Ok(()) + } + + + #[tokio::test] + async fn test_package_check_valid() -> Result<(), Box> { + let temp_dir = TempDir::new()?; + let _config = create_test_config(temp_dir.path().to_str().unwrap()); + + + let install_content = r#" +[package] +name = "test-pkg" +version = "1.0.0" +arch = "X86_64" # Используйте архитектуру хоста или измените для теста архитектуры +descr = "A test package" +license = "MIT" +url = "/repo/test-pkg.mesk" + +[install] +path = "/tmp/test_binary" +user = "root" +group = "root" +mode = "755" +"#; + let install_path = temp_dir.path().join("INSTALL"); + let mut install_file = fs::File::create(&install_path)?; + install_file.write_all(install_content.as_bytes())?; + + + let archive_path = temp_dir.path().join("test_pkg_with_install.tar.gz"); + let file = fs::File::create(&archive_path)?; + let gz_encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default()); + let mut tar_builder = tar::Builder::new(gz_encoder); + tar_builder.append_path_with_name(&install_path, "INSTALL")?; + tar_builder.into_inner()?.finish()?; + + let result = Package::check(archive_path.to_str().unwrap().to_string()); + + assert!(result.is_ok()); + assert_eq!(result.unwrap(), true); + + Ok(()) + } + + + #[tokio::test] + async fn test_package_check_missing_install() -> Result<(), Box> { + let temp_dir = TempDir::new()?; + + let archive_path = temp_dir.path().join("test_pkg_without_install.tar.gz"); + let file = fs::File::create(&archive_path)?; + let gz_encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default()); + let mut tar_builder = tar::Builder::new(gz_encoder); + let dummy_path = temp_dir.path().join("dummy.txt"); + fs::write(&dummy_path, "dummy")?; + tar_builder.append_path_with_name(&dummy_path, "dummy.txt")?; + tar_builder.into_inner()?.finish()?; + + let result = Package::check(archive_path.to_str().unwrap().to_string()); + + assert!(result.is_err()); + let err = result.unwrap_err(); + assert_eq!(err.kind(), std::io::ErrorKind::NotFound); + assert!(err.to_string().contains("INSTALL file not found")); + + Ok(()) + } + + + #[tokio::test] + async fn test_package_check_empty_install() -> Result<(), Box> { + let temp_dir = TempDir::new()?; + + let install_path = temp_dir.path().join("INSTALL"); + fs::write(&install_path, "")?; + let archive_path = temp_dir.path().join("test_pkg_with_empty_install.tar.gz"); + let file = fs::File::create(&archive_path)?; + let gz_encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default()); + let mut tar_builder = tar::Builder::new(gz_encoder); + tar_builder.append_path_with_name(&install_path, "INSTALL")?; + tar_builder.into_inner()?.finish()?; + + + let result = Package::check(archive_path.to_str().unwrap().to_string()); + + assert!(result.is_err()); + let err = result.unwrap_err(); + assert_eq!(err.kind(), std::io::ErrorKind::InvalidData); + assert!(err.to_string().contains("INSTALL file is empty")); + + Ok(()) + } + + + #[tokio::test] + async fn test_package_check_arch_mismatch() -> Result<(), Box> { + let temp_dir = TempDir::new()?; + let _config = create_test_config(temp_dir.path().to_str().unwrap()); + + let host_arch = std::env::consts::ARCH; + let wrong_arch = if host_arch == "x86" { "x86_64" } else { "x86" }; + let install_content = format!(r#" +[package] +name = "test-pkg" +version = "1.0.0" +arch = "{}" +descr = "A test package for arch mismatch" +license = "MIT" +url = "/repo/test-pkg.mesk" + +[install] +path = "/tmp/test_binary" +user = "root" +group = "root" +mode = "755" +"#, wrong_arch); + + let install_path = temp_dir.path().join("INSTALL"); + let mut install_file = fs::File::create(&install_path)?; + install_file.write_all(install_content.as_bytes())?; + + let archive_path = temp_dir.path().join("test_pkg_with_wrong_arch.tar.gz"); + let file = fs::File::create(&archive_path)?; + let gz_encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default()); + let mut tar_builder = tar::Builder::new(gz_encoder); + tar_builder.append_path_with_name(&install_path, "INSTALL")?; + tar_builder.into_inner()?.finish()?; + + let result = Package::check(archive_path.to_str().unwrap().to_string()); + + + assert!(result.is_err()); + let err = result.unwrap_err(); + assert_eq!(err.kind(), std::io::ErrorKind::InvalidData); + assert!(err.to_string().contains("Arch mismatch")); + + Ok(()) + } + +} \ No newline at end of file -- cgit v1.2.3