diff options
| -rw-r--r-- | src/sub_commands/send.rs | 72 | ||||
| -rw-r--r-- | tests/send.rs | 192 |
2 files changed, 231 insertions, 33 deletions
diff --git a/src/sub_commands/send.rs b/src/sub_commands/send.rs index 23016e1..6417870 100644 --- a/src/sub_commands/send.rs +++ b/src/sub_commands/send.rs | |||
| @@ -50,43 +50,49 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { | |||
| 50 | if args.since_or_revision_range.is_empty() | 50 | if args.since_or_revision_range.is_empty() |
| 51 | || args.since_or_revision_range.eq("master..HEAD") | 51 | || args.since_or_revision_range.eq("master..HEAD") |
| 52 | { | 52 | { |
| 53 | let (from_branch, to_branch, ahead, behind) = | 53 | let (branch_name, tip) = git_repo.get_main_or_master_branch()?; |
| 54 | identify_ahead_behind(&git_repo, &None, &None)?; | 54 | if branch_name.eq("main") || branch_name.eq("master") { |
| 55 | println!("creating 1 patch from latest commit"); | ||
| 56 | vec![tip] | ||
| 57 | } else { | ||
| 58 | let (from_branch, to_branch, ahead, behind) = | ||
| 59 | identify_ahead_behind(&git_repo, &None, &None)?; | ||
| 55 | 60 | ||
| 56 | if ahead.is_empty() { | 61 | if ahead.is_empty() { |
| 57 | bail!(format!( | 62 | bail!(format!( |
| 58 | "'{from_branch}' is 0 commits ahead of '{to_branch}' so no patches were created" | 63 | "'{from_branch}' is 0 commits ahead of '{to_branch}' so no patches were created" |
| 59 | )); | 64 | )); |
| 60 | } | 65 | } |
| 61 | 66 | ||
| 62 | if behind.is_empty() { | 67 | if behind.is_empty() { |
| 63 | println!( | 68 | println!( |
| 64 | "creating patch for {} commits from '{from_branch}' that can be merged into '{to_branch}'", | 69 | "creating patch for {} commits from '{from_branch}' that can be merged into '{to_branch}'", |
| 65 | ahead.len(), | ||
| 66 | ); | ||
| 67 | } else { | ||
| 68 | if !Interactor::default().confirm( | ||
| 69 | PromptConfirmParms::default() | ||
| 70 | .with_prompt( | ||
| 71 | format!( | ||
| 72 | "'{from_branch}' is {} commits behind '{to_branch}' and {} ahead. Consider rebasing before sending patches. Proceed anyway?", | ||
| 73 | behind.len(), | ||
| 74 | ahead.len(), | 70 | ahead.len(), |
| 75 | ) | 71 | ); |
| 76 | ) | 72 | } else { |
| 77 | .with_default(false) | 73 | if !Interactor::default().confirm( |
| 78 | ).context("failed to get confirmation response from interactor confirm")? { | 74 | PromptConfirmParms::default() |
| 79 | bail!("aborting so branch can be rebased"); | 75 | .with_prompt( |
| 80 | } | 76 | format!( |
| 81 | println!( | 77 | "'{from_branch}' is {} commits behind '{to_branch}' and {} ahead. Consider rebasing before sending patches. Proceed anyway?", |
| 82 | "creating patch for {} commit{} from '{from_branch}' that {} {} behind '{to_branch}'", | 78 | behind.len(), |
| 83 | ahead.len(), | 79 | ahead.len(), |
| 84 | if ahead.len() > 1 { "s" } else { "" }, | 80 | ) |
| 85 | if ahead.len() > 1 { "are" } else { "is" }, | 81 | ) |
| 86 | behind.len(), | 82 | .with_default(false) |
| 87 | ); | 83 | ).context("failed to get confirmation response from interactor confirm")? { |
| 84 | bail!("aborting so branch can be rebased"); | ||
| 85 | } | ||
| 86 | println!( | ||
| 87 | "creating patch for {} commit{} from '{from_branch}' that {} {} behind '{to_branch}'", | ||
| 88 | ahead.len(), | ||
| 89 | if ahead.len() > 1 { "s" } else { "" }, | ||
| 90 | if ahead.len() > 1 { "are" } else { "is" }, | ||
| 91 | behind.len(), | ||
| 92 | ); | ||
| 93 | } | ||
| 94 | ahead | ||
| 88 | } | 95 | } |
| 89 | ahead | ||
| 90 | } else { | 96 | } else { |
| 91 | let ahead = git_repo | 97 | let ahead = git_repo |
| 92 | .parse_starting_commits(&args.since_or_revision_range) | 98 | .parse_starting_commits(&args.since_or_revision_range) |
diff --git a/tests/send.rs b/tests/send.rs index 4b68821..2d02a7d 100644 --- a/tests/send.rs +++ b/tests/send.rs | |||
| @@ -1137,6 +1137,198 @@ mod sends_2_patches_without_cover_letter { | |||
| 1137 | Ok(()) | 1137 | Ok(()) |
| 1138 | } | 1138 | } |
| 1139 | } | 1139 | } |
| 1140 | |||
| 1141 | mod when_on_main_branch_defaults_to_last_commit { | ||
| 1142 | use super::*; | ||
| 1143 | |||
| 1144 | fn prep_git_repo() -> Result<GitTestRepo> { | ||
| 1145 | let test_repo = GitTestRepo::default(); | ||
| 1146 | test_repo.populate()?; | ||
| 1147 | // dont checkout feature branch | ||
| 1148 | std::fs::write(test_repo.dir.join("t3.md"), "some content")?; | ||
| 1149 | test_repo.stage_and_commit("add t3.md")?; | ||
| 1150 | std::fs::write(test_repo.dir.join("t4.md"), "some content")?; | ||
| 1151 | test_repo.stage_and_commit("add t4.md")?; | ||
| 1152 | Ok(test_repo) | ||
| 1153 | } | ||
| 1154 | |||
| 1155 | fn cli_tester_create_proposal(git_repo: &GitTestRepo) -> CliTester { | ||
| 1156 | let args = vec![ | ||
| 1157 | "--nsec", | ||
| 1158 | TEST_KEY_1_NSEC, | ||
| 1159 | "--password", | ||
| 1160 | TEST_PASSWORD, | ||
| 1161 | "--disable-cli-spinners", | ||
| 1162 | "send", | ||
| 1163 | "--no-cover-letter", | ||
| 1164 | ]; | ||
| 1165 | CliTester::new_from_dir(&git_repo.dir, args) | ||
| 1166 | } | ||
| 1167 | fn expect_msgs_first(p: &mut CliTester) -> Result<()> { | ||
| 1168 | p.expect("creating 1 patch from latest commit\r\n")?; | ||
| 1169 | p.expect("searching for profile and relay updates...\r\n")?; | ||
| 1170 | p.expect("\r")?; | ||
| 1171 | p.expect("logged in as fred\r\n")?; | ||
| 1172 | p.expect("posting 1 patches without a covering letter...\r\n")?; | ||
| 1173 | Ok(()) | ||
| 1174 | } | ||
| 1175 | async fn prep_run_create_proposal() -> Result<( | ||
| 1176 | Relay<'static>, | ||
| 1177 | Relay<'static>, | ||
| 1178 | Relay<'static>, | ||
| 1179 | Relay<'static>, | ||
| 1180 | Relay<'static>, | ||
| 1181 | )> { | ||
| 1182 | let git_repo = prep_git_repo()?; | ||
| 1183 | |||
| 1184 | // fallback (51,52) user write (53, 55) repo (55, 56) | ||
| 1185 | let (mut r51, mut r52, mut r53, mut r55, mut r56) = ( | ||
| 1186 | Relay::new( | ||
| 1187 | 8051, | ||
| 1188 | None, | ||
| 1189 | Some(&|relay, client_id, subscription_id, _| -> Result<()> { | ||
| 1190 | relay.respond_events( | ||
| 1191 | client_id, | ||
| 1192 | &subscription_id, | ||
| 1193 | &vec![ | ||
| 1194 | generate_test_key_1_metadata_event("fred"), | ||
| 1195 | generate_test_key_1_relay_list_event(), | ||
| 1196 | ], | ||
| 1197 | )?; | ||
| 1198 | Ok(()) | ||
| 1199 | }), | ||
| 1200 | ), | ||
| 1201 | Relay::new(8052, None, None), | ||
| 1202 | Relay::new(8053, None, None), | ||
| 1203 | Relay::new( | ||
| 1204 | 8055, | ||
| 1205 | None, | ||
| 1206 | Some(&|relay, client_id, subscription_id, _| -> Result<()> { | ||
| 1207 | relay.respond_events( | ||
| 1208 | client_id, | ||
| 1209 | &subscription_id, | ||
| 1210 | &vec![generate_repo_ref_event()], | ||
| 1211 | )?; | ||
| 1212 | Ok(()) | ||
| 1213 | }), | ||
| 1214 | ), | ||
| 1215 | Relay::new(8056, None, None), | ||
| 1216 | ); | ||
| 1217 | |||
| 1218 | // // check relay had the right number of events | ||
| 1219 | let cli_tester_handle = std::thread::spawn(move || -> Result<()> { | ||
| 1220 | let mut p = cli_tester_create_proposal(&git_repo); | ||
| 1221 | p.expect_end_eventually()?; | ||
| 1222 | for p in [51, 52, 53, 55, 56] { | ||
| 1223 | relay::shutdown_relay(8000 + p)?; | ||
| 1224 | } | ||
| 1225 | Ok(()) | ||
| 1226 | }); | ||
| 1227 | |||
| 1228 | // launch relay | ||
| 1229 | let _ = join!( | ||
| 1230 | r51.listen_until_close(), | ||
| 1231 | r52.listen_until_close(), | ||
| 1232 | r53.listen_until_close(), | ||
| 1233 | r55.listen_until_close(), | ||
| 1234 | r56.listen_until_close(), | ||
| 1235 | ); | ||
| 1236 | cli_tester_handle.join().unwrap()?; | ||
| 1237 | Ok((r51, r52, r53, r55, r56)) | ||
| 1238 | } | ||
| 1239 | mod cli_ouput { | ||
| 1240 | use super::*; | ||
| 1241 | |||
| 1242 | async fn run_test_async() -> Result<()> { | ||
| 1243 | let git_repo = prep_git_repo()?; | ||
| 1244 | |||
| 1245 | let (mut r51, mut r52, mut r53, mut r55, mut r56) = ( | ||
| 1246 | Relay::new( | ||
| 1247 | 8051, | ||
| 1248 | None, | ||
| 1249 | Some(&|relay, client_id, subscription_id, _| -> Result<()> { | ||
| 1250 | relay.respond_events( | ||
| 1251 | client_id, | ||
| 1252 | &subscription_id, | ||
| 1253 | &vec![ | ||
| 1254 | generate_test_key_1_metadata_event("fred"), | ||
| 1255 | generate_test_key_1_relay_list_event(), | ||
| 1256 | ], | ||
| 1257 | )?; | ||
| 1258 | Ok(()) | ||
| 1259 | }), | ||
| 1260 | ), | ||
| 1261 | Relay::new(8052, None, None), | ||
| 1262 | Relay::new(8053, None, None), | ||
| 1263 | Relay::new( | ||
| 1264 | 8055, | ||
| 1265 | None, | ||
| 1266 | Some(&|relay, client_id, subscription_id, _| -> Result<()> { | ||
| 1267 | relay.respond_events( | ||
| 1268 | client_id, | ||
| 1269 | &subscription_id, | ||
| 1270 | &vec![generate_repo_ref_event()], | ||
| 1271 | )?; | ||
| 1272 | Ok(()) | ||
| 1273 | }), | ||
| 1274 | ), | ||
| 1275 | Relay::new(8056, None, None), | ||
| 1276 | ); | ||
| 1277 | |||
| 1278 | // // check relay had the right number of events | ||
| 1279 | let cli_tester_handle = std::thread::spawn(move || -> Result<()> { | ||
| 1280 | let mut p = cli_tester_create_proposal(&git_repo); | ||
| 1281 | |||
| 1282 | expect_msgs_first(&mut p)?; | ||
| 1283 | relay::expect_send_with_progress( | ||
| 1284 | &mut p, | ||
| 1285 | vec![ | ||
| 1286 | (" [my-relay] [repo-relay] ws://localhost:8055", true, ""), | ||
| 1287 | (" [my-relay] ws://localhost:8053", true, ""), | ||
| 1288 | (" [repo-relay] ws://localhost:8056", true, ""), | ||
| 1289 | (" [default] ws://localhost:8051", true, ""), | ||
| 1290 | (" [default] ws://localhost:8052", true, ""), | ||
| 1291 | ], | ||
| 1292 | 1, | ||
| 1293 | )?; | ||
| 1294 | p.expect_end_with_whitespace()?; | ||
| 1295 | for p in [51, 52, 53, 55, 56] { | ||
| 1296 | relay::shutdown_relay(8000 + p)?; | ||
| 1297 | } | ||
| 1298 | Ok(()) | ||
| 1299 | }); | ||
| 1300 | |||
| 1301 | // launch relay | ||
| 1302 | let _ = join!( | ||
| 1303 | r51.listen_until_close(), | ||
| 1304 | r52.listen_until_close(), | ||
| 1305 | r53.listen_until_close(), | ||
| 1306 | r55.listen_until_close(), | ||
| 1307 | r56.listen_until_close(), | ||
| 1308 | ); | ||
| 1309 | cli_tester_handle.join().unwrap()?; | ||
| 1310 | Ok(()) | ||
| 1311 | } | ||
| 1312 | |||
| 1313 | #[tokio::test] | ||
| 1314 | #[serial] | ||
| 1315 | async fn check_cli_output() -> Result<()> { | ||
| 1316 | run_test_async().await?; | ||
| 1317 | Ok(()) | ||
| 1318 | } | ||
| 1319 | } | ||
| 1320 | |||
| 1321 | #[tokio::test] | ||
| 1322 | #[serial] | ||
| 1323 | async fn one_patch_event_sent() -> Result<()> { | ||
| 1324 | let (_, _, r53, r55, r56) = prep_run_create_proposal().await?; | ||
| 1325 | for relay in [&r53, &r55, &r56] { | ||
| 1326 | assert_eq!(relay.events.iter().filter(|e| is_patch(e)).count(), 1); | ||
| 1327 | } | ||
| 1328 | Ok(()) | ||
| 1329 | } | ||
| 1330 | } | ||
| 1331 | |||
| 1140 | mod specify_starting_commits_whist_on_main_branch { | 1332 | mod specify_starting_commits_whist_on_main_branch { |
| 1141 | use super::*; | 1333 | use super::*; |
| 1142 | 1334 | ||