From 70dd57966969f24fb81aabe412fb3667486f87b5 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 22 Feb 2024 12:58:12 +0000 Subject: test(list): local commits on uptodate proposal add test for scenario and tweak copy --- src/sub_commands/list.rs | 62 +++++++-------- tests/list.rs | 203 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 228 insertions(+), 37 deletions(-) diff --git a/src/sub_commands/list.rs b/src/sub_commands/list.rs index fbf04a9..6c620ad 100644 --- a/src/sub_commands/list.rs +++ b/src/sub_commands/list.rs @@ -369,6 +369,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { }; } + // new proposal revision / rebase // tip of local in proposal history (new, ammended or rebased version but no // local changes) if commits_events.iter().any(|patch| { @@ -436,43 +437,42 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { }; } - // TODO: write tests // tip of proposal in branch in history (local appendments made to up-to-date // proposal) if let Ok((local_ahead_of_proposal, _)) = git_repo.get_commits_ahead_behind(&proposal_tip, &local_branch_tip) { println!( - "local proposal branch exists with {} unpublished commits on top of the most up-to-date version of the proposal", - local_ahead_of_proposal.len() + "local proposal branch exists with {} unpublished commits on top of the most up-to-date version of the proposal ({} ahead {} behind '{main_branch_name}')", + local_ahead_of_proposal.len(), + local_ahead_of_main.len(), + proposal_behind_main.len(), ); return match Interactor::default().choice( - PromptChoiceParms::default().with_default(0) - .with_choices( - vec![ - format!( - "checkout proposal branch with {} unpublished commits ({} ahead {} behind '{main_branch_name}')", - local_ahead_of_proposal.len(), - local_ahead_of_main.len(), - proposal_behind_main.len(), - ), - "back".to_string(), - ], - ) - )? { - 0 => { - git_repo.checkout(&cover_letter.branch_name)?; + PromptChoiceParms::default() + .with_default(0) + .with_choices(vec![ format!( - "checked out proposal branch with {} unpublished commits ({} ahead {} behind '{main_branch_name}')", + "checkout proposal branch with {} unpublished commits", local_ahead_of_proposal.len(), - local_ahead_of_main.len(), - proposal_behind_main.len(), - ); - Ok(()) - - }, - 1 => { continue }, - _ => { bail!("unexpected choice")} + ), + "back".to_string(), + ]), + )? { + 0 => { + git_repo.checkout(&cover_letter.branch_name)?; + println!( + "checked out proposal branch with {} unpublished commits ({} ahead {} behind '{main_branch_name}')", + local_ahead_of_proposal.len(), + local_ahead_of_main.len(), + proposal_behind_main.len(), + ); + Ok(()) + } + 1 => continue, + _ => { + bail!("unexpected choice") + } }; } @@ -516,7 +516,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { 0 => { check_clean(&git_repo)?; git_repo.checkout(&cover_letter.branch_name)?; - format!( + println!( "checked out old proposal in existing branch ({} ahead {} behind '{main_branch_name}')", local_ahead_of_main.len(), local_beind_main.len(), @@ -531,7 +531,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { let _ = git_repo .apply_patch_chain(&cover_letter.branch_name, most_recent_proposal_patch_chain) .context("cannot apply patch chain")?; - format!( + println!( "checked out new version of proposal ({} ahead {} behind '{main_branch_name}'), replacing old version ({} ahead {} behind '{main_branch_name}')", chain_length, proposal_behind_main.len(), @@ -596,7 +596,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { 0 => { check_clean(&git_repo)?; git_repo.checkout(&cover_letter.branch_name)?; - format!( + println!( "checked out old proposal in existing branch ({} ahead {} behind '{main_branch_name}')", local_ahead_of_main.len(), local_beind_main.len(), @@ -611,7 +611,7 @@ pub async fn launch(_cli_args: &Cli, _args: &SubCommandArgs) -> Result<()> { let _ = git_repo .apply_patch_chain(&cover_letter.branch_name, most_recent_proposal_patch_chain) .context("cannot apply patch chain")?; - format!( + println!( "checked out new version of proposal ({} ahead {} behind '{main_branch_name}'), replacing old version ({} ahead {} behind '{main_branch_name}'). consider creating a temporary branch with your existing unchanges first.", chain_length, proposal_behind_main.len(), diff --git a/tests/list.rs b/tests/list.rs index eb6dc3d..3f96da1 100644 --- a/tests/list.rs +++ b/tests/list.rs @@ -1208,14 +1208,205 @@ mod when_main_branch_is_uptodate { } } - mod when_branch_is_ahead { - // use super::*; - // TODO latest commit in proposal builds off an older commit in - // proposal instead of previous. - // TODO current git user created commit on branch + mod when_local_commits_on_uptodate_proposal { + 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"]); + + create_and_populate_branch( + &test_repo, + FEATURE_BRANCH_NAME_1, + "a", + false, + )?; + // add appended commit to local branch + std::fs::write(test_repo.dir.join("appended.md"), "some content")?; + test_repo.stage_and_commit("appended commit")?; + + 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( + "local proposal branch exists with 1 unpublished commits on top of the most up-to-date version of the proposal (3 ahead 0 behind 'main')\r\n", + )?; + + let mut c = p.expect_choice( + "", + vec![ + format!("checkout proposal branch with 1 unpublished commits"), + format!("back"), + ], + )?; + c.succeeds_with(0, false, Some(0))?; + p.expect("checked out proposal branch with 1 unpublished commits (3 ahead 0 behind 'main')\r\n")?; + p.expect_end()?; + + 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"]); + + create_and_populate_branch( + &test_repo, + FEATURE_BRANCH_NAME_1, + "a", + false, + )?; + // add appended commit to local branch + std::fs::write(test_repo.dir.join("appended.md"), "some content")?; + test_repo.stage_and_commit("appended commit")?; + + 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( + "local proposal branch exists with 1 unpublished commits on top of the most up-to-date version of the proposal (3 ahead 0 behind 'main')\r\n", + )?; + + let mut c = p.expect_choice( + "", + vec![ + format!("checkout proposal branch with 1 unpublished commits"), + format!("back"), + ], + )?; + c.succeeds_with(0, false, Some(0))?; + p.expect("checked out proposal branch with 1 unpublished commits (3 ahead 0 behind 'main')\r\n")?; + p.expect_end()?; + + 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 prompts_to_choose_from_proposal_titles() -> Result<()> { + let _ = run_async_prompts_to_choose_from_proposal_titles().await; + Ok(()) + } + } + + #[tokio::test] + #[serial] + async fn proposal_branch_checked_out() -> Result<()> { + let (_, test_repo) = prep_and_run().await?; + assert_eq!( + FEATURE_BRANCH_NAME_1, + test_repo.get_checked_out_branch_name()?, + ); + Ok(()) + } + + #[tokio::test] + #[serial] + async fn didnt_overwrite_local_appendments() -> Result<()> { + let (originating_repo, test_repo) = prep_and_run().await?; + assert_ne!( + 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_latest_event_rebases_branch { + mod when_latest_revision_rebases_branch { use std::time::Duration; use super::*; -- cgit v1.2.3