diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-24 10:58:07 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-24 10:58:07 +0100 |
| commit | 6d086a6608652193898ec548382e780733ea2f5a (patch) | |
| tree | 29515ad88e09ae82eb3239927d241da2111efad1 /test_utils/src/lib.rs | |
| parent | b67376ff54abeab31422921ba5f4883d5d3dccdb (diff) | |
test: refactor `pull` and `push` to abstract code
into lib which makes reading and maintaining tests easier
Diffstat (limited to 'test_utils/src/lib.rs')
| -rw-r--r-- | test_utils/src/lib.rs | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs index de7aee5..5cc6dd5 100644 --- a/test_utils/src/lib.rs +++ b/test_utils/src/lib.rs | |||
| @@ -993,3 +993,169 @@ pub fn get_proposal_branch_name( | |||
| 993 | } | 993 | } |
| 994 | bail!("cannot find proposal root with branch-name tag matching title") | 994 | bail!("cannot find proposal root with branch-name tag matching title") |
| 995 | } | 995 | } |
| 996 | |||
| 997 | pub static FEATURE_BRANCH_NAME_1: &str = "feature-example-t"; | ||
| 998 | pub static FEATURE_BRANCH_NAME_2: &str = "feature-example-f"; | ||
| 999 | pub static FEATURE_BRANCH_NAME_3: &str = "feature-example-c"; | ||
| 1000 | |||
| 1001 | pub static PROPOSAL_TITLE_1: &str = "proposal a"; | ||
| 1002 | pub static PROPOSAL_TITLE_2: &str = "proposal b"; | ||
| 1003 | pub static PROPOSAL_TITLE_3: &str = "proposal c"; | ||
| 1004 | |||
| 1005 | pub fn cli_tester_create_proposals() -> Result<GitTestRepo> { | ||
| 1006 | let git_repo = GitTestRepo::default(); | ||
| 1007 | git_repo.populate()?; | ||
| 1008 | cli_tester_create_proposal( | ||
| 1009 | &git_repo, | ||
| 1010 | FEATURE_BRANCH_NAME_1, | ||
| 1011 | "a", | ||
| 1012 | Some((PROPOSAL_TITLE_1, "proposal a description")), | ||
| 1013 | None, | ||
| 1014 | )?; | ||
| 1015 | cli_tester_create_proposal( | ||
| 1016 | &git_repo, | ||
| 1017 | FEATURE_BRANCH_NAME_2, | ||
| 1018 | "b", | ||
| 1019 | Some((PROPOSAL_TITLE_2, "proposal b description")), | ||
| 1020 | None, | ||
| 1021 | )?; | ||
| 1022 | cli_tester_create_proposal( | ||
| 1023 | &git_repo, | ||
| 1024 | FEATURE_BRANCH_NAME_3, | ||
| 1025 | "c", | ||
| 1026 | Some((PROPOSAL_TITLE_3, "proposal c description")), | ||
| 1027 | None, | ||
| 1028 | )?; | ||
| 1029 | Ok(git_repo) | ||
| 1030 | } | ||
| 1031 | |||
| 1032 | pub fn create_and_populate_branch( | ||
| 1033 | test_repo: &GitTestRepo, | ||
| 1034 | branch_name: &str, | ||
| 1035 | prefix: &str, | ||
| 1036 | only_one_commit: bool, | ||
| 1037 | ) -> Result<()> { | ||
| 1038 | test_repo.checkout("main")?; | ||
| 1039 | test_repo.create_branch(branch_name)?; | ||
| 1040 | test_repo.checkout(branch_name)?; | ||
| 1041 | std::fs::write( | ||
| 1042 | test_repo.dir.join(format!("{}3.md", prefix)), | ||
| 1043 | "some content", | ||
| 1044 | )?; | ||
| 1045 | test_repo.stage_and_commit(format!("add {}3.md", prefix).as_str())?; | ||
| 1046 | if !only_one_commit { | ||
| 1047 | std::fs::write( | ||
| 1048 | test_repo.dir.join(format!("{}4.md", prefix)), | ||
| 1049 | "some content", | ||
| 1050 | )?; | ||
| 1051 | test_repo.stage_and_commit(format!("add {}4.md", prefix).as_str())?; | ||
| 1052 | } | ||
| 1053 | Ok(()) | ||
| 1054 | } | ||
| 1055 | |||
| 1056 | pub fn cli_tester_create_proposal( | ||
| 1057 | test_repo: &GitTestRepo, | ||
| 1058 | branch_name: &str, | ||
| 1059 | prefix: &str, | ||
| 1060 | cover_letter_title_and_description: Option<(&str, &str)>, | ||
| 1061 | in_reply_to: Option<String>, | ||
| 1062 | ) -> Result<()> { | ||
| 1063 | create_and_populate_branch(test_repo, branch_name, prefix, false)?; | ||
| 1064 | |||
| 1065 | if let Some(in_reply_to) = in_reply_to { | ||
| 1066 | let mut p = CliTester::new_from_dir( | ||
| 1067 | &test_repo.dir, | ||
| 1068 | [ | ||
| 1069 | "--nsec", | ||
| 1070 | TEST_KEY_1_NSEC, | ||
| 1071 | "--password", | ||
| 1072 | TEST_PASSWORD, | ||
| 1073 | "--disable-cli-spinners", | ||
| 1074 | "send", | ||
| 1075 | "HEAD~2", | ||
| 1076 | "--no-cover-letter", | ||
| 1077 | "--in-reply-to", | ||
| 1078 | in_reply_to.as_str(), | ||
| 1079 | ], | ||
| 1080 | ); | ||
| 1081 | p.expect_end_eventually()?; | ||
| 1082 | } else if let Some((title, description)) = cover_letter_title_and_description { | ||
| 1083 | let mut p = CliTester::new_from_dir( | ||
| 1084 | &test_repo.dir, | ||
| 1085 | [ | ||
| 1086 | "--nsec", | ||
| 1087 | TEST_KEY_1_NSEC, | ||
| 1088 | "--password", | ||
| 1089 | TEST_PASSWORD, | ||
| 1090 | "--disable-cli-spinners", | ||
| 1091 | "send", | ||
| 1092 | "HEAD~2", | ||
| 1093 | "--title", | ||
| 1094 | format!("\"{title}\"").as_str(), | ||
| 1095 | "--description", | ||
| 1096 | format!("\"{description}\"").as_str(), | ||
| 1097 | ], | ||
| 1098 | ); | ||
| 1099 | p.expect_end_eventually()?; | ||
| 1100 | } else { | ||
| 1101 | let mut p = CliTester::new_from_dir( | ||
| 1102 | &test_repo.dir, | ||
| 1103 | [ | ||
| 1104 | "--nsec", | ||
| 1105 | TEST_KEY_1_NSEC, | ||
| 1106 | "--password", | ||
| 1107 | TEST_PASSWORD, | ||
| 1108 | "--disable-cli-spinners", | ||
| 1109 | "send", | ||
| 1110 | "HEAD~2", | ||
| 1111 | "--no-cover-letter", | ||
| 1112 | ], | ||
| 1113 | ); | ||
| 1114 | p.expect_end_eventually()?; | ||
| 1115 | } | ||
| 1116 | Ok(()) | ||
| 1117 | } | ||
| 1118 | /// returns (originating_repo, test_repo) | ||
| 1119 | pub fn create_proposals_and_repo_with_first_proposal_pulled_and_checkedout() | ||
| 1120 | -> Result<(GitTestRepo, GitTestRepo)> { | ||
| 1121 | Ok(( | ||
| 1122 | cli_tester_create_proposals()?, | ||
| 1123 | create_repo_with_first_proposal_branch_pulled_and_checkedout()?, | ||
| 1124 | )) | ||
| 1125 | } | ||
| 1126 | |||
| 1127 | pub fn create_repo_with_first_proposal_branch_pulled_and_checkedout() -> Result<GitTestRepo> { | ||
| 1128 | let test_repo = GitTestRepo::default(); | ||
| 1129 | test_repo.populate()?; | ||
| 1130 | use_ngit_list_to_download_and_checkout_first_proposal_branch(&test_repo)?; | ||
| 1131 | Ok(test_repo) | ||
| 1132 | } | ||
| 1133 | |||
| 1134 | pub fn use_ngit_list_to_download_and_checkout_first_proposal_branch( | ||
| 1135 | test_repo: &GitTestRepo, | ||
| 1136 | ) -> Result<()> { | ||
| 1137 | let mut p = CliTester::new_from_dir(&test_repo.dir, ["list"]); | ||
| 1138 | p.expect("fetching updates...\r\n")?; | ||
| 1139 | p.expect_eventually("\r\n")?; // some updates listed here | ||
| 1140 | let mut c = p.expect_choice( | ||
| 1141 | "all proposals", | ||
| 1142 | vec![ | ||
| 1143 | format!("\"{PROPOSAL_TITLE_3}\""), | ||
| 1144 | format!("\"{PROPOSAL_TITLE_2}\""), | ||
| 1145 | format!("\"{PROPOSAL_TITLE_1}\""), | ||
| 1146 | ], | ||
| 1147 | )?; | ||
| 1148 | c.succeeds_with(2, true, None)?; | ||
| 1149 | let mut c = p.expect_choice( | ||
| 1150 | "", | ||
| 1151 | vec![ | ||
| 1152 | format!("create and checkout proposal branch (2 ahead 0 behind 'main')"), | ||
| 1153 | format!("apply to current branch with `git am`"), | ||
| 1154 | format!("download to ./patches"), | ||
| 1155 | format!("back"), | ||
| 1156 | ], | ||
| 1157 | )?; | ||
| 1158 | c.succeeds_with(0, false, Some(0))?; | ||
| 1159 | p.expect_end_eventually()?; | ||
| 1160 | Ok(()) | ||
| 1161 | } | ||