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 2 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
53 changes: 47 additions & 6 deletions cargo-shuttle/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl ShuttleInit for ShuttleInitAxum {
manifest_path,
url,
get_dependency_version_fn,
false,
);

set_inline_table_dependency_features(
Expand All @@ -50,6 +51,7 @@ impl ShuttleInit for ShuttleInitAxum {
manifest_path,
url,
get_dependency_version_fn,
false,
);
}

Expand Down Expand Up @@ -88,6 +90,7 @@ impl ShuttleInit for ShuttleInitRocket {
manifest_path,
url,
get_dependency_version_fn,
true,
);

set_inline_table_dependency_features(
Expand Down Expand Up @@ -138,6 +141,7 @@ impl ShuttleInit for ShuttleInitTide {
manifest_path,
url,
get_dependency_version_fn,
false,
);
}

Expand Down Expand Up @@ -177,6 +181,7 @@ impl ShuttleInit for ShuttleInitPoem {
manifest_path,
url,
get_dependency_version_fn,
false,
);
}

Expand Down Expand Up @@ -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);
coszio marked this conversation as resolved.
Show resolved Hide resolved
dependencies[crate_name] = value(dependency_version);
}

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

Expand All @@ -420,13 +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 {
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 +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 {
Expand Down Expand Up @@ -559,6 +573,7 @@ mod shuttle_init_tests {
&manifest_path,
&url,
mock_get_latest_dependency_version,
false,
);

let expected = indoc! {r#"
Expand Down Expand Up @@ -725,4 +740,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 shuttle-service version found")
chesedo marked this conversation as resolved.
Show resolved Hide resolved
.to_string();

assert_eq!(version, expected);
}
}