From b257b2259dc3ad7d0aa4df86ef241b66932204a9 Mon Sep 17 00:00:00 2001 From: diogo464 Date: Mon, 23 Jun 2025 10:36:24 +0100 Subject: Add pre-commit hook for cargo fmt and apply formatting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Created pre-commit hook that runs cargo fmt --check - If formatting issues found, runs cargo fmt and asks for re-commit - Applied cargo fmt to fix existing formatting issues 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/main.rs | 73 ++++++++++++++++++++++++++++++++++++++++---------------- tests/cli.rs | 78 ++++++++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 105 insertions(+), 46 deletions(-) diff --git a/src/main.rs b/src/main.rs index dd7793f..22c2937 100644 --- a/src/main.rs +++ b/src/main.rs @@ -293,7 +293,14 @@ fn run_command(command: Commands) -> Result<()> { let show_stdout = !args.stderr || args.stdout; let show_stderr = !args.stdout || args.stderr; let root_dir = resolve_root_dir(&args.global)?; - tail_logs(&args.id, show_stdout, show_stderr, args.follow, args.lines, &root_dir) + tail_logs( + &args.id, + show_stdout, + show_stderr, + args.follow, + args.lines, + &root_dir, + ) } Commands::Cat(args) => { let show_stdout = !args.stderr || args.stdout; @@ -326,25 +333,27 @@ fn run_command(command: Commands) -> Result<()> { fn find_git_root() -> Result { let mut current = std::env::current_dir()?; - + // Find the git root directory let git_root = loop { let git_path = current.join(".git"); if git_path.exists() { break current; } - + match current.parent() { Some(parent) => current = parent.to_path_buf(), - None => return Err(anyhow::anyhow!( - "No git repository found. Please specify --root-dir or run from within a git repository" - )), + None => { + return Err(anyhow::anyhow!( + "No git repository found. Please specify --root-dir or run from within a git repository" + )); + } } }; - + // Create .demon subdirectory within git root let demon_dir = git_root.join(".demon"); - + // Handle the case where .demon already exists if demon_dir.exists() { if !demon_dir.is_dir() { @@ -356,13 +365,13 @@ fn find_git_root() -> Result { // .demon exists and is a directory, we can use it return Ok(demon_dir); } - + // Create .demon directory std::fs::create_dir(&demon_dir) .with_context(|| format!("Failed to create daemon directory {}", demon_dir.display()))?; - + tracing::info!("Created daemon directory: {}", demon_dir.display()); - + Ok(demon_dir) } @@ -370,13 +379,19 @@ fn resolve_root_dir(global: &Global) -> Result { match &global.root_dir { Some(dir) => { if !dir.exists() { - return Err(anyhow::anyhow!("Specified root directory does not exist: {}", dir.display())); + return Err(anyhow::anyhow!( + "Specified root directory does not exist: {}", + dir.display() + )); } if !dir.is_dir() { - return Err(anyhow::anyhow!("Specified root path is not a directory: {}", dir.display())); + return Err(anyhow::anyhow!( + "Specified root path is not a directory: {}", + dir.display() + )); } Ok(dir.clone()) - }, + } None => find_git_root(), } } @@ -428,7 +443,11 @@ fn run_daemon(id: &str, command: &[String], root_dir: &Path) -> Result<()> { // Don't wait for the child - let it run detached std::mem::forget(child); - println!("Started daemon '{}' with PID written to {}", id, pid_file.display()); + println!( + "Started daemon '{}' with PID written to {}", + id, + pid_file.display() + ); Ok(()) } @@ -820,7 +839,8 @@ fn list_daemons(quiet: bool, root_dir: &Path) -> Result<()> { for entry in find_pid_files(root_dir)? { found_any = true; let path = entry.path(); - let filename = path.file_name() + let filename = path + .file_name() .and_then(|name| name.to_str()) .unwrap_or_default(); @@ -904,14 +924,22 @@ fn status_daemon(id: &str, root_dir: &Path) -> Result<()> { // Show file information if stdout_file.exists() { let metadata = std::fs::metadata(&stdout_file)?; - println!("Stdout file: {} ({} bytes)", stdout_file.display(), metadata.len()); + println!( + "Stdout file: {} ({} bytes)", + stdout_file.display(), + metadata.len() + ); } else { println!("Stdout file: {} (not found)", stdout_file.display()); } if stderr_file.exists() { let metadata = std::fs::metadata(&stderr_file)?; - println!("Stderr file: {} ({} bytes)", stderr_file.display(), metadata.len()); + println!( + "Stderr file: {} ({} bytes)", + stderr_file.display(), + metadata.len() + ); } else { println!("Stderr file: {} (not found)", stderr_file.display()); } @@ -942,7 +970,8 @@ fn clean_orphaned_files(root_dir: &Path) -> Result<()> { // Find all .pid files in root directory for entry in find_pid_files(root_dir)? { let path = entry.path(); - let filename = path.file_name() + let filename = path + .file_name() .and_then(|name| name.to_str()) .unwrap_or_default(); let id = filename.strip_suffix(".pid").unwrap_or(filename); @@ -1000,7 +1029,11 @@ fn clean_orphaned_files(root_dir: &Path) -> Result<()> { Err(PidFileReadError::FileInvalid(_)) | Err(PidFileReadError::IoError(_)) => { println!("Cleaning up invalid PID file: {}", path.display()); if let Err(e) = std::fs::remove_file(&path) { - tracing::warn!("Failed to remove invalid PID file {}: {}", path.display(), e); + tracing::warn!( + "Failed to remove invalid PID file {}: {}", + path.display(), + e + ); } else { tracing::info!("Removed invalid PID file {}", path.display()); cleaned_count += 1; diff --git a/tests/cli.rs b/tests/cli.rs index b97d331..deaa3f0 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -36,10 +36,15 @@ fn test_run_missing_command() { let temp_dir = TempDir::new().unwrap(); let mut cmd = Command::cargo_bin("demon").unwrap(); - cmd.args(&["run", "--root-dir", temp_dir.path().to_str().unwrap(), "test"]) - .assert() - .failure() - .stderr(predicate::str::contains("Command cannot be empty")); + cmd.args(&[ + "run", + "--root-dir", + temp_dir.path().to_str().unwrap(), + "test", + ]) + .assert() + .failure() + .stderr(predicate::str::contains("Command cannot be empty")); } #[test] @@ -47,10 +52,17 @@ fn test_run_creates_files() { let temp_dir = TempDir::new().unwrap(); let mut cmd = Command::cargo_bin("demon").unwrap(); - cmd.args(&["run", "--root-dir", temp_dir.path().to_str().unwrap(), "test", "echo", "hello"]) - .assert() - .success() - .stdout(predicate::str::contains("Started daemon 'test'")); + cmd.args(&[ + "run", + "--root-dir", + temp_dir.path().to_str().unwrap(), + "test", + "echo", + "hello", + ]) + .assert() + .success() + .stdout(predicate::str::contains("Started daemon 'test'")); // Verify files were created assert!(temp_dir.path().join("test.pid").exists()); @@ -71,9 +83,16 @@ fn test_run_duplicate_process() { // Start a long-running process let mut cmd = Command::cargo_bin("demon").unwrap(); - cmd.args(&["run", "--root-dir", temp_dir.path().to_str().unwrap(), "long", "sleep", "30"]) - .assert() - .success(); + cmd.args(&[ + "run", + "--root-dir", + temp_dir.path().to_str().unwrap(), + "long", + "sleep", + "30", + ]) + .assert() + .success(); // Try to start another with the same ID let mut cmd = Command::cargo_bin("demon").unwrap(); @@ -267,9 +286,16 @@ fn test_clean_with_orphans() { // Create a dead process let mut cmd = Command::cargo_bin("demon").unwrap(); - cmd.args(&["run", "--root-dir", temp_dir.path().to_str().unwrap(), "dead", "echo", "hello"]) - .assert() - .success(); + cmd.args(&[ + "run", + "--root-dir", + temp_dir.path().to_str().unwrap(), + "dead", + "echo", + "hello", + ]) + .assert() + .success(); // Wait for process to complete std::thread::sleep(Duration::from_millis(100)); @@ -300,14 +326,14 @@ fn test_clean_removes_stdout_stderr_files() { // Create a process that outputs to both stdout and stderr let mut cmd = Command::cargo_bin("demon").unwrap(); cmd.args(&[ - "run", - "--root-dir", - temp_dir.path().to_str().unwrap(), + "run", + "--root-dir", + temp_dir.path().to_str().unwrap(), "test_output", "--", "sh", "-c", - "echo 'stdout content'; echo 'stderr content' >&2" + "echo 'stdout content'; echo 'stderr content' >&2", ]) .assert() .success(); @@ -319,7 +345,7 @@ fn test_clean_removes_stdout_stderr_files() { assert!(temp_dir.path().join("test_output.pid").exists()); assert!(temp_dir.path().join("test_output.stdout").exists()); assert!(temp_dir.path().join("test_output.stderr").exists()); - + let stdout_content = fs::read_to_string(temp_dir.path().join("test_output.stdout")).unwrap(); let stderr_content = fs::read_to_string(temp_dir.path().join("test_output.stderr")).unwrap(); assert!(stdout_content.contains("stdout content")); @@ -343,16 +369,16 @@ fn test_clean_removes_stdout_stderr_files() { fn test_default_demon_directory_creation() { // This test verifies that when no --root-dir is specified, // the system creates and uses a .demon subdirectory in the git root - + // Create a temporary git repo let temp_dir = TempDir::new().unwrap(); let git_dir = temp_dir.path().join(".git"); std::fs::create_dir(&git_dir).unwrap(); - + // Change to the temp directory let original_dir = std::env::current_dir().unwrap(); std::env::set_current_dir(temp_dir.path()).unwrap(); - + // Restore directory when done struct DirGuard(PathBuf); impl Drop for DirGuard { @@ -361,17 +387,17 @@ fn test_default_demon_directory_creation() { } } let _guard = DirGuard(original_dir); - + // Run a command without --root-dir to test default behavior let mut cmd = Command::cargo_bin("demon").unwrap(); cmd.args(&["run", "default_test", "echo", "hello"]) .assert() .success() .stdout(predicate::str::contains("Started daemon 'default_test'")); - + // Wait for process to complete std::thread::sleep(Duration::from_millis(100)); - + // Verify that .demon directory was created and files are in it let demon_dir = temp_dir.path().join(".demon"); assert!(demon_dir.exists()); @@ -379,7 +405,7 @@ fn test_default_demon_directory_creation() { assert!(demon_dir.join("default_test.pid").exists()); assert!(demon_dir.join("default_test.stdout").exists()); assert!(demon_dir.join("default_test.stderr").exists()); - + // Verify the stdout content let stdout_content = fs::read_to_string(demon_dir.join("default_test.stdout")).unwrap(); assert_eq!(stdout_content.trim(), "hello"); -- cgit