From 6d086a6608652193898ec548382e780733ea2f5a Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 24 Jul 2024 10:58:07 +0100 Subject: test: refactor `pull` and `push` to abstract code into lib which makes reading and maintaining tests easier --- tests/push.rs | 279 +++------------------------------------------------------- 1 file changed, 12 insertions(+), 267 deletions(-) (limited to 'tests/push.rs') diff --git a/tests/push.rs b/tests/push.rs index b771d4b..e300fe2 100644 --- a/tests/push.rs +++ b/tests/push.rs @@ -3,94 +3,6 @@ use futures::join; use serial_test::serial; use test_utils::{git::GitTestRepo, relay::Relay, *}; -static FEATURE_BRANCH_NAME_1: &str = "feature-example-t"; -static FEATURE_BRANCH_NAME_2: &str = "feature-example-f"; -static FEATURE_BRANCH_NAME_3: &str = "feature-example-c"; - -static PROPOSAL_TITLE_1: &str = "proposal a"; -static PROPOSAL_TITLE_2: &str = "proposal b"; -static PROPOSAL_TITLE_3: &str = "proposal c"; - -fn cli_tester_create_proposals() -> Result { - let git_repo = GitTestRepo::default(); - git_repo.populate()?; - cli_tester_create_proposal( - &git_repo, - FEATURE_BRANCH_NAME_1, - "a", - PROPOSAL_TITLE_1, - "proposal a description", - )?; - cli_tester_create_proposal( - &git_repo, - FEATURE_BRANCH_NAME_2, - "b", - PROPOSAL_TITLE_2, - "proposal b description", - )?; - cli_tester_create_proposal( - &git_repo, - FEATURE_BRANCH_NAME_3, - "c", - PROPOSAL_TITLE_3, - "proposal c description", - )?; - Ok(git_repo) -} - -fn create_and_populate_branch( - test_repo: &GitTestRepo, - branch_name: &str, - prefix: &str, - only_one_commit: bool, -) -> Result<()> { - test_repo.checkout("main")?; - test_repo.create_branch(branch_name)?; - test_repo.checkout(branch_name)?; - std::fs::write( - test_repo.dir.join(format!("{}3.md", prefix)), - "some content", - )?; - test_repo.stage_and_commit(format!("add {}3.md", prefix).as_str())?; - if !only_one_commit { - std::fs::write( - test_repo.dir.join(format!("{}4.md", prefix)), - "some content", - )?; - test_repo.stage_and_commit(format!("add {}4.md", prefix).as_str())?; - } - Ok(()) -} - -fn cli_tester_create_proposal( - test_repo: &GitTestRepo, - branch_name: &str, - prefix: &str, - title: &str, - description: &str, -) -> Result<()> { - create_and_populate_branch(test_repo, branch_name, prefix, false)?; - - let mut p = CliTester::new_from_dir( - &test_repo.dir, - [ - "--nsec", - TEST_KEY_1_NSEC, - "--password", - TEST_PASSWORD, - "--disable-cli-spinners", - "send", - "HEAD~2", - "--title", - format!("\"{title}\"").as_str(), - "--description", - format!("\"{description}\"").as_str(), - ], - ); - p.expect_end_eventually()?; - Ok(()) -} - mod when_main_is_checked_out { use super::*; @@ -196,37 +108,8 @@ mod when_branch_is_checked_out { r55.events.push(generate_test_key_1_relay_list_event()); let cli_tester_handle = std::thread::spawn(move || -> Result<()> { - cli_tester_create_proposals()?; - - let test_repo = GitTestRepo::default(); - test_repo.populate()?; - - // create proposal branch - let mut p = CliTester::new_from_dir(&test_repo.dir, ["list"]); - p.expect("fetching updates...\r\n")?; - p.expect_eventually("\r\n")?; // some updates listed here - let mut c = p.expect_choice( - "all proposals", - vec![ - format!("\"{PROPOSAL_TITLE_3}\""), - format!("\"{PROPOSAL_TITLE_2}\""), - format!("\"{PROPOSAL_TITLE_1}\""), - ], - )?; - c.succeeds_with(2, true, None)?; - let mut c = p.expect_choice( - "", - vec![ - format!( - "create and checkout proposal branch (2 ahead 0 behind 'main')" - ), - format!("apply to current branch with `git am`"), - format!("download to ./patches"), - format!("back"), - ], - )?; - c.succeeds_with(0, false, Some(0))?; - p.expect_end_eventually()?; + let (_, test_repo) = + create_proposals_and_repo_with_first_proposal_pulled_and_checkedout()?; let mut p = CliTester::new_from_dir(&test_repo.dir, ["push"]); p.expect("fetching updates...\r\n")?; @@ -279,37 +162,8 @@ mod when_branch_is_checked_out { r55.events.push(generate_test_key_1_relay_list_event()); let cli_tester_handle = std::thread::spawn(move || -> Result<()> { - cli_tester_create_proposals()?; - - let test_repo = GitTestRepo::default(); - test_repo.populate()?; - - // create proposal branch - let mut p = CliTester::new_from_dir(&test_repo.dir, ["list"]); - p.expect("fetching updates...\r\n")?; - p.expect_eventually("\r\n")?; // some updates listed here - let mut c = p.expect_choice( - "all proposals", - vec![ - format!("\"{PROPOSAL_TITLE_3}\""), - format!("\"{PROPOSAL_TITLE_2}\""), - format!("\"{PROPOSAL_TITLE_1}\""), - ], - )?; - c.succeeds_with(2, true, None)?; - let mut c = p.expect_choice( - "", - vec![ - format!( - "create and checkout proposal branch (2 ahead 0 behind 'main')" - ), - format!("apply to current branch with `git am`"), - format!("download to ./patches"), - format!("back"), - ], - )?; - c.succeeds_with(0, false, Some(0))?; - p.expect_end_eventually()?; + let (_, test_repo) = + create_proposals_and_repo_with_first_proposal_pulled_and_checkedout()?; // remove latest commit so it is behind let branch_name = test_repo.get_checked_out_branch_name()?; @@ -380,37 +234,9 @@ mod when_branch_is_checked_out { let cli_tester_handle = std::thread::spawn(move || -> Result<(GitTestRepo, GitTestRepo)> { - let originating_repo = cli_tester_create_proposals()?; - - let test_repo = GitTestRepo::default(); - test_repo.populate()?; + let (originating_repo, test_repo) = + create_proposals_and_repo_with_first_proposal_pulled_and_checkedout()?; - // create proposal branch - let mut p = CliTester::new_from_dir(&test_repo.dir, ["list"]); - p.expect("fetching updates...\r\n")?; - p.expect_eventually("\r\n")?; // some updates listed here - let mut c = p.expect_choice( - "all proposals", - vec![ - format!("\"{PROPOSAL_TITLE_3}\""), - format!("\"{PROPOSAL_TITLE_2}\""), - format!("\"{PROPOSAL_TITLE_1}\""), - ], - )?; - c.succeeds_with(2, true, None)?; - let mut c = p.expect_choice( - "", - vec![ - format!( - "create and checkout proposal branch (2 ahead 0 behind 'main')" - ), - format!("apply to current branch with `git am`"), - format!("download to ./patches"), - format!("back"), - ], - )?; - c.succeeds_with(0, false, Some(0))?; - p.expect_end_eventually()?; // add another commit (so we have an ammened local branch) std::fs::write(test_repo.dir.join("ammended-commit.md"), "some content")?; test_repo.stage_and_commit("add ammended-commit.md")?; @@ -488,35 +314,9 @@ mod when_branch_is_checked_out { r55.events.push(generate_test_key_1_relay_list_event()); let cli_tester_handle = std::thread::spawn(move || -> Result { - cli_tester_create_proposals()?; + let (_, test_repo) = + create_proposals_and_repo_with_first_proposal_pulled_and_checkedout()?; - let test_repo = GitTestRepo::default(); - test_repo.populate()?; - - // create proposal branch - let mut p = CliTester::new_from_dir(&test_repo.dir, ["list"]); - p.expect("fetching updates...\r\n")?; - p.expect_eventually("\r\n")?; // some updates listed here - let mut c = p.expect_choice( - "all proposals", - vec![ - format!("\"{PROPOSAL_TITLE_3}\""), - format!("\"{PROPOSAL_TITLE_2}\""), - format!("\"{PROPOSAL_TITLE_1}\""), - ], - )?; - c.succeeds_with(2, true, None)?; - let mut c = p.expect_choice( - "", - vec![ - format!("create and checkout proposal branch (2 ahead 0 behind 'main')"), - format!("apply to current branch with `git am`"), - format!("download to ./patches"), - format!("back"), - ], - )?; - c.succeeds_with(0, false, Some(0))?; - p.expect_end_eventually()?; // add another commit (so we have an ammened local branch) std::fs::write(test_repo.dir.join("ammended-commit.md"), "some content")?; test_repo.stage_and_commit("add ammended-commit.md")?; @@ -597,36 +397,8 @@ mod when_branch_is_checked_out { r55.events.push(generate_test_key_1_relay_list_event()); let cli_tester_handle = std::thread::spawn(move || -> Result<()> { - cli_tester_create_proposals()?; - - // discover proposal branch name - let tmp_repo = GitTestRepo::default(); - tmp_repo.populate()?; - let mut p = CliTester::new_from_dir(&tmp_repo.dir, ["list"]); - p.expect("fetching updates...\r\n")?; - p.expect_eventually("\r\n")?; // some updates listed here - let mut c = p.expect_choice( - "all proposals", - vec![ - format!("\"{PROPOSAL_TITLE_3}\""), - format!("\"{PROPOSAL_TITLE_2}\""), - format!("\"{PROPOSAL_TITLE_1}\""), - ], - )?; - c.succeeds_with(2, true, None)?; - let mut c = p.expect_choice( - "", - vec![ - format!( - "create and checkout proposal branch (2 ahead 0 behind 'main')" - ), - format!("apply to current branch with `git am`"), - format!("download to ./patches"), - format!("back"), - ], - )?; - c.succeeds_with(0, false, Some(0))?; - p.expect_end_eventually()?; + let (_, tmp_repo) = + create_proposals_and_repo_with_first_proposal_pulled_and_checkedout()?; let branch_name = tmp_repo.get_checked_out_branch_name()?; let test_repo = GitTestRepo::default(); @@ -691,35 +463,8 @@ mod when_branch_is_checked_out { r55.events.push(generate_test_key_1_relay_list_event()); let cli_tester_handle = std::thread::spawn(move || -> Result<()> { - cli_tester_create_proposals()?; - // discover proposal branch name - let tmp_repo = GitTestRepo::default(); - tmp_repo.populate()?; - let mut p = CliTester::new_from_dir(&tmp_repo.dir, ["list"]); - p.expect("fetching updates...\r\n")?; - p.expect_eventually("\r\n")?; // some updates listed here - let mut c = p.expect_choice( - "all proposals", - vec![ - format!("\"{PROPOSAL_TITLE_3}\""), - format!("\"{PROPOSAL_TITLE_2}\""), - format!("\"{PROPOSAL_TITLE_1}\""), - ], - )?; - c.succeeds_with(2, true, None)?; - let mut c = p.expect_choice( - "", - vec![ - format!( - "create and checkout proposal branch (2 ahead 0 behind 'main')" - ), - format!("apply to current branch with `git am`"), - format!("download to ./patches"), - format!("back"), - ], - )?; - c.succeeds_with(0, false, Some(0))?; - p.expect_end_eventually()?; + let (_, tmp_repo) = + create_proposals_and_repo_with_first_proposal_pulled_and_checkedout()?; let branch_name = tmp_repo.get_checked_out_branch_name()?; let test_repo = GitTestRepo::default(); -- cgit v1.2.3