From ee798dfa5eb6750d01631bd380cfceb45fbebf07 Mon Sep 17 00:00:00 2001 From: bing Date: Thu, 15 Dec 2022 20:09:09 +0800 Subject: [PATCH] fix: latest version panic (#315) * fix parsing version strings * fix error message * delete toolchain_override * use .get() in plugins check as well * fix clippy * update fuelup_check tests * clippy --- src/ops/fuelup_check.rs | 73 ++++++++++++++++++++--------------------- tests/commands.rs | 28 +++++++++------- 2 files changed, 53 insertions(+), 48 deletions(-) diff --git a/src/ops/fuelup_check.rs b/src/ops/fuelup_check.rs index 167d5dc20..3d7e95715 100644 --- a/src/ops/fuelup_check.rs +++ b/src/ops/fuelup_check.rs @@ -7,7 +7,7 @@ use crate::{ target_triple::TargetTriple, toolchain::{DistToolchainDescription, Toolchain}, }; -use anyhow::{bail, Result}; +use anyhow::Result; use component::{self, Components}; use semver::Version; use std::io::Write; @@ -111,50 +111,49 @@ fn check_toolchain(toolchain: &str, verbose: bool) -> Result<()> { bold(|s| writeln!(s, "{}", &toolchain.name)); for component in Components::collect_exclude_plugins()? { - let component_executable = toolchain.bin_path.join(&component.name); - let mut latest_version = &latest_package_versions[&component.name]; - match Command::new(&component_executable) - .arg("--version") - .output() - { - Ok(o) => { - let output = String::from_utf8_lossy(&o.stdout).into_owned(); - - match output.split_whitespace().last() { - Some(v) => { - let version = Version::parse(v)?; - bold(|s| write!(s, " {} - ", &component.name)); - compare_and_print_versions(&version, latest_version)?; - } - None => { - error!(" {} - Error getting version string", &component.name); + if let Some(latest_version) = latest_package_versions.get(&component.name) { + let component_executable = toolchain.bin_path.join(&component.name); + match Command::new(&component_executable) + .arg("--version") + .output() + { + Ok(o) => { + let output = String::from_utf8_lossy(&o.stdout).into_owned(); + + match output.split_whitespace().last() { + Some(v) => { + let version = Version::parse(v)?; + bold(|s| write!(s, " {} - ", &component.name)); + compare_and_print_versions(&version, latest_version)?; + } + None => { + error!(" {} - Error getting version string", &component.name); + } } } - } - Err(_) => bail!(""), - }; + Err(_) => error!(" {} - Error getting version string", &component.name), + }; - if verbose && component.name == component::FORC { - for plugin in component::Components::collect_plugins()? { - if !plugin.is_main_executable() { - bold(|s| writeln!(s, " - {}", plugin.name)); - } + if verbose && component.name == component::FORC { + for plugin in component::Components::collect_plugins()? { + if !plugin.is_main_executable() { + bold(|s| writeln!(s, " - {}", plugin.name)); + } - for (index, executable) in plugin.executables.iter().enumerate() { - let plugin_executable = toolchain.bin_path.join(executable); + for (index, executable) in plugin.executables.iter().enumerate() { + let plugin_executable = toolchain.bin_path.join(executable); - let mut plugin_name = &plugin.name; + let mut plugin_name = &plugin.name; - if !plugin.is_main_executable() { - print!(" "); - plugin_name = &plugin.executables[index]; - } + if !plugin.is_main_executable() { + print!(" "); + plugin_name = &plugin.executables[index]; + } - if latest_package_versions.contains_key(&plugin.name) { - latest_version = &latest_package_versions[&plugin.name]; + if let Some(latest_version) = latest_package_versions.get(&plugin.name) { + check_plugin(&plugin_executable, plugin_name, latest_version)?; + } } - - check_plugin(&plugin_executable, plugin_name, latest_version)?; } } } diff --git a/tests/commands.rs b/tests/commands.rs index 8fcbd0b0c..0155c3790 100644 --- a/tests/commands.rs +++ b/tests/commands.rs @@ -239,29 +239,35 @@ fn fuelup_toolchain_uninstall_active_switches_default() -> Result<()> { #[test] fn fuelup_check() -> Result<()> { + let latest = format!("latest-{}\n", TargetTriple::from_host().unwrap()); + let beta_1 = format!("beta-1-{}\n", TargetTriple::from_host().unwrap()); + let forc = "forc -"; + let fuel_core = "fuel-core -"; + let fuel_indexer = "fuel-indexer -"; testcfg::setup(FuelupState::Empty, &|cfg| { let output = cfg.fuelup(&["check"]); - assert!(output.status.success()); + assert!(!output.stdout.contains(&latest)); + assert!(!output.stdout.contains(forc)); + assert!(!output.stdout.contains(fuel_core)); + assert!(!output.stdout.contains(fuel_indexer)); })?; // Test that only the 'latest' toolchain shows. testcfg::setup(FuelupState::LatestAndCustomInstalled, &|cfg| { let output = cfg.fuelup(&["check"]); - assert_eq!( - output.stdout, - format!("latest-{}\n\n", TargetTriple::from_host().unwrap()) - ); - assert!(output.status.success()); + assert!(output.stdout.contains(&latest)); + assert!(output.stdout.contains(forc)); + assert!(output.stdout.contains(fuel_core)); + assert!(output.stdout.contains(fuel_indexer)); })?; // Test that toolchain names with '-' inside are parsed correctly. testcfg::setup(FuelupState::Beta1Installed, &|cfg| { let output = cfg.fuelup(&["check"]); - assert_eq!( - output.stdout, - format!("beta-1-{}\n\n", TargetTriple::from_host().unwrap()) - ); - assert!(output.status.success()); + assert!(output.stdout.contains(&beta_1)); + assert!(output.stdout.contains(forc)); + assert!(output.stdout.contains(fuel_core)); + assert!(!output.stdout.contains(fuel_indexer)); })?; Ok(())