Skip to content

Commit 10bb26c

Browse files
Add tests to make sure the publisher works as expected. (#2365)
* Add tests to make sure the publisher works as expected. * Fix lint.
1 parent ca3ef20 commit 10bb26c

File tree

1 file changed

+48
-19
lines changed

1 file changed

+48
-19
lines changed

tools/ci-build/publisher/src/subcommand/upgrade_runtime_crates_version.rs

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::fs::Fs;
77
use anyhow::{anyhow, bail, Context};
88
use clap::Parser;
99
use regex::Regex;
10+
use std::borrow::Cow;
1011
use std::path::{Path, PathBuf};
1112

1213
#[derive(Parser, Debug)]
@@ -27,35 +28,42 @@ pub async fn subcommand_upgrade_runtime_crates_version(
2728
.with_context(|| format!("{} is not a valid semver version", &args.version))?;
2829
let fs = Fs::Real;
2930
let gradle_properties = read_gradle_properties(fs, &args.gradle_properties_path).await?;
31+
let updated_gradle_properties = update_gradle_properties(&gradle_properties, &upgraded_version)
32+
.with_context(|| {
33+
format!(
34+
"Failed to extract the expected runtime crates version from `{:?}`",
35+
&args.gradle_properties_path
36+
)
37+
})?;
38+
update_gradle_properties_file(
39+
fs,
40+
&args.gradle_properties_path,
41+
updated_gradle_properties.as_ref(),
42+
)
43+
.await?;
44+
Ok(())
45+
}
46+
47+
fn update_gradle_properties<'a>(
48+
gradle_properties: &'a str,
49+
upgraded_version: &'a semver::Version,
50+
) -> Result<Cow<'a, str>, anyhow::Error> {
3051
let version_regex =
3152
Regex::new(r"(?P<field>smithy\.rs\.runtime\.crate\.version=)(?P<version>\d+\.\d+\.\d+.*)")
3253
.unwrap();
33-
let current_version = version_regex.captures(&gradle_properties).ok_or_else(|| {
34-
anyhow!(
35-
"Failed to extract the expected runtime crates version from `{:?}`",
36-
&args.gradle_properties_path
37-
)
38-
})?;
54+
let current_version = version_regex
55+
.captures(gradle_properties)
56+
.ok_or_else(|| anyhow!("Failed to extract the expected runtime crates version"))?;
3957
let current_version = current_version.name("version").unwrap();
4058
let current_version = semver::Version::parse(current_version.as_str())
4159
.with_context(|| format!("{} is not a valid semver version", current_version.as_str()))?;
42-
if current_version > upgraded_version
60+
if &current_version > upgraded_version
4361
// Special version tag used on the `main` branch
4462
&& current_version != semver::Version::parse("0.0.0-smithy-rs-head").unwrap()
4563
{
4664
bail!("Moving from {current_version} to {upgraded_version} would be a *downgrade*. This command doesn't allow it!");
4765
}
48-
let updated_gradle_properties = version_regex.replace(
49-
&gradle_properties,
50-
format!("${{field}}{}", upgraded_version),
51-
);
52-
update_gradle_properties(
53-
fs,
54-
&args.gradle_properties_path,
55-
updated_gradle_properties.as_ref(),
56-
)
57-
.await?;
58-
Ok(())
66+
Ok(version_regex.replace(gradle_properties, format!("${{field}}{}", upgraded_version)))
5967
}
6068

6169
async fn read_gradle_properties(fs: Fs, path: &Path) -> Result<String, anyhow::Error> {
@@ -65,11 +73,32 @@ async fn read_gradle_properties(fs: Fs, path: &Path) -> Result<String, anyhow::E
6573
Ok(contents)
6674
}
6775

68-
async fn update_gradle_properties(
76+
async fn update_gradle_properties_file(
6977
fs: Fs,
7078
path: &Path,
7179
contents: &str,
7280
) -> Result<(), anyhow::Error> {
7381
fs.write_file(path, contents.as_bytes()).await?;
7482
Ok(())
7583
}
84+
85+
#[cfg(test)]
86+
mod tests {
87+
use crate::subcommand::upgrade_runtime_crates_version::update_gradle_properties;
88+
89+
#[test]
90+
fn upgrading_works_with_actual_version() {
91+
let gradle_properties = "smithy.rs.runtime.crate.version=0.54.2";
92+
let version = semver::Version::new(0, 54, 3);
93+
let updated = update_gradle_properties(gradle_properties, &version).unwrap();
94+
assert_eq!("smithy.rs.runtime.crate.version=0.54.3", updated);
95+
}
96+
97+
#[test]
98+
fn upgrading_works_with_dummy_version() {
99+
let gradle_properties = "smithy.rs.runtime.crate.version=0.0.0-smithy-rs-head";
100+
let version = semver::Version::new(0, 54, 3);
101+
let updated = update_gradle_properties(gradle_properties, &version).unwrap();
102+
assert_eq!("smithy.rs.runtime.crate.version=0.54.3", updated);
103+
}
104+
}

0 commit comments

Comments
 (0)