diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2023-10-01 00:00:00 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2023-10-01 00:00:00 +0100 |
| commit | 6e9245542f070c39a1975f0d53d88913c4ac667d (patch) | |
| tree | 835c1d3db05f76f437c5d8ebc5591f1796cdab60 /tests | |
| parent | aa48a626c08cec353d5563a8831239d2e69c9f3d (diff) | |
feat(prs-create) find commits and create events
- identify commits
- create pull request event
- create patch events
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/prs_create.rs | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/tests/prs_create.rs b/tests/prs_create.rs new file mode 100644 index 0000000..d598e34 --- /dev/null +++ b/tests/prs_create.rs | |||
| @@ -0,0 +1,163 @@ | |||
| 1 | use anyhow::Result; | ||
| 2 | use serial_test::serial; | ||
| 3 | use test_utils::{git::GitTestRepo, *}; | ||
| 4 | |||
| 5 | #[test] | ||
| 6 | fn when_to_branch_doesnt_exist_return_error() -> Result<()> { | ||
| 7 | let test_repo = GitTestRepo::default(); | ||
| 8 | test_repo.populate()?; | ||
| 9 | let mut p = CliTester::new_from_dir( | ||
| 10 | &test_repo.dir, | ||
| 11 | ["prs", "create", "--to-branch", "nonexistant"], | ||
| 12 | ); | ||
| 13 | p.expect("Error: cannot find to_branch 'nonexistant'")?; | ||
| 14 | Ok(()) | ||
| 15 | } | ||
| 16 | |||
| 17 | #[test] | ||
| 18 | fn when_no_to_branch_specified_and_no_main_or_master_branch_return_error() -> Result<()> { | ||
| 19 | let test_repo = GitTestRepo::new("notmain")?; | ||
| 20 | test_repo.populate()?; | ||
| 21 | let mut p = CliTester::new_from_dir(&test_repo.dir, ["prs", "create"]); | ||
| 22 | p.expect("Error: a destination branch (to_branch) is not specified and the defaults (main or master) do not exist")?; | ||
| 23 | Ok(()) | ||
| 24 | } | ||
| 25 | |||
| 26 | #[test] | ||
| 27 | fn when_from_branch_doesnt_exist_return_error() -> Result<()> { | ||
| 28 | let test_repo = GitTestRepo::default(); | ||
| 29 | test_repo.populate()?; | ||
| 30 | let mut p = CliTester::new_from_dir( | ||
| 31 | &test_repo.dir, | ||
| 32 | ["prs", "create", "--from-branch", "nonexistant"], | ||
| 33 | ); | ||
| 34 | p.expect("Error: cannot find from_branch 'nonexistant'")?; | ||
| 35 | Ok(()) | ||
| 36 | } | ||
| 37 | |||
| 38 | #[test] | ||
| 39 | fn when_no_commits_ahead_of_main_return_error() -> Result<()> { | ||
| 40 | let test_repo = GitTestRepo::default(); | ||
| 41 | test_repo.populate()?; | ||
| 42 | // create feature branch with 1 commit ahead | ||
| 43 | test_repo.create_branch("feature")?; | ||
| 44 | test_repo.checkout("feature")?; | ||
| 45 | |||
| 46 | let mut p = CliTester::new_from_dir(&test_repo.dir, ["prs", "create"]); | ||
| 47 | p.expect("Error: 'head' is 0 commits ahead of 'main' so no patches were created")?; | ||
| 48 | Ok(()) | ||
| 49 | } | ||
| 50 | |||
| 51 | mod when_commits_behind_ask_to_proceed { | ||
| 52 | use super::*; | ||
| 53 | |||
| 54 | fn prep_test_repo() -> Result<GitTestRepo> { | ||
| 55 | let test_repo = GitTestRepo::default(); | ||
| 56 | test_repo.populate()?; | ||
| 57 | // create feature branch with 2 commit ahead | ||
| 58 | test_repo.create_branch("feature")?; | ||
| 59 | test_repo.checkout("feature")?; | ||
| 60 | std::fs::write(test_repo.dir.join("t3.md"), "some content")?; | ||
| 61 | test_repo.stage_and_commit("add t3.md")?; | ||
| 62 | std::fs::write(test_repo.dir.join("t4.md"), "some content")?; | ||
| 63 | test_repo.stage_and_commit("add t4.md")?; | ||
| 64 | // checkout main and add 1 commit | ||
| 65 | test_repo.checkout("main")?; | ||
| 66 | std::fs::write(test_repo.dir.join("t5.md"), "some content")?; | ||
| 67 | test_repo.stage_and_commit("add t5.md")?; | ||
| 68 | // checkout feature branch | ||
| 69 | test_repo.checkout("feature")?; | ||
| 70 | Ok(test_repo) | ||
| 71 | } | ||
| 72 | static BEHIND_LEN: u8 = 1; | ||
| 73 | static AHEAD_LEN: u8 = 2; | ||
| 74 | |||
| 75 | fn expect_confirm_prompt( | ||
| 76 | p: &mut CliTester, | ||
| 77 | behind: u8, | ||
| 78 | ahead: u8, | ||
| 79 | ) -> Result<CliTesterConfirmPrompt> { | ||
| 80 | p.expect_confirm( | ||
| 81 | format!("'head' is {behind} commits behind 'main' and {ahead} ahead. Consider rebasing before sending patches. Proceed anyway?").as_str(), | ||
| 82 | Some(false), | ||
| 83 | ) | ||
| 84 | } | ||
| 85 | |||
| 86 | #[test] | ||
| 87 | fn asked_with_default_no() -> Result<()> { | ||
| 88 | let test_repo = prep_test_repo()?; | ||
| 89 | |||
| 90 | let mut p = CliTester::new_from_dir(&test_repo.dir, ["prs", "create"]); | ||
| 91 | expect_confirm_prompt(&mut p, BEHIND_LEN, AHEAD_LEN)?; | ||
| 92 | p.exit()?; | ||
| 93 | Ok(()) | ||
| 94 | } | ||
| 95 | |||
| 96 | #[test] | ||
| 97 | fn when_response_is_false_aborts() -> Result<()> { | ||
| 98 | let test_repo = prep_test_repo()?; | ||
| 99 | |||
| 100 | let mut p = CliTester::new_from_dir(&test_repo.dir, ["prs", "create"]); | ||
| 101 | |||
| 102 | expect_confirm_prompt(&mut p, BEHIND_LEN, AHEAD_LEN)?.succeeds_with(Some(false))?; | ||
| 103 | |||
| 104 | p.expect_end_with("Error: aborting so branch can be rebased\r\n")?; | ||
| 105 | |||
| 106 | Ok(()) | ||
| 107 | } | ||
| 108 | #[test] | ||
| 109 | #[serial] | ||
| 110 | fn when_response_is_true_proceeds() -> Result<()> { | ||
| 111 | let test_repo = prep_test_repo()?; | ||
| 112 | |||
| 113 | let mut p = CliTester::new_from_dir(&test_repo.dir, ["prs", "create"]); | ||
| 114 | expect_confirm_prompt(&mut p, BEHIND_LEN, AHEAD_LEN)?.succeeds_with(Some(true))?; | ||
| 115 | p.expect( | ||
| 116 | format!("creating patch for {AHEAD_LEN} commits from 'head' that are {BEHIND_LEN} behind 'main'",) | ||
| 117 | .as_str(), | ||
| 118 | )?; | ||
| 119 | p.exit()?; | ||
| 120 | Ok(()) | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | mod when_no_commits_behind { | ||
| 125 | use super::*; | ||
| 126 | |||
| 127 | #[test] | ||
| 128 | #[serial] | ||
| 129 | fn message_for_creating_patches() -> Result<()> { | ||
| 130 | let test_repo = GitTestRepo::default(); | ||
| 131 | test_repo.populate()?; | ||
| 132 | // create feature branch with 2 commit ahead | ||
| 133 | test_repo.create_branch("feature")?; | ||
| 134 | test_repo.checkout("feature")?; | ||
| 135 | std::fs::write(test_repo.dir.join("t3.md"), "some content")?; | ||
| 136 | test_repo.stage_and_commit("add t3.md")?; | ||
| 137 | std::fs::write(test_repo.dir.join("t4.md"), "some content")?; | ||
| 138 | test_repo.stage_and_commit("add t4.md")?; | ||
| 139 | |||
| 140 | let mut p = CliTester::new_from_dir(&test_repo.dir, ["prs", "create"]); | ||
| 141 | |||
| 142 | p.expect("creating patch for 2 commits from 'head' that can be merged into 'main'")?; | ||
| 143 | p.exit()?; | ||
| 144 | Ok(()) | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | // #[test] | ||
| 149 | // #[serial] | ||
| 150 | // fn succeeds_with_text_logged_in_as_npub() -> Result<()> { | ||
| 151 | // with_fresh_config(|| { | ||
| 152 | // let mut p = CliTester::new(["login"]); | ||
| 153 | |||
| 154 | // p.expect_input(EXPECTED_NSEC_PROMPT)? | ||
| 155 | // .succeeds_with(TEST_KEY_1_NSEC)?; | ||
| 156 | |||
| 157 | // p.expect_password(EXPECTED_SET_PASSWORD_PROMPT)? | ||
| 158 | // .with_confirmation(EXPECTED_SET_PASSWORD_CONFIRM_PROMPT)? | ||
| 159 | // .succeeds_with(TEST_PASSWORD)?; | ||
| 160 | |||
| 161 | // p.expect_end_with(format!("logged in as {}\r\n", | ||
| 162 | // TEST_KEY_1_NPUB).as_str()) }) | ||
| 163 | // } | ||