From 4f8038af3c1140ff07f26b2a323db8fcf7dde79c Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Sat, 24 Jun 2023 22:27:45 +0200 Subject: Some extra test helper functions to make tests a bit more succinct --- tests/cli.rs | 169 +++++++++++++++++++++++++++-------------------------------- 1 file changed, 77 insertions(+), 92 deletions(-) (limited to 'tests') diff --git a/tests/cli.rs b/tests/cli.rs index 7bda913..02f1f93 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -2,7 +2,7 @@ use anyhow::Result; use assert_cmd::{cargo::cargo_bin, Command}; use rexpect::session::{spawn_command, PtySession}; use shackle_shell::user_info::{get_user_groups, get_username}; -use std::path::Path; +use std::path::{Path, PathBuf}; use tempfile::TempDir; struct TestContext { @@ -10,6 +10,38 @@ struct TestContext { workdir: TempDir, } +impl TestContext { + fn personal_repo_dir(&self, repo_name: &str) -> PathBuf { + let username = get_username().unwrap(); + self.workdir + .as_ref() + .join("git") + .join(username) + .join(&format!("{}.git", repo_name)) + } + + fn group_repo_dir(&self, group: &str, repo_name: &str) -> PathBuf { + self.workdir + .as_ref() + .join("git") + .join(group) + .join(&format!("{}.git", repo_name)) + } +} + +fn personal_repo_path(repo_name: &str) -> String { + let username = get_username().unwrap(); + format!("git/{username}/{repo_name}.git") +} + +fn arbitrary_user_group() -> String { + get_user_groups().into_iter().next().unwrap() +} + +fn group_repo_path(group: &str, repo_name: &str) -> String { + format!("git/{group}/{repo_name}.git") +} + fn spawn_interactive_process() -> Result { let workdir = tempfile::tempdir()?; @@ -130,21 +162,15 @@ fn verify_repo_config_value(repo_dir: &Path, config_key: &str, config_value: Opt #[test] fn can_init_a_new_git_repo() -> Result<()> { let mut c = spawn_interactive_process()?; - let username = get_username().unwrap(); let repo_name = "my-new-repo"; c.p.send_line(&format!("init {}", repo_name))?; c.p.exp_string(&format!( - "Successfully created \"git/{}/{}.git\"", - username, repo_name + "Successfully created \"{}\"", + personal_repo_path(repo_name) ))?; expect_prompt(&mut c.p)?; - let repo_dir = c - .workdir - .as_ref() - .join("git") - .join(username) - .join(&format!("{}.git", repo_name)); + let repo_dir = c.personal_repo_dir(repo_name); verify_repo_exists(&repo_dir); verify_current_branch(&repo_dir, "refs/heads/main"); verify_repo_config_value(&repo_dir, "core.sharedrepository", None); @@ -155,21 +181,16 @@ fn can_init_a_new_git_repo() -> Result<()> { #[test] fn can_init_a_new_shared_git_repo() -> Result<()> { let mut c = spawn_interactive_process()?; - let group = get_user_groups().into_iter().next().unwrap(); + let group = arbitrary_user_group(); let repo_name = "my-new-shared-repo"; c.p.send_line(&format!("init --group {} {}", group, repo_name))?; c.p.exp_string(&format!( - "Successfully created \"git/{}/{}.git\"", - group, repo_name + "Successfully created \"{}\"", + group_repo_path(&group, repo_name) ))?; expect_prompt(&mut c.p)?; - let repo_dir = c - .workdir - .as_ref() - .join("git") - .join(&group) - .join(&format!("{}.git", repo_name)); + let repo_dir = c.group_repo_dir(&group, repo_name); verify_repo_exists(&repo_dir); verify_repo_config_value(&repo_dir, "core.sharedrepository", Some("1")); @@ -189,12 +210,11 @@ fn does_not_init_shared_repo_if_the_user_isnt_in_the_group() -> Result<()> { #[test] fn runs_a_single_command_and_exit_with_cli_flag() -> Result<()> { - let username = get_username().unwrap(); let repo_name = "another-new-repo"; let mut c = run_batch_command(&format!("init {}", repo_name))?; c.p.exp_string(&format!( - "Successfully created \"git/{}/{}.git\"", - username, repo_name + "Successfully created \"{}\"", + personal_repo_path(repo_name) ))?; c.p.exp_eof()?; Ok(()) @@ -202,12 +222,12 @@ fn runs_a_single_command_and_exit_with_cli_flag() -> Result<()> { #[test] fn allows_quotes_arguments() -> Result<()> { - let username = get_username().unwrap(); + let repo_name = "another-new-repo"; let mut c = spawn_interactive_process()?; - c.p.send_line("\"init\" 'another-new-repo'")?; + c.p.send_line(&format!("\"init\" '{repo_name}'"))?; c.p.exp_string(&format!( - "Successfully created \"git/{}/another-new-repo.git\"", - username + "Successfully created \"{}\"", + personal_repo_path(repo_name) ))?; Ok(()) } @@ -230,12 +250,12 @@ fn errors_with_an_open_single_quote() -> Result<()> { #[test] fn allows_single_quotes_and_spaces_inside_double_quotes() -> Result<()> { - let username = get_username().unwrap(); + let repo_name = "shukkie's new repo"; let mut c = spawn_interactive_process()?; - c.p.send_line("init \"shukkie's new repo\"")?; + c.p.send_line(&format!("init \"{repo_name}\""))?; c.p.exp_string(&format!( - "Successfully created \"git/{}/shukkie's new repo.git\"", - username + "Successfully created \"{}\"", + personal_repo_path(repo_name) ))?; Ok(()) } @@ -271,19 +291,18 @@ fn list_can_print_an_empty_list() -> Result<()> { #[test] fn list_can_print_a_list_of_personal_repos_with_descriptions() -> Result<()> { let mut c = spawn_interactive_process()?; - let username = get_username().unwrap(); let repo_name = "my-personal-repo"; c.p.send_line(&format!("init {}", repo_name))?; c.p.exp_string(&format!( - "Successfully created \"git/{}/{}.git\"", - username, repo_name + "Successfully created \"{}\"", + personal_repo_path(repo_name) ))?; expect_prompt(&mut c.p)?; expect_list_table( &mut c, &[( - format!("git/{username}/{repo_name}.git"), + personal_repo_path(repo_name), DEFAULT_DESCRIPTION.to_owned(), )], )?; @@ -294,21 +313,20 @@ fn list_can_print_a_list_of_personal_repos_with_descriptions() -> Result<()> { #[test] fn list_can_print_a_list_of_all_repos_with_descriptions() -> Result<()> { let mut c = spawn_interactive_process()?; - let username = get_username().unwrap(); let personal_repo_name = "my-personal-repo"; c.p.send_line(&format!("init {}", personal_repo_name))?; c.p.exp_string(&format!( - "Successfully created \"git/{}/{}.git\"", - username, personal_repo_name + "Successfully created \"{}\"", + personal_repo_path(personal_repo_name) ))?; expect_prompt(&mut c.p)?; - let group = get_user_groups().into_iter().next().unwrap(); + let group = arbitrary_user_group(); let shared_repo_name = "my-shared-repo"; c.p.send_line(&format!("init --group {} {}", group, shared_repo_name))?; c.p.exp_string(&format!( - "Successfully created \"git/{}/{}.git\"", - group, shared_repo_name + "Successfully created \"{}\"", + group_repo_path(&group, shared_repo_name) ))?; expect_prompt(&mut c.p)?; @@ -316,11 +334,11 @@ fn list_can_print_a_list_of_all_repos_with_descriptions() -> Result<()> { &mut c, &[ ( - format!("git/{username}/{personal_repo_name}.git"), + personal_repo_path(personal_repo_name), DEFAULT_DESCRIPTION.to_owned(), ), ( - format!("git/{group}/{shared_repo_name}.git"), + group_repo_path(&group, shared_repo_name), DEFAULT_DESCRIPTION.to_owned(), ), ], @@ -332,20 +350,17 @@ fn list_can_print_a_list_of_all_repos_with_descriptions() -> Result<()> { #[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!("init --description \"{description}\" {repo_name}"))?; c.p.exp_string(&format!( - "Successfully created \"git/{user}/{repo_name}.git\"", + "Successfully created \"{}\"", + personal_repo_path(repo_name) ))?; expect_list_table( &mut c, - &[( - format!("git/{user}/{repo_name}.git"), - description.to_owned(), - )], + &[(personal_repo_path(repo_name), description.to_owned())], )?; Ok(()) @@ -354,25 +369,17 @@ fn can_set_the_description_on_a_repo_during_init() -> Result<()> { #[test] fn can_change_the_description_on_a_repo() -> 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"; + let repo_path = personal_repo_path(repo_name); c.p.send_line(&format!("init {repo_name}"))?; - c.p.exp_string(&format!( - "Successfully created \"git/{user}/{repo_name}.git\"", - ))?; + c.p.exp_string(&format!("Successfully created \"{repo_path}\"",))?; c.p.send_line(&format!( - "set-description \"git/{user}/{repo_name}.git\" \"{description}\"" + "set-description \"{repo_path}\" \"{description}\"" ))?; c.p.exp_string("Successfully updated description")?; expect_prompt(&mut c.p)?; - expect_list_table( - &mut c, - &[( - format!("git/{user}/{repo_name}.git"), - description.to_owned(), - )], - )?; + expect_list_table(&mut c, &[(repo_path, description.to_owned())])?; Ok(()) } @@ -380,21 +387,15 @@ fn can_change_the_description_on_a_repo() -> Result<()> { #[test] fn can_set_the_main_branch_of_a_new_git_repo() -> Result<()> { let mut c = spawn_interactive_process()?; - let username = get_username().unwrap(); let repo_name = "my-new-repo"; let main_branch = "foobar"; c.p.send_line(&format!("init --branch {} {}", main_branch, repo_name))?; c.p.exp_string(&format!( - "Successfully created \"git/{}/{}.git\"", - username, repo_name + "Successfully created \"{}\"", + personal_repo_path(repo_name) ))?; - let repo_dir = c - .workdir - .as_ref() - .join("git") - .join(username) - .join(&format!("{}.git", repo_name)); + let repo_dir = c.personal_repo_dir(repo_name); verify_current_branch(&repo_dir, &format!("refs/heads/{main_branch}")); Ok(()) @@ -403,26 +404,17 @@ fn can_set_the_main_branch_of_a_new_git_repo() -> Result<()> { #[test] fn can_change_the_main_branch_on_a_repo() -> Result<()> { let mut c = spawn_interactive_process()?; - let username = get_username().unwrap(); let repo_name = "my-new-repo"; let main_branch = "foobar"; + let repo_path = personal_repo_path(repo_name); + c.p.send_line(&format!("init {}", repo_name))?; - c.p.exp_string(&format!( - "Successfully created \"git/{}/{}.git\"", - username, repo_name - ))?; + c.p.exp_string(&format!("Successfully created \"{repo_path}\""))?; - c.p.send_line(&format!( - "set-branch \"git/{username}/{repo_name}.git\" \"{main_branch}\"" - ))?; + c.p.send_line(&format!("set-branch \"{repo_path}\" \"{main_branch}\""))?; c.p.exp_string("Successfully updated branch")?; - let repo_dir = c - .workdir - .as_ref() - .join("git") - .join(username) - .join(&format!("{}.git", repo_name)); + let repo_dir = c.personal_repo_dir(repo_name); verify_current_branch(&repo_dir, &format!("refs/heads/{main_branch}")); Ok(()) @@ -431,15 +423,9 @@ fn can_change_the_main_branch_on_a_repo() -> Result<()> { #[test] fn can_delete_a_repo() -> Result<()> { let mut c = spawn_interactive_process()?; - let username = get_username().unwrap(); let repo_name = "an-old-repo"; - let repo_path = format!("git/{username}/{repo_name}.git"); - let repo_dir = c - .workdir - .as_ref() - .join("git") - .join(username) - .join(&format!("{}.git", repo_name)); + let repo_path = personal_repo_path(repo_name); + let repo_dir = c.personal_repo_dir(repo_name); c.p.send_line(&format!("init {}", repo_name))?; c.p.exp_string(&format!("Successfully created \"{repo_path}\""))?; @@ -447,7 +433,6 @@ fn can_delete_a_repo() -> Result<()> { c.p.send_line(&format!("delete \"{repo_path}\""))?; c.p.exp_string(&format!("Successfully deleted \"{repo_path}\""))?; - verify_repo_does_not_exist(&repo_dir); Ok(()) -- cgit v1.2.3