Skip to content

Commit 149c453

Browse files
committed
chore: Support overriding the fallback settings file path
1 parent 97eaa9f commit 149c453

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/config.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,14 @@ impl Cfg {
196196

197197
// Centralised file for multi-user systems to provide admin/distributor set initial values.
198198
let fallback_settings = if cfg!(not(windows)) {
199-
FallbackSettings::new(PathBuf::from(UNIX_FALLBACK_SETTINGS))?
199+
// If present, use the RUSTUP_OVERRIDE_UNIX_FALLBACK_SETTINGS environment
200+
// variable as settings path, or UNIX_FALLBACK_SETTINGS otherwise
201+
FallbackSettings::new(
202+
match process().var("RUSTUP_OVERRIDE_UNIX_FALLBACK_SETTINGS") {
203+
Ok(s) => PathBuf::from(s),
204+
Err(_) => PathBuf::from(UNIX_FALLBACK_SETTINGS),
205+
},
206+
)?
200207
} else {
201208
None
202209
};

tests/cli-rustup.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,3 +1940,41 @@ fn check_host_goes_away() {
19401940
);
19411941
})
19421942
}
1943+
1944+
#[cfg(unix)]
1945+
#[test]
1946+
fn check_unix_settings_fallback() {
1947+
setup(&|config| {
1948+
// No default toolchain specified yet
1949+
expect_err(
1950+
config,
1951+
&["rustup", "default"],
1952+
r"no default toolchain configured",
1953+
);
1954+
1955+
// Default toolchain specified in fallback settings file
1956+
let mock_settings_file = config.current_dir().join("mock_fallback_settings.toml");
1957+
raw::write_file(
1958+
&mock_settings_file,
1959+
for_host!(r"default_toolchain = 'nightly-{0}'"),
1960+
)
1961+
.unwrap();
1962+
1963+
let mut cmd = clitools::cmd(config, "rustup", &["default"]);
1964+
clitools::env(config, &mut cmd);
1965+
1966+
// Override the path to the fallback settings file to be the mock file
1967+
cmd.env("RUSTUP_OVERRIDE_UNIX_FALLBACK_SETTINGS", mock_settings_file);
1968+
1969+
let out = cmd.output().unwrap();
1970+
assert!(out.status.success());
1971+
let stdout = String::from_utf8(out.stdout).unwrap();
1972+
assert_eq!(
1973+
&stdout,
1974+
for_host!(
1975+
r"nightly-{0} (default)
1976+
"
1977+
)
1978+
);
1979+
});
1980+
}

tests/mock/clitools.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,13 @@ pub fn env<E: rustup_test::Env>(config: &Config, cmd: &mut E) {
446446
.join("tests/mock/signing-key.pub.asc"),
447447
);
448448

449+
// The unix fallback settings file may be present in the test environment, so override
450+
// the path to the settings file with a non-existing path to avoid intereference
451+
cmd.env(
452+
"RUSTUP_OVERRIDE_UNIX_FALLBACK_SETTINGS",
453+
"/bogus-config-file.toml",
454+
);
455+
449456
if let Some(root) = config.rustup_update_root.as_ref() {
450457
cmd.env("RUSTUP_UPDATE_ROOT", root);
451458
}

0 commit comments

Comments
 (0)