Skip to content

Commit f309243

Browse files
committed
(wip) Add unit tests for Cfg overriding, moving black box tests over
1 parent 5d8a064 commit f309243

File tree

2 files changed

+94
-74
lines changed

2 files changed

+94
-74
lines changed

src/config.rs

+94-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ use crate::{
3636
utils::utils,
3737
};
3838

39+
#[cfg(feature = "test")]
40+
use crate::test::mock::clitools;
41+
3942
#[derive(Debug, ThisError)]
4043
enum OverrideFileConfigError {
4144
#[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 {
149152
}
150153
}
151154

152-
#[derive(Default, Debug)]
155+
#[derive(Default, Debug, PartialEq)]
153156
struct OverrideCfg {
154157
toolchain: Option<LocalToolchainName>,
155158
components: Vec<String>,
@@ -1005,6 +1008,29 @@ impl Cfg {
10051008
}
10061009
}
10071010

1011+
#[cfg(feature = "test")]
1012+
impl From<clitools::Config> for Cfg {
1013+
fn from(cfg: clitools::Config) -> Self {
1014+
let rustup_dir = &cfg.rustupdir;
1015+
let dist_root_server = dist::DEFAULT_DIST_SERVER;
1016+
1017+
Self {
1018+
rustup_dir: rustup_dir.rustupdir.clone(),
1019+
fallback_settings: None,
1020+
settings_file: SettingsFile::new(rustup_dir.join("settings.toml")),
1021+
toolchains_dir: rustup_dir.join("toolchains"),
1022+
update_hash_dir: rustup_dir.join("update-hashes"),
1023+
download_dir: rustup_dir.join("downloads"),
1024+
dist_root_url: dist_root_server.to_owned() + "/dist",
1025+
temp_cfg: temp::Cfg::new(rustup_dir.join("tmp"), dist_root_server, Box::new(|_| {})),
1026+
toolchain_override: None,
1027+
profile_override: None,
1028+
env_override: None,
1029+
notify_handler: Arc::new(|_| {}),
1030+
}
1031+
}
1032+
}
1033+
10081034
fn update_override(
10091035
override_: &mut Option<(OverrideFile, OverrideReason)>,
10101036
file: OverrideFile,
@@ -1046,7 +1072,7 @@ enum ParseMode {
10461072
mod tests {
10471073
use rustup_macros::unit_test as test;
10481074

1049-
use crate::{cli::common::set_globals, utils::raw};
1075+
use crate::{test::mock::clitools::setup_test_state, utils::raw};
10501076

10511077
use super::*;
10521078

@@ -1254,8 +1280,10 @@ channel = nightly
12541280
/// Checks that `rust-toolchain.toml` configs can be overridden by `<proxy> +<toolchain>`.
12551281
/// See: <https://github.com/rust-lang/rustup/issues/3483>
12561282
#[test]
1257-
fn find_override_config_with_toolchain_override() {
1258-
let cwd = crate::test::test_dir().unwrap();
1283+
fn toolchain_override_beats_toml() {
1284+
let test_dist_dir = crate::test::test_dist_dir().unwrap();
1285+
let (cwd, config) = setup_test_state(test_dist_dir);
1286+
12591287
let toolchain_file = cwd.path().join("rust-toolchain.toml");
12601288
raw::write_file(
12611289
&toolchain_file,
@@ -1267,10 +1295,70 @@ channel = nightly
12671295
)
12681296
.unwrap();
12691297

1270-
let mut cfg = set_globals(true, false).unwrap();
1298+
let mut cfg = Cfg::try_from(config).unwrap();
1299+
let default_host_triple = cfg.get_default_host_triple().unwrap();
12711300
cfg.toolchain_override = Some("beta".try_into().unwrap());
12721301

12731302
let found_override = cfg.find_override_config(cwd.path()).unwrap();
1274-
assert!(dbg!(found_override).is_none());
1303+
let override_cfg = found_override.map(|it| it.0);
1304+
1305+
let expected_override_cfg = OverrideCfg {
1306+
toolchain: Some(LocalToolchainName::Named(ToolchainName::Official(
1307+
ToolchainDesc {
1308+
channel: "beta".to_owned(),
1309+
date: None,
1310+
target: default_host_triple,
1311+
},
1312+
))),
1313+
components: vec!["rls".to_owned()],
1314+
targets: vec![],
1315+
profile: None,
1316+
};
1317+
assert_eq!(override_cfg, Some(expected_override_cfg));
1318+
}
1319+
1320+
/// Checks that `rust-toolchain.toml` configs can be overridden by `RUSTUP_TOOLCHAIN`.
1321+
/// See: <https://github.com/rust-lang/rustup/issues/3483>
1322+
#[test]
1323+
fn env_override_beats_toml() {
1324+
let test_dist_dir = crate::test::test_dist_dir().unwrap();
1325+
let (cwd, config) = setup_test_state(test_dist_dir);
1326+
1327+
let toolchain_file = cwd.path().join("rust-toolchain.toml");
1328+
raw::write_file(
1329+
&toolchain_file,
1330+
r#"
1331+
[toolchain]
1332+
channel = "nightly"
1333+
components = [ "rls" ]
1334+
"#,
1335+
)
1336+
.unwrap();
1337+
1338+
let mut cfg = Cfg::try_from(config).unwrap();
1339+
let default_host_triple = cfg.get_default_host_triple().unwrap();
1340+
cfg.env_override = Some(
1341+
ResolvableLocalToolchainName::try_from("beta")
1342+
.unwrap()
1343+
.resolve(&default_host_triple)
1344+
.unwrap(),
1345+
);
1346+
1347+
let found_override = cfg.find_override_config(cwd.path()).unwrap();
1348+
let override_cfg = found_override.map(|it| it.0);
1349+
1350+
let expected_override_cfg = OverrideCfg {
1351+
toolchain: Some(LocalToolchainName::Named(ToolchainName::Official(
1352+
ToolchainDesc {
1353+
channel: "beta".to_owned(),
1354+
date: None,
1355+
target: default_host_triple.clone(),
1356+
},
1357+
))),
1358+
components: vec!["rls".to_owned()],
1359+
targets: vec![],
1360+
profile: None,
1361+
};
1362+
assert_eq!(override_cfg, Some(expected_override_cfg));
12751363
}
12761364
}

tests/suite/cli_rustup.rs

-68
Original file line numberDiff line numberDiff line change
@@ -2376,74 +2376,6 @@ fn only_toml_in_rust_toolchain_toml() {
23762376
});
23772377
}
23782378

2379-
/// Checks that `rust-toolchain.toml` configs can be overridden by `rustup override set` or `<proxy> +<toolchain>`.
2380-
/// See: <https://github.com/rust-lang/rustup/issues/3483>
2381-
#[test]
2382-
fn rust_toolchain_toml_with_rustup_override() {
2383-
test(&|config| {
2384-
config.with_scenario(Scenario::SimpleV2, &|config| {
2385-
config.expect_ok(&["rustup", "default", "stable"]);
2386-
2387-
let stable = "hash-stable-1.1.0";
2388-
config.expect_stdout_ok(&["rustc", "--version"], stable);
2389-
2390-
config.expect_ok(&["rustup", "override", "set", "beta"]);
2391-
2392-
let cwd = config.current_dir();
2393-
let toolchain_file = cwd.join("rust-toolchain.toml");
2394-
raw::write_file(
2395-
&toolchain_file,
2396-
r#"
2397-
[toolchain]
2398-
channel = "nightly"
2399-
components = [ "rls" ]
2400-
"#,
2401-
)
2402-
.unwrap();
2403-
2404-
let beta = "hash-beta-1.2.0";
2405-
config.expect_stdout_ok(&["rustc", "--version"], beta);
2406-
config.expect_stdout_ok(&["rls", "--version"], beta);
2407-
2408-
config.expect_stderr_ok(&["rls", "+stable", "--version"], stable);
2409-
})
2410-
});
2411-
}
2412-
2413-
/// Checks that `rust-toolchain.toml` configs can be overridden by `RUSTUP_TOOLCHAIN`.
2414-
/// See: <https://github.com/rust-lang/rustup/issues/3483>
2415-
#[test]
2416-
fn rust_toolchain_toml_with_rustup_toolchain() {
2417-
test(&|config| {
2418-
config.with_scenario(Scenario::SimpleV2, &|config| {
2419-
config.expect_err(
2420-
&["rustc", "--version"],
2421-
"rustup could not choose a version of rustc to run",
2422-
);
2423-
2424-
let cwd = config.current_dir();
2425-
let toolchain_file = cwd.join("rust-toolchain.toml");
2426-
raw::write_file(
2427-
&toolchain_file,
2428-
r#"
2429-
[toolchain]
2430-
channel = "nightly"
2431-
components = [ "rls" ]
2432-
"#,
2433-
)
2434-
.unwrap();
2435-
2436-
let env = &[("RUSTUP_TOOLCHAIN", "beta")];
2437-
let beta = "hash-beta-1.2.0";
2438-
config.expect_stdout_ok_with_env(&["rustc", "--version"], env, beta);
2439-
config.expect_stdout_ok_with_env(&["rls", "--version"], env, beta);
2440-
2441-
// At this point, `nightly` is still NOT installed.
2442-
config.expect_not_stderr_ok(&["rustup", "toolchain", "list"], "nightly");
2443-
})
2444-
});
2445-
}
2446-
24472379
/// Checks that `rust-toolchain.toml` configs can override `rustup override set` at different directories.
24482380
/// See: <https://github.com/rust-lang/rustup/issues/3483>
24492381
#[test]

0 commit comments

Comments
 (0)