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
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue