397
.unwrap_or_default()
398
}
399
400
+/// File names of the packages that will ship but have no `version` yet. A
401
+/// package in `PendingUpload` gets its version only when the commit ingests
402
+/// it, so before the commit the file name is all there is to go on.
403
+pub fn pending_file_names(submission: &Value) -> Vec<String> {
404
+ submission
405
+ .get("applicationPackages")
406
+ .and_then(Value::as_array)
407
+ .map(|packages| {
408
+ packages
409
+ .iter()
410
+ .filter(|p| str_at(p, "/fileStatus") != Some("PendingDelete"))
411
+ .filter(|p| str_at(p, "/version").is_none())
412
+ .filter_map(|p| str_at(p, "/fileName").map(str::to_string))
413
+ .collect()
414
+ })
415
+ .unwrap_or_default()
416
+}
417
+
418
+/// Every dotted number in a file name, whole: `siGit-1.3.2.0.msix` gives
419
+/// `1.3.2.0`, and `x64_11.3.2.0.msixupload` gives `11.3.2.0`, never `1.3.2.0`.
420
+pub fn versions_in_file_name(name: &str) -> Vec<&str> {
421
+ name.split(|c: char| !(c.is_ascii_digit() || c == '.'))
422
+ .map(|run| run.trim_matches('.'))
423
+ .filter(|run| run.contains('.'))
424
+ .collect()
425
+}
426
+
427
+/// The guard behind `--expect-package-version`, before the commit. An
428
+/// ingested package is matched by its `version`; a package still pending
429
+/// upload has none, so it is matched by the version in its file name.
430
+pub fn check_expected_version(submission: &Value, expected: &str) -> Result<(), String> {
431
+ let versions = package_versions(submission);
432
+ if versions.iter().any(|v| v == expected) {
433
+ return Ok(());
434
+ }
435
+ let pending = pending_file_names(submission);
436
+ if pending
437
+ .iter()
438
+ .any(|name| versions_in_file_name(name).contains(&expected))
439
+ {
440
+ return Ok(());
441
+ }
442
+ if !pending.is_empty()
443
+ && versions.is_empty()
444
+ && pending.iter().all(|n| versions_in_file_name(n).is_empty())
445
+ {
446
+ return Err(format!(
447
+ "the pending package [{}] has no version yet and its file name does not carry one, so {expected} cannot be checked; nothing was committed",
448
+ pending.join(", ")
449
+ ));
450
+ }
451
+ Err(format!(
452
+ "submission carries package versions [{}] and pending packages [{}], not {expected}; nothing was committed",
453
+ versions.join(", "),
454
+ pending.join(", ")
455
+ ))
456
+}
457
+
458
/// The error messages of a status record, for a `...Failed` status.
459
fn status_errors(status: &Value) -> Vec<String> {
460
status
481
pub noop: bool,
482
pub submission_id: String,
483
pub package_versions: Vec<String>,
484
+ /// Files still pending upload before the commit, matched by name.
485
+ pub pending_uploads: Vec<String>,
486
+ /// Versions the Store reports after the commit, once it has ingested the
487
+ /// packages. `None` on a dry run or no-op, or while it still reports none.
488
+ pub package_versions_after: Option<Vec<String>>,
489
/// The status before this call.
490
pub status_before: String,
491
/// The status after the commit and the wait. `None` on a dry run or no-op.
495
/// Commit the pending submission to certification.
496
///
497
/// Fails closed before writing when there is no pending submission or, with
435
-/// `expect_package_version`, when no shipping package has that version. A
436
-/// submission already committed is a no-op. After the commit the status is
498
+/// `expect_package_version`, when no shipping package has that version (see
499
+/// [`check_expected_version`]; a package still pending upload is matched by
500
+/// its file name). After the commit the Store's own `version` is checked
501
+/// strictly. A submission already committed is a no-op. After the commit the status is
502
/// polled until it leaves `CommitStarted` or `wait_seconds` runs out; a status
503
/// ending in `Failed` is an error carrying the status details.
504
pub async fn commit_pending(
515
.to_string();
516
let submission = client.get_submission(product_id, &id).await.map_err(err)?;
517
let versions = package_versions(&submission);
518
+ let pending_uploads = pending_file_names(&submission);
519
if let Some(expected) = expect_package_version {
454
- if !versions.iter().any(|v| v == expected) {
455
- return Err(format!(
456
- "submission {id} carries package versions [{}], not {expected}; nothing was committed",
457
- versions.join(", ")
458
- ));
459
- }
520
+ check_expected_version(&submission, expected)?;
521
}
522
let status = client
523
.submission_status(product_id, &id)
524
.await
525
.map_err(err)?;
526
let before = str_at(&status, "/status").unwrap_or("").to_string();
466
- let result = |noop, after| Committed {
527
+ let result = |noop, after, versions_after| Committed {
528
dry_run,
529
noop,
530
submission_id: id.clone(),
531
package_versions: versions.clone(),
532
+ pending_uploads: pending_uploads.clone(),
533
+ package_versions_after: versions_after,
534
status_before: before.clone(),
535
status_after: after,
536
};
537
if is_past_commit(&before) {
475
- return Ok(result(true, None));
538
+ return Ok(result(true, None, None));
539
}
540
if dry_run {
478
- return Ok(result(false, None));
541
+ return Ok(result(false, None, None));
542
}
543
client
544
.commit_submission(product_id, &id)
559
));
560
}
561
if current != "CommitStarted" || std::time::Instant::now() >= deadline {
499
- return Ok(result(false, Some(current)));
562
+ // The Store has ingested the packages by now, so `version` is
563
+ // there and the strict comparison applies. An empty list means
564
+ // the wait ran out first: unknown, not a mismatch.
565
+ let after = client.get_submission(product_id, &id).await.map_err(err)?;
566
+ let after_versions = package_versions(&after);
567
+ if let Some(expected) = expect_package_version {
568
+ if !after_versions.is_empty() && !after_versions.iter().any(|v| v == expected) {
569
+ return Err(format!(
570
+ "submission {id} was committed (status {current}) but carries package versions [{}], not {expected}",
571
+ after_versions.join(", ")
572
+ ));
573
+ }
574
+ }
575
+ let reported = (!after_versions.is_empty()).then_some(after_versions);
576
+ return Ok(result(false, Some(current), reported));
577
}
578
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
579
}
597
assert!(package_versions(&serde_json::json!({})).is_empty());
598
}
599
600
+ #[test]
601
+ fn pending_upload_is_matched_by_file_name() {
602
+ let draft = serde_json::json!({"applicationPackages": [
603
+ {"fileName": "siGit-1.3.2.0.msix", "fileStatus": "PendingUpload"}]});
604
+ assert!(package_versions(&draft).is_empty());
605
+ assert_eq!(pending_file_names(&draft), vec!["siGit-1.3.2.0.msix"]);
606
+ assert!(check_expected_version(&draft, "1.3.2.0").is_ok());
607
+ }
608
+
609
+ #[test]
610
+ fn pending_upload_with_another_version_is_a_mismatch() {
611
+ let draft = serde_json::json!({"applicationPackages": [
612
+ {"fileName": "siGit-1.3.1.0.msix", "fileStatus": "PendingUpload"}]});
613
+ let error = check_expected_version(&draft, "1.3.2.0").unwrap_err();
614
+ assert!(error.contains("siGit-1.3.1.0.msix") && error.contains("not 1.3.2.0"));
615
+ }
616
+
617
+ #[test]
618
+ fn pending_upload_without_a_version_in_its_name_cannot_be_checked() {
619
+ let draft = serde_json::json!({"applicationPackages": [
620
+ {"fileName": "release.msix", "fileStatus": "PendingUpload"}]});
621
+ let error = check_expected_version(&draft, "1.3.2.0").unwrap_err();
622
+ assert!(error.contains("has no version yet"));
623
+ }
624
+
625
+ #[test]
626
+ fn file_name_versions_are_matched_whole() {
627
+ assert_eq!(versions_in_file_name("siGit-1.3.2.0.msix"), vec!["1.3.2.0"]);
628
+ assert_eq!(
629
+ versions_in_file_name("x64_1.3.2.0.msixupload"),
630
+ vec!["1.3.2.0"]
631
+ );
632
+ assert_eq!(versions_in_file_name("x64_11.3.2.0.msix"), vec!["11.3.2.0"]);
633
+ assert_eq!(versions_in_file_name("a-1.3.2.01.msix"), vec!["1.3.2.01"]);
634
+ assert!(versions_in_file_name("release.msix").is_empty());
635
+ for name in ["x64_11.3.2.0.msix", "a-1.3.2.01.msix"] {
636
+ let draft = serde_json::json!({"applicationPackages": [
637
+ {"fileName": name, "fileStatus": "PendingUpload"}]});
638
+ assert!(check_expected_version(&draft, "1.3.2.0").is_err());
639
+ }
640
+ }
641
+
642
+ #[test]
643
+ fn ingested_package_is_still_checked_by_version() {
644
+ assert!(check_expected_version(&submission(), "1.3.2.0").is_ok());
645
+ assert!(check_expected_version(&submission(), "1.3.1.0").is_err());
646
+ }
647
+
648
#[test]
649
fn only_committed_statuses_are_past_commit() {
650
for s in ["CommitStarted", "PreProcessing", "Certification", "Release"] {