From 50df657f45966bc6cbf4a392a815fcc2c331f888 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 24 Jul 2024 16:09:28 +0100 Subject: test: abstract `list` and `pull` test code to enable improved debugging --- test_utils/src/lib.rs | 82 +++++++++++++++++++++++++++++++++++---------------- tests/list.rs | 2 +- tests/pull.rs | 28 ++++-------------- 3 files changed, 62 insertions(+), 50 deletions(-) diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs index 866eafb..5125d20 100644 --- a/test_utils/src/lib.rs +++ b/test_utils/src/lib.rs @@ -1196,13 +1196,13 @@ pub fn remove_latest_commit_so_proposal_branch_is_behind_and_checkout_main( Ok(branch_name) } -pub fn amend_last_commit(test_repo: &GitTestRepo) -> Result { +pub fn amend_last_commit(test_repo: &GitTestRepo, commit_msg: &str) -> Result { let branch_name = remove_latest_commit_so_proposal_branch_is_behind_and_checkout_main(test_repo)?; // add another commit (so we have an ammened local branch) test_repo.checkout(&branch_name)?; - std::fs::write(test_repo.dir.join("ammended-commit.md"), "some content")?; - test_repo.stage_and_commit("add ammended-commit.md")?; + std::fs::write(test_repo.dir.join("ammended-commit.md"), commit_msg)?; + test_repo.stage_and_commit(commit_msg)?; Ok(branch_name) } @@ -1210,6 +1210,33 @@ pub fn create_proposals_with_first_rebased_and_repo_with_latest_main_and_unrebas -> Result<(GitTestRepo, GitTestRepo)> { let (_, test_repo) = create_proposals_and_repo_with_proposal_pulled_and_checkedout(1)?; + // recreate proposal 1 on top of a another commit (like a rebase on top + // of one extra commit) + let second_originating_repo = GitTestRepo::default(); + second_originating_repo.populate()?; + std::fs::write( + second_originating_repo.dir.join("amazing.md"), + "some content", + )?; + second_originating_repo.stage_and_commit("commit for rebasing on top of")?; + cli_tester_create_proposal( + &second_originating_repo, + FEATURE_BRANCH_NAME_1, + "a", + Some((PROPOSAL_TITLE_1, "proposal a description")), + Some(get_first_proposal_event_id()?.to_string()), + )?; + + // pretend we have pulled the updated main branch + let branch_name = test_repo.get_checked_out_branch_name()?; + test_repo.checkout("main")?; + std::fs::write(test_repo.dir.join("amazing.md"), "some content")?; + test_repo.stage_and_commit("commit for rebasing on top of")?; + test_repo.checkout(&branch_name)?; + Ok((second_originating_repo, test_repo)) +} + +fn get_first_proposal_event_id() -> Result { // get proposal id of first let client = Client::default(); Handle::current().block_on(client.add_relay("ws://localhost:8055"))?; @@ -1236,28 +1263,31 @@ pub fn create_proposals_with_first_rebased_and_repo_with_latest_main_and_unrebas }) .unwrap() .id; - // recreate proposal 1 on top of a another commit (like a rebase on top - // of one extra commit) - let second_originating_repo = GitTestRepo::default(); - second_originating_repo.populate()?; - std::fs::write( - second_originating_repo.dir.join("amazing.md"), - "some content", - )?; - second_originating_repo.stage_and_commit("commit for rebasing on top of")?; - cli_tester_create_proposal( - &second_originating_repo, - FEATURE_BRANCH_NAME_1, - "a", - Some((PROPOSAL_TITLE_1, "proposal a description")), - Some(proposal_1_id.to_string()), - )?; + Ok(proposal_1_id) +} - // pretend we have pulled the updated main branch - let branch_name = test_repo.get_checked_out_branch_name()?; - test_repo.checkout("main")?; - std::fs::write(test_repo.dir.join("amazing.md"), "some content")?; - test_repo.stage_and_commit("commit for rebasing on top of")?; - test_repo.checkout(&branch_name)?; - Ok((second_originating_repo, test_repo)) +pub fn create_proposals_with_first_revised_and_repo_with_unrevised_proposal_checkedout() +-> Result<(GitTestRepo, GitTestRepo)> { + let (originating_repo, test_repo) = + create_proposals_and_repo_with_proposal_pulled_and_checkedout(1)?; + + use_ngit_list_to_download_and_checkout_proposal_branch(&originating_repo, 1)?; + + amend_last_commit(&originating_repo, "add some ammended-commit.md")?; + + let mut p = CliTester::new_from_dir( + &originating_repo.dir, + [ + "--nsec", + TEST_KEY_1_NSEC, + "--password", + TEST_PASSWORD, + "--disable-cli-spinners", + "push", + "--force", + ], + ); + p.expect_end_eventually()?; + + Ok((originating_repo, test_repo)) } diff --git a/tests/list.rs b/tests/list.rs index 9ffc26a..6e509ac 100644 --- a/tests/list.rs +++ b/tests/list.rs @@ -1105,7 +1105,7 @@ mod when_main_branch_is_uptodate { let (_, test_repo) = create_proposals_and_repo_with_proposal_pulled_and_checkedout(1)?; - amend_last_commit(&test_repo)?; + amend_last_commit(&test_repo, "add ammended-commit.md")?; test_repo.checkout("main")?; // run test diff --git a/tests/pull.rs b/tests/pull.rs index 0b0c3ba..057e827 100644 --- a/tests/pull.rs +++ b/tests/pull.rs @@ -331,28 +331,11 @@ 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<()> { - let (originating_repo, test_repo) = - create_proposals_and_repo_with_proposal_pulled_and_checkedout(1)?; + let (o, test_repo) = + create_proposals_with_first_revised_and_repo_with_unrevised_proposal_checkedout()?; + + amend_last_commit(&test_repo, "add different ammended-commit.md")?; - let branch_name = amend_last_commit(&test_repo)?; - - // create and send a revision from another repository - originating_repo.checkout("main")?; - test_repo.checkout("main")?; - test_repo.git_repo.branch( - &branch_name, - &test_repo - .git_repo - .find_commit(test_repo.get_tip_of_local_branch(&branch_name)?)? - .parent(0)?, - true, - )?; - originating_repo.checkout(&branch_name)?; - test_repo.checkout(&branch_name)?; - std::fs::write(test_repo.dir.join("ammended-commit.md"), "some content")?; - test_repo.stage_and_commit("add ammended-commit.md")?; - let mut p = CliTester::new_from_dir(&test_repo.dir, ["push", "--force"]); - p.expect_end_eventually()?; // test when branch is ammended an older version of the proposal let mut p = CliTester::new_from_dir(&test_repo.dir, ["pull"]); p.expect("fetching updates...\r\n")?; @@ -384,7 +367,6 @@ mod when_branch_is_checked_out { r56.listen_until_close(), ); cli_tester_handle.join().unwrap()?; - println!("{:?}", r55.events); Ok(()) } } @@ -419,7 +401,7 @@ mod when_branch_is_checked_out { let (_, test_repo) = create_proposals_and_repo_with_proposal_pulled_and_checkedout(1)?; - amend_last_commit(&test_repo)?; + amend_last_commit(&test_repo, "add ammended-commit.md")?; // run test let mut p = CliTester::new_from_dir(&test_repo.dir, ["pull"]); -- cgit v1.2.3