diff --git a/cargo-shuttle/src/init.rs b/cargo-shuttle/src/init.rs index da9522481..b61efd8d2 100644 --- a/cargo-shuttle/src/init.rs +++ b/cargo-shuttle/src/init.rs @@ -36,6 +36,7 @@ impl ShuttleInit for ShuttleInitAxum { dependencies, manifest_path, url, + false, get_dependency_version_fn, ); @@ -49,6 +50,7 @@ impl ShuttleInit for ShuttleInitAxum { dependencies, manifest_path, url, + false, get_dependency_version_fn, ); } @@ -87,6 +89,7 @@ impl ShuttleInit for ShuttleInitRocket { dependencies, manifest_path, url, + true, get_dependency_version_fn, ); @@ -137,6 +140,7 @@ impl ShuttleInit for ShuttleInitTide { dependencies, manifest_path, url, + false, get_dependency_version_fn, ); } @@ -176,6 +180,7 @@ impl ShuttleInit for ShuttleInitPoem { dependencies, manifest_path, url, + false, get_dependency_version_fn, ); } @@ -219,6 +224,7 @@ impl ShuttleInit for ShuttleInitTower { dependencies, manifest_path, url, + false, get_dependency_version_fn, ); @@ -229,6 +235,7 @@ impl ShuttleInit for ShuttleInitTower { dependencies, manifest_path, url, + false, get_dependency_version_fn, ); @@ -356,6 +363,7 @@ pub fn cargo_shuttle_init(path: PathBuf, framework: Box) -> Res &mut dependencies, &manifest_path, &url, + false, get_latest_dependency_version, ); @@ -389,9 +397,11 @@ fn set_key_value_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, manifest_path, url); + let dependency_version = + get_dependency_version_fn(crate_name, flag_allow_prerelease, manifest_path, url); dependencies[crate_name] = value(dependency_version); } @@ -402,9 +412,11 @@ 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, 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); } @@ -420,13 +432,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 { - let latest_version = get_latest_dependency(crate_name, false, 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"); @@ -478,6 +496,7 @@ mod shuttle_init_tests { fn mock_get_latest_dependency_version( _crate_name: &str, + _flag_allow_prerelease: bool, _manifest_path: &Path, _url: &Url, ) -> String { @@ -535,6 +554,7 @@ mod shuttle_init_tests { dependencies, &manifest_path, &url, + false, mock_get_latest_dependency_version, ); @@ -558,6 +578,7 @@ mod shuttle_init_tests { dependencies, &manifest_path, &url, + false, mock_get_latest_dependency_version, ); @@ -581,6 +602,7 @@ mod shuttle_init_tests { dependencies, &manifest_path, &url, + false, mock_get_latest_dependency_version, ); @@ -613,6 +635,7 @@ mod shuttle_init_tests { dependencies, &manifest_path, &url, + false, mock_get_latest_dependency_version, ); @@ -644,6 +667,7 @@ mod shuttle_init_tests { dependencies, &manifest_path, &url, + false, mock_get_latest_dependency_version, ); @@ -675,6 +699,7 @@ mod shuttle_init_tests { dependencies, &manifest_path, &url, + false, mock_get_latest_dependency_version, ); @@ -707,6 +732,7 @@ mod shuttle_init_tests { dependencies, &manifest_path, &url, + false, mock_get_latest_dependency_version, ); @@ -725,4 +751,30 @@ mod shuttle_init_tests { assert_eq!(cargo_toml.to_string(), expected); } + + #[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(); + + 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") + .version() + .expect("no rocket version found") + .to_string(); + + assert_eq!(version, expected); + } }