Skip to content
Merged
Show file tree
Hide file tree
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
55 changes: 54 additions & 1 deletion crates/rb-sys-env/src/ruby_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl RubyVersion {

let mut ruby_version = env_ruby_version
.split('.')
.map(|s| s.parse().expect("version component is not a number"));
.map(|s| parse_version_component(s, "RUBY_VERSION"));

Self {
major: ruby_version.next().expect("major"),
Expand All @@ -140,9 +140,20 @@ impl RubyVersion {
}
}

fn parse_version_component(value: &str, name: &str) -> u8 {
let digits: String = value.chars().take_while(|c| c.is_ascii_digit()).collect();
if digits.is_empty() {
panic!("{} is not a number", name);
}
digits.parse().expect("version component is not a number")
}

#[cfg(test)]
mod tests {
use super::*;
use std::sync::Mutex;

static ENV_LOCK: Mutex<()> = Mutex::new(());

#[test]
fn test_equality_from_tuple() {
Expand All @@ -162,4 +173,46 @@ mod tests {
RubyVersion::from((3, 0, 0))
);
}

#[test]
fn test_from_hashmap_with_ruby_version_suffix() {
let mut env = HashMap::new();
env.insert("ruby_version".to_string(), "4.1.0+1".to_string());

assert_eq!(
RubyVersion::from_raw_environment(&env),
RubyVersion::from((4, 1, 0))
);
}

#[test]
fn test_from_env_with_ruby_version_suffix() {
let _guard = ENV_LOCK.lock().unwrap();
let previous = std::env::var("RUBY_VERSION").ok();

std::env::set_var("RUBY_VERSION", "4.1.0+1");
let env = HashMap::new();

let parsed = RubyVersion::from_raw_environment(&env);

if let Some(value) = previous {
std::env::set_var("RUBY_VERSION", value);
} else {
std::env::remove_var("RUBY_VERSION");
}

assert_eq!(parsed, RubyVersion::from((4, 1, 0)));
}

#[test]
fn test_from_hashmap_rejects_major_suffix() {
let mut env = HashMap::new();
env.insert("MAJOR".to_string(), "4+1".to_string());
env.insert("MINOR".to_string(), "1".to_string());
env.insert("TEENY".to_string(), "0".to_string());

let result = std::panic::catch_unwind(|| RubyVersion::from_raw_environment(&env));

assert!(result.is_err());
}
}
13 changes: 13 additions & 0 deletions crates/rb-sys/build/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,16 @@ impl std::fmt::Display for Version {
write!(f, "{}.{}", self.0, self.1)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_current_with_ruby_version_suffix() {
let mut rbconfig = RbConfig::default();
rbconfig.set_value_for_key("ruby_version", "4.1.0+1".to_string());

assert_eq!(Version::current(&rbconfig), Version::new(4, 1));
}
}
10 changes: 5 additions & 5 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading