Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: init rocket dependency version #314

Merged
merged 5 commits into from
Aug 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 58 additions & 6 deletions cargo-shuttle/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl ShuttleInit for ShuttleInitAxum {
dependencies,
manifest_path,
url,
false,
get_dependency_version_fn,
);

Expand All @@ -49,6 +50,7 @@ impl ShuttleInit for ShuttleInitAxum {
dependencies,
manifest_path,
url,
false,
get_dependency_version_fn,
);
}
Expand Down Expand Up @@ -87,6 +89,7 @@ impl ShuttleInit for ShuttleInitRocket {
dependencies,
manifest_path,
url,
true,
get_dependency_version_fn,
);

Expand Down Expand Up @@ -137,6 +140,7 @@ impl ShuttleInit for ShuttleInitTide {
dependencies,
manifest_path,
url,
false,
get_dependency_version_fn,
);
}
Expand Down Expand Up @@ -176,6 +180,7 @@ impl ShuttleInit for ShuttleInitPoem {
dependencies,
manifest_path,
url,
false,
get_dependency_version_fn,
);
}
Expand Down Expand Up @@ -219,6 +224,7 @@ impl ShuttleInit for ShuttleInitTower {
dependencies,
manifest_path,
url,
false,
get_dependency_version_fn,
);

Expand All @@ -229,6 +235,7 @@ impl ShuttleInit for ShuttleInitTower {
dependencies,
manifest_path,
url,
false,
get_dependency_version_fn,
);

Expand Down Expand Up @@ -356,6 +363,7 @@ pub fn cargo_shuttle_init(path: PathBuf, framework: Box<dyn ShuttleInit>) -> Res
&mut dependencies,
&manifest_path,
&url,
false,
get_latest_dependency_version,
);

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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");
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -535,6 +554,7 @@ mod shuttle_init_tests {
dependencies,
&manifest_path,
&url,
false,
mock_get_latest_dependency_version,
);

Expand All @@ -558,6 +578,7 @@ mod shuttle_init_tests {
dependencies,
&manifest_path,
&url,
false,
mock_get_latest_dependency_version,
);

Expand All @@ -581,6 +602,7 @@ mod shuttle_init_tests {
dependencies,
&manifest_path,
&url,
false,
mock_get_latest_dependency_version,
);

Expand Down Expand Up @@ -613,6 +635,7 @@ mod shuttle_init_tests {
dependencies,
&manifest_path,
&url,
false,
mock_get_latest_dependency_version,
);

Expand Down Expand Up @@ -644,6 +667,7 @@ mod shuttle_init_tests {
dependencies,
&manifest_path,
&url,
false,
mock_get_latest_dependency_version,
);

Expand Down Expand Up @@ -675,6 +699,7 @@ mod shuttle_init_tests {
dependencies,
&manifest_path,
&url,
false,
mock_get_latest_dependency_version,
);

Expand Down Expand Up @@ -707,6 +732,7 @@ mod shuttle_init_tests {
dependencies,
&manifest_path,
&url,
false,
mock_get_latest_dependency_version,
);

Expand All @@ -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);
}
}