From acf8bd658435370d67c6de4bc1cee5424139f11d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Coss=C3=ADo?= Date: Tue, 23 Aug 2022 13:07:49 -0500 Subject: [PATCH 1/5] fix: rocket dependency version fixes #313, which needed the prerelease version of `rocket` --- cargo-shuttle/src/init.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/cargo-shuttle/src/init.rs b/cargo-shuttle/src/init.rs index da9522481..6fb4536b8 100644 --- a/cargo-shuttle/src/init.rs +++ b/cargo-shuttle/src/init.rs @@ -425,7 +425,10 @@ type GetDependencyVersionFn = fn(&str, &Path, &Url) -> String; /// Gets the latest version for a dependency of `crate_name`. /// This is a wrapper function for `cargo_edit::get_latest_dependency` function. fn get_latest_dependency_version(crate_name: &str, manifest_path: &Path, url: &Url) -> String { - let latest_version = get_latest_dependency(crate_name, false, manifest_path, Some(url)) + // some crates need to be on the prerelease version + let flag_allow_prerelease = matches!(crate_name, "rocket"); + + let latest_version = get_latest_dependency(crate_name, flag_allow_prerelease, manifest_path, Some(url)) .unwrap_or_else(|_| panic!("Could not query the latest version of {}", crate_name)); let latest_version = latest_version .version() @@ -725,4 +728,22 @@ mod shuttle_init_tests { assert_eq!(cargo_toml.to_string(), expected); } + + #[test] + fn test_get_latest_dependency_version_rocket() { + + let manifest_path = PathBuf::new(); + + let url = Url::parse("https://github.com/rust-lang/crates.io-index").unwrap(); + + let version = get_latest_dependency_version("rocket", &manifest_path, &url); + + let expected = get_latest_dependency("rocket", true, &manifest_path, Some(&url)) + .expect("Could not query the latest version of rocket") + .version() + .expect("no shuttle-service version found") + .to_string(); + + assert_eq!(version, expected); + } } From 3e73dd4bdfb83a499570f3e560965610cce22376 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Coss=C3=ADo?= Date: Wed, 24 Aug 2022 16:00:09 -0500 Subject: [PATCH 2/5] refactor: make `flag_allow_prerelease` a function argument --- cargo-shuttle/src/init.rs | 44 ++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/cargo-shuttle/src/init.rs b/cargo-shuttle/src/init.rs index 6fb4536b8..697b482f7 100644 --- a/cargo-shuttle/src/init.rs +++ b/cargo-shuttle/src/init.rs @@ -37,6 +37,7 @@ impl ShuttleInit for ShuttleInitAxum { manifest_path, url, get_dependency_version_fn, + false, ); set_inline_table_dependency_features( @@ -50,6 +51,7 @@ impl ShuttleInit for ShuttleInitAxum { manifest_path, url, get_dependency_version_fn, + false, ); } @@ -88,6 +90,7 @@ impl ShuttleInit for ShuttleInitRocket { manifest_path, url, get_dependency_version_fn, + true, ); set_inline_table_dependency_features( @@ -138,6 +141,7 @@ impl ShuttleInit for ShuttleInitTide { manifest_path, url, get_dependency_version_fn, + false, ); } @@ -177,6 +181,7 @@ impl ShuttleInit for ShuttleInitPoem { manifest_path, url, get_dependency_version_fn, + false, ); } @@ -390,8 +395,10 @@ fn set_key_value_dependency_version( manifest_path: &Path, url: &Url, get_dependency_version_fn: GetDependencyVersionFn, + flag_allow_prerelease: bool, ) { - let dependency_version = get_dependency_version_fn(crate_name, manifest_path, url); + let dependency_version = + get_dependency_version_fn(crate_name, flag_allow_prerelease, manifest_path, url); dependencies[crate_name] = value(dependency_version); } @@ -404,7 +411,7 @@ fn set_inline_table_dependency_version( url: &Url, get_dependency_version_fn: GetDependencyVersionFn, ) { - let dependency_version = get_dependency_version_fn(crate_name, manifest_path, url); + let dependency_version = get_dependency_version_fn(crate_name, false, manifest_path, url); dependencies[crate_name]["version"] = value(dependency_version); } @@ -420,16 +427,19 @@ fn set_inline_table_dependency_features( } /// Abstract type for `get_latest_dependency_version` function. -type GetDependencyVersionFn = fn(&str, &Path, &Url) -> String; +type GetDependencyVersionFn = fn(&str, bool, &Path, &Url) -> String; /// Gets the latest version for a dependency of `crate_name`. /// This is a wrapper function for `cargo_edit::get_latest_dependency` function. -fn get_latest_dependency_version(crate_name: &str, manifest_path: &Path, url: &Url) -> String { - // some crates need to be on the prerelease version - let flag_allow_prerelease = matches!(crate_name, "rocket"); - - let latest_version = get_latest_dependency(crate_name, flag_allow_prerelease, manifest_path, Some(url)) - .unwrap_or_else(|_| panic!("Could not query the latest version of {}", crate_name)); +fn get_latest_dependency_version( + crate_name: &str, + flag_allow_prerelease: bool, + manifest_path: &Path, + url: &Url, +) -> String { + let latest_version = + get_latest_dependency(crate_name, flag_allow_prerelease, manifest_path, Some(url)) + .unwrap_or_else(|_| panic!("Could not query the latest version of {}", crate_name)); let latest_version = latest_version .version() .expect("No latest shuttle-service version available"); @@ -481,6 +491,7 @@ mod shuttle_init_tests { fn mock_get_latest_dependency_version( _crate_name: &str, + _flag_allow_prerelease: bool, _manifest_path: &Path, _url: &Url, ) -> String { @@ -562,6 +573,7 @@ mod shuttle_init_tests { &manifest_path, &url, mock_get_latest_dependency_version, + false, ); let expected = indoc! {r#" @@ -730,13 +742,21 @@ mod shuttle_init_tests { } #[test] + /// Makes sure that Rocket uses allow_prerelease flag when fetching the latest version fn test_get_latest_dependency_version_rocket() { - + let mut cargo_toml = cargo_toml_factory(); + let dependencies = cargo_toml["dependencies"].as_table_mut().unwrap(); let manifest_path = PathBuf::new(); - let url = Url::parse("https://github.com/rust-lang/crates.io-index").unwrap(); - let version = get_latest_dependency_version("rocket", &manifest_path, &url); + ShuttleInitRocket.set_cargo_dependencies( + dependencies, + &manifest_path, + &url, + get_latest_dependency_version, + ); + + let version = dependencies["rocket"].as_str().unwrap(); let expected = get_latest_dependency("rocket", true, &manifest_path, Some(&url)) .expect("Could not query the latest version of rocket") From e5f3cdee2a37ee4ec2803d0d5fb564c578763bc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Coss=C3=ADo?= Date: Wed, 24 Aug 2022 16:21:44 -0500 Subject: [PATCH 3/5] refactor: make `flag_allow_prerelease` an argument in `set_inline_table_dependency_version` --- cargo-shuttle/src/init.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/cargo-shuttle/src/init.rs b/cargo-shuttle/src/init.rs index 697b482f7..1aec9e519 100644 --- a/cargo-shuttle/src/init.rs +++ b/cargo-shuttle/src/init.rs @@ -36,8 +36,8 @@ impl ShuttleInit for ShuttleInitAxum { dependencies, manifest_path, url, - get_dependency_version_fn, false, + get_dependency_version_fn, ); set_inline_table_dependency_features( @@ -50,8 +50,8 @@ impl ShuttleInit for ShuttleInitAxum { dependencies, manifest_path, url, - get_dependency_version_fn, false, + get_dependency_version_fn, ); } @@ -89,8 +89,8 @@ impl ShuttleInit for ShuttleInitRocket { dependencies, manifest_path, url, - get_dependency_version_fn, true, + get_dependency_version_fn, ); set_inline_table_dependency_features( @@ -140,8 +140,8 @@ impl ShuttleInit for ShuttleInitTide { dependencies, manifest_path, url, - get_dependency_version_fn, false, + get_dependency_version_fn, ); } @@ -180,8 +180,8 @@ impl ShuttleInit for ShuttleInitPoem { dependencies, manifest_path, url, - get_dependency_version_fn, false, + get_dependency_version_fn, ); } @@ -224,6 +224,7 @@ impl ShuttleInit for ShuttleInitTower { dependencies, manifest_path, url, + false, get_dependency_version_fn, ); @@ -234,6 +235,7 @@ impl ShuttleInit for ShuttleInitTower { dependencies, manifest_path, url, + false, get_dependency_version_fn, ); @@ -361,6 +363,7 @@ pub fn cargo_shuttle_init(path: PathBuf, framework: Box) -> Res &mut dependencies, &manifest_path, &url, + false, get_latest_dependency_version, ); @@ -394,8 +397,8 @@ fn set_key_value_dependency_version( dependencies: &mut Table, manifest_path: &Path, url: &Url, - get_dependency_version_fn: GetDependencyVersionFn, flag_allow_prerelease: bool, + get_dependency_version_fn: GetDependencyVersionFn, ) { let dependency_version = get_dependency_version_fn(crate_name, flag_allow_prerelease, manifest_path, url); @@ -409,9 +412,10 @@ fn set_inline_table_dependency_version( dependencies: &mut Table, manifest_path: &Path, url: &Url, + flag_allow_prerelease: bool, get_dependency_version_fn: GetDependencyVersionFn, ) { - let dependency_version = get_dependency_version_fn(crate_name, false, manifest_path, url); + let dependency_version = get_dependency_version_fn(crate_name, flag_allow_prerelease, manifest_path, url); dependencies[crate_name]["version"] = value(dependency_version); } @@ -549,6 +553,7 @@ mod shuttle_init_tests { dependencies, &manifest_path, &url, + false, mock_get_latest_dependency_version, ); @@ -572,8 +577,8 @@ mod shuttle_init_tests { dependencies, &manifest_path, &url, - mock_get_latest_dependency_version, false, + mock_get_latest_dependency_version, ); let expected = indoc! {r#" @@ -596,6 +601,7 @@ mod shuttle_init_tests { dependencies, &manifest_path, &url, + false, mock_get_latest_dependency_version, ); @@ -628,6 +634,7 @@ mod shuttle_init_tests { dependencies, &manifest_path, &url, + false, mock_get_latest_dependency_version, ); @@ -659,6 +666,7 @@ mod shuttle_init_tests { dependencies, &manifest_path, &url, + false, mock_get_latest_dependency_version, ); @@ -690,6 +698,7 @@ mod shuttle_init_tests { dependencies, &manifest_path, &url, + false, mock_get_latest_dependency_version, ); @@ -722,6 +731,7 @@ mod shuttle_init_tests { dependencies, &manifest_path, &url, + false, mock_get_latest_dependency_version, ); From b94cfbe4e22c4c5df2f2ef00a9e8dec4bc7398cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Coss=C3=ADo?= Date: Wed, 24 Aug 2022 16:24:40 -0500 Subject: [PATCH 4/5] fmt: cargo fmt --- cargo-shuttle/src/init.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cargo-shuttle/src/init.rs b/cargo-shuttle/src/init.rs index 1aec9e519..4c064be4d 100644 --- a/cargo-shuttle/src/init.rs +++ b/cargo-shuttle/src/init.rs @@ -415,7 +415,8 @@ fn set_inline_table_dependency_version( flag_allow_prerelease: bool, get_dependency_version_fn: GetDependencyVersionFn, ) { - let dependency_version = get_dependency_version_fn(crate_name, flag_allow_prerelease, manifest_path, url); + let dependency_version = + get_dependency_version_fn(crate_name, flag_allow_prerelease, manifest_path, url); dependencies[crate_name]["version"] = value(dependency_version); } From 3ec52bf04f2fa46d0ebba661669fc3fa4393c972 Mon Sep 17 00:00:00 2001 From: Pieter Date: Thu, 25 Aug 2022 08:28:17 +0200 Subject: [PATCH 5/5] Update cargo-shuttle/src/init.rs --- cargo-shuttle/src/init.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cargo-shuttle/src/init.rs b/cargo-shuttle/src/init.rs index 4c064be4d..b61efd8d2 100644 --- a/cargo-shuttle/src/init.rs +++ b/cargo-shuttle/src/init.rs @@ -772,7 +772,7 @@ mod shuttle_init_tests { let expected = get_latest_dependency("rocket", true, &manifest_path, Some(&url)) .expect("Could not query the latest version of rocket") .version() - .expect("no shuttle-service version found") + .expect("no rocket version found") .to_string(); assert_eq!(version, expected);