diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 0c2c5bc49..16e872f4a 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -274,11 +274,11 @@ pub trait Backend: Debug + Send + Sync { } fn list_installed_versions_matching(&self, query: &str) -> eyre::Result> { 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> { let versions = self.list_remote_versions()?; - fuzzy_match_filter(versions, query) + self.fuzzy_match_filter(versions, query) } fn latest_version(&self, query: Option) -> eyre::Result> { match query { @@ -501,27 +501,29 @@ pub trait Backend: Debug + Send + Sync { fn dependency_env(&self) -> eyre::Result> { self.depedency_toolset()?.full_env() } -} -fn fuzzy_match_filter(versions: Vec, query: &str) -> eyre::Result> { - let mut query = query; - if query == "latest" { - query = "v?[0-9].*"; + fn fuzzy_match_filter(&self, versions: Vec, query: &str) -> eyre::Result> { + 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 { diff --git a/src/plugins/core/java.rs b/src/plugins/core/java.rs index 690f769da..2c3223613 100644 --- a/src/plugins/core/java.rs +++ b/src/plugins/core/java.rs @@ -315,12 +315,12 @@ impl Backend for JavaPlugin { fn list_installed_versions_matching(&self, query: &str) -> eyre::Result> { 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> { let versions = self.list_remote_versions()?; - fuzzy_match_filter(versions, query) + self.fuzzy_match_filter(versions, query) } fn get_aliases(&self) -> Result> { @@ -391,6 +391,29 @@ impl Backend for JavaPlugin { )]); Ok(map) } + + fn fuzzy_match_filter(&self, versions: Vec, query: &str) -> eyre::Result> { + 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 { @@ -413,27 +436,6 @@ fn arch() -> &'static str { } } -fn fuzzy_match_filter(versions: Vec, query: &str) -> eyre::Result> { - 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 { diff --git a/src/plugins/core/ruby.rs b/src/plugins/core/ruby.rs index 65771b7c5..99724a7ee 100644 --- a/src/plugins/core/ruby.rs +++ b/src/plugins/core/ruby.rs @@ -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!(