summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2023-07-03 21:08:24 +0200
committerJustin Wernick <justin@worthe-it.co.za>2023-07-03 21:08:24 +0200
commitff05e4ab91b3b84ffb04b3d8089052112e7bd51c (patch)
tree65d40250f0597c1bf747dee0e7c1e47539d5b6c4
parent205bc70cb1bfc692f8e412d34c8452ac11aedceb (diff)
Extract common test code into function
-rw-r--r--tests/cli.rs89
1 files changed, 32 insertions, 57 deletions
diff --git a/tests/cli.rs b/tests/cli.rs
index a814975..12e701f 100644
--- a/tests/cli.rs
+++ b/tests/cli.rs
@@ -30,6 +30,17 @@ impl TestContext {
.join(group)
.join(&format!("{}.git", repo_name))
}
+
+ fn expect_prompt(&mut self) -> Result<()> {
+ self.p.exp_string("> ")?;
+ Ok(())
+ }
+
+ fn expect_successful_init_message(&mut self, repo_path: &str) -> Result<()> {
+ self.p
+ .exp_string(&format!("Successfully created \"{repo_path}\""))?;
+ self.expect_prompt()
+ }
}
fn personal_repo_path(repo_name: &str) -> String {
@@ -51,9 +62,10 @@ fn spawn_interactive_process() -> Result<TestContext> {
let path = cargo_bin(env!("CARGO_PKG_NAME"));
let mut command = std::process::Command::new(&path);
command.current_dir(&workdir);
- let mut p = spawn_command(command, Some(3000))?;
- expect_prompt(&mut p)?;
- Ok(TestContext { p, workdir })
+ let p = spawn_command(command, Some(3000))?;
+ let mut c = TestContext { p, workdir };
+ c.expect_prompt()?;
+ Ok(c)
}
fn run_batch_command(batch_command: &str) -> Result<TestContext> {
@@ -68,11 +80,6 @@ fn run_batch_command(batch_command: &str) -> Result<TestContext> {
Ok(TestContext { p, workdir })
}
-fn expect_prompt(p: &mut PtySession) -> Result<()> {
- p.exp_string("> ")?;
- Ok(())
-}
-
#[test]
fn shows_a_prompt() -> Result<()> {
spawn_interactive_process()?;
@@ -83,9 +90,9 @@ fn shows_a_prompt() -> Result<()> {
fn does_nothing_after_receiving_whitespace_input() -> Result<()> {
let mut c = spawn_interactive_process()?;
c.p.send_line("")?;
- expect_prompt(&mut c.p)?;
+ c.expect_prompt()?;
c.p.send_line(" ")?;
- expect_prompt(&mut c.p)?;
+ c.expect_prompt()?;
Ok(())
}
@@ -110,7 +117,7 @@ fn reports_error_with_unsupported_shell_commands() -> Result<()> {
let mut c = spawn_interactive_process()?;
c.p.send_line("ls")?;
c.p.exp_string("error: unrecognized subcommand 'ls'")?;
- expect_prompt(&mut c.p)?;
+ c.expect_prompt()?;
Ok(())
}
@@ -119,7 +126,7 @@ fn reports_error_with_nonsense_input() -> Result<()> {
let mut c = spawn_interactive_process()?;
c.p.send_line(" asd fg ")?;
c.p.exp_string("error: unrecognized subcommand 'asd'")?;
- expect_prompt(&mut c.p)?;
+ c.expect_prompt()?;
Ok(())
}
@@ -166,11 +173,7 @@ fn verify_repo_config_value(repo_dir: &Path, config_key: &str, config_value: Opt
fn can_init_a_new_git_repo() -> Result<()> {
let mut c = spawn_interactive_process()?;
c.p.send_line(&format!("init {}", REPO_NAME))?;
- c.p.exp_string(&format!(
- "Successfully created \"{}\"",
- personal_repo_path(REPO_NAME)
- ))?;
- expect_prompt(&mut c.p)?;
+ c.expect_successful_init_message(&personal_repo_path(REPO_NAME))?;
let repo_dir = c.personal_repo_dir(REPO_NAME);
verify_repo_exists(&repo_dir);
@@ -185,11 +188,7 @@ fn can_init_a_new_shared_git_repo() -> Result<()> {
let mut c = spawn_interactive_process()?;
let group = arbitrary_user_group();
c.p.send_line(&format!("init --group {} {}", group, REPO_NAME))?;
- c.p.exp_string(&format!(
- "Successfully created \"{}\"",
- group_repo_path(&group, REPO_NAME)
- ))?;
- expect_prompt(&mut c.p)?;
+ c.expect_successful_init_message(&group_repo_path(&group, REPO_NAME))?;
let repo_dir = c.group_repo_dir(&group, REPO_NAME);
verify_repo_exists(&repo_dir);
@@ -223,10 +222,7 @@ fn runs_a_single_command_and_exit_with_cli_flag() -> Result<()> {
fn allows_quotes_arguments() -> Result<()> {
let mut c = spawn_interactive_process()?;
c.p.send_line(&format!("\"init\" '{REPO_NAME}'"))?;
- c.p.exp_string(&format!(
- "Successfully created \"{}\"",
- personal_repo_path(REPO_NAME)
- ))?;
+ c.expect_successful_init_message(&personal_repo_path(REPO_NAME))?;
Ok(())
}
@@ -251,10 +247,7 @@ fn allows_single_quotes_and_spaces_inside_double_quotes() -> Result<()> {
let repo_name = "shukkie's new repo";
let mut c = spawn_interactive_process()?;
c.p.send_line(&format!("init \"{repo_name}\""))?;
- c.p.exp_string(&format!(
- "Successfully created \"{}\"",
- personal_repo_path(repo_name)
- ))?;
+ c.expect_successful_init_message(&personal_repo_path(repo_name))?;
Ok(())
}
@@ -274,7 +267,7 @@ fn expect_list_table(c: &mut TestContext, repos: &[(String, String)]) -> Result<
c.p.exp_regex(r" +\|")?;
}
c.p.exp_regex(r"\+-+\+-+\+")?;
- expect_prompt(&mut c.p)?;
+ c.expect_prompt()?;
Ok(())
}
@@ -290,11 +283,7 @@ fn list_can_print_an_empty_list() -> Result<()> {
fn list_can_print_a_list_of_personal_repos_with_descriptions() -> Result<()> {
let mut c = spawn_interactive_process()?;
c.p.send_line(&format!("init {}", REPO_NAME))?;
- c.p.exp_string(&format!(
- "Successfully created \"{}\"",
- personal_repo_path(REPO_NAME)
- ))?;
- expect_prompt(&mut c.p)?;
+ c.expect_successful_init_message(&personal_repo_path(REPO_NAME))?;
expect_list_table(
&mut c,
@@ -311,19 +300,11 @@ fn list_can_print_a_list_of_personal_repos_with_descriptions() -> Result<()> {
fn list_can_print_a_list_of_all_repos_with_descriptions() -> Result<()> {
let mut c = spawn_interactive_process()?;
c.p.send_line(&format!("init {}", REPO_NAME))?;
- c.p.exp_string(&format!(
- "Successfully created \"{}\"",
- personal_repo_path(REPO_NAME)
- ))?;
- expect_prompt(&mut c.p)?;
+ c.expect_successful_init_message(&personal_repo_path(REPO_NAME))?;
let group = arbitrary_user_group();
c.p.send_line(&format!("init --group {} {}", group, REPO_NAME_2))?;
- c.p.exp_string(&format!(
- "Successfully created \"{}\"",
- group_repo_path(&group, REPO_NAME_2)
- ))?;
- expect_prompt(&mut c.p)?;
+ c.expect_successful_init_message(&group_repo_path(&group, REPO_NAME_2))?;
expect_list_table(
&mut c,
@@ -347,10 +328,7 @@ fn can_set_the_description_on_a_repo_during_init() -> Result<()> {
let mut c = spawn_interactive_process()?;
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 \"{}\"",
- personal_repo_path(REPO_NAME)
- ))?;
+ c.expect_successful_init_message(&personal_repo_path(REPO_NAME))?;
expect_list_table(
&mut c,
@@ -371,7 +349,7 @@ fn can_change_the_description_on_a_repo() -> Result<()> {
"set-description \"{repo_path}\" \"{description}\""
))?;
c.p.exp_string("Successfully updated description")?;
- expect_prompt(&mut c.p)?;
+ c.expect_prompt()?;
expect_list_table(&mut c, &[(repo_path, description.to_owned())])?;
Ok(())
@@ -382,10 +360,7 @@ fn can_set_the_main_branch_of_a_new_git_repo() -> Result<()> {
let mut c = spawn_interactive_process()?;
let main_branch = "foobar";
c.p.send_line(&format!("init --branch {} {}", main_branch, REPO_NAME))?;
- c.p.exp_string(&format!(
- "Successfully created \"{}\"",
- personal_repo_path(REPO_NAME)
- ))?;
+ c.expect_successful_init_message(&personal_repo_path(REPO_NAME))?;
let repo_dir = c.personal_repo_dir(REPO_NAME);
verify_current_branch(&repo_dir, &format!("refs/heads/{main_branch}"));
@@ -400,7 +375,7 @@ fn can_change_the_main_branch_on_a_repo() -> Result<()> {
let repo_path = personal_repo_path(REPO_NAME);
c.p.send_line(&format!("init {}", REPO_NAME))?;
- c.p.exp_string(&format!("Successfully created \"{repo_path}\""))?;
+ c.expect_successful_init_message(&repo_path)?;
c.p.send_line(&format!("set-branch \"{repo_path}\" \"{main_branch}\""))?;
c.p.exp_string("Successfully updated branch")?;
@@ -418,7 +393,7 @@ fn can_delete_a_repo() -> Result<()> {
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}\""))?;
+ c.expect_successful_init_message(&repo_path)?;
verify_repo_exists(&repo_dir);
c.p.send_line(&format!("delete \"{repo_path}\""))?;