diff --git a/src/config.rs b/src/config.rs index 495b9f2b5d2..c7252dbf7d1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -36,6 +36,9 @@ use crate::{ utils::utils, }; +#[cfg(feature = "test")] +use crate::test::mock::clitools; + #[derive(Debug, ThisError)] enum OverrideFileConfigError { #[error("empty toolchain override file detected. Please remove it, or else specify the desired toolchain properties in the file")] @@ -149,7 +152,7 @@ impl Display for OverrideReason { } } -#[derive(Default, Debug)] +#[derive(Default, Debug, PartialEq)] struct OverrideCfg { toolchain: Option, components: Vec, @@ -1005,6 +1008,29 @@ impl Cfg { } } +#[cfg(feature = "test")] +impl From for Cfg { + fn from(cfg: clitools::Config) -> Self { + let rustup_dir = &cfg.rustupdir; + let dist_root_server = dist::DEFAULT_DIST_SERVER; + + Self { + rustup_dir: rustup_dir.rustupdir.clone(), + fallback_settings: None, + settings_file: SettingsFile::new(rustup_dir.join("settings.toml")), + toolchains_dir: rustup_dir.join("toolchains"), + update_hash_dir: rustup_dir.join("update-hashes"), + download_dir: rustup_dir.join("downloads"), + dist_root_url: dist_root_server.to_owned() + "/dist", + temp_cfg: temp::Cfg::new(rustup_dir.join("tmp"), dist_root_server, Box::new(|_| {})), + toolchain_override: None, + profile_override: None, + env_override: None, + notify_handler: Arc::new(|_| {}), + } + } +} + fn update_override( override_: &mut Option<(OverrideFile, OverrideReason)>, file: OverrideFile, @@ -1046,7 +1072,7 @@ enum ParseMode { mod tests { use rustup_macros::unit_test as test; - use crate::{cli::common::set_globals, utils::raw}; + use crate::{test::mock::clitools::setup_test_state, utils::raw}; use super::*; @@ -1254,8 +1280,10 @@ channel = nightly /// Checks that `rust-toolchain.toml` configs can be overridden by ` +`. /// See: #[test] - fn find_override_config_with_toolchain_override() { - let cwd = crate::test::test_dir().unwrap(); + fn toolchain_override_beats_toml() { + let test_dist_dir = crate::test::test_dist_dir().unwrap(); + let (cwd, config) = setup_test_state(test_dist_dir); + let toolchain_file = cwd.path().join("rust-toolchain.toml"); raw::write_file( &toolchain_file, @@ -1267,10 +1295,70 @@ channel = nightly ) .unwrap(); - let mut cfg = set_globals(true, false).unwrap(); + let mut cfg = Cfg::try_from(config).unwrap(); + let default_host_triple = cfg.get_default_host_triple().unwrap(); cfg.toolchain_override = Some("beta".try_into().unwrap()); let found_override = cfg.find_override_config(cwd.path()).unwrap(); - assert!(dbg!(found_override).is_none()); + let override_cfg = found_override.map(|it| it.0); + + let expected_override_cfg = OverrideCfg { + toolchain: Some(LocalToolchainName::Named(ToolchainName::Official( + ToolchainDesc { + channel: "beta".to_owned(), + date: None, + target: default_host_triple, + }, + ))), + components: vec!["rls".to_owned()], + targets: vec![], + profile: None, + }; + assert_eq!(override_cfg, Some(expected_override_cfg)); + } + + /// Checks that `rust-toolchain.toml` configs can be overridden by `RUSTUP_TOOLCHAIN`. + /// See: + #[test] + fn env_override_beats_toml() { + let test_dist_dir = crate::test::test_dist_dir().unwrap(); + let (cwd, config) = setup_test_state(test_dist_dir); + + let toolchain_file = cwd.path().join("rust-toolchain.toml"); + raw::write_file( + &toolchain_file, + r#" + [toolchain] + channel = "nightly" + components = [ "rls" ] + "#, + ) + .unwrap(); + + let mut cfg = Cfg::try_from(config).unwrap(); + let default_host_triple = cfg.get_default_host_triple().unwrap(); + cfg.env_override = Some( + ResolvableLocalToolchainName::try_from("beta") + .unwrap() + .resolve(&default_host_triple) + .unwrap(), + ); + + let found_override = cfg.find_override_config(cwd.path()).unwrap(); + let override_cfg = found_override.map(|it| it.0); + + let expected_override_cfg = OverrideCfg { + toolchain: Some(LocalToolchainName::Named(ToolchainName::Official( + ToolchainDesc { + channel: "beta".to_owned(), + date: None, + target: default_host_triple.clone(), + }, + ))), + components: vec!["rls".to_owned()], + targets: vec![], + profile: None, + }; + assert_eq!(override_cfg, Some(expected_override_cfg)); } } diff --git a/tests/suite/cli_rustup.rs b/tests/suite/cli_rustup.rs index 74aa7221366..0a1a940d630 100644 --- a/tests/suite/cli_rustup.rs +++ b/tests/suite/cli_rustup.rs @@ -2376,74 +2376,6 @@ fn only_toml_in_rust_toolchain_toml() { }); } -/// Checks that `rust-toolchain.toml` configs can be overridden by `rustup override set` or ` +`. -/// See: -#[test] -fn rust_toolchain_toml_with_rustup_override() { - test(&|config| { - config.with_scenario(Scenario::SimpleV2, &|config| { - config.expect_ok(&["rustup", "default", "stable"]); - - let stable = "hash-stable-1.1.0"; - config.expect_stdout_ok(&["rustc", "--version"], stable); - - config.expect_ok(&["rustup", "override", "set", "beta"]); - - let cwd = config.current_dir(); - let toolchain_file = cwd.join("rust-toolchain.toml"); - raw::write_file( - &toolchain_file, - r#" - [toolchain] - channel = "nightly" - components = [ "rls" ] - "#, - ) - .unwrap(); - - let beta = "hash-beta-1.2.0"; - config.expect_stdout_ok(&["rustc", "--version"], beta); - config.expect_stdout_ok(&["rls", "--version"], beta); - - config.expect_stderr_ok(&["rls", "+stable", "--version"], stable); - }) - }); -} - -/// Checks that `rust-toolchain.toml` configs can be overridden by `RUSTUP_TOOLCHAIN`. -/// See: -#[test] -fn rust_toolchain_toml_with_rustup_toolchain() { - test(&|config| { - config.with_scenario(Scenario::SimpleV2, &|config| { - config.expect_err( - &["rustc", "--version"], - "rustup could not choose a version of rustc to run", - ); - - let cwd = config.current_dir(); - let toolchain_file = cwd.join("rust-toolchain.toml"); - raw::write_file( - &toolchain_file, - r#" - [toolchain] - channel = "nightly" - components = [ "rls" ] - "#, - ) - .unwrap(); - - let env = &[("RUSTUP_TOOLCHAIN", "beta")]; - let beta = "hash-beta-1.2.0"; - config.expect_stdout_ok_with_env(&["rustc", "--version"], env, beta); - config.expect_stdout_ok_with_env(&["rls", "--version"], env, beta); - - // At this point, `nightly` is still NOT installed. - config.expect_not_stderr_ok(&["rustup", "toolchain", "list"], "nightly"); - }) - }); -} - /// Checks that `rust-toolchain.toml` configs can override `rustup override set` at different directories. /// See: #[test]