Skip to content

Commit

Permalink
Add tests to make sure the publisher works as expected. (smithy-lang#…
Browse files Browse the repository at this point in the history
…2365)

* Add tests to make sure the publisher works as expected.

* Fix lint.
  • Loading branch information
LukeMathWalker authored Feb 14, 2023
1 parent ca3ef20 commit 10bb26c
Showing 1 changed file with 48 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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<Cow<'a, str>, anyhow::Error> {
let version_regex =
Regex::new(r"(?P<field>smithy\.rs\.runtime\.crate\.version=)(?P<version>\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 &current_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<String, anyhow::Error> {
Expand All @@ -65,11 +73,32 @@ async fn read_gradle_properties(fs: Fs, path: &Path) -> Result<String, anyhow::E
Ok(contents)
}

async fn update_gradle_properties(
async fn update_gradle_properties_file(
fs: Fs,
path: &Path,
contents: &str,
) -> Result<(), anyhow::Error> {
fs.write_file(path, contents.as_bytes()).await?;
Ok(())
}

#[cfg(test)]
mod tests {
use crate::subcommand::upgrade_runtime_crates_version::update_gradle_properties;

#[test]
fn upgrading_works_with_actual_version() {
let gradle_properties = "smithy.rs.runtime.crate.version=0.54.2";
let version = semver::Version::new(0, 54, 3);
let updated = update_gradle_properties(gradle_properties, &version).unwrap();
assert_eq!("smithy.rs.runtime.crate.version=0.54.3", updated);
}

#[test]
fn upgrading_works_with_dummy_version() {
let gradle_properties = "smithy.rs.runtime.crate.version=0.0.0-smithy-rs-head";
let version = semver::Version::new(0, 54, 3);
let updated = update_gradle_properties(gradle_properties, &version).unwrap();
assert_eq!("smithy.rs.runtime.crate.version=0.54.3", updated);
}
}

0 comments on commit 10bb26c

Please sign in to comment.