From 10bb26c8522d8020242a5f7878054cda0763f4e3 Mon Sep 17 00:00:00 2001 From: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> Date: Tue, 14 Feb 2023 15:36:14 +0000 Subject: [PATCH] Add tests to make sure the publisher works as expected. (#2365) * Add tests to make sure the publisher works as expected. * Fix lint. --- .../upgrade_runtime_crates_version.rs | 67 +++++++++++++------ 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/tools/ci-build/publisher/src/subcommand/upgrade_runtime_crates_version.rs b/tools/ci-build/publisher/src/subcommand/upgrade_runtime_crates_version.rs index e5b6552cdf..8132e1fa66 100644 --- a/tools/ci-build/publisher/src/subcommand/upgrade_runtime_crates_version.rs +++ b/tools/ci-build/publisher/src/subcommand/upgrade_runtime_crates_version.rs @@ -7,6 +7,7 @@ use crate::fs::Fs; use anyhow::{anyhow, bail, Context}; use clap::Parser; use regex::Regex; +use std::borrow::Cow; use std::path::{Path, PathBuf}; #[derive(Parser, Debug)] @@ -27,35 +28,42 @@ pub async fn subcommand_upgrade_runtime_crates_version( .with_context(|| format!("{} is not a valid semver version", &args.version))?; let fs = Fs::Real; let gradle_properties = read_gradle_properties(fs, &args.gradle_properties_path).await?; + let updated_gradle_properties = update_gradle_properties(&gradle_properties, &upgraded_version) + .with_context(|| { + format!( + "Failed to extract the expected runtime crates version from `{:?}`", + &args.gradle_properties_path + ) + })?; + update_gradle_properties_file( + fs, + &args.gradle_properties_path, + updated_gradle_properties.as_ref(), + ) + .await?; + Ok(()) +} + +fn update_gradle_properties<'a>( + gradle_properties: &'a str, + upgraded_version: &'a semver::Version, +) -> Result, anyhow::Error> { let version_regex = Regex::new(r"(?Psmithy\.rs\.runtime\.crate\.version=)(?P\d+\.\d+\.\d+.*)") .unwrap(); - let current_version = version_regex.captures(&gradle_properties).ok_or_else(|| { - anyhow!( - "Failed to extract the expected runtime crates version from `{:?}`", - &args.gradle_properties_path - ) - })?; + let current_version = version_regex + .captures(gradle_properties) + .ok_or_else(|| anyhow!("Failed to extract the expected runtime crates version"))?; let current_version = current_version.name("version").unwrap(); let current_version = semver::Version::parse(current_version.as_str()) .with_context(|| format!("{} is not a valid semver version", current_version.as_str()))?; - if current_version > upgraded_version + if ¤t_version > upgraded_version // Special version tag used on the `main` branch && current_version != semver::Version::parse("0.0.0-smithy-rs-head").unwrap() { bail!("Moving from {current_version} to {upgraded_version} would be a *downgrade*. This command doesn't allow it!"); } - let updated_gradle_properties = version_regex.replace( - &gradle_properties, - format!("${{field}}{}", upgraded_version), - ); - update_gradle_properties( - fs, - &args.gradle_properties_path, - updated_gradle_properties.as_ref(), - ) - .await?; - Ok(()) + Ok(version_regex.replace(gradle_properties, format!("${{field}}{}", upgraded_version))) } async fn read_gradle_properties(fs: Fs, path: &Path) -> Result { @@ -65,7 +73,7 @@ async fn read_gradle_properties(fs: Fs, path: &Path) -> Result