From 108fef99d19ade71013dc25562e3c3c3e23347ef Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Fri, 7 Apr 2023 11:50:57 +0200 Subject: Failing test for setting description --- src/git.rs | 6 ++++- src/lib.rs | 8 ++++-- src/parser.rs | 2 ++ tests/cli.rs | 85 +++++++++++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 78 insertions(+), 23 deletions(-) diff --git a/src/git.rs b/src/git.rs index 3e6ef98..1271712 100644 --- a/src/git.rs +++ b/src/git.rs @@ -65,7 +65,11 @@ fn is_valid_git_repo_path(path: &PathBuf) -> Result { } } -pub fn init(repo_name: &str, group: &Option) -> Result { +pub fn init( + repo_name: &str, + group: &Option, + description: &Option, +) -> Result { fn init_group(repo_name: &str, group: &str) -> Result { if !verify_user_is_in_group(group) { return Err(ShackleError::InvalidGroup); diff --git a/src/lib.rs b/src/lib.rs index 4685e53..7d4d2df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,8 +26,12 @@ pub fn run_command(user_input: String) -> Result, ShackleErr println!("{table}"); } - Ok(ShackleCommand::GitInit(GitInitArgs { repo_name, group })) => { - let init_result = git::init(&repo_name, &group)?; + Ok(ShackleCommand::GitInit(GitInitArgs { + repo_name, + group, + description, + })) => { + let init_result = git::init(&repo_name, &group, &description)?; println!("Successfully created \"{}\"", init_result.path.display()); } Ok(ShackleCommand::GitUploadPack(upload_pack_args)) => { diff --git a/src/parser.rs b/src/parser.rs index 88b32bb..5295483 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -22,6 +22,8 @@ pub enum ShackleCommand { pub struct GitInitArgs { #[arg(long)] pub group: Option, + #[arg(long)] + pub description: Option, pub repo_name: String, } diff --git a/tests/cli.rs b/tests/cli.rs index e64ce6b..dfbb915 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -225,15 +225,30 @@ fn allows_single_quotes_and_spaces_inside_double_quotes() -> Result<()> { Ok(()) } +const DEFAULT_DESCRIPTION: &str = + "Unnamed repository; edit this file 'description' to name the repository."; + +fn expect_list_table(c: &mut TestContext, repos: &[(String, String)]) -> Result<()> { + c.p.send_line("list")?; + c.p.exp_regex(r"\+-+\+-+\+")?; + c.p.exp_regex(r"\| path +\| description +\|")?; + c.p.exp_regex(r"\+=+\+")?; + for (path, description) in repos { + c.p.exp_string("| ")?; + c.p.exp_string(path)?; + c.p.exp_regex(r" +\| ")?; + c.p.exp_string(&description)?; + c.p.exp_regex(r" +\|")?; + } + c.p.exp_regex(r"\+-+\+-+\+")?; + expect_prompt(&mut c.p)?; + Ok(()) +} + #[test] fn list_can_print_an_empty_list() -> Result<()> { let mut c = spawn_interactive_process()?; - c.p.send_line("list")?; - c.p.exp_string("+------+-------------+")?; - c.p.exp_string("| path | description |")?; - c.p.exp_string("+====================+")?; - c.p.exp_string("+------+-------------+")?; - expect_prompt(&mut c.p)?; + expect_list_table(&mut c, &[])?; Ok(()) } @@ -245,13 +260,13 @@ fn list_can_print_a_list_of_personal_repos_with_descriptions() -> Result<()> { let personal_repo_name = "my-personal-repo"; c.p.send_line(&format!("git-init {}", personal_repo_name))?; - c.p.send_line("list")?; - c.p.exp_regex(r"\+-+\+-+\+")?; - c.p.exp_regex(r"\| path +\| description +\|")?; - c.p.exp_regex(r"\+=+\+")?; - c.p.exp_regex(&format!(r"\| git/{user}/{personal_repo_name}\.git +\| Unnamed repository; edit this file 'description' to name the repository\. +\|"))?; - c.p.exp_regex(r"\+-+\+-+\+")?; - expect_prompt(&mut c.p)?; + expect_list_table( + &mut c, + &[( + format!("git/{user}/{personal_repo_name}.git"), + DEFAULT_DESCRIPTION.to_owned(), + )], + )?; Ok(()) } @@ -267,13 +282,43 @@ fn list_can_print_a_list_of_all_repos_with_descriptions() -> Result<()> { let shared_repo_name = "my-shared-repo"; c.p.send_line(&format!("git-init --group {} {}", group, shared_repo_name))?; - c.p.send_line("list")?; - c.p.exp_regex(r"\+-+\+-+\+")?; - c.p.exp_regex(r"| path +| description +|")?; - c.p.exp_regex(r"\+=+\+")?; - c.p.exp_regex(&format!(r"\| git/{user}/{personal_repo_name}\.git +\| Unnamed repository; edit this file 'description' to name the repository\. +\|"))?; - c.p.exp_regex(&format!(r"\| git/{group}/{shared_repo_name}\.git +\| Unnamed repository; edit this file 'description' to name the repository\. +\|"))?; - c.p.exp_regex(r"\+-+\+-+\+")?; + expect_list_table( + &mut c, + &[ + ( + format!("git/{user}/{personal_repo_name}.git"), + DEFAULT_DESCRIPTION.to_owned(), + ), + ( + format!("git/{group}/{shared_repo_name}.git"), + DEFAULT_DESCRIPTION.to_owned(), + ), + ], + )?; + + Ok(()) +} + +#[test] +fn can_set_the_description_on_a_repo_during_init() -> Result<()> { + let mut c = spawn_interactive_process()?; + let user = get_username().unwrap(); + let repo_name = "my-personal-repo"; + let description = "A cool repo that does cool things"; + c.p.send_line(&format!( + "git-init --description \"{description}\" {repo_name}" + ))?; + c.p.exp_string(&format!( + "Successfully created \"git/{user}/{repo_name}.git\"", + ))?; + + expect_list_table( + &mut c, + &[( + format!("git/{user}/{repo_name}.git"), + description.to_owned(), + )], + )?; Ok(()) } -- cgit v1.2.3