Skip to content

Commit

Permalink
(wip) Add unit tests for Cfg overriding, moving black box tests over
Browse files Browse the repository at this point in the history
  • Loading branch information
rami3l committed Nov 3, 2023
1 parent 5d8a064 commit f309243
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 74 deletions.
100 changes: 94 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -149,7 +152,7 @@ impl Display for OverrideReason {
}
}

#[derive(Default, Debug)]
#[derive(Default, Debug, PartialEq)]
struct OverrideCfg {
toolchain: Option<LocalToolchainName>,
components: Vec<String>,
Expand Down Expand Up @@ -1005,6 +1008,29 @@ impl Cfg {
}
}

#[cfg(feature = "test")]
impl From<clitools::Config> 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,
Expand Down Expand Up @@ -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::*;

Expand Down Expand Up @@ -1254,8 +1280,10 @@ channel = nightly
/// Checks that `rust-toolchain.toml` configs can be overridden by `<proxy> +<toolchain>`.
/// See: <https://github.com/rust-lang/rustup/issues/3483>
#[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,
Expand All @@ -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: <https://github.com/rust-lang/rustup/issues/3483>
#[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));
}
}
68 changes: 0 additions & 68 deletions tests/suite/cli_rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<proxy> +<toolchain>`.
/// See: <https://github.com/rust-lang/rustup/issues/3483>
#[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: <https://github.com/rust-lang/rustup/issues/3483>
#[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: <https://github.com/rust-lang/rustup/issues/3483>
#[test]
Expand Down

0 comments on commit f309243

Please sign in to comment.