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
20 changes: 14 additions & 6 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,10 +765,22 @@ mod tests {

opts.opts.insert(
"prerelease".to_string(),
toml::Value::String("false".into()),
toml::Value::String("FALSE".into()),
);
assert!(!backend.include_prereleases(&opts));

opts.opts
.insert("prerelease".to_string(), toml::Value::String("1".into()));
assert!(backend.include_prereleases(&opts));

opts.opts
.insert("prerelease".to_string(), toml::Value::String("0".into()));
assert!(!backend.include_prereleases(&opts));

opts.opts
.insert("prerelease".to_string(), toml::Value::String("00".into()));
assert!(!backend.include_prereleases(&opts));

// Defense-in-depth: also accept a native TOML boolean, in case a future
// config path stores the value without string normalization.
opts.opts
Expand Down Expand Up @@ -2881,11 +2893,7 @@ pub(crate) fn mark_prerelease(mut version: VersionInfo) -> VersionInfo {
}

fn tool_option_bool(value: &toml::Value) -> bool {
match value {
toml::Value::Boolean(b) => *b,
toml::Value::String(s) => s.parse::<bool>().unwrap_or(false),
_ => false,
}
crate::backend::options::bool_value_or_default("prerelease", value, false)
}

/// Fuzzy-match `versions` against `query` with PEP 440 prerelease detection
Expand Down
118 changes: 115 additions & 3 deletions src/backend/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,18 @@ impl<'a> BackendOptions<'a> {

pub(crate) fn platform_bool_for_target(&self, key: &str, target: &PlatformTarget) -> bool {
self.platform_string_for_target(key, target)
.is_some_and(|v| is_truthy(&v))
.is_some_and(|v| bool_str_or_default(key, &v, false))
}

pub(crate) fn bool(&self, key: &str) -> bool {
self.raw.get_string(key).is_some_and(|v| is_truthy(&v))
self.bool_with_default(key, false)
}

pub(crate) fn bool_with_default(&self, key: &str, default: bool) -> bool {
self.raw
.opts
.get(key)
.map_or(default, |value| bool_value_or_default(key, value, default))
}

pub(crate) fn available_platforms_with_key(&self, key: &str) -> Vec<String> {
Expand All @@ -60,14 +67,119 @@ impl<'a> BackendOptions<'a> {
}

pub(crate) fn is_truthy(value: &str) -> bool {
matches!(value.trim(), "true" | "1")
matches!(value.trim().to_ascii_lowercase().as_str(), "true" | "1")
}

pub(crate) fn is_falsey(value: &str) -> bool {
matches!(value.trim().to_ascii_lowercase().as_str(), "false" | "0")
}

pub(crate) fn bool_value_or_default(key: &str, value: &toml::Value, default: bool) -> bool {
bool_value(key, value).unwrap_or(default)
}

pub(crate) fn bool_value(key: &str, value: &toml::Value) -> Option<bool> {
let parsed = match value {
toml::Value::Boolean(value) => Some(*value),
toml::Value::String(value) => parse_bool_str(value),
toml::Value::Integer(0) => Some(false),
toml::Value::Integer(1) => Some(true),
_ => None,
};
if parsed.is_none() {
warn_invalid_bool_value(key, value);
}
parsed
}

fn bool_str_or_default(key: &str, value: &str, default: bool) -> bool {
parse_bool_str(value).unwrap_or_else(|| {
warn_invalid_bool_value(key, value);
default
})
}

fn parse_bool_str(value: &str) -> Option<bool> {
if is_truthy(value) {
Some(true)
} else if is_falsey(value) {
Some(false)
} else {
None
}
}

fn warn_invalid_bool_value(key: &str, value: impl std::fmt::Display) {
warn!(
"invalid boolean value for tool option `{key}`: {value}; expected true, false, 1, or 0; using default"
);
}

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

fn opts_with_value(key: &str, value: toml::Value) -> ToolVersionOptions {
let mut opts = ToolVersionOptions::default();
opts.opts.insert(key.to_string(), value);
opts
}

#[test]
fn test_bool_parses_consistent_formats() {
assert!(
BackendOptions::new(&opts_with_value("flag", toml::Value::Boolean(true))).bool("flag")
);
assert!(
!BackendOptions::new(&opts_with_value("flag", toml::Value::Boolean(false)))
.bool("flag")
);
assert!(
BackendOptions::new(&opts_with_value("flag", toml::Value::String("TRUE".into())))
.bool("flag")
);
assert!(
!BackendOptions::new(&opts_with_value(
"flag",
toml::Value::String("FALSE".into())
))
.bool("flag")
);
assert!(
BackendOptions::new(&opts_with_value("flag", toml::Value::String("1".into())))
.bool("flag")
);
assert!(
!BackendOptions::new(&opts_with_value("flag", toml::Value::String("0".into())))
.bool("flag")
);
assert!(
BackendOptions::new(&opts_with_value("flag", toml::Value::Integer(1))).bool("flag")
);
assert!(
!BackendOptions::new(&opts_with_value("flag", toml::Value::Integer(0))).bool("flag")
);
}

#[test]
fn test_bool_invalid_values_fall_back_to_default() {
assert!(!BackendOptions::new(&ToolVersionOptions::default()).bool("missing"));
assert!(
!BackendOptions::new(&opts_with_value("flag", toml::Value::String("00".into())))
.bool("flag")
);
assert!(
BackendOptions::new(&opts_with_value("flag", toml::Value::String("00".into())))
.bool_with_default("flag", true)
);
assert!(
BackendOptions::new(&opts_with_value("flag", toml::Value::Integer(2)))
.bool_with_default("flag", true)
);
assert_eq!(bool_value("flag", &toml::Value::String("00".into())), None);
}

#[test]
fn test_platform_bool_for_target_uses_requested_target() {
let mut opts = ToolVersionOptions::default();
Expand Down
67 changes: 60 additions & 7 deletions src/plugins/core/dotnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ use versions::Versioning;

use crate::backend::Backend;
use crate::backend::VersionInfo;
use crate::backend::options::BackendOptions;
use crate::cli::args::BackendArg;
use crate::cmd::CmdLineRunner;
use crate::config::{Config, Settings};
use crate::http::{HTTP, HTTP_FETCH};
use crate::install_context::InstallContext;
use crate::parallel;
use crate::toolset::{ToolVersion, Toolset};
use crate::toolset::{ToolVersion, ToolVersionOptions, Toolset};
use crate::ui::progress_report::SingleReport;
use crate::{dirs, env, file, plugins};

Expand All @@ -24,6 +25,27 @@ pub struct DotnetPlugin {
ba: Arc<BackendArg>,
}

#[derive(Debug, Clone, Copy)]
struct DotnetOptions<'a> {
values: BackendOptions<'a>,
}

impl<'a> DotnetOptions<'a> {
fn new(raw: &'a ToolVersionOptions) -> Self {
Self {
values: BackendOptions::new(raw),
}
}

fn runtime(&self) -> Option<&'a str> {
self.values.str("runtime")
}

fn runtime_framework_name(&self) -> Option<&'static str> {
self.runtime().and_then(runtime_framework_name)
}
}

impl DotnetPlugin {
pub fn new() -> Self {
Self {
Expand All @@ -36,7 +58,9 @@ impl DotnetPlugin {
}

async fn test_dotnet(&self, ctx: &InstallContext, tv: &ToolVersion) -> Result<()> {
if tv.request.options().get("runtime").is_some() {
let raw_opts = tv.request.options();
let opts = DotnetOptions::new(&raw_opts);
if opts.runtime().is_some() {
// Skip version check for runtime-only installs — `dotnet --version` exits non-zero without an SDK
return Ok(());
}
Expand Down Expand Up @@ -126,9 +150,11 @@ impl Backend for DotnetPlugin {
file::create_dir_all(&install_dir)?;

// Read and validate runtime options
let runtime = tv.request.options().get("runtime").map(|s| s.to_string());
let raw_opts = tv.request.options();
let opts = DotnetOptions::new(&raw_opts);
let runtime = opts.runtime().map(str::to_string);
if let Some(ref rt) = runtime
&& runtime_framework_name(rt).is_none()
&& opts.runtime_framework_name().is_none()
{
return Err(eyre::eyre!(
"Invalid runtime option '{}'. Valid options: dotnet, aspnetcore, windowsdesktop",
Expand Down Expand Up @@ -176,10 +202,11 @@ impl Backend for DotnetPlugin {
if Self::is_isolated() {
// Isolated: mise handles removal of install_path by default
} else {
let runtime = tv.request.options().get("runtime").map(|s| s.to_string());
if let Some(rt) = runtime {
let raw_opts = tv.request.options();
let opts = DotnetOptions::new(&raw_opts);
if opts.runtime().is_some() {
// Runtime: remove the shared runtime directory for this version
let Some(framework) = runtime_framework_name(&rt) else {
let Some(framework) = opts.runtime_framework_name() else {
return Ok(());
};
let runtime_dir = dotnet_root()
Expand Down Expand Up @@ -386,3 +413,29 @@ impl PartialOrd for SortedVersion {
Some(self.cmp(other))
}
}

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

fn opts_with_runtime(runtime: &str) -> ToolVersionOptions {
let mut opts = ToolVersionOptions::default();
opts.opts.insert(
"runtime".to_string(),
toml::Value::String(runtime.to_string()),
);
opts
}

#[test]
fn dotnet_options_reads_runtime() {
let opts = opts_with_runtime("aspnetcore");
let parsed = DotnetOptions::new(&opts);

assert_eq!(parsed.runtime(), Some("aspnetcore"));
assert_eq!(
parsed.runtime_framework_name(),
Some("Microsoft.AspNetCore.App")
);
}
}
Loading
Loading