Skip to content

Commit

Permalink
chore: Support overriding the fallback settings file path
Browse files Browse the repository at this point in the history
  • Loading branch information
frazar committed Nov 12, 2020
1 parent 97eaa9f commit 149c453
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,14 @@ impl Cfg {

// Centralised file for multi-user systems to provide admin/distributor set initial values.
let fallback_settings = if cfg!(not(windows)) {
FallbackSettings::new(PathBuf::from(UNIX_FALLBACK_SETTINGS))?
// If present, use the RUSTUP_OVERRIDE_UNIX_FALLBACK_SETTINGS environment
// variable as settings path, or UNIX_FALLBACK_SETTINGS otherwise
FallbackSettings::new(
match process().var("RUSTUP_OVERRIDE_UNIX_FALLBACK_SETTINGS") {
Ok(s) => PathBuf::from(s),
Err(_) => PathBuf::from(UNIX_FALLBACK_SETTINGS),
},
)?
} else {
None
};
Expand Down
38 changes: 38 additions & 0 deletions tests/cli-rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1940,3 +1940,41 @@ fn check_host_goes_away() {
);
})
}

#[cfg(unix)]
#[test]
fn check_unix_settings_fallback() {
setup(&|config| {
// No default toolchain specified yet
expect_err(
config,
&["rustup", "default"],
r"no default toolchain configured",
);

// Default toolchain specified in fallback settings file
let mock_settings_file = config.current_dir().join("mock_fallback_settings.toml");
raw::write_file(
&mock_settings_file,
for_host!(r"default_toolchain = 'nightly-{0}'"),
)
.unwrap();

let mut cmd = clitools::cmd(config, "rustup", &["default"]);
clitools::env(config, &mut cmd);

// Override the path to the fallback settings file to be the mock file
cmd.env("RUSTUP_OVERRIDE_UNIX_FALLBACK_SETTINGS", mock_settings_file);

let out = cmd.output().unwrap();
assert!(out.status.success());
let stdout = String::from_utf8(out.stdout).unwrap();
assert_eq!(
&stdout,
for_host!(
r"nightly-{0} (default)
"
)
);
});
}
7 changes: 7 additions & 0 deletions tests/mock/clitools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,13 @@ pub fn env<E: rustup_test::Env>(config: &Config, cmd: &mut E) {
.join("tests/mock/signing-key.pub.asc"),
);

// The unix fallback settings file may be present in the test environment, so override
// the path to the settings file with a non-existing path to avoid intereference
cmd.env(
"RUSTUP_OVERRIDE_UNIX_FALLBACK_SETTINGS",
"/bogus-config-file.toml",
);

if let Some(root) = config.rustup_update_root.as_ref() {
cmd.env("RUSTUP_UPDATE_ROOT", root);
}
Expand Down

0 comments on commit 149c453

Please sign in to comment.