Skip to content

Commit

Permalink
fix: cannot install truffelruby (#2581)
Browse files Browse the repository at this point in the history
  • Loading branch information
roele authored Sep 15, 2024
1 parent bc4fae3 commit 0c88ede
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 44 deletions.
44 changes: 23 additions & 21 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,11 @@ pub trait Backend: Debug + Send + Sync {
}
fn list_installed_versions_matching(&self, query: &str) -> eyre::Result<Vec<String>> {
let versions = self.list_installed_versions()?;
fuzzy_match_filter(versions, query)
self.fuzzy_match_filter(versions, query)
}
fn list_versions_matching(&self, query: &str) -> eyre::Result<Vec<String>> {
let versions = self.list_remote_versions()?;
fuzzy_match_filter(versions, query)
self.fuzzy_match_filter(versions, query)
}
fn latest_version(&self, query: Option<String>) -> eyre::Result<Option<String>> {
match query {
Expand Down Expand Up @@ -501,27 +501,29 @@ pub trait Backend: Debug + Send + Sync {
fn dependency_env(&self) -> eyre::Result<BTreeMap<String, String>> {
self.depedency_toolset()?.full_env()
}
}

fn fuzzy_match_filter(versions: Vec<String>, query: &str) -> eyre::Result<Vec<String>> {
let mut query = query;
if query == "latest" {
query = "v?[0-9].*";
fn fuzzy_match_filter(&self, versions: Vec<String>, query: &str) -> eyre::Result<Vec<String>> {
let escaped_query = regex::escape(query);
let query = if query == "latest" {
"v?[0-9].*"
} else {
&escaped_query
};
let query_regex = Regex::new(&format!("^{}([-.].+)?$", query))?;
let versions = versions
.into_iter()
.filter(|v| {
if query == v {
return true;
}
if VERSION_REGEX.is_match(v) {
return false;
}
query_regex.is_match(v)
})
.collect();
Ok(versions)
}
let query_regex = Regex::new(&format!("^{}([-.].+)?$", query))?;
let versions = versions
.into_iter()
.filter(|v| {
if query == v {
return true;
}
if VERSION_REGEX.is_match(v) {
return false;
}
query_regex.is_match(v)
})
.collect();
Ok(versions)
}

fn find_match_in_list(list: &[String], query: &str) -> Option<String> {
Expand Down
48 changes: 25 additions & 23 deletions src/plugins/core/java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,12 @@ impl Backend for JavaPlugin {

fn list_installed_versions_matching(&self, query: &str) -> eyre::Result<Vec<String>> {
let versions = self.list_installed_versions()?;
fuzzy_match_filter(versions, query)
self.fuzzy_match_filter(versions, query)
}

fn list_versions_matching(&self, query: &str) -> eyre::Result<Vec<String>> {
let versions = self.list_remote_versions()?;
fuzzy_match_filter(versions, query)
self.fuzzy_match_filter(versions, query)
}

fn get_aliases(&self) -> Result<BTreeMap<String, String>> {
Expand Down Expand Up @@ -391,6 +391,29 @@ impl Backend for JavaPlugin {
)]);
Ok(map)
}

fn fuzzy_match_filter(&self, versions: Vec<String>, query: &str) -> eyre::Result<Vec<String>> {
let escaped_query = regex::escape(query);
let query = if query == "latest" {
"[0-9].*"
} else {
&escaped_query
};
let query_regex = Regex::new(&format!("^{}([+-.].+)?$", query))?;
let versions = versions
.into_iter()
.filter(|v| {
if query == v {
return true;
}
if VERSION_REGEX.is_match(v) {
return false;
}
query_regex.is_match(v)
})
.collect();
Ok(versions)
}
}

fn os() -> &'static str {
Expand All @@ -413,27 +436,6 @@ fn arch() -> &'static str {
}
}

fn fuzzy_match_filter(versions: Vec<String>, query: &str) -> eyre::Result<Vec<String>> {
let mut query = query;
if query == "latest" {
query = "[0-9].*";
}
let query_regex = Regex::new(&format!("^{}([+-.].+)?$", query))?;
let versions = versions
.into_iter()
.filter(|v| {
if query == v {
return true;
}
if VERSION_REGEX.is_match(v) {
return false;
}
query_regex.is_match(v)
})
.collect();
Ok(versions)
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[serde(default)]
struct JavaMetadata {
Expand Down
23 changes: 23 additions & 0 deletions src/plugins/core/ruby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,29 @@ mod tests {

use super::*;

#[test]
fn test_list_versions_matching() {
let plugin = RubyPlugin::new();
assert!(
!plugin.list_versions_matching("3").unwrap().is_empty(),
"versions for 3 should not be empty"
);
assert!(
!plugin
.list_versions_matching("truffleruby-24")
.unwrap()
.is_empty(),
"versions for truffleruby-24 should not be empty"
);
assert!(
!plugin
.list_versions_matching("truffleruby+graalvm-24")
.unwrap()
.is_empty(),
"versions for truffleruby+graalvm-24 should not be empty"
);
}

#[test]
fn test_parse_gemfile() {
assert_eq!(
Expand Down

0 comments on commit 0c88ede

Please sign in to comment.