diff --git a/src/handlers.rs b/src/handlers.rs index 4df025e..854e2ca 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -328,32 +328,18 @@ pub fn cmd_mv(ticket_id: &str, target_status: &str, force: bool, start: &Path) - )); } - // Read and update the ticket content - let mut content = fs::read_to_string(current_path.join(&found_filename))?; - - // Update Status in Metadata section - if let Some(start_pos) = content.find("- **Status**") { - // Skip "- " to find "**Status**" - let status_start = start_pos + 2; - let after_status = &content[status_start + "**Status**".len()..]; // after "**Status**" - if after_status.starts_with(": ") { - let rest = &after_status[2..]; - let value_end = rest.find('\n').unwrap_or(rest.len()); - let _value = &rest[..value_end]; - let before = &content[..status_start]; - let after = &rest[value_end..]; - content = format!("{}**-Status**: {}\n{}", before, target_status, after); - } - } + // Read and update the ticket content before touching the filesystem, so a ticket + // without a Status field is never half-moved. + let source_path = current_path.join(&found_filename); + let content = fs::read_to_string(&source_path)?; + let content = set_status_field(&content, target_status, &title)?; // Move the file - fs::rename( - current_path.join(&found_filename), - target_path.join(&found_filename), - )?; + let dest_path = target_path.join(&found_filename); + fs::rename(&source_path, &dest_path)?; // Write back updated content - fs::write(target_path.join(&found_filename), content)?; + fs::write(&dest_path, content)?; println!( "Moved ticket '{}' from {} to {}", diff --git a/src/util.rs b/src/util.rs index d50ea03..e6524e9 100644 --- a/src/util.rs +++ b/src/util.rs @@ -348,6 +348,37 @@ pub fn parse_ticket_field(content: &str, field: &str) -> String { .unwrap_or("_Not set_".to_string()) } +fn is_status_line(line: &str) -> bool { + // The second spelling is what earlier versions wrote, repair it on the way through. + line.starts_with("- **Status**:") || line.starts_with("- **-Status**:") +} + +// Rewrite the Status field in the Metadata section, leaving every other line untouched. +pub fn set_status_field(content: &str, target_status: &str, title: &str) -> Result { + let mut updated = String::with_capacity(content.len()); + let mut found = false; + + for line in content.lines() { + if !found && is_status_line(line) { + updated.push_str("- **Status**: "); + updated.push_str(target_status); + found = true; + } else { + updated.push_str(line); + } + updated.push('\n'); + } + + if !found { + return Err(anyhow!( + "Ticket '{}' has no Status field in its Metadata section", + title + )); + } + + Ok(updated) +} + // Format created_at for display with proper spacing and formatting. pub fn format_created_at(created_at: &str) -> String { if created_at == "_Not set_" || created_at.is_empty() { diff --git a/tests/cmd_mv.rs b/tests/cmd_mv.rs index 620c592..d5b6006 100644 --- a/tests/cmd_mv.rs +++ b/tests/cmd_mv.rs @@ -109,8 +109,10 @@ fn mv_forwards_from_backlog_to_committed() { // Verify status updated let content = fs::read_to_string(worklane.join("committed").join(&filename)).unwrap(); - eprintln!("CONTENT: {:?}", content); - assert!(content.contains("- **-Status**: committed")); + assert!(content.contains("- **Status**: committed")); + + // No stray blank line inserted after the rewritten field + assert!(content.ends_with("- **Status**: committed\n")); } #[test] @@ -236,7 +238,7 @@ fn mv_allows_skipping_stages_with_force() { assert!(worklane.join("done").join(&filename).exists()); let content = fs::read_to_string(worklane.join("done").join(&filename)).unwrap(); - assert!(content.contains("- **-Status**: done")); + assert!(content.contains("- **Status**: done")); } #[test] @@ -331,3 +333,101 @@ fn mv_uses_ticket_id_for_resolution() { assert!(stdout.contains("ID Resolution")); assert!(wl.join("committed").join(&filename).exists()); } + +#[test] +fn mv_keeps_status_in_sync_across_consecutive_moves() { + let temp = TempDir::new().unwrap(); + let worklane = setup_worklane(&temp); + + let id = "abc123def456789012345678901234567890"; + let filename = create_ticket( + &worklane, + "backlog", + "Two Hop", + "Moved twice.", + id, + "ida", + "2026-04-01", + "test", + ); + + for target in ["committed", "reviewing"] { + Command::cargo_bin(BIN_NAME) + .unwrap() + .current_dir(&temp.path()) + .arg("mv") + .arg("abc123def4567") + .arg("--status") + .arg(target) + .assert() + .success(); + } + + assert!(worklane.join("reviewing").join(&filename).exists()); + + // The second move must update the field too, not silently skip it + let content = fs::read_to_string(worklane.join("reviewing").join(&filename)).unwrap(); + assert!(content.contains("- **Status**: reviewing")); + assert!(!content.contains("-Status")); + assert!(!content.contains("committed")); +} + +#[test] +fn mv_repairs_legacy_status_field() { + let temp = TempDir::new().unwrap(); + let worklane = setup_worklane(&temp); + + let filename = "abc123def456789012345678901234567890_legacy.md"; + let path = worklane.join("committed").join(filename); + fs::write( + &path, + "# Legacy Ticket\n\ + \n\ + ## Metadata\n\ + \n\ + - **ID**: abc123def456789012345678901234567890\n\ + - **-Status**: committed\n", + ) + .unwrap(); + + Command::cargo_bin(BIN_NAME) + .unwrap() + .current_dir(&temp.path()) + .arg("mv") + .arg("abc123def4567") + .arg("--status") + .arg("reviewing") + .assert() + .success(); + + let content = fs::read_to_string(worklane.join("reviewing").join(filename)).unwrap(); + assert!(content.contains("- **Status**: reviewing")); + assert!(!content.contains("-Status")); +} + +#[test] +fn mv_fails_when_status_field_is_missing() { + let temp = TempDir::new().unwrap(); + let worklane = setup_worklane(&temp); + + let filename = "abc123def456789012345678901234567890_no-status.md"; + let path = worklane.join("backlog").join(filename); + fs::write(&path, "# No Status\n\n## Metadata\n\n- **Owner**: jack\n").unwrap(); + + let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); + cmd.current_dir(&temp.path()) + .arg("mv") + .arg("abc123def4567") + .arg("--status") + .arg("committed") + .assert() + .failure(); + + let stderr = get_stderr(&mut cmd); + assert!(stderr.contains("No Status")); + assert!(stderr.contains("has no Status field")); + + // The ticket must not be half-moved + assert!(worklane.join("backlog").join(filename).exists()); + assert!(!worklane.join("committed").join(filename).exists()); +}