From 51c2ae0a28356301a68a2027c201592c84bd39d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dario=20Vladovi=C4=87?= Date: Thu, 15 Apr 2021 08:48:12 +0200 Subject: fix(elixir): correctly parse dev and rc versions (#2573) --- src/modules/elixir.rs | 48 +++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) (limited to 'src/modules/elixir.rs') diff --git a/src/modules/elixir.rs b/src/modules/elixir.rs index 8d823655e..fa642c085 100644 --- a/src/modules/elixir.rs +++ b/src/modules/elixir.rs @@ -4,12 +4,7 @@ use crate::configs::elixir::ElixirConfig; use crate::formatter::StringFormatter; use once_cell::sync::Lazy; -use regex::Regex; use std::ops::Deref; -const ELIXIR_VERSION_PATTERN: &str = "\ -Erlang/OTP (?P\\d+)[^\\n]+ - -Elixir (?P\\d[.\\d]+).*"; /// Create a module with the current Elixir version pub fn module<'a>(context: &'a Context) -> Option> { @@ -73,13 +68,15 @@ fn get_elixir_version(context: &Context) -> Option<(String, String)> { } fn parse_elixir_version(version: &str) -> Option<(String, String)> { - let version_regex = Regex::new(ELIXIR_VERSION_PATTERN).ok()?; - let captures = version_regex.captures(version)?; - - let otp_version = captures["otp"].to_owned(); - let elixir_version = captures["elixir"].to_owned(); - - Some((otp_version, elixir_version)) + let mut lines = version.lines(); + // split line into ["Erlang/OTP", "22", "[erts-10.5]", ...], take "22" + let otp_version = lines.next()?.split_whitespace().nth(1)?; + // skip empty line + let _ = lines.next()?; + // split line into ["Elixir", "1.10", "(compiled", ...], take "1.10" + let elixir_version = lines.next()?.split_whitespace().nth(1)?; + + Some((otp_version.to_string(), elixir_version.to_string())) } #[cfg(test)] @@ -92,15 +89,32 @@ mod tests { #[test] fn test_parse_elixir_version() { - const OUTPUT: &str = "\ -Erlang/OTP 22 [erts-10.5] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe] + let stable_input = "\ +Erlang/OTP 23 [erts-11.1.7] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] + +Elixir 1.11.3 (compiled with Erlang/OTP 21) +"; + let rc_input = "\ +Erlang/OTP 23 [erts-11.1.7] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] -Elixir 1.10 (compiled with Erlang/OTP 22) +Elixir 1.12.0-rc.0 (31d2b99) (compiled with Erlang/OTP 21) "; + let dev_input = "\ +Erlang/OTP 23 [erts-11.1.7] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] +Elixir 1.13.0-dev (compiled with Erlang/OTP 23) +"; + assert_eq!( + parse_elixir_version(stable_input), + Some(("23".to_string(), "1.11.3".to_string())) + ); + assert_eq!( + parse_elixir_version(rc_input), + Some(("23".to_string(), "1.12.0-rc.0".to_string())) + ); assert_eq!( - parse_elixir_version(OUTPUT), - Some(("22".to_owned(), "1.10".to_owned())) + parse_elixir_version(dev_input), + Some(("23".to_string(), "1.13.0-dev".to_string())) ); } -- cgit v1.2.3