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:
parent
01bcda10d0
commit
bde6181820
3 changed files with 31 additions and 46 deletions
|
|
@ -1,20 +1,8 @@
|
|||
use std::path::{Path, PathBuf};
|
||||
use std::path::Path;
|
||||
use tempfile::TempDir;
|
||||
|
||||
pub struct TestEnvironment {
|
||||
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 {
|
||||
pub fn setup_test_environment() -> TempDir {
|
||||
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");
|
||||
|
||||
// Create content directory and file
|
||||
|
|
@ -24,17 +12,7 @@ pub fn setup_test_environment() -> TestEnvironment {
|
|||
// Generate test certificates
|
||||
generate_test_certificates(temp_dir.path());
|
||||
|
||||
// Use a unique port based on process ID to avoid conflicts
|
||||
let port = 1967 + (std::process::id() % 1000) as u16;
|
||||
|
||||
TestEnvironment {
|
||||
temp_dir,
|
||||
config_path,
|
||||
cert_path,
|
||||
key_path,
|
||||
content_path,
|
||||
port,
|
||||
}
|
||||
temp_dir
|
||||
}
|
||||
|
||||
fn generate_test_certificates(temp_dir: &Path) {
|
||||
|
|
|
|||
|
|
@ -18,18 +18,19 @@ fn test_missing_config_file() {
|
|||
|
||||
#[test]
|
||||
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#"
|
||||
root = "{}"
|
||||
cert = "{}"
|
||||
key = "{}"
|
||||
bind_host = "127.0.0.1"
|
||||
"#, env.content_path.display(), env.cert_path.display(), env.key_path.display());
|
||||
std::fs::write(&env.config_path, config_content).unwrap();
|
||||
"#, temp_dir.path().join("content").display(), temp_dir.path().join("cert.pem").display(), temp_dir.path().join("key.pem").display());
|
||||
std::fs::write(&config_path, config_content).unwrap();
|
||||
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
|
||||
.arg("--config")
|
||||
.arg(&env.config_path)
|
||||
.arg(&config_path)
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
|
|
@ -41,19 +42,20 @@ fn test_missing_hostname() {
|
|||
|
||||
#[test]
|
||||
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#"
|
||||
root = "/definitely/does/not/exist"
|
||||
cert = "{}"
|
||||
key = "{}"
|
||||
hostname = "example.com"
|
||||
bind_host = "127.0.0.1"
|
||||
"#, env.cert_path.display(), env.key_path.display());
|
||||
std::fs::write(&env.config_path, config_content).unwrap();
|
||||
"#, temp_dir.path().join("cert.pem").display(), temp_dir.path().join("key.pem").display());
|
||||
std::fs::write(&config_path, config_content).unwrap();
|
||||
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
|
||||
.arg("--config")
|
||||
.arg(&env.config_path)
|
||||
.arg(&config_path)
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
|
|
@ -65,19 +67,20 @@ fn test_nonexistent_root_directory() {
|
|||
|
||||
#[test]
|
||||
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#"
|
||||
root = "{}"
|
||||
cert = "/nonexistent/cert.pem"
|
||||
key = "{}"
|
||||
hostname = "example.com"
|
||||
bind_host = "127.0.0.1"
|
||||
"#, env.content_path.display(), env.key_path.display());
|
||||
std::fs::write(&env.config_path, config_content).unwrap();
|
||||
"#, temp_dir.path().join("content").display(), temp_dir.path().join("key.pem").display());
|
||||
std::fs::write(&config_path, config_content).unwrap();
|
||||
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
|
||||
.arg("--config")
|
||||
.arg(&env.config_path)
|
||||
.arg(&config_path)
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
|
|
@ -89,7 +92,9 @@ fn test_missing_certificate_file() {
|
|||
|
||||
#[test]
|
||||
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#"
|
||||
root = "{}"
|
||||
cert = "{}"
|
||||
|
|
@ -97,12 +102,12 @@ fn test_valid_config_startup() {
|
|||
hostname = "localhost"
|
||||
bind_host = "127.0.0.1"
|
||||
port = {}
|
||||
"#, env.content_path.display(), env.cert_path.display(), env.key_path.display(), env.port);
|
||||
std::fs::write(&env.config_path, config_content).unwrap();
|
||||
"#, temp_dir.path().join("content").display(), temp_dir.path().join("cert.pem").display(), temp_dir.path().join("key.pem").display(), port);
|
||||
std::fs::write(&config_path, config_content).unwrap();
|
||||
|
||||
let mut server_process = Command::new(env!("CARGO_BIN_EXE_pollux"))
|
||||
.arg("--config")
|
||||
.arg(&env.config_path)
|
||||
.arg(&config_path)
|
||||
.spawn()
|
||||
.unwrap();
|
||||
|
||||
|
|
|
|||
|
|
@ -2,9 +2,11 @@ mod common;
|
|||
|
||||
#[test]
|
||||
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
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
let config_content = format!(r#"
|
||||
root = "{}"
|
||||
cert = "{}"
|
||||
|
|
@ -13,13 +15,13 @@ fn test_rate_limiting_with_concurrent_requests() {
|
|||
bind_host = "127.0.0.1"
|
||||
port = {}
|
||||
max_concurrent_requests = 1
|
||||
"#, env.content_path.display(), env.cert_path.display(), env.key_path.display(), env.port);
|
||||
std::fs::write(&env.config_path, config_content).unwrap();
|
||||
"#, temp_dir.path().join("content").display(), temp_dir.path().join("cert.pem").display(), temp_dir.path().join("key.pem").display(), port);
|
||||
std::fs::write(&config_path, config_content).unwrap();
|
||||
|
||||
// Start server binary with test delay to simulate processing time
|
||||
let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
|
||||
.arg("--config")
|
||||
.arg(&env.config_path)
|
||||
.arg(&config_path)
|
||||
.arg("--test-processing-delay")
|
||||
.arg("1") // 1 second delay per request
|
||||
.spawn()
|
||||
|
|
@ -31,7 +33,7 @@ fn test_rate_limiting_with_concurrent_requests() {
|
|||
// Spawn 5 concurrent client processes
|
||||
let mut handles = vec![];
|
||||
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 || {
|
||||
std::process::Command::new("python3")
|
||||
.arg("tests/gemini_test_client.py")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue