From b6f68cf782ee242f9390c6601a76a18367e2ab4d Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 22 Feb 2024 16:25:38 +0000 Subject: test(list): local proposal amended added test to cover local propsal being amended this should catch error for rebased proposals as well --- tests/list.rs | 211 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 209 insertions(+), 2 deletions(-) diff --git a/tests/list.rs b/tests/list.rs index 3f96da1..08c2e93 100644 --- a/tests/list.rs +++ b/tests/list.rs @@ -1208,6 +1208,215 @@ mod when_main_branch_is_uptodate { } } + mod when_latest_proposal_ammended_locally { + // other rebase scenarios should work if this test passes + use super::*; + async fn prep_and_run() -> Result<(GitTestRepo, GitTestRepo)> { + // fallback (51,52) user write (53, 55) repo (55, 56) + let (mut r51, mut r52, mut r53, mut r55, mut r56) = ( + Relay::new(8051, None, None), + Relay::new(8052, None, None), + Relay::new(8053, None, None), + Relay::new(8055, None, None), + Relay::new(8056, None, None), + ); + + r51.events.push(generate_test_key_1_relay_list_event()); + r51.events.push(generate_test_key_1_metadata_event("fred")); + r51.events.push(generate_repo_ref_event()); + + r55.events.push(generate_repo_ref_event()); + r55.events.push(generate_test_key_1_metadata_event("fred")); + r55.events.push(generate_test_key_1_relay_list_event()); + + 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 mut p = CliTester::new_from_dir(&test_repo.dir, ["list"]); + + // simulating checking out the proposal (the commits_ids will match) + create_and_populate_branch( + &test_repo, + "different-branch-name", + "a", + false, + )?; + // simulating ammending the proposal + create_and_populate_branch( + &test_repo, + FEATURE_BRANCH_NAME_1, + "a-changed", + false, + )?; + + test_repo.checkout("main")?; + p.expect("finding proposals...\r\n")?; + let mut c = p.expect_choice( + "all proposals", + vec![ + format!("\"{PROPOSAL_TITLE_1}\""), + format!("\"{PROPOSAL_TITLE_2}\""), + format!("\"{PROPOSAL_TITLE_3}\""), + ], + )?; + c.succeeds_with(0, true, None)?; + p.expect("finding commits...\r\n")?; + p.expect_eventually("--force`\r\n")?; + + let mut c = p.expect_choice( + "", + vec![ + format!("checkout local branch with unpublished changes"), + format!( + "discard unpublished changes and checkout new revision" + ), + format!("apply to current branch with `git am`"), + format!("download to ./patches"), + "back".to_string(), + ], + )?; + c.succeeds_with(1, false, Some(0))?; + + p.expect_end_eventually_and_print()?; + + for p in [51, 52, 53, 55, 56] { + relay::shutdown_relay(8000 + p)?; + } + Ok((originating_repo, test_repo)) + }); + // launch relay + let _ = join!( + r51.listen_until_close(), + r52.listen_until_close(), + r53.listen_until_close(), + r55.listen_until_close(), + r56.listen_until_close(), + ); + let res = cli_tester_handle.join().unwrap()?; + + Ok(res) + } + + mod cli_prompts { + use super::*; + async fn run_async_prompts_to_choose_from_proposal_titles() -> Result<()> { + let (mut r51, mut r52, mut r53, mut r55, mut r56) = ( + Relay::new(8051, None, None), + Relay::new(8052, None, None), + Relay::new(8053, None, None), + Relay::new(8055, None, None), + Relay::new(8056, None, None), + ); + + r51.events.push(generate_test_key_1_relay_list_event()); + r51.events.push(generate_test_key_1_metadata_event("fred")); + r51.events.push(generate_repo_ref_event()); + + r55.events.push(generate_repo_ref_event()); + r55.events.push(generate_test_key_1_metadata_event("fred")); + 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()?; + let mut p = CliTester::new_from_dir(&test_repo.dir, ["list"]); + + // simulating checking out the proposal (the commits_ids will match) + create_and_populate_branch( + &test_repo, + "different-branch-name", + "a", + false, + )?; + // simulating ammending the proposal + create_and_populate_branch( + &test_repo, + FEATURE_BRANCH_NAME_1, + "a-changed", + false, + )?; + + test_repo.checkout("main")?; + p.expect("finding proposals...\r\n")?; + let mut c = p.expect_choice( + "all proposals", + vec![ + format!("\"{PROPOSAL_TITLE_1}\""), + format!("\"{PROPOSAL_TITLE_2}\""), + format!("\"{PROPOSAL_TITLE_3}\""), + ], + )?; + c.succeeds_with(0, true, None)?; + p.expect("finding commits...\r\n")?; + p.expect("you have an ammended/rebase version the proposal that is unpublished\r\n")?; + p.expect("you have previously applied the latest version of the proposal (2 ahead 0 behind 'main') but your local proposal branch has ammended or rebased it (2 ahead 0 behind 'main')\r\n")?; + p.expect("to view the latest proposal but retain your changes:\r\n")?; + p.expect(" 1) create a new branch off the tip commit of this one to store your changes\r\n")?; + p.expect(" 2) run `ngit list` and checkout the latest published version of this proposal\r\n")?; + p.expect("if you are confident in your changes consider running `ngit push --force`\r\n")?; + + let mut c = p.expect_choice( + "", + vec![ + format!("checkout local branch with unpublished changes"), + format!( + "discard unpublished changes and checkout new revision" + ), + format!("apply to current branch with `git am`"), + format!("download to ./patches"), + "back".to_string(), + ], + )?; + c.succeeds_with(1, false, Some(1))?; + p.expect_end_with("checked out latest version of proposal (2 ahead 0 behind 'main'), replacing unpublished version (2 ahead 0 behind 'main')\r\n")?; + + for p in [51, 52, 53, 55, 56] { + relay::shutdown_relay(8000 + p)?; + } + Ok(()) + }); + + // launch relay + let _ = join!( + r51.listen_until_close(), + r52.listen_until_close(), + r53.listen_until_close(), + r55.listen_until_close(), + r56.listen_until_close(), + ); + cli_tester_handle.join().unwrap()?; + println!("{:?}", r55.events); + Ok(()) + } + + #[tokio::test] + #[serial] + async fn out_reflects_second_choice_discarding_old_and_applying_new() + -> Result<()> { + let _ = run_async_prompts_to_choose_from_proposal_titles().await; + Ok(()) + } + } + + #[tokio::test] + #[serial] + async fn second_choice_discarded_unpublished_commits_and_checked_out_latest_revision() + -> Result<()> { + let (originating_repo, test_repo) = prep_and_run().await?; + println!("test_dir: {:?}", test_repo.dir); + assert_eq!( + test_repo.get_tip_of_local_branch(FEATURE_BRANCH_NAME_1)?, + originating_repo.get_tip_of_local_branch(FEATURE_BRANCH_NAME_1)?, + ); + Ok(()) + } + } + mod when_local_commits_on_uptodate_proposal { use super::*; async fn prep_and_run() -> Result<(GitTestRepo, GitTestRepo)> { @@ -1627,8 +1836,6 @@ mod when_main_branch_is_uptodate { c.succeeds_with(0, true, None)?; p.expect("finding commits...\r\n")?; p.expect("updated proposal available (2 ahead 0 behind 'main'). existing version is 2 ahead 1 behind 'main'\r\n")?; - // its got here but tmpgit-32.. indicates that creatubg the eature - // branch in line 1291 didnt work so the choices will be different let mut c = p.expect_choice( "", vec![ -- cgit v1.2.3