(339c65a) Add comment command
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
use assert_cmd::Command;
|
||||
use assert_fs::fixture::{PathChild, PathCreateDir};
|
||||
use assert_fs::TempDir;
|
||||
use std::fs;
|
||||
use std::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: &PathBuf,
|
||||
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::<String>();
|
||||
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());
|
||||
}
|
||||
Reference in New Issue
Block a user