Simplify test environment setup to return TempDir directly

- Remove TestEnvironment struct and return TempDir from setup function
- Update tests to compute paths from temp_dir.path() on-demand
- Eliminate unused field warnings and reduce code complexity
- Maintain all test functionality with cleaner design
This commit is contained in:
Jeena 2026-01-17 00:06:27 +00:00
parent 01bcda10d0
commit bde6181820
3 changed files with 31 additions and 46 deletions

View file

@ -1,20 +1,8 @@
use std::path::{Path, PathBuf}; use std::path::Path;
use tempfile::TempDir; use tempfile::TempDir;
pub struct TestEnvironment { pub fn setup_test_environment() -> TempDir {
pub temp_dir: TempDir,
pub config_path: PathBuf,
pub cert_path: PathBuf,
pub key_path: PathBuf,
pub content_path: PathBuf,
pub port: u16,
}
pub fn setup_test_environment() -> TestEnvironment {
let temp_dir = TempDir::new().unwrap(); let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("config.toml");
let cert_path = temp_dir.path().join("cert.pem");
let key_path = temp_dir.path().join("key.pem");
let content_path = temp_dir.path().join("content"); let content_path = temp_dir.path().join("content");
// Create content directory and file // Create content directory and file
@ -24,17 +12,7 @@ pub fn setup_test_environment() -> TestEnvironment {
// Generate test certificates // Generate test certificates
generate_test_certificates(temp_dir.path()); generate_test_certificates(temp_dir.path());
// Use a unique port based on process ID to avoid conflicts temp_dir
let port = 1967 + (std::process::id() % 1000) as u16;
TestEnvironment {
temp_dir,
config_path,
cert_path,
key_path,
content_path,
port,
}
} }
fn generate_test_certificates(temp_dir: &Path) { fn generate_test_certificates(temp_dir: &Path) {

View file

@ -18,18 +18,19 @@ fn test_missing_config_file() {
#[test] #[test]
fn test_missing_hostname() { fn test_missing_hostname() {
let env = common::setup_test_environment(); let temp_dir = common::setup_test_environment();
let config_path = temp_dir.path().join("config.toml");
let config_content = format!(r#" let config_content = format!(r#"
root = "{}" root = "{}"
cert = "{}" cert = "{}"
key = "{}" key = "{}"
bind_host = "127.0.0.1" bind_host = "127.0.0.1"
"#, env.content_path.display(), env.cert_path.display(), env.key_path.display()); "#, temp_dir.path().join("content").display(), temp_dir.path().join("cert.pem").display(), temp_dir.path().join("key.pem").display());
std::fs::write(&env.config_path, config_content).unwrap(); std::fs::write(&config_path, config_content).unwrap();
let output = Command::new(env!("CARGO_BIN_EXE_pollux")) let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config") .arg("--config")
.arg(&env.config_path) .arg(&config_path)
.output() .output()
.unwrap(); .unwrap();
@ -41,19 +42,20 @@ fn test_missing_hostname() {
#[test] #[test]
fn test_nonexistent_root_directory() { fn test_nonexistent_root_directory() {
let env = common::setup_test_environment(); let temp_dir = common::setup_test_environment();
let config_path = temp_dir.path().join("config.toml");
let config_content = format!(r#" let config_content = format!(r#"
root = "/definitely/does/not/exist" root = "/definitely/does/not/exist"
cert = "{}" cert = "{}"
key = "{}" key = "{}"
hostname = "example.com" hostname = "example.com"
bind_host = "127.0.0.1" bind_host = "127.0.0.1"
"#, env.cert_path.display(), env.key_path.display()); "#, temp_dir.path().join("cert.pem").display(), temp_dir.path().join("key.pem").display());
std::fs::write(&env.config_path, config_content).unwrap(); std::fs::write(&config_path, config_content).unwrap();
let output = Command::new(env!("CARGO_BIN_EXE_pollux")) let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config") .arg("--config")
.arg(&env.config_path) .arg(&config_path)
.output() .output()
.unwrap(); .unwrap();
@ -65,19 +67,20 @@ fn test_nonexistent_root_directory() {
#[test] #[test]
fn test_missing_certificate_file() { fn test_missing_certificate_file() {
let env = common::setup_test_environment(); let temp_dir = common::setup_test_environment();
let config_path = temp_dir.path().join("config.toml");
let config_content = format!(r#" let config_content = format!(r#"
root = "{}" root = "{}"
cert = "/nonexistent/cert.pem" cert = "/nonexistent/cert.pem"
key = "{}" key = "{}"
hostname = "example.com" hostname = "example.com"
bind_host = "127.0.0.1" bind_host = "127.0.0.1"
"#, env.content_path.display(), env.key_path.display()); "#, temp_dir.path().join("content").display(), temp_dir.path().join("key.pem").display());
std::fs::write(&env.config_path, config_content).unwrap(); std::fs::write(&config_path, config_content).unwrap();
let output = Command::new(env!("CARGO_BIN_EXE_pollux")) let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config") .arg("--config")
.arg(&env.config_path) .arg(&config_path)
.output() .output()
.unwrap(); .unwrap();
@ -89,7 +92,9 @@ fn test_missing_certificate_file() {
#[test] #[test]
fn test_valid_config_startup() { fn test_valid_config_startup() {
let env = common::setup_test_environment(); let temp_dir = common::setup_test_environment();
let port = 1967 + (std::process::id() % 1000) as u16;
let config_path = temp_dir.path().join("config.toml");
let config_content = format!(r#" let config_content = format!(r#"
root = "{}" root = "{}"
cert = "{}" cert = "{}"
@ -97,12 +102,12 @@ fn test_valid_config_startup() {
hostname = "localhost" hostname = "localhost"
bind_host = "127.0.0.1" bind_host = "127.0.0.1"
port = {} port = {}
"#, env.content_path.display(), env.cert_path.display(), env.key_path.display(), env.port); "#, temp_dir.path().join("content").display(), temp_dir.path().join("cert.pem").display(), temp_dir.path().join("key.pem").display(), port);
std::fs::write(&env.config_path, config_content).unwrap(); std::fs::write(&config_path, config_content).unwrap();
let mut server_process = Command::new(env!("CARGO_BIN_EXE_pollux")) let mut server_process = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config") .arg("--config")
.arg(&env.config_path) .arg(&config_path)
.spawn() .spawn()
.unwrap(); .unwrap();

View file

@ -2,9 +2,11 @@ mod common;
#[test] #[test]
fn test_rate_limiting_with_concurrent_requests() { fn test_rate_limiting_with_concurrent_requests() {
let env = common::setup_test_environment(); let temp_dir = common::setup_test_environment();
let port = 1967 + (std::process::id() % 1000) as u16;
// Create config with rate limiting enabled // Create config with rate limiting enabled
let config_path = temp_dir.path().join("config.toml");
let config_content = format!(r#" let config_content = format!(r#"
root = "{}" root = "{}"
cert = "{}" cert = "{}"
@ -13,13 +15,13 @@ fn test_rate_limiting_with_concurrent_requests() {
bind_host = "127.0.0.1" bind_host = "127.0.0.1"
port = {} port = {}
max_concurrent_requests = 1 max_concurrent_requests = 1
"#, env.content_path.display(), env.cert_path.display(), env.key_path.display(), env.port); "#, temp_dir.path().join("content").display(), temp_dir.path().join("cert.pem").display(), temp_dir.path().join("key.pem").display(), port);
std::fs::write(&env.config_path, config_content).unwrap(); std::fs::write(&config_path, config_content).unwrap();
// Start server binary with test delay to simulate processing time // Start server binary with test delay to simulate processing time
let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux")) let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config") .arg("--config")
.arg(&env.config_path) .arg(&config_path)
.arg("--test-processing-delay") .arg("--test-processing-delay")
.arg("1") // 1 second delay per request .arg("1") // 1 second delay per request
.spawn() .spawn()
@ -31,7 +33,7 @@ fn test_rate_limiting_with_concurrent_requests() {
// Spawn 5 concurrent client processes // Spawn 5 concurrent client processes
let mut handles = vec![]; let mut handles = vec![];
for _ in 0..5 { for _ in 0..5 {
let url = format!("gemini://localhost:{}/test.gmi", env.port); let url = format!("gemini://localhost:{}/test.gmi", port);
let handle = std::thread::spawn(move || { let handle = std::thread::spawn(move || {
std::process::Command::new("python3") std::process::Command::new("python3")
.arg("tests/gemini_test_client.py") .arg("tests/gemini_test_client.py")