From c48a05cd9edd59d6581b31ebc1dc091195507265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20=C5=A0im=C3=A1nek?= Date: Fri, 2 Oct 2026 23:02:29 +0200 Subject: [PATCH] Isolate working directories in CLI tests --- crates/rb-cli/tests/runtime_helpers_tests.rs | 58 ++++++++------------ 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/crates/rb-cli/tests/runtime_helpers_tests.rs b/crates/rb-cli/tests/runtime_helpers_tests.rs index 377ed0a..e173520 100644 --- a/crates/rb-cli/tests/runtime_helpers_tests.rs +++ b/crates/rb-cli/tests/runtime_helpers_tests.rs @@ -1,6 +1,6 @@ use rb_cli::config::{RbConfig, TrackedConfig}; -use rb_cli::runtime_helpers::{CommandContext, new_command_wrapper}; -use std::path::PathBuf; +use rb_cli::runtime_helpers::CommandContext; +use std::{path::PathBuf, process::Command}; fn create_test_context() -> CommandContext { let config = RbConfig::default(); @@ -12,49 +12,35 @@ fn create_test_context() -> CommandContext { #[test] fn test_new_command_wrapper_creates_file() { - let temp_dir = std::env::temp_dir().join(format!("rb-runtime-new-{}", std::process::id())); - std::fs::create_dir_all(&temp_dir).unwrap(); - - let original_dir = std::env::current_dir().unwrap(); - std::env::set_current_dir(&temp_dir).unwrap(); - - let result = new_command_wrapper(); - assert!(result.is_ok()); - - assert!(temp_dir.join("rbproject.toml").exists()); - - std::env::set_current_dir(&original_dir).unwrap(); - std::fs::remove_dir_all(&temp_dir).ok(); + let temp_dir = tempfile::tempdir().unwrap(); + let output = Command::new(env!("CARGO_BIN_EXE_rb")) + .arg("new") + .current_dir(temp_dir.path()) + .output() + .unwrap(); + + assert!(output.status.success(), "{output:?}"); + assert!(temp_dir.path().join("rbproject.toml").exists()); } #[test] fn test_new_command_wrapper_fails_if_file_exists() { - let temp_dir = - std::env::temp_dir().join(format!("rb-runtime-new-exists-{}", std::process::id())); - std::fs::create_dir_all(&temp_dir).unwrap(); - - let project_file = temp_dir.join("rbproject.toml"); + let temp_dir = tempfile::tempdir().unwrap(); + let project_file = temp_dir.path().join("rbproject.toml"); std::fs::write(&project_file, "existing").unwrap(); - assert!( - project_file.exists(), - "Test precondition failed: file should exist" - ); - if let Ok(file) = std::fs::File::open(&project_file) { - let _ = file.sync_all(); - } + let output = Command::new(env!("CARGO_BIN_EXE_rb")) + .arg("new") + .current_dir(temp_dir.path()) + .output() + .unwrap(); - let original_dir = std::env::current_dir().unwrap(); - std::env::set_current_dir(&temp_dir).unwrap(); - - let result = new_command_wrapper(); + assert!(!output.status.success(), "{output:?}"); assert!( - result.is_err(), - "Expected error when rbproject.toml already exists" + String::from_utf8_lossy(&output.stderr).contains("already graces this directory"), + "{output:?}" ); - - std::env::set_current_dir(&original_dir).unwrap(); - std::fs::remove_dir_all(&temp_dir).ok(); + assert_eq!(std::fs::read_to_string(project_file).unwrap(), "existing"); } #[test]