From ea64b7211a05bec435c9a437651e96cb4681ee7f Mon Sep 17 00:00:00 2001 From: Matthew James Briggs Date: Thu, 7 Dec 2023 17:38:05 -0800 Subject: [PATCH] fix release.toml version check We had a mistake in https://github.com/bottlerocket-os/twoliter/pull/112 commit 67ef875. The check to make sure the version Release.toml matched the release version in Twoliter.toml could never succeed due to quotation marks around the version string. This fixes it and adds a test to prove that it works. --- twoliter/src/project.rs | 62 ++++++++++++++++++++++++++- twoliter/src/test/data/Release-1.toml | 1 + twoliter/src/test/data/Release-2.toml | 1 + 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 twoliter/src/test/data/Release-1.toml create mode 100644 twoliter/src/test/data/Release-2.toml diff --git a/twoliter/src/project.rs b/twoliter/src/project.rs index b81e7a7b3..f9530546a 100644 --- a/twoliter/src/project.rs +++ b/twoliter/src/project.rs @@ -226,7 +226,8 @@ impl UnvalidatedProject { return Ok(()); } } - .to_string(); + .as_str() + .context("The version in Release.toml is not a string")?; ensure!( version == self.release_version, "The version found in Release.toml, '{version}', does not match the release-version \ @@ -328,4 +329,63 @@ mod test { project.toolchain("aarch64").unwrap().to_string() ); } + + #[tokio::test] + async fn test_release_toml_check_error() { + let tempdir = TempDir::new().unwrap(); + let p = tempdir.path(); + let from = data_dir(); + let twoliter_toml_from = from.join("Twoliter-1.toml"); + let twoliter_toml_to = p.join("Twoliter.toml"); + let release_toml_from = from.join("Release-2.toml"); + let release_toml_to = p.join("Release.toml"); + fs::copy(&twoliter_toml_from, &twoliter_toml_to) + .await + .expect(&format!( + "Unable to copy {} to {}", + twoliter_toml_from.display(), + twoliter_toml_to.display() + )); + fs::copy(&release_toml_from, &release_toml_to) + .await + .expect(&format!( + "Unable to copy {} to {}", + release_toml_from.display(), + release_toml_to.display() + )); + let result = Project::find_and_load(&p).await; + assert!( + result.is_err(), + "Expected the loading of the project to fail because of a mismatched version in \ + Release.toml, but the project loaded without an error." + ); + } + + #[tokio::test] + async fn test_release_toml_check_ok() { + let tempdir = TempDir::new().unwrap(); + let p = tempdir.path(); + let from = data_dir(); + let twoliter_toml_from = from.join("Twoliter-1.toml"); + let twoliter_toml_to = p.join("Twoliter.toml"); + let release_toml_from = from.join("Release-1.toml"); + let release_toml_to = p.join("Release.toml"); + fs::copy(&twoliter_toml_from, &twoliter_toml_to) + .await + .expect(&format!( + "Unable to copy {} to {}", + twoliter_toml_from.display(), + twoliter_toml_to.display() + )); + fs::copy(&release_toml_from, &release_toml_to) + .await + .expect(&format!( + "Unable to copy {} to {}", + release_toml_from.display(), + release_toml_to.display() + )); + + // The project should load because Release.toml and Twoliter.toml versions match. + Project::find_and_load(&p).await.unwrap(); + } } diff --git a/twoliter/src/test/data/Release-1.toml b/twoliter/src/test/data/Release-1.toml new file mode 100644 index 000000000..11a716ec1 --- /dev/null +++ b/twoliter/src/test/data/Release-1.toml @@ -0,0 +1 @@ +version = "1.0.0" diff --git a/twoliter/src/test/data/Release-2.toml b/twoliter/src/test/data/Release-2.toml new file mode 100644 index 000000000..1b236407f --- /dev/null +++ b/twoliter/src/test/data/Release-2.toml @@ -0,0 +1 @@ +version = "2.0.0"