diff options
| author | Namilskyy <alive6863@gmail.com> | 2025-12-01 14:17:47 +0300 |
|---|---|---|
| committer | Namilskyy <alive6863@gmail.com> | 2025-12-01 14:17:47 +0300 |
| commit | b157e34176858766738be7e6903cc188285a5aeb (patch) | |
| tree | 5a58fbb2eda53cb4c3fd131f2457fb9639285ad6 /tests | |
| parent | 3fc368fbd6a818a8f9f210c995d82725d11e6e45 (diff) | |
Formatted code && fix clippy warn
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/http_funcs.rs | 110 | ||||
| -rw-r--r-- | tests/i2p_functions.rs | 16 | ||||
| -rw-r--r-- | tests/pkgtoolkit_funcs.rs | 31 | ||||
| -rw-r--r-- | tests/secondary_funcs.rs | 6 | ||||
| -rw-r--r-- | tests/shared.rs | 40 |
5 files changed, 117 insertions, 86 deletions
diff --git a/tests/http_funcs.rs b/tests/http_funcs.rs index efbaa53..232d3b8 100644 --- a/tests/http_funcs.rs +++ b/tests/http_funcs.rs @@ -1,28 +1,26 @@ -use mesk::pkgtoolkit::pkgtools::{Package, Archs}; -use mesk::net::http_package::HTTPPackage; +use mesk::net::http_package::HTTPPackage; +use mesk::pkgtoolkit::pkgtools::{Archs, Package}; use std::fs; mod shared; use shared::create_test_config; use std::collections::HashMap; -use tempfile::TempDir; +use tempfile::TempDir; use tokio; // Add these imports for logging -use log::{info, debug, error}; +use log::{debug, error, info}; #[cfg(test)] mod http_package_tests { use super::*; - + // Helper function to initialize the logger for each test. // This uses `call_once` to ensure it's only initialized once, // even if called multiple times, preventing panics. fn init_logger() { - let _ = env_logger::builder() - .is_test(true) - .try_init(); + let _ = env_logger::builder().is_test(true).try_init(); } #[tokio::test] @@ -57,7 +55,7 @@ license = "MIT" let _mock = server .mock("GET", "/INDEX.tar.gz") .with_status(200) - .with_header("content-length", &mock_body.len().to_string()) + .with_header("content-length", &mock_body.len().to_string()) .with_body(mock_body) .create_async() .await; @@ -66,7 +64,7 @@ license = "MIT" let config = create_test_config(temp_config_dir.path().to_str().unwrap()); let mut config_with_mock_url = config.clone(); - config_with_mock_url.repo.repo_url = server.url(); + config_with_mock_url.repo.repo_url = server.url(); let mut http_pkg = HTTPPackage::new(config_with_mock_url); @@ -75,25 +73,41 @@ license = "MIT" info!("Checking if fetch_index_http result is OK"); assert!(result.is_ok(), "fetch_index_http should return Ok"); - + info!("Checking if fetch_index_http returned true"); - assert_eq!(result.unwrap(), true, "fetch_index_http should indicate success"); - + assert_eq!( + result.unwrap(), + true, + "fetch_index_http should indicate success" + ); info!("Checking if index_packages is populated"); - assert!(http_pkg.index_packages.is_some(), "index_packages should not be None after successful fetch"); - + assert!( + http_pkg.index_packages.is_some(), + "index_packages should not be None after successful fetch" + ); + let packages = http_pkg.index_packages.as_ref().unwrap(); info!("Checking number of packages in the index"); - assert_eq!(packages.len(), 1, "There should be exactly one package in the index"); - + assert_eq!( + packages.len(), + 1, + "There should be exactly one package in the index" + ); + info!("Checking if 'test-pkg' exists in the index"); - assert!(packages.contains_key("test-pkg"), "The package 'test-pkg' should be present in the index"); - + assert!( + packages.contains_key("test-pkg"), + "The package 'test-pkg' should be present in the index" + ); + let pkg_info = packages.get("test-pkg").unwrap(); info!("Checking if package URL is correctly formed"); - assert!(pkg_info.url.starts_with(&server.url()), "Package URL should be prefixed with the server URL"); + assert!( + pkg_info.url.starts_with(&server.url()), + "Package URL should be prefixed with the server URL" + ); Ok(()) } @@ -121,11 +135,18 @@ license = "MIT" let result = http_pkg.fetch_index_http().await; info!("Checking if fetch_index_http resulted in an error as expected"); - assert!(result.is_err(), "fetch_index_http should return an Err for a 404 response"); - + assert!( + result.is_err(), + "fetch_index_http should return an Err for a 404 response" + ); + let err_msg = result.unwrap_err().to_string(); info!("Verifying error message contains 'HTTP Error: 404'"); - assert!(err_msg.contains("HTTP Error: 404"), "Error message should contain 'HTTP Error: 404', got: {}", err_msg); + assert!( + err_msg.contains("HTTP Error: 404"), + "Error message should contain 'HTTP Error: 404', got: {}", + err_msg + ); Ok(()) } @@ -155,10 +176,17 @@ license = "MIT" let pkg_info = http_pkg.fetch_package_info("test-pkg"); info!("Checking if fetch_package_info returned successfully"); - assert!(pkg_info.is_ok(), "fetch_package_info should return Ok for an existing package"); - + assert!( + pkg_info.is_ok(), + "fetch_package_info should return Ok for an existing package" + ); + info!("Checking the retrieved package name"); - assert_eq!(pkg_info.unwrap().name, "test-pkg", "Retrieved package name should be 'test-pkg'"); + assert_eq!( + pkg_info.unwrap().name, + "test-pkg", + "Retrieved package name should be 'test-pkg'" + ); Ok(()) } @@ -171,16 +199,23 @@ license = "MIT" let temp_config_dir = TempDir::new()?; let config = create_test_config(temp_config_dir.path().to_str().unwrap()); - let http_pkg = HTTPPackage::new(config.clone()); + let http_pkg = HTTPPackage::new(config.clone()); debug!("Attempting to fetch package info before index load"); let result = http_pkg.fetch_package_info("nonexistent-pkg"); info!("Checking if fetch_package_info failed due to unloaded index"); - assert!(result.is_err(), "fetch_package_info should fail when index is not loaded"); - + assert!( + result.is_err(), + "fetch_package_info should fail when index is not loaded" + ); + let err_msg = result.unwrap_err().to_string(); info!("Verifying error message contains 'Index not loaded'"); - assert!(err_msg.contains("Index not loaded"), "Error message should contain 'Index not loaded', got: {}", err_msg); + assert!( + err_msg.contains("Index not loaded"), + "Error message should contain 'Index not loaded', got: {}", + err_msg + ); let mut http_pkg_empty_index = HTTPPackage::new(config); http_pkg_empty_index.index_packages = Some(HashMap::new()); @@ -188,12 +223,19 @@ license = "MIT" let result_empty = http_pkg_empty_index.fetch_package_info("nonexistent-pkg"); info!("Checking if fetch_package_info failed due to missing package in index"); - assert!(result_empty.is_err(), "fetch_package_info should fail for a package not in the index"); - + assert!( + result_empty.is_err(), + "fetch_package_info should fail for a package not in the index" + ); + let err_msg_empty = result_empty.unwrap_err().to_string(); info!("Verifying error message contains 'not found in index'"); - assert!(err_msg_empty.contains("not found in index"), "Error message should contain 'not found in index', got: {}", err_msg_empty); + assert!( + err_msg_empty.contains("not found in index"), + "Error message should contain 'not found in index', got: {}", + err_msg_empty + ); Ok(()) } -}
\ No newline at end of file +} diff --git a/tests/i2p_functions.rs b/tests/i2p_functions.rs index 6b24d14..3cbc1ac 100644 --- a/tests/i2p_functions.rs +++ b/tests/i2p_functions.rs @@ -1,11 +1,11 @@ -use mesk::pkgtoolkit::pkgtools::{Package, Archs}; -use mesk::net::i2p_package::I2PPackage; +use mesk::net::i2p_package::I2PPackage; +use mesk::pkgtoolkit::pkgtools::{Archs, Package}; mod shared; use shared::create_test_config; use std::collections::HashMap; -use tempfile::TempDir; +use tempfile::TempDir; use tokio; #[cfg(test)] @@ -14,7 +14,7 @@ mod i2p_package_tests { // fn parse_http_response(response_bytes: &[u8]) -> Result<(u16, &[u8]), Box<dyn std::error::Error>> { ... } // #[test] - // fn test_parse_http_response() { ... } + // fn test_parse_http_response() { ... } /* #[tokio::test] async fn test_i2p_fetch_index_success() -> Result<(), Box<dyn std::error::Error>> { @@ -26,18 +26,18 @@ mod i2p_package_tests { let mut mock_session = MockSession::new(); mock_session .expect_connect() - .with(eq("dummy.i2p")) + .with(eq("dummy.i2p")) .returning(|_| { // Возвращаем мок-поток, который возвращает байты архива let index_toml_content = r#"[[packages]] name = "test-pkg" ... "#; let mut archive_data = Vec::new(); // ... заполняем archive_data как в test_http_fetch_index_success ... let response_body = format!( - "HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n{}", + "HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n{}", archive_data.len(), std::str::from_utf8(&archive_data).unwrap() ).into_bytes(); - Ok(MockStream::new(response_body)) + Ok(MockStream::new(response_body)) }); @@ -67,7 +67,7 @@ mod i2p_package_tests { }; packages_map.insert("test-pkg".to_string(), pkg); i2p_pkg.index_packages = Some(packages_map); - + let pkg_info = i2p_pkg.fetch_package_info("test-pkg"); assert!(pkg_info.is_ok()); diff --git a/tests/pkgtoolkit_funcs.rs b/tests/pkgtoolkit_funcs.rs index ae0c4d7..667592d 100644 --- a/tests/pkgtoolkit_funcs.rs +++ b/tests/pkgtoolkit_funcs.rs @@ -1,13 +1,11 @@ -use mesk::pkgtoolkit::pkgtools::{Package, Archs}; - +use mesk::pkgtoolkit::pkgtools::{Archs, Package}; mod shared; use shared::create_test_config; -use tempfile::TempDir; +use tempfile::TempDir; use tokio; - // Pkg toolkit tests #[cfg(test)] mod package_tests { @@ -29,7 +27,6 @@ mod package_tests { 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)?; @@ -41,10 +38,8 @@ mod package_tests { 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)?; @@ -53,13 +48,11 @@ mod package_tests { Ok(()) } - #[tokio::test] async fn test_package_check_valid() -> Result<(), Box<dyn std::error::Error>> { let temp_dir = TempDir::new()?; let _config = create_test_config(temp_dir.path().to_str().unwrap()); - let install_content = r#" [package] name = "test-pkg" @@ -79,7 +72,6 @@ mode = "755" 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()); @@ -95,7 +87,6 @@ mode = "755" Ok(()) } - #[tokio::test] async fn test_package_check_missing_install() -> Result<(), Box<dyn std::error::Error>> { let temp_dir = TempDir::new()?; @@ -119,13 +110,12 @@ mode = "755" Ok(()) } - #[tokio::test] async fn test_package_check_empty_install() -> Result<(), Box<dyn std::error::Error>> { let temp_dir = TempDir::new()?; let install_path = temp_dir.path().join("INSTALL"); - fs::write(&install_path, "")?; + 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()); @@ -133,7 +123,6 @@ mode = "755" 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()); @@ -144,15 +133,15 @@ mode = "755" Ok(()) } - #[tokio::test] async fn test_package_check_arch_mismatch() -> Result<(), Box<dyn std::error::Error>> { 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#" + let wrong_arch = if host_arch == "x86" { "x86_64" } else { "x86" }; + let install_content = format!( + r#" [package] name = "test-pkg" version = "1.0.0" @@ -166,7 +155,9 @@ path = "/tmp/test_binary" user = "root" group = "root" mode = "755" -"#, wrong_arch); +"#, + wrong_arch + ); let install_path = temp_dir.path().join("INSTALL"); let mut install_file = fs::File::create(&install_path)?; @@ -181,7 +172,6 @@ mode = "755" 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); @@ -189,5 +179,4 @@ mode = "755" Ok(()) } - -}
\ No newline at end of file +} diff --git a/tests/secondary_funcs.rs b/tests/secondary_funcs.rs index 84034d8..566af1a 100644 --- a/tests/secondary_funcs.rs +++ b/tests/secondary_funcs.rs @@ -1,7 +1,7 @@ // use mesk::cfg::config::Config; // use toml; -/* +/* fn create_test_config(temp_dir_path: &str) -> Config { /* #[derive(Deserialize, Debug, Clone)] @@ -19,11 +19,11 @@ fn create_test_config(temp_dir_path: &str) -> Config { #[derive(Deserialize, Debug, Clone)] pub struct PathConfig { pub cache_dir: String, - ... + ... } */ let cfg = Config::default().unwrap(); assert!(toml::to_string(&cfg).is_ok()); cfg } -*/
\ No newline at end of file +*/ diff --git a/tests/shared.rs b/tests/shared.rs index 2ea0bf5..bdbc81a 100644 --- a/tests/shared.rs +++ b/tests/shared.rs @@ -1,22 +1,22 @@ -use mesk::cfg::config::{Config, Repo, Log, Loglevel, Paths}; +use mesk::cfg::config::{Config, Log, Loglevel, Paths, Repo}; pub fn create_test_config(temp_dir_path: &str) -> Config { - let cfg: Config = Config { - repo: Repo { - repo_url: format!(r"http://mesk.anthrill.i2p/repo/{}", std::env::consts::ARCH), - auto_update: true, - destination: (String::from("mesk"), String::from("mesk")), - repo_http_url: None, - }, - log: Log { - log_file: String::from("/var/log/mesk.log"), - log_level: Loglevel::Info, - }, - paths: Paths { - cache_dir: String::from(temp_dir_path), - build_dir: String::from(format!("{}/build", String::from(temp_dir_path))) - }, - }; - - cfg -}
\ No newline at end of file + let cfg: Config = Config { + repo: Repo { + repo_url: format!(r"http://mesk.anthrill.i2p/repo/{}", std::env::consts::ARCH), + auto_update: true, + destination: (String::from("mesk"), String::from("mesk")), + repo_http_url: None, + }, + log: Log { + log_file: String::from("/var/log/mesk.log"), + log_level: Loglevel::Info, + }, + paths: Paths { + cache_dir: String::from(temp_dir_path), + build_dir: String::from(format!("{}/build", String::from(temp_dir_path))), + }, + }; + + cfg +} |
