diff --git a/src/env/mod.rs b/src/env/mod.rs index 610b198..c200d9d 100644 --- a/src/env/mod.rs +++ b/src/env/mod.rs @@ -1 +1,6 @@ -pub mod redisclient; \ No newline at end of file +mod redisclient; + +pub use redisclient::*; + +#[cfg(test)] +mod tests; \ No newline at end of file diff --git a/src/env/tests.rs b/src/env/tests.rs new file mode 100644 index 0000000..8d417a5 --- /dev/null +++ b/src/env/tests.rs @@ -0,0 +1,126 @@ +use super::*; +use std::env; +use redis::RedisResult; + +#[cfg(test)] +mod redis_client_tests { + use super::*; + + #[test] + fn test_env_vars() { + // Save original REDISDB value to restore later + let original_redisdb = env::var("REDISDB").ok(); + + // Set test environment variables + env::set_var("REDISDB", "5"); + + // Test with invalid value + env::set_var("REDISDB", "invalid"); + + // Test with unset value + env::remove_var("REDISDB"); + + // Restore original REDISDB value + if let Some(redisdb) = original_redisdb { + env::set_var("REDISDB", redisdb); + } else { + env::remove_var("REDISDB"); + } + } + + #[test] + fn test_redis_client_creation_mock() { + // This is a simplified test that doesn't require an actual Redis server + // It just verifies that the function handles environment variables correctly + + // Save original HOME value to restore later + let original_home = env::var("HOME").ok(); + + // Set HOME to a test value + env::set_var("HOME", "/tmp"); + + // The actual client creation would be tested in integration tests + // with a real Redis server or a mock + + // Restore original HOME value + if let Some(home) = original_home { + env::set_var("HOME", home); + } else { + env::remove_var("HOME"); + } + } + + #[test] + fn test_reset_mock() { + // This is a simplified test that doesn't require an actual Redis server + // In a real test, we would need to mock the Redis client + + // Just verify that the reset function doesn't panic + // This is a minimal test - in a real scenario, we would use mocking + // to verify that the client is properly reset + if let Err(_) = reset() { + // If Redis is not available, this is expected to fail + // So we don't assert anything here + } + } +} + +// Integration tests that require a real Redis server +// These tests will be skipped if Redis is not available +#[cfg(test)] +mod redis_integration_tests { + use super::*; + + // Helper function to check if Redis is available + fn is_redis_available() -> bool { + match get_redis_client() { + Ok(_) => true, + Err(_) => false, + } + } + + #[test] + fn test_redis_client_integration() { + if !is_redis_available() { + println!("Skipping Redis integration tests - Redis server not available"); + return; + } + + println!("Running Redis integration tests..."); + + // Test basic operations + test_basic_redis_operations(); + } + + fn test_basic_redis_operations() { + if !is_redis_available() { + return; + } + + // Test setting and getting values + let client_result = get_redis_client(); + + if client_result.is_err() { + // Skip the test if we can't connect to Redis + return; + } + + // Create SET command + let mut set_cmd = redis::cmd("SET"); + set_cmd.arg("test_key").arg("test_value"); + + // Execute SET command + let set_result: RedisResult<()> = execute(&mut set_cmd); + assert!(set_result.is_ok()); + + // Create GET command + let mut get_cmd = redis::cmd("GET"); + get_cmd.arg("test_key"); + + // Execute GET command and check the result + if let Ok(value) = execute::(&mut get_cmd) { + assert_eq!(value, "test_value"); + } + let _: RedisResult<()> = execute(&mut redis::cmd("DEL").arg("test_key")); + } +} \ No newline at end of file diff --git a/src/git/git_executor.rs b/src/git/git_executor.rs index adbfc8a..d3a2132 100644 --- a/src/git/git_executor.rs +++ b/src/git/git_executor.rs @@ -5,7 +5,7 @@ use std::collections::HashMap; use redis::Cmd; use serde::{Deserialize, Serialize}; -use crate::env::redisclient; +use crate::env; use crate::git::git::parse_git_url; // Define a custom error type for GitExecutor operations @@ -127,7 +127,7 @@ impl GitExecutor { cmd.arg("GET").arg("herocontext:git"); // Execute the command - let result: redis::RedisResult = redisclient::execute(&mut cmd); + let result: redis::RedisResult = env::execute(&mut cmd); match result { Ok(json_str) => { diff --git a/src/process/mgmt.rs b/src/process/mgmt.rs index 82643c3..3daabcf 100644 --- a/src/process/mgmt.rs +++ b/src/process/mgmt.rs @@ -1,5 +1,4 @@ use std::process::Command; -use std::collections::HashMap; use std::fmt; use std::error::Error; use std::io; diff --git a/src/process/run.rs b/src/process/run.rs index 3743683..f7e3cc8 100644 --- a/src/process/run.rs +++ b/src/process/run.rs @@ -2,7 +2,6 @@ use std::io::{BufRead, BufReader, Write}; use std::fs::{self, File}; use std::path::{Path, PathBuf}; use std::process::{Child, Command, Output, Stdio}; -use std::collections::HashMap; use std::fmt; use std::error::Error; use std::io;