diff options
| -rw-r--r-- | src/main.rs | 28 | ||||
| -rw-r--r-- | tests/cli.rs | 50 |
2 files changed, 74 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 6fbab07..dd7793f 100644 --- a/src/main.rs +++ b/src/main.rs | |||
| @@ -327,10 +327,11 @@ fn run_command(command: Commands) -> Result<()> { | |||
| 327 | fn find_git_root() -> Result<PathBuf> { | 327 | fn find_git_root() -> Result<PathBuf> { |
| 328 | let mut current = std::env::current_dir()?; | 328 | let mut current = std::env::current_dir()?; |
| 329 | 329 | ||
| 330 | loop { | 330 | // Find the git root directory |
| 331 | let git_root = loop { | ||
| 331 | let git_path = current.join(".git"); | 332 | let git_path = current.join(".git"); |
| 332 | if git_path.exists() { | 333 | if git_path.exists() { |
| 333 | return Ok(current); | 334 | break current; |
| 334 | } | 335 | } |
| 335 | 336 | ||
| 336 | match current.parent() { | 337 | match current.parent() { |
| @@ -339,7 +340,30 @@ fn find_git_root() -> Result<PathBuf> { | |||
| 339 | "No git repository found. Please specify --root-dir or run from within a git repository" | 340 | "No git repository found. Please specify --root-dir or run from within a git repository" |
| 340 | )), | 341 | )), |
| 341 | } | 342 | } |
| 343 | }; | ||
| 344 | |||
| 345 | // Create .demon subdirectory within git root | ||
| 346 | let demon_dir = git_root.join(".demon"); | ||
| 347 | |||
| 348 | // Handle the case where .demon already exists | ||
| 349 | if demon_dir.exists() { | ||
| 350 | if !demon_dir.is_dir() { | ||
| 351 | return Err(anyhow::anyhow!( | ||
| 352 | "Path {} exists but is not a directory. Please remove it or specify --root-dir", | ||
| 353 | demon_dir.display() | ||
| 354 | )); | ||
| 355 | } | ||
| 356 | // .demon exists and is a directory, we can use it | ||
| 357 | return Ok(demon_dir); | ||
| 342 | } | 358 | } |
| 359 | |||
| 360 | // Create .demon directory | ||
| 361 | std::fs::create_dir(&demon_dir) | ||
| 362 | .with_context(|| format!("Failed to create daemon directory {}", demon_dir.display()))?; | ||
| 363 | |||
| 364 | tracing::info!("Created daemon directory: {}", demon_dir.display()); | ||
| 365 | |||
| 366 | Ok(demon_dir) | ||
| 343 | } | 367 | } |
| 344 | 368 | ||
| 345 | fn resolve_root_dir(global: &Global) -> Result<PathBuf> { | 369 | fn resolve_root_dir(global: &Global) -> Result<PathBuf> { |
diff --git a/tests/cli.rs b/tests/cli.rs index 005468c..b97d331 100644 --- a/tests/cli.rs +++ b/tests/cli.rs | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | use assert_cmd::Command; | 1 | use assert_cmd::Command; |
| 2 | use predicates::prelude::*; | 2 | use predicates::prelude::*; |
| 3 | use std::fs; | 3 | use std::fs; |
| 4 | use std::path::PathBuf; | ||
| 4 | use std::time::Duration; | 5 | use std::time::Duration; |
| 5 | use tempfile::TempDir; | 6 | use tempfile::TempDir; |
| 6 | 7 | ||
| @@ -95,8 +96,7 @@ fn test_list_empty() { | |||
| 95 | let temp_dir = TempDir::new().unwrap(); | 96 | let temp_dir = TempDir::new().unwrap(); |
| 96 | 97 | ||
| 97 | let mut cmd = Command::cargo_bin("demon").unwrap(); | 98 | let mut cmd = Command::cargo_bin("demon").unwrap(); |
| 98 | cmd.args(["--root-dir", temp_dir.path().to_str().unwrap()]) | 99 | cmd.args(&["list", "--root-dir", temp_dir.path().to_str().unwrap()]) |
| 99 | .args(&["list"]) | ||
| 100 | .assert() | 100 | .assert() |
| 101 | .success() | 101 | .success() |
| 102 | .stdout(predicate::str::contains("ID")) | 102 | .stdout(predicate::str::contains("ID")) |
| @@ -340,6 +340,52 @@ fn test_clean_removes_stdout_stderr_files() { | |||
| 340 | } | 340 | } |
| 341 | 341 | ||
| 342 | #[test] | 342 | #[test] |
| 343 | fn test_default_demon_directory_creation() { | ||
| 344 | // This test verifies that when no --root-dir is specified, | ||
| 345 | // the system creates and uses a .demon subdirectory in the git root | ||
| 346 | |||
| 347 | // Create a temporary git repo | ||
| 348 | let temp_dir = TempDir::new().unwrap(); | ||
| 349 | let git_dir = temp_dir.path().join(".git"); | ||
| 350 | std::fs::create_dir(&git_dir).unwrap(); | ||
| 351 | |||
| 352 | // Change to the temp directory | ||
| 353 | let original_dir = std::env::current_dir().unwrap(); | ||
| 354 | std::env::set_current_dir(temp_dir.path()).unwrap(); | ||
| 355 | |||
| 356 | // Restore directory when done | ||
| 357 | struct DirGuard(PathBuf); | ||
| 358 | impl Drop for DirGuard { | ||
| 359 | fn drop(&mut self) { | ||
| 360 | let _ = std::env::set_current_dir(&self.0); | ||
| 361 | } | ||
| 362 | } | ||
| 363 | let _guard = DirGuard(original_dir); | ||
| 364 | |||
| 365 | // Run a command without --root-dir to test default behavior | ||
| 366 | let mut cmd = Command::cargo_bin("demon").unwrap(); | ||
| 367 | cmd.args(&["run", "default_test", "echo", "hello"]) | ||
| 368 | .assert() | ||
| 369 | .success() | ||
| 370 | .stdout(predicate::str::contains("Started daemon 'default_test'")); | ||
| 371 | |||
| 372 | // Wait for process to complete | ||
| 373 | std::thread::sleep(Duration::from_millis(100)); | ||
| 374 | |||
| 375 | // Verify that .demon directory was created and files are in it | ||
| 376 | let demon_dir = temp_dir.path().join(".demon"); | ||
| 377 | assert!(demon_dir.exists()); | ||
| 378 | assert!(demon_dir.is_dir()); | ||
| 379 | assert!(demon_dir.join("default_test.pid").exists()); | ||
| 380 | assert!(demon_dir.join("default_test.stdout").exists()); | ||
| 381 | assert!(demon_dir.join("default_test.stderr").exists()); | ||
| 382 | |||
| 383 | // Verify the stdout content | ||
| 384 | let stdout_content = fs::read_to_string(demon_dir.join("default_test.stdout")).unwrap(); | ||
| 385 | assert_eq!(stdout_content.trim(), "hello"); | ||
| 386 | } | ||
| 387 | |||
| 388 | #[test] | ||
| 343 | fn test_run_with_complex_command() { | 389 | fn test_run_with_complex_command() { |
| 344 | let temp_dir = TempDir::new().unwrap(); | 390 | let temp_dir = TempDir::new().unwrap(); |
| 345 | 391 | ||
