Unverified Commit affd87c9 authored by ysaito1001's avatar ysaito1001 Committed by GitHub
Browse files

Make versions comparison more flexible in the fix-manifest subcommand of publisher (#3025)

## Motivation and Context
Attempts to resolve [a workflow
failure](https://github.com/awslabs/aws-sdk-rust/actions/runs/6395300944/job/17358637767#step:6)
in `aws-sdk-rust`.

## Description
We have observed a series of `WARN`s from the `check-manifests` job. It
is caused by #3009, specifically `update_dep` in the `fix-manifests`
needs to be updated where it compares a version in a `Table` versus a
current version being looked at. For instance, prior to the PR, `0.56.1`
and `~0.56` for a crate were considered a mismatch when it should be a
match according to our latest logic.

## Testing
- Added a unit test for a helper function.

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._
parent bb356886
Loading
Loading
Loading
Loading
+34 −1
Original line number Diff line number Diff line
@@ -217,7 +217,7 @@ fn update_dep(table: &mut Table, dep_name: &str, versions: &VersionView) -> Resu
    );
    match previous_version {
        None => Ok(1),
        Some(prev_version) if prev_version.as_str() == Some(&package_version) => Ok(0),
        Some(prev_version) if versions_match(&prev_version, &package_version) => Ok(0),
        Some(mismatched_version) => {
            tracing::warn!(expected = ?package_version, actual = ?mismatched_version, "version was set but it did not match");
            Ok(1)
@@ -251,6 +251,23 @@ fn convert_to_tilde_requirement(package_version: &semver::Version) -> Result<Str
    Ok("~".to_string() + package_version)
}

// Determines if `prev_version` and `current_version` are considered a match
//
// Each input can be either a semver-compliant version or a tilde version requirement.
fn versions_match(prev_version: &Value, current_version: &str) -> bool {
    match prev_version.as_str() {
        Some(prev_version) => {
            if prev_version == current_version {
                return true;
            }
            let prev_version = prev_version.strip_prefix('~').unwrap_or(prev_version);
            let current_version = current_version.strip_prefix('~').unwrap_or(current_version);
            prev_version.starts_with(current_version) || current_version.starts_with(prev_version)
        }
        _ => false,
    }
}

fn fix_dep_sets(versions: &VersionView, metadata: &mut toml::Value) -> Result<usize> {
    let mut changed = fix_dep_set(versions, "dependencies", metadata)?;
    // allow dev dependencies to be unpublished
@@ -530,4 +547,20 @@ mod tests {
            "aws-sdk-rust/examples/foo/bar/Cargo.toml"
        ));
    }

    #[test]
    fn test_versions_match() {
        assert!(versions_match(&Value::String("0.56.1".to_owned()), "~0.56"));
        assert!(versions_match(&Value::String("~0.56".to_owned()), "0.56.1"));
        assert!(!versions_match(&Value::String("~0.56".to_owned()), "~0.57"));
        assert!(!versions_match(&Value::String("~0.57".to_owned()), "~0.56"));
        assert!(!versions_match(
            &Value::String("0.56.1".to_owned()),
            "0.56.2"
        ));
        assert!(!versions_match(
            &Value::String("0.56.1".to_owned()),
            "0.57.1"
        ));
    }
}