use assert_cmd::Command; use assert_fs::TempDir; use assert_fs::fixture::{PathChild, PathCreateDir}; use std::fs; use std::path::{Path, PathBuf}; const BIN_NAME: &str = env!("CARGO_BIN_EXE_worklane"); fn setup_worklane(temp: &TempDir) -> PathBuf { let worklane = temp.child(".worklane"); worklane.create_dir_all().unwrap(); for dir in ["backlog", "committed", "reviewing", "done"] { worklane.child(dir).create_dir_all().unwrap(); } worklane.path().to_path_buf() } fn create_ticket(worklane_dir: &Path, status: &str, title: &str) -> String { let uuid = uuid::Uuid::new_v4().to_string(); let slug = title .to_lowercase() .chars() .filter(|c| c.is_alphanumeric() || *c == ' ' || *c == '-' || *c == '_') .map(|c| if c == ' ' { '-' } else { c }) .collect::(); let filename = format!("{}_{}.md", uuid, slug); let content = format!( "# {}\n\ \n\ ## Metadata\n\ \n\ - **ID**: {}\n\ - **Status**: {}\n\ ", title, uuid, status ); fs::create_dir_all(worklane_dir.join(status)).unwrap(); fs::write(worklane_dir.join(status).join(&filename), content).unwrap(); uuid } #[test] fn comment_creates_file_with_valid_ticket_uuid() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Test Ticket"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .arg("comment") .args([&uuid, "This is a test comment"]) .assert() .success(); let comments_dir = worklane.join("comments").join(&uuid); assert!(comments_dir.exists()); let files: Vec<_> = fs::read_dir(&comments_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); assert_eq!(files.len(), 1); let content = fs::read_to_string(files[0].path()).unwrap(); assert!(content.contains("This is a test comment")); } #[test] fn comment_creates_file_with_valid_ticket_title() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "committed", "My Ticket"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .arg("comment") .args(["My Ticket", "Testing title lookup"]) .assert() .success(); } #[test] fn comment_error_when_ticket_not_found() { let _temp = TempDir::new().unwrap(); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.arg("comment") .arg("Nonexistent") .arg("Some comment") .assert() .code(1); } #[test] fn comment_includes_git_user_and_body() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Comment Test"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .arg("comment") .args([&uuid, "My comment body"]) .assert() .success(); let comments_dir = worklane.join("comments").join(&uuid); let files: Vec<_> = fs::read_dir(&comments_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); let content = fs::read_to_string(files[0].path()).unwrap(); assert!(content.contains("My comment body")); assert!(content.contains("**Author**")); } #[test] fn comment_stored_in_timestamped_file() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "done", "Timestamp Test"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .arg("comment") .args([&uuid, "Timestamp test"]) .assert() .success(); let comments_dir = worklane.join("comments").join(&uuid); let files: Vec<_> = fs::read_dir(&comments_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); assert_eq!(files.len(), 1); let filename = files[0].file_name().into_string().unwrap(); assert!(filename.ends_with(".md")); } #[test] fn comment_creates_comments_directory() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "reviewing", "Dir Test"); let comments_dir = worklane.join("comments").join(&uuid); assert!(!comments_dir.exists()); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .arg("comment") .args([&uuid, "Create dir test"]) .assert() .success(); assert!(comments_dir.exists()); } #[test] fn comment_uses_owner_flag_when_provided() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Owner Test"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .arg("comment") .args([&uuid, "Test comment with owner"]) .arg("-o") .arg("TestUser") .assert() .success(); let comments_dir = worklane.join("comments").join(&uuid); let files: Vec<_> = fs::read_dir(&comments_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); let content = fs::read_to_string(files[0].path()).unwrap(); assert!(content.contains("**Author**: TestUser")); } #[test] fn comment_falls_back_to_git_user_when_owner_omitted() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "done", "Fallback Test"); let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .arg("comment") .args([&uuid, "Test comment fallback"]) .assert() .success(); let comments_dir = worklane.join("comments").join(&uuid); let files: Vec<_> = fs::read_dir(&comments_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); let content = fs::read_to_string(files[0].path()).unwrap(); assert!(content.contains("**Author**")); } /// Second-resolution filenames meant the second of two quick comments /// truncated the first. #[test] fn comments_added_back_to_back_are_both_kept() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Rapid Fire"); for body in ["first comment", "second comment", "third comment"] { Command::cargo_bin(BIN_NAME) .unwrap() .current_dir(temp.path()) .arg("comment") .args([&uuid, body]) .assert() .success(); } let comments_dir = worklane.join("comments").join(&uuid); let files: Vec<_> = fs::read_dir(&comments_dir) .unwrap() .filter_map(|e| e.ok()) .collect(); assert_eq!(files.len(), 3, "a comment was overwritten"); // And show has to render them in the order they were added let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()).arg("show").arg(&uuid); let assert = cmd.assert().success(); let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap(); let first = stdout.find("first comment").expect("first comment missing"); let second = stdout.find("second comment").expect("second comment missing"); let third = stdout.find("third comment").expect("third comment missing"); assert!(first < second && second < third, "comments out of order"); } /// The body is a positional, and the case that motivated this: a review comment /// quoting a flag name used to be refused outright. #[test] fn comment_accepts_a_body_that_begins_with_a_hyphen() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Hyphen body"); for body in ["--print is the flag I mean", "-f skips the check", "-"] { let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(temp.path()) .args(["comment", &uuid, body]) .assert() .success(); } let bodies: String = fs::read_dir(worklane.join("comments").join(&uuid)) .unwrap() .filter_map(|e| e.ok()) .map(|e| fs::read_to_string(e.path()).unwrap()) .collect(); for body in ["--print is the flag I mean", "-f skips the check"] { assert!(bodies.contains(body), "{body:?} missing from {bodies:?}"); } } /// The ticket positional was deliberately left alone, so a hyphen-leading search /// term is still a usage error. A bare separator is the way through, and it is the /// only way through for `comment` since the body follows in the same position. #[test] fn comment_needs_a_separator_for_a_hyphen_leading_ticket() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); create_ticket(&worklane, "backlog", "-leading"); let mut without = Command::cargo_bin(BIN_NAME).unwrap(); without .current_dir(temp.path()) .args(["comment", "-leading", "a body"]) .assert() .failure(); let mut with = Command::cargo_bin(BIN_NAME).unwrap(); with.current_dir(temp.path()) .args(["comment", "--", "-leading", "a body"]) .assert() .success(); } /// The one shape `allow_hyphen_values` does not reach: a lone `--` in a positional /// is clap's end-of-options marker, consumed before any value is assigned. So a /// comment body of exactly `--` needs its own separator in front of it. /// /// Not a silent drop either way, which is what matters: without the separator the /// body is reported missing and nothing is written. #[test] fn a_bare_double_hyphen_body_needs_a_separator() { let temp = TempDir::new().unwrap(); let worklane = setup_worklane(&temp); let uuid = create_ticket(&worklane, "backlog", "Bare separator"); let mut without = Command::cargo_bin(BIN_NAME).unwrap(); let assert = without .current_dir(temp.path()) .args(["comment", &uuid, "--"]) .assert() .failure(); let stderr = String::from_utf8(assert.get_output().stderr.clone()).unwrap(); assert!(stderr.contains(""), "unclear error: {stderr}"); assert!( !worklane.join("comments").join(&uuid).exists(), "a comment was written despite the error" ); let mut with = Command::cargo_bin(BIN_NAME).unwrap(); with.current_dir(temp.path()) .args(["comment", &uuid, "--", "--"]) .assert() .success(); let stored: String = fs::read_dir(worklane.join("comments").join(&uuid)) .unwrap() .filter_map(|e| e.ok()) .map(|e| fs::read_to_string(e.path()).unwrap()) .collect(); assert!(stored.contains("**Comment**\n--\n"), "was {stored:?}"); }