Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show installed toolchains if a user tries to set an incorrect toolchain name to the default. #632

Merged
merged 9 commits into from
Jun 20, 2024
16 changes: 14 additions & 2 deletions src/ops/fuelup_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::str::FromStr;
use tracing::info;

use crate::{
config,
fmt::print_header,
path::settings_file,
settings::SettingsFile,
toolchain::{DistToolchainDescription, Toolchain},
Expand Down Expand Up @@ -43,15 +45,25 @@ pub fn default(toolchain: Option<String>) -> Result<()> {
};

if !new_default.exists() {
bail!("Toolchain with name '{}' does not exist", &new_default.name);
let cfg = config::Config::from_env()?;
let toolchains = cfg.list_toolchains()?;

info!("Toolchain with name '{}' does not exist", &new_default.name);
print_header("Installed toolchains");
for toolchain in toolchains {
info!("{}", toolchain);
}
// TODO: we should consider migrating to use thiserror in order to return a custom error here
// so we can match on it and prompt the user for another attempt.
bail!("");
};

let settings = SettingsFile::new(settings_file());
settings.with_mut(|s| {
s.default_toolchain = Some(new_default.name.clone());
Ok(())
})?;
info!("default toolchain set to '{}'", new_default.name);
info!("Default toolchain set to '{}'", new_default.name);

Ok(())
}
2 changes: 1 addition & 1 deletion src/ops/fuelup_show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn show() -> Result<()> {
info!("{}: {}", bold("Default host"), TargetTriple::from_host()?);
info!("{}: {}", bold("fuelup home"), fuelup_dir().display());

print_header("installed toolchains");
print_header("Installed toolchains");
let cfg = Config::from_env()?;
let mut active_toolchain = Toolchain::from_settings()?;

Expand Down
2 changes: 1 addition & 1 deletion src/ops/fuelup_toolchain/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn new(command: NewCommand) -> Result<()> {
ensure_dir_exists(&toolchains_dir.join(toolchain_bin_dir))?;
info!(
"New toolchain initialized: {name}
default toolchain set to '{name}'"
Default toolchain set to '{name}'"
);

Ok(())
Expand Down
1 change: 1 addition & 0 deletions tests/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn fuelup_check() -> Result<()> {
let output = cfg.fuelup(&["check"]);
let stripped = strip_ansi_escapes::strip(output.stdout);
let stdout = String::from_utf8_lossy(&stripped);
println!("\n{}", &stdout);
JoshuaBatty marked this conversation as resolved.
Show resolved Hide resolved
assert!(stdout.contains(&latest));
assert!(stdout.contains(forc));
assert!(stdout.contains(fuel_core));
Expand Down
39 changes: 31 additions & 8 deletions tests/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ fn fuelup_default_latest_and_custom() -> Result<()> {
testcfg::setup(FuelupState::LatestAndCustomInstalled, &|cfg| {
let output = cfg.fuelup(&["default", "latest"]);
let expected_stdout = format!(
"default toolchain set to 'latest-{}'\n",
"Default toolchain set to 'latest-{}'\n",
TargetTriple::from_host().unwrap()
);

assert_eq!(output.stdout, expected_stdout);

let output = cfg.fuelup(&["default", CUSTOM_TOOLCHAIN_NAME]);
let expected_stdout = format!("default toolchain set to '{CUSTOM_TOOLCHAIN_NAME}'\n");
let expected_stdout = format!("Default toolchain set to '{CUSTOM_TOOLCHAIN_NAME}'\n");

assert_eq!(output.stdout, expected_stdout);
})?;
Expand All @@ -54,12 +54,15 @@ fn fuelup_default_latest_and_custom() -> Result<()> {
#[test]
fn fuelup_default_uninstalled_toolchain() -> Result<()> {
testcfg::setup(FuelupState::LatestToolchainInstalled, &|cfg| {
let triple = TargetTriple::from_host().unwrap();
let output = cfg.fuelup(&["default", "nightly"]);
let expected_stdout = format!(
"Toolchain with name 'nightly-{}' does not exist\n",
TargetTriple::from_host().unwrap()
"Toolchain with name 'nightly-{}' does not exist\n\n\
\u{1b}[1mInstalled toolchains\u{1b}[0m\n\
--------------------\n\
latest-{}\n\n",
triple, triple
);

assert_eq!(output.stdout, expected_stdout);
})?;

Expand All @@ -71,7 +74,7 @@ fn fuelup_default_nightly() -> Result<()> {
testcfg::setup(FuelupState::AllInstalled, &|cfg| {
let output = cfg.fuelup(&["default", "nightly"]);
let expected_stdout = format!(
"default toolchain set to 'nightly-{}'\n",
"Default toolchain set to 'nightly-{}'\n",
TargetTriple::from_host().unwrap()
);

Expand All @@ -88,7 +91,7 @@ fn fuelup_default_nightly_and_nightly_date() -> Result<()> {
let stdout = String::from_utf8_lossy(&stripped);

let expected_stdout = format!(
"default toolchain set to 'nightly-{}'\n",
"Default toolchain set to 'nightly-{}'\n",
TargetTriple::from_host().unwrap()
);
assert_eq!(stdout, expected_stdout);
Expand All @@ -97,7 +100,7 @@ fn fuelup_default_nightly_and_nightly_date() -> Result<()> {
strip_ansi_escapes::strip(cfg.fuelup(&["default", &format!("nightly-{DATE}")]).stdout);
let stdout = String::from_utf8_lossy(&stripped);
let expected_stdout = format!(
"default toolchain set to 'nightly-{}-{}'\n",
"Default toolchain set to 'nightly-{}-{}'\n",
DATE,
TargetTriple::from_host().unwrap()
);
Expand All @@ -120,3 +123,23 @@ fn fuelup_default_override() -> Result<()> {

Ok(())
}

#[test]
fn fuelup_default_missing_toolchain() -> Result<()> {
testcfg::setup(FuelupState::AllInstalled, &|cfg| {
let triple = TargetTriple::from_host().unwrap();
let output = cfg.fuelup(&["default", "abcd"]);
let expected_stdout = format!(
"Toolchain with name 'abcd' does not exist\n\n\
\u{1b}[1mInstalled toolchains\u{1b}[0m\n\
--------------------\n\
latest-{triple}\n\
nightly-{triple}\n\
nightly-2022-08-30-{triple}\n\n"
);

println!("\n{}", output.stdout);
assert_eq!(output.stdout, expected_stdout);
})?;
Ok(())
}
14 changes: 7 additions & 7 deletions tests/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn fuelup_show_latest() -> Result<()> {
r#"Default host: {target}
fuelup home: {fuelup_home_str}

installed toolchains
Installed toolchains
--------------------
latest-{target} (default)
nightly-{target}
Expand Down Expand Up @@ -66,7 +66,7 @@ fn fuelup_show_and_switch() -> Result<()> {
r#"Default host: {target}
fuelup home: {fuelup_home_str}

installed toolchains
Installed toolchains
--------------------
latest-{target} (default)
nightly-{target}
Expand Down Expand Up @@ -102,7 +102,7 @@ latest-{target} (default)
r#"Default host: {target}
fuelup home: {fuelup_home_str}

installed toolchains
Installed toolchains
--------------------
latest-{target}
nightly-{target} (default)
Expand Down Expand Up @@ -146,7 +146,7 @@ fn fuelup_show_custom() -> Result<()> {
r#"Default host: {target}
fuelup home: {fuelup_home_str}

installed toolchains
Installed toolchains
--------------------
my_toolchain (default)

Expand Down Expand Up @@ -187,7 +187,7 @@ fn fuelup_show_override() -> Result<()> {
r#"Default host: {target}
fuelup home: {fuelup_home_str}

installed toolchains
Installed toolchains
--------------------
latest-{target} (default)

Expand Down Expand Up @@ -229,7 +229,7 @@ fn fuelup_show_latest_then_override() -> Result<()> {
r#"Default host: {target}
fuelup home: {fuelup_home_str}

installed toolchains
Installed toolchains
--------------------
latest-{target} (default)
nightly-{target}
Expand Down Expand Up @@ -278,7 +278,7 @@ latest-{target} (default)
r#"Default host: {target}
fuelup home: {fuelup_home_str}

installed toolchains
Installed toolchains
--------------------
latest-{target} (default)
nightly-{target}
Expand Down
2 changes: 1 addition & 1 deletion tests/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn fuelup_toolchain_new() -> Result<()> {
let output = cfg.fuelup(&["toolchain", "new", CUSTOM_TOOLCHAIN_NAME]);
let expected_stdout = format!(
"New toolchain initialized: {CUSTOM_TOOLCHAIN_NAME}
default toolchain set to '{CUSTOM_TOOLCHAIN_NAME}'\n"
Default toolchain set to '{CUSTOM_TOOLCHAIN_NAME}'\n"
);

assert_eq!(output.stdout, expected_stdout);
Expand Down
Loading