diff --git a/component/src/lib.rs b/component/src/lib.rs index c12f3dfe6..8beb0758e 100644 --- a/component/src/lib.rs +++ b/component/src/lib.rs @@ -180,13 +180,10 @@ impl Components { } pub fn collect_plugin_executables() -> Result> { - let plugins = Self::collect_plugins()?; - let mut executables = vec![]; - - for plugin in plugins.iter() { - executables.extend(plugin.executables.clone().into_iter()) - } - + let executables = Self::collect_plugins()? + .iter() + .flat_map(|p| p.executables.clone()) + .collect(); Ok(executables) } @@ -195,7 +192,6 @@ impl Components { if let Some(forc) = components.component.get(FORC) { return forc.executables.contains(&plugin_name.to_string()); }; - false } } diff --git a/src/channel.rs b/src/channel.rs index c6b8f1d67..a0cfe7ba0 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -72,9 +72,9 @@ fn construct_channel_url(desc: &DistToolchainDescription) -> Result { match desc.name { DistToolchainName::Latest => { if let Some(date) = desc.date { - url.push_str(&format!("channels/latest/channel-fuel-latest-{date}.toml")) + url.push_str(&format!("channels/latest/channel-fuel-latest-{date}.toml")); } else { - url.push_str(CHANNEL_LATEST_FILE_NAME) + url.push_str(CHANNEL_LATEST_FILE_NAME); } } @@ -83,7 +83,7 @@ fn construct_channel_url(desc: &DistToolchainDescription) -> Result { url.push_str(&format_nightly_url(&date)?); url.push('/'); } - url.push_str(CHANNEL_NIGHTLY_FILE_NAME) + url.push_str(CHANNEL_NIGHTLY_FILE_NAME); } DistToolchainName::Beta1 => url.push_str(CHANNEL_BETA_1_FILE_NAME), DistToolchainName::Beta2 => url.push_str(CHANNEL_BETA_2_FILE_NAME), @@ -100,12 +100,10 @@ fn construct_channel_url(desc: &DistToolchainDescription) -> Result { impl Channel { pub fn from_dist_channel(desc: &DistToolchainDescription) -> Result { let channel_url = construct_channel_url(desc)?; - let toml = match download(&channel_url) { Ok(t) => String::from_utf8(t)?, Err(_) => bail!("Could not read {}", &channel_url), }; - Self::from_toml(&toml) } diff --git a/src/commands/completions.rs b/src/commands/completions.rs index 4b7629943..0d48c3fcc 100644 --- a/src/commands/completions.rs +++ b/src/commands/completions.rs @@ -1,9 +1,8 @@ +use crate::ops::fuelup_completions; use anyhow::Result; use clap::Parser; use clap_complete::Shell; -use crate::ops::fuelup_completions; - /// Generate tab-completion scripts for your shell #[derive(Debug, Parser)] pub struct CompletionsCommand { @@ -14,6 +13,5 @@ pub struct CompletionsCommand { pub fn exec(command: CompletionsCommand) -> Result<()> { fuelup_completions::completions(command)?; - Ok(()) } diff --git a/src/commands/component.rs b/src/commands/component.rs index 2bf0518a2..47b83af87 100644 --- a/src/commands/component.rs +++ b/src/commands/component.rs @@ -1,8 +1,7 @@ +use crate::ops::fuelup_component::{add::add, list::list, remove::remove}; use anyhow::Result; use clap::Parser; -use crate::ops::fuelup_component::{add::add, list::list, remove::remove}; - #[derive(Debug, Parser)] pub enum ComponentCommand { /// Add a component to the currently active custom toolchain. @@ -10,7 +9,7 @@ pub enum ComponentCommand { /// Remove a component from the currently active custom toolchain Remove(RemoveCommand), /// List installed and installable components - List(ListCommand), + List, } #[derive(Debug, Parser)] @@ -25,15 +24,11 @@ pub struct RemoveCommand { pub component: String, } -#[derive(Debug, Parser)] -pub struct ListCommand {} - pub fn exec(command: ComponentCommand) -> Result<()> { match command { ComponentCommand::Add(command) => add(command)?, ComponentCommand::Remove(command) => remove(command)?, - ComponentCommand::List(command) => list(command)?, + ComponentCommand::List => list()?, }; - Ok(()) } diff --git a/src/commands/default.rs b/src/commands/default.rs index d7bfeb55f..4ce129b56 100644 --- a/src/commands/default.rs +++ b/src/commands/default.rs @@ -1,8 +1,7 @@ +use crate::ops::fuelup_default; use anyhow::Result; use clap::Parser; -use crate::ops::fuelup_default; - #[derive(Debug, Parser)] pub struct DefaultCommand { /// Set the default toolchain. @@ -11,6 +10,5 @@ pub struct DefaultCommand { pub fn exec(command: DefaultCommand) -> Result<()> { let DefaultCommand { toolchain } = command; - fuelup_default::default(toolchain) } diff --git a/src/commands/fuelup.rs b/src/commands/fuelup.rs index b90c00124..92634603a 100644 --- a/src/commands/fuelup.rs +++ b/src/commands/fuelup.rs @@ -1,8 +1,7 @@ +use crate::ops::fuelup_self::{self_uninstall, self_update}; use anyhow::{bail, Result}; use clap::Parser; -use crate::ops::fuelup_self::{self_uninstall, self_update}; - #[derive(Debug, Parser)] pub enum FuelupCommand { /// Updates fuelup @@ -27,7 +26,6 @@ pub fn update_exec(force: bool) -> Result<()> { if let Err(e) = self_update(force) { bail!("fuelup failed to update itself: {}", e) }; - Ok(()) } @@ -35,6 +33,5 @@ pub fn remove_exec(force: bool) -> Result<()> { if let Err(e) = self_uninstall(force) { bail!("fuelup failed to update itself: {}", e) }; - Ok(()) } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 558ff428a..5db91936c 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -3,7 +3,5 @@ pub mod completions; pub mod component; pub mod default; pub mod fuelup; -pub mod show; pub mod toolchain; -pub mod update; pub mod upgrade; diff --git a/src/commands/show.rs b/src/commands/show.rs deleted file mode 100644 index 38e6900bd..000000000 --- a/src/commands/show.rs +++ /dev/null @@ -1,11 +0,0 @@ -use anyhow::Result; -use clap::Parser; - -use crate::ops::fuelup_show; - -#[derive(Debug, Parser)] -pub struct ShowCommand {} - -pub fn exec() -> Result<()> { - fuelup_show::show() -} diff --git a/src/commands/toolchain.rs b/src/commands/toolchain.rs index 7e9fe7e47..035fad7e6 100644 --- a/src/commands/toolchain.rs +++ b/src/commands/toolchain.rs @@ -1,11 +1,8 @@ -use anyhow::{bail, Result}; -use clap::Parser; - -use crate::ops::fuelup_toolchain::install::install; -use crate::ops::fuelup_toolchain::new::new; -use crate::ops::fuelup_toolchain::uninstall::uninstall; +use crate::ops::fuelup_toolchain::{install::install, new::new, uninstall::uninstall}; use crate::target_triple::TargetTriple; use crate::toolchain::RESERVED_TOOLCHAIN_NAMES; +use anyhow::{bail, Result}; +use clap::Parser; #[derive(Debug, Parser)] pub enum ToolchainCommand { diff --git a/src/commands/update.rs b/src/commands/update.rs deleted file mode 100644 index 905916b91..000000000 --- a/src/commands/update.rs +++ /dev/null @@ -1,13 +0,0 @@ -use anyhow::Result; -use clap::Parser; - -use crate::ops::fuelup_update; - -#[derive(Debug, Parser)] -pub struct UpdateCommand {} - -pub fn exec() -> Result<()> { - fuelup_update::update()?; - - Ok(()) -} diff --git a/src/commands/upgrade.rs b/src/commands/upgrade.rs index 3eb64ce50..0b85f76c1 100644 --- a/src/commands/upgrade.rs +++ b/src/commands/upgrade.rs @@ -1,8 +1,7 @@ +use crate::ops::fuelup_upgrade; use anyhow::Result; use clap::Parser; -use crate::ops::fuelup_upgrade; - #[derive(Debug, Parser)] pub struct UpgradeCommand { #[clap(long, short)] @@ -11,6 +10,5 @@ pub struct UpgradeCommand { pub fn exec(force: bool) -> Result<()> { fuelup_upgrade::upgrade(force)?; - Ok(()) } diff --git a/src/config.rs b/src/config.rs index 7ca4da3d3..696e29b1a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,12 +1,8 @@ -use std::fs; -use std::path::PathBuf; - -use anyhow::Result; -use std::io; - use crate::fmt::format_toolchain_with_target; use crate::path::toolchains_dir; use crate::toolchain::RESERVED_TOOLCHAIN_NAMES; +use anyhow::Result; +use std::{fs, io, path::PathBuf}; pub struct Config { toolchains_dir: PathBuf, @@ -33,9 +29,9 @@ impl Config { .iter() .any(|t| toolchain == format_toolchain_with_target(t)) { - toolchains.push(toolchain) + toolchains.push(toolchain); } else { - custom_toolchains.push(toolchain) + custom_toolchains.push(toolchain); } } @@ -61,10 +57,9 @@ impl Config { for name in RESERVED_TOOLCHAIN_NAMES { let dist_toolchain = format_toolchain_with_target(name); if installed_toolchains.contains(&dist_toolchain) { - dist_toolchains.push(name.to_string()) + dist_toolchains.push(name.to_string()); } } - Ok(dist_toolchains) } else { Ok(Vec::new()) diff --git a/src/fmt.rs b/src/fmt.rs index f698f71d4..a17d00a37 100644 --- a/src/fmt.rs +++ b/src/fmt.rs @@ -1,8 +1,7 @@ -use std::io::{self, Write}; - use crate::target_triple::TargetTriple; use ansi_term::Colour; use ansiterm::Style; +use std::io::{self, Write}; use tracing::info; pub fn println_error>(txt: X) { diff --git a/src/fuelup_cli.rs b/src/fuelup_cli.rs index b1fdf9bbe..4c97fc7f6 100644 --- a/src/fuelup_cli.rs +++ b/src/fuelup_cli.rs @@ -1,19 +1,15 @@ -use anyhow::Result; -use clap::Parser; - -use crate::commands::show::ShowCommand; use crate::commands::{ - check, completions, component, default, fuelup, show, toolchain, update, upgrade, + check::{self, CheckCommand}, + completions::{self, CompletionsCommand}, + component::{self, ComponentCommand}, + default::{self, DefaultCommand}, + fuelup::{self, FuelupCommand}, + toolchain::{self, ToolchainCommand}, + upgrade::{self, UpgradeCommand}, }; - -use crate::commands::check::CheckCommand; -use crate::commands::completions::CompletionsCommand; -use crate::commands::component::ComponentCommand; -use crate::commands::default::DefaultCommand; -use crate::commands::fuelup::FuelupCommand; -use crate::commands::toolchain::ToolchainCommand; -use crate::commands::update::UpdateCommand; -use crate::commands::upgrade::UpgradeCommand; +use crate::ops::{fuelup_show, fuelup_update}; +use anyhow::Result; +use clap::Parser; #[derive(Debug, Parser)] #[clap(name = "fuelup", about = "Fuel Toolchain Manager", version)] @@ -40,9 +36,9 @@ enum Commands { #[clap(subcommand)] Toolchain(ToolchainCommand), /// Show the active and installed toolchains, as well as the host and fuelup home - Show(ShowCommand), + Show, /// Updates the distributable toolchains, if already installed - Update(UpdateCommand), + Update, /// Updates fuelup itself, switches to the `latest` channel and updates components in all channels. Upgrade(UpgradeCommand), } @@ -59,9 +55,9 @@ pub fn fuelup_cli() -> Result<()> { FuelupCommand::Update(update) => fuelup::update_exec(update.force), FuelupCommand::Uninstall(remove) => fuelup::remove_exec(remove.force), }, - Commands::Show(_command) => show::exec(), + Commands::Show => fuelup_show::show(), Commands::Toolchain(command) => toolchain::exec(command), - Commands::Update(_command) => update::exec(), + Commands::Update => fuelup_update::update(), Commands::Upgrade(command) => upgrade::exec(command.force), } } diff --git a/src/ops/fuelup_check.rs b/src/ops/fuelup_check.rs index 0a1001878..bf11e6939 100644 --- a/src/ops/fuelup_check.rs +++ b/src/ops/fuelup_check.rs @@ -2,48 +2,43 @@ use crate::{ channel::Channel, commands::check::CheckCommand, config::Config, - download::DownloadCfg, + download::get_latest_version, file::get_bin_version, fmt::{bold, colored_bold}, - target_triple::TargetTriple, toolchain::{DistToolchainDescription, Toolchain}, }; use ansiterm::Color; -use anyhow::{anyhow, Result}; +use anyhow::Result; use component::{self, Components}; use semver::Version; -use std::collections::HashMap; -use std::str::FromStr; use std::{ - cmp::Ordering::{Equal, Greater, Less}, + cmp::Ordering::{self, Equal, Greater, Less}, + collections::HashMap, path::Path, + str::FromStr, }; use tracing::{error, info}; fn collect_package_versions(channel: Channel) -> HashMap { let mut latest_versions: HashMap = HashMap::new(); - - for (name, package) in channel.pkg.into_iter() { + for (name, package) in channel.pkg { latest_versions.insert(name, package.version); } - latest_versions } -fn format_version_comparison(current_version: &Version, latest_version: &Version) -> String { - match current_version.cmp(latest_version) { +fn format_version_comparison( + order: &Ordering, + current_version: &Version, + latest_version: &Version, +) -> String { + match order { Less => { format!( "{} : {current_version} -> {latest_version}", colored_bold(Color::Yellow, "Update available") ) } - Equal => { - format!( - "{} : {current_version}", - colored_bold(Color::Green, "Up to date") - ) - } Greater => { format!( " : {} {} -> {} {}", @@ -53,98 +48,145 @@ fn format_version_comparison(current_version: &Version, latest_version: &Version colored_bold(Color::Green, "(recommended)") ) } + Equal => { + format!( + "{} : {current_version}", + colored_bold(Color::Green, "Up to date") + ) + } } } -fn check_plugin(plugin_executable: &Path, plugin: &str, latest_version: &Version) { +fn check_plugin( + plugin_executable: &Path, + plugin: &str, + latest_version: &Version, + verbose: bool, + num_updates: &mut u16, +) { let version_or_err = match get_bin_version(plugin_executable) { - Ok(version) => format_version_comparison(&version, latest_version), + Ok(version) => { + let order = version.cmp(latest_version); + if order != Equal { + *num_updates += 1; + } + format_version_comparison(&order, &version, latest_version) + } Err(err) => err.to_string(), }; - info!("{:>4}- {} - {}", "", plugin, version_or_err); + if verbose { + info!("{:>4}- {} - {}", "", plugin, version_or_err); + } } -fn check_fuelup() -> Result<()> { +fn check_fuelup(verbose: bool, max_length: usize) -> Result<()> { let fuelup_version: Version = Version::parse(clap::crate_version!())?; - - if let Ok(fuelup_download_cfg) = DownloadCfg::new( - component::FUELUP, - TargetTriple::from_component(component::FUELUP)?, - None, - ) { - info!( - "{} - {}", - bold(component::FUELUP), - format_version_comparison(&fuelup_version, &fuelup_download_cfg.version) - ); + if let Ok(latest) = get_latest_version(component::FUELUP) { + let order = fuelup_version.cmp(&latest); + if verbose { + let res = format_version_comparison(&order, &fuelup_version, &latest); + info!("{} - {}", bold(component::FUELUP), res); + } else { + let text = if order == Equal { + colored_bold(Color::Green, "Up to date") + } else { + colored_bold(Color::Yellow, "Update available") + }; + let padded_fuelup = format!("{: Result<()> { +fn check_toolchain(toolchain: &str, verbose: bool) -> Result { let description = DistToolchainDescription::from_str(toolchain)?; - let dist_channel = Channel::from_dist_channel(&description)?; let latest_package_versions = collect_package_versions(dist_channel); - let toolchain = Toolchain::new(toolchain)?; + if verbose { + info!("{}: {}", bold("Toolchain: "), &toolchain.name); + } - info!("{}", bold(&toolchain.name)); - - for component in Components::collect_exclude_plugins()? { + let components = Components::collect_exclude_plugins()?; + let plugins = component::Components::collect_plugins()?; + let mut num_updates = 0; + components.iter().for_each(|component| { if let Some(latest_version) = latest_package_versions.get(&component.name) { let component_executable = toolchain.bin_path.join(&component.name); let version_text = match get_bin_version(&component_executable) { - Ok(version) => format_version_comparison(&version, latest_version), + Ok(version) => { + let order = version.cmp(latest_version); + format_version_comparison(&order, &version, latest_version) + } Err(err) => err.to_string(), }; - info!("{:>2}{} - {}", "", bold(&component.name), version_text); - if verbose && component.name == component::FORC { - for plugin in component::Components::collect_plugins()? { - if !plugin.is_main_executable() { + if verbose { + info!("{:>2}{} - {}", "", bold(&component.name), version_text); + } + if component.name == component::FORC { + plugins.iter().for_each(|plugin| { + if !plugin.is_main_executable() && verbose { info!("{:>4}- {}", "", bold(&plugin.name)); } - for (index, executable) in plugin.executables.iter().enumerate() { let plugin_executable = toolchain.bin_path.join(executable); - let mut plugin_name = &plugin.name; - - if !plugin.is_main_executable() { + if !plugin.is_main_executable() && verbose { print!("{:>2}", ""); - plugin_name = plugin - .executables - .get(index) - .ok_or_else(|| anyhow!("Plugin name not found"))?; + if let Some(exe_name) = plugin.executables.get(index) { + plugin_name = exe_name; + } else { + error!("Plugin name not found"); + } } - let maybe_latest_version = plugin.publish.map_or_else( || latest_package_versions.get(component::FORC), |_| latest_package_versions.get(plugin_name), ); - if let Some(latest_version) = maybe_latest_version { - check_plugin(&plugin_executable, plugin_name, latest_version); + check_plugin( + &plugin_executable, + plugin_name, + latest_version, + verbose, + &mut num_updates, + ); } } - } + }); } } - } - Ok(()) + }); + Ok(num_updates) } pub fn check(command: CheckCommand) -> Result<()> { let CheckCommand { verbose } = command; - let cfg = Config::from_env()?; - for toolchain in cfg.list_dist_toolchains()? { - check_toolchain(&toolchain, verbose)?; - } + // Find the maximum length of toolchain names + let toolchains = cfg.list_dist_toolchains()?; + let max_length = toolchains.iter().map(|t| t.len()).max().unwrap_or(0); - check_fuelup()?; + for toolchain in toolchains { + let num_updates = check_toolchain(&toolchain, verbose)?; + if !verbose { + let s = if num_updates == 0 { + colored_bold(Color::Green, "Up to date") + } else { + colored_bold( + Color::Yellow, + &format!("Update available ({num_updates} updates)",), + ) + }; + // Pad the toolchain name with spaces to align the `-` signs + let padded_toolchain = format!("{: Result<()> { let AddCommand { diff --git a/src/ops/fuelup_component/list.rs b/src/ops/fuelup_component/list.rs index 1e35dfea2..8578b5cf5 100644 --- a/src/ops/fuelup_component/list.rs +++ b/src/ops/fuelup_component/list.rs @@ -1,7 +1,4 @@ -use crate::{ - commands::component::ListCommand, download::get_latest_version, file::get_bin_version, - fmt::bold, toolchain::Toolchain, -}; +use crate::{download::get_latest_version, file::get_bin_version, fmt::bold, toolchain::Toolchain}; use anyhow::Result; use component::Components; use std::fmt::Write; @@ -23,7 +20,7 @@ fn format_installable_component_info(name: &str, latest_version: &str) -> String format!("{:>2}{name} (latest: {latest_version})\n", "") } -fn format_forc_default_plugins(plugin_executables: Vec) -> String { +fn format_forc_default_plugins(plugin_executables: &[String]) -> String { plugin_executables .iter() .filter(|c| *c != component::FORC) @@ -33,7 +30,7 @@ fn format_forc_default_plugins(plugin_executables: Vec) -> String { }) } -pub fn list(_command: ListCommand) -> Result<()> { +pub fn list() -> Result<()> { let toolchain = Toolchain::from_settings()?; let mut installed_components_summary = String::from("\nInstalled:\n"); let mut available_components_summary = String::from("Installable:\n"); @@ -60,7 +57,7 @@ pub fn list(_command: ListCommand) -> Result<()> { if component.name == component::FORC { installed_components_summary - .push_str(&format_forc_default_plugins(component.executables)) + .push_str(&format_forc_default_plugins(&component.executables)); } } else { available_components_summary.push_str(&format_installable_component_info( @@ -70,7 +67,7 @@ pub fn list(_command: ListCommand) -> Result<()> { if component.name == component::FORC { available_components_summary - .push_str(&format_forc_default_plugins(component.executables)) + .push_str(&format_forc_default_plugins(&component.executables)); } } } diff --git a/src/ops/fuelup_component/remove.rs b/src/ops/fuelup_component/remove.rs index 0d538b29d..fdf040dc9 100644 --- a/src/ops/fuelup_component/remove.rs +++ b/src/ops/fuelup_component/remove.rs @@ -1,10 +1,8 @@ -use anyhow::{bail, Result}; - use crate::{commands::component::RemoveCommand, toolchain::Toolchain}; +use anyhow::{bail, Result}; pub fn remove(command: RemoveCommand) -> Result<()> { let RemoveCommand { component } = command; - let toolchain = Toolchain::from_settings()?; if toolchain.is_distributed() { diff --git a/src/ops/fuelup_default.rs b/src/ops/fuelup_default.rs index c2d1850a3..1fb8ab9c3 100644 --- a/src/ops/fuelup_default.rs +++ b/src/ops/fuelup_default.rs @@ -1,7 +1,3 @@ -use anyhow::{bail, Result}; -use std::str::FromStr; -use tracing::info; - use crate::{ config, fmt::print_header, @@ -10,40 +6,37 @@ use crate::{ toolchain::{DistToolchainDescription, Toolchain}, toolchain_override::ToolchainOverride, }; +use anyhow::{bail, Result}; +use std::str::FromStr; +use tracing::info; pub fn default(toolchain: Option) -> Result<()> { - let toolchain = match toolchain { - Some(toolchain) => toolchain, - None => { - let mut result = String::new(); - let current_toolchain = Toolchain::from_settings()?; - - if let Some(to) = ToolchainOverride::from_project_root() { - let name = - match DistToolchainDescription::from_str(&to.cfg.toolchain.channel.to_string()) - { - Ok(desc) => desc.to_string(), - Err(_) => to.cfg.toolchain.channel.to_string(), - }; - result.push_str(&format!("{name} (override)")); - - if current_toolchain.exists() { - result.push_str(", ") - } + let toolchain = if let Some(toolchain) = toolchain { + toolchain + } else { + let mut result = String::new(); + let current_toolchain = Toolchain::from_settings()?; + if let Some(to) = ToolchainOverride::from_project_root() { + let name = + match DistToolchainDescription::from_str(&to.cfg.toolchain.channel.to_string()) { + Ok(desc) => desc.to_string(), + Err(_) => to.cfg.toolchain.channel.to_string(), + }; + result.push_str(&format!("{name} (override)")); + + if current_toolchain.exists() { + result.push_str(", "); } - - result.push_str(&format!("{} (default)", current_toolchain.name)); - - info!("{}", result); - return Ok(()); } + result.push_str(&format!("{} (default)", current_toolchain.name)); + info!("{}", result); + return Ok(()); }; let new_default = match DistToolchainDescription::from_str(&toolchain) { Ok(desc) => Toolchain::from_path(&desc.to_string()), Err(_) => Toolchain::from_path(&toolchain), }; - if !new_default.exists() { let cfg = config::Config::from_env()?; let toolchains = cfg.list_toolchains()?; @@ -57,13 +50,11 @@ pub fn default(toolchain: Option) -> Result<()> { // 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); - Ok(()) } diff --git a/src/ops/fuelup_self.rs b/src/ops/fuelup_self.rs index cda7f4d21..437dc31b7 100644 --- a/src/ops/fuelup_self.rs +++ b/src/ops/fuelup_self.rs @@ -1,12 +1,3 @@ -use anyhow::{bail, Context, Result}; -use component::{self, Components}; -use std::{ - fs::{self, remove_dir_all}, - path::Path, -}; -use tempfile; -use tracing::{error, info}; - use crate::{ download::{download_file_and_unpack, unpack_bins, DownloadCfg}, file::{get_bin_version, hard_or_symlink_file, read_file, write_file}, @@ -15,6 +6,14 @@ use crate::{ shell::Shell, target_triple::TargetTriple, }; +use anyhow::{bail, Context, Result}; +use component::{self, Components}; +use std::{ + fs::{self, remove_dir_all}, + path::Path, +}; +use tempfile; +use tracing::{error, info}; pub fn attempt_install_self(download_cfg: DownloadCfg, dst: &Path) -> Result<()> { download_file_and_unpack(&download_cfg, dst)?; @@ -26,9 +25,9 @@ pub fn attempt_install_self(download_cfg: DownloadCfg, dst: &Path) -> Result<()> #[inline] fn remove_path_from_content(file_content: &str) -> (bool, String) { let mut is_modified = false; - let whole_definition = format!("PATH={}", FUELUP_DIR); - let suffix = format!("{}:", FUELUP_DIR); - let prefix = format!("{}:", FUELUP_DIR); + let whole_definition = format!("PATH={FUELUP_DIR}"); + let suffix = format!("{FUELUP_DIR}:"); + let prefix = format!("{FUELUP_DIR}:"); let lines = file_content .trim_end_matches('\n') .trim_end_matches('\r') @@ -86,7 +85,7 @@ This will uninstall all Sway toolchains and data, and remove, {}/bin from your P ]; remove_fuelup_from_path()?; - for (info, path) in remove.into_iter() { + for (info, path) in remove { println_info(info); match remove_dir_all(&path) { Ok(()) => {} @@ -98,7 +97,6 @@ This will uninstall all Sway toolchains and data, and remove, {}/bin from your P } } } - Ok(()) } else { Ok(()) diff --git a/src/ops/fuelup_show.rs b/src/ops/fuelup_show.rs index 7822b26f6..f30daa9e9 100644 --- a/src/ops/fuelup_show.rs +++ b/src/ops/fuelup_show.rs @@ -40,13 +40,13 @@ pub fn show() -> Result<()> { for toolchain in cfg.list_toolchains()? { let mut message = toolchain.clone(); if toolchain == active_toolchain.name { - message.push_str(" (default)") + message.push_str(" (default)"); } if Some(toolchain) == override_name { message.push_str(" (override)"); } - info!("{}", message) + info!("{message}"); } let mut active_toolchain_message = String::new(); @@ -69,7 +69,7 @@ pub fn show() -> Result<()> { }; print_header("active toolchain"); - info!("{}", active_toolchain_message); + info!("{active_toolchain_message}"); let mut version_map: HashMap = HashMap::new(); for component in Components::collect_exclude_plugins()? { @@ -77,7 +77,7 @@ pub fn show() -> Result<()> { let version_text: String = match get_bin_version(component_executable.as_path()) { Ok(version) => { version_map.insert(component.name.clone(), version.clone()); - format!("{}", version) + format!("{version}") } Err(e) => e.to_string(), }; @@ -94,7 +94,7 @@ pub fn show() -> Result<()> { let version_text = match get_bin_version(plugin_executable.as_path()) { Ok(version) => { version_map.insert(executable.clone(), version.clone()); - format!("{}", version) + format!("{version}") } Err(e) => e.to_string(), }; @@ -105,7 +105,7 @@ pub fn show() -> Result<()> { let version_text = match get_bin_version(plugin_executable.as_path()) { Ok(version) => { version_map.insert(plugin.name.clone(), version.clone()); - format!("{}", version) + format!("{version}") } Err(e) => e.to_string(), }; diff --git a/src/ops/fuelup_toolchain/install.rs b/src/ops/fuelup_toolchain/install.rs index d86fd7e2d..ee6e9bc89 100644 --- a/src/ops/fuelup_toolchain/install.rs +++ b/src/ops/fuelup_toolchain/install.rs @@ -3,8 +3,7 @@ use crate::settings::SettingsFile; use crate::toolchain::{DistToolchainDescription, Toolchain}; use crate::{channel::Channel, commands::toolchain::InstallCommand}; use anyhow::{bail, Result}; -use std::fmt::Write; -use std::str::FromStr; +use std::{fmt::Write, str::FromStr}; use tracing::{error, info}; pub fn install(command: InstallCommand) -> Result<()> { @@ -51,7 +50,7 @@ pub fn install(command: InstallCommand) -> Result<()> { info!("\nInstalled:\n{}", installed_bins); info!("\nThe Fuel toolchain is installed and up to date"); } else if installed_bins.is_empty() { - error!("\nfuelup failed to install:\n{}", errored_bins) + error!("\nfuelup failed to install:\n{}", errored_bins); } else { info!( "\nThe Fuel toolchain is partially installed.\nfuelup failed to install: {}", diff --git a/src/ops/fuelup_toolchain/new.rs b/src/ops/fuelup_toolchain/new.rs index eda3b89c8..3fff93e5c 100644 --- a/src/ops/fuelup_toolchain/new.rs +++ b/src/ops/fuelup_toolchain/new.rs @@ -8,19 +8,13 @@ use tracing::info; pub fn new(command: NewCommand) -> Result<()> { let NewCommand { name } = command; - let toolchains_dir = toolchains_dir(); - let toolchain_exists = Toolchain::all()?.into_iter().any(|x| x == name); - if toolchain_exists { bail!("Toolchain with name '{}' already exists", &name) } - let toolchain_bin_dir = toolchain_bin_dir(&name); - let settings_file = settings_file(); - let settings = SettingsFile::new(settings_file); settings.with_mut(|s| { s.default_toolchain = Some(name.clone()); @@ -32,6 +26,5 @@ pub fn new(command: NewCommand) -> Result<()> { "New toolchain initialized: {name} Default toolchain set to '{name}'" ); - Ok(()) } diff --git a/src/ops/fuelup_toolchain/uninstall.rs b/src/ops/fuelup_toolchain/uninstall.rs index d37544cf4..6a4e53091 100644 --- a/src/ops/fuelup_toolchain/uninstall.rs +++ b/src/ops/fuelup_toolchain/uninstall.rs @@ -1,25 +1,21 @@ -use anyhow::Result; -use std::str::FromStr; - use crate::{ commands::toolchain::UninstallCommand, fmt::{println_error, println_warn}, toolchain::{DistToolchainDescription, Toolchain}, }; +use anyhow::Result; +use std::str::FromStr; pub fn uninstall(command: UninstallCommand) -> Result<()> { let UninstallCommand { name } = command; - let toolchain = match DistToolchainDescription::from_str(&name) { Ok(desc) => Toolchain::from_path(&desc.to_string()), Err(_) => Toolchain::from_path(&name), }; - if !toolchain.exists() { println_warn(format!("Toolchain '{}' does not exist", &toolchain.name)); return Ok(()); } - let active_toolchain = Toolchain::from_settings()?; if active_toolchain.name == toolchain.name { println_error(format!("Cannot uninstall '{}' as it is currently the default toolchain. Run `fuelup default ` to update the default toolchain.", &toolchain.name)); @@ -27,12 +23,11 @@ pub fn uninstall(command: UninstallCommand) -> Result<()> { } match toolchain.uninstall_self() { - Ok(_) => println!("Toolchain '{}' uninstalled", &toolchain.name), + Ok(()) => println!("Toolchain '{}' uninstalled", &toolchain.name), Err(e) => println_error(format!( "Failed to uninstall toolchain '{}': {}", &toolchain.name, e )), }; - Ok(()) } diff --git a/src/path.rs b/src/path.rs index c115ff0e3..6f762ddb4 100644 --- a/src/path.rs +++ b/src/path.rs @@ -2,10 +2,9 @@ use crate::{constants::FUEL_TOOLCHAIN_TOML_FILE, fmt::println_warn}; use anyhow::{bail, Result}; use component::Components; use dirs; -use std::borrow::Cow; -use std::env; use std::{ - fs, + borrow::Cow, + env, fs, path::{Path, PathBuf}, }; diff --git a/src/proxy_cli.rs b/src/proxy_cli.rs index 12e93a3b7..8da9a3f61 100644 --- a/src/proxy_cli.rs +++ b/src/proxy_cli.rs @@ -1,17 +1,20 @@ +use crate::{ + download::DownloadCfg, + store::Store, + target_triple::TargetTriple, + toolchain::{DistToolchainDescription, Toolchain}, + toolchain_override::ToolchainOverride, +}; use anyhow::Result; -use std::ffi::OsString; -use std::io::{Error, ErrorKind}; -use std::os::unix::prelude::CommandExt; -use std::process::{Command, ExitCode, Stdio}; -use std::str::FromStr; -use std::{env, io}; - -use crate::download::DownloadCfg; -use crate::store::Store; -use crate::target_triple::TargetTriple; -use crate::toolchain::{DistToolchainDescription, Toolchain}; -use crate::toolchain_override::ToolchainOverride; use component::Components; +use std::{ + env, + ffi::OsString, + io::{self, Error, ErrorKind}, + os::unix::prelude::CommandExt, + process::{Command, ExitCode, Stdio}, + str::FromStr, +}; /// Runs forc or fuel-core in proxy mode pub fn proxy_run(arg0: &str) -> Result { @@ -75,10 +78,7 @@ fn direct_proxy(proc_name: &str, args: &[OsString], toolchain: &Toolchain) -> Re (toolchain.bin_path.join(proc_name), description.to_string()) } } - None => ( - toolchain.bin_path.join(proc_name), - toolchain.name.to_owned(), - ), + None => (toolchain.bin_path.join(proc_name), toolchain.name.clone()), }; let mut cmd = Command::new(bin_path); diff --git a/src/settings.rs b/src/settings.rs index 9f55cb0e2..2be69e5ac 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,11 +1,9 @@ +use crate::file; +use anyhow::Result; use serde::{Deserialize, Serialize}; use std::{cell::RefCell, path::PathBuf}; use toml_edit::{de, ser, Document}; -use anyhow::Result; - -use crate::file; - pub struct SettingsFile { path: PathBuf, cache: RefCell>, @@ -21,8 +19,7 @@ impl SettingsFile { fn write_settings(&self) -> Result<()> { let s = self.cache.borrow().as_ref().unwrap().clone(); - - let parent_exists = self.path.parent().map(|dir| dir.exists()).unwrap_or(true); + let parent_exists = self.path.parent().map_or(true, |dir| dir.exists()); if !parent_exists { std::fs::create_dir_all(self.path.parent().unwrap())?; } @@ -40,7 +37,7 @@ impl SettingsFile { Settings::parse(&content)? } else { needs_save = true; - Default::default() + Settings::default() }); } } diff --git a/src/shell.rs b/src/shell.rs index 9c6d99c88..4eabcf89c 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -15,10 +15,7 @@ impl Shell { pub fn does_exists(&self) -> bool { match self { - Shell::Posix => true, - Shell::Bash => true, - Shell::Zsh => true, - Shell::Fish => true, + Shell::Posix | Shell::Bash | Shell::Zsh | Shell::Fish => true, } } diff --git a/src/store.rs b/src/store.rs index c96a9d826..75ae0e0f1 100644 --- a/src/store.rs +++ b/src/store.rs @@ -1,10 +1,11 @@ -use std::fs; -use std::io::Write; -use std::path::{Path, PathBuf}; - use anyhow::Result; use component::Component; use semver::Version; +use std::{ + fs, + io::Write, + path::{Path, PathBuf}, +}; use tracing::{info, warn}; use crate::{ @@ -90,7 +91,6 @@ impl Store { version: &Version, ) -> std::io::Result { let dirname = component_dirname(name, version); - fs::read_to_string(self.path().join(dirname).join(FUELS_VERSION_FILE)) } } diff --git a/src/target_triple.rs b/src/target_triple.rs index 4d41e9d17..da18f9c50 100644 --- a/src/target_triple.rs +++ b/src/target_triple.rs @@ -13,28 +13,22 @@ impl fmt::Display for TargetTriple { impl TargetTriple { pub fn new(s: &str) -> Result { - let (architecture, rest) = match s.split_once('-') { - Some((architecture, rest)) => (architecture, rest), - None => bail!("missing vendor-os specifier"), + let Some((architecture, rest)) = s.split_once('-') else { + bail!("missing vendor-os specifier") + }; + let Some((vendor, os)) = rest.split_once('-') else { + bail!("missing os specifier") }; if !["aarch64", "x86_64"].contains(&architecture) { bail!("Unsupported architecture: '{}'", architecture); } - - let (vendor, os) = match rest.split_once('-') { - Some((vendor, os)) => (vendor, os), - None => bail!("missing os specifier"), - }; - if !["apple", "unknown"].contains(&vendor) { bail!("Unsupported vendor: '{}'", vendor); } - if !["darwin", "linux-gnu"].contains(&os) { bail!("Unsupported os: '{}'", os); } - Ok(Self(s.to_string())) } diff --git a/src/toolchain.rs b/src/toolchain.rs index ccd9c873e..30cc47671 100644 --- a/src/toolchain.rs +++ b/src/toolchain.rs @@ -1,22 +1,26 @@ -use crate::channel::{self, Channel}; -use crate::constants::DATE_FORMAT; -use crate::download::DownloadCfg; -use crate::file::{get_bin_version, hard_or_symlink_file, is_executable}; -use crate::path::{ - ensure_dir_exists, fuelup_bin_dir, fuelup_bin_or_current_bin, fuelup_tmp_dir, settings_file, - toolchain_bin_dir, toolchain_dir, toolchains_dir, +use crate::{ + channel::{self, Channel}, + constants::DATE_FORMAT, + download::DownloadCfg, + file::{get_bin_version, hard_or_symlink_file, is_executable}, + path::{ + ensure_dir_exists, fuelup_bin_dir, fuelup_bin_or_current_bin, fuelup_tmp_dir, + settings_file, toolchain_bin_dir, toolchain_dir, toolchains_dir, + }, + settings::SettingsFile, + store::Store, + target_triple::TargetTriple, }; -use crate::settings::SettingsFile; -use crate::store::Store; -use crate::target_triple::TargetTriple; use anyhow::{bail, Context, Result}; use component::{self, Components}; -use std::collections::VecDeque; -use std::fmt; -use std::fs::{read_dir, remove_dir_all, remove_file}; -use std::path::PathBuf; -use std::process::Command; -use std::str::FromStr; +use std::{ + collections::VecDeque, + fmt, + fs::{read_dir, remove_dir_all, remove_file}, + path::PathBuf, + process::Command, + str::FromStr, +}; use time::Date; use tracing::{error, info}; @@ -234,7 +238,6 @@ fn cache_sway_std_libs(forc_bin_path: PathBuf) -> Result<()> { error!("Failed to fetch core forc dependencies"); }; }; - Ok(()) } @@ -258,14 +261,14 @@ impl Toolchain { pub fn all() -> Result> { let toolchains_dir = toolchains_dir(); - Ok(if !toolchains_dir.is_dir() { - vec![] - } else { + Ok(if toolchains_dir.is_dir() { read_dir(&toolchains_dir)? .filter_map(std::io::Result::ok) .filter(|e| e.path().is_dir()) .map(|e| e.file_name().into_string().ok().unwrap_or_default()) .collect() + } else { + vec![] }) } @@ -316,11 +319,6 @@ impl Toolchain { } } - fn can_remove(&self, component: &str) -> bool { - // Published components are the ones downloadable, and hence removable. - Components::contains_published(component) - } - pub fn add_component(&self, download_cfg: DownloadCfg) -> Result { // Pre-install checks: ensuring toolchain dir, fuelup bin dir, and fuelup exist ensure_dir_exists(&self.bin_path)?; @@ -436,11 +434,11 @@ impl Toolchain { } pub fn remove_component(&self, component: &str) -> Result<()> { - if self.can_remove(component) { + if Toolchain::can_remove(component) { if self.has_component(component) { info!("Removing '{}' from toolchain '{}'", component, self.name); match self.remove_executables(component) { - Ok(_) => info!("'{}' removed from toolchain '{}'", component, self.name), + Ok(()) => info!("'{}' removed from toolchain '{}'", component, self.name), Err(e) => error!( "Failed to remove '{}' from toolchain '{}': {}", component, self.name, e @@ -452,7 +450,6 @@ impl Toolchain { } else { info!("'{}' is not a removable component", component); } - Ok(()) } @@ -500,10 +497,15 @@ impl Toolchain { .try_for_each(remove_dir_all)?; if self.exists() { - remove_dir_all(self.path.clone())? + remove_dir_all(self.path.clone())?; } Ok(()) } + + fn can_remove(component: &str) -> bool { + // Published components are the ones downloadable, and hence removable. + Components::contains_published(component) + } } #[cfg(test)] diff --git a/src/toolchain_override.rs b/src/toolchain_override.rs index 663648cce..5fdbc3f82 100644 --- a/src/toolchain_override.rs +++ b/src/toolchain_override.rs @@ -1,22 +1,22 @@ +use crate::{ + channel::{is_beta_toolchain, LATEST, NIGHTLY}, + constants::{DATE_FORMAT, FUEL_TOOLCHAIN_TOML_FILE}, + download::DownloadCfg, + file, + path::get_fuel_toolchain_toml, + target_triple::TargetTriple, + toolchain::{DistToolchainDescription, Toolchain}, +}; use anyhow::{bail, Result}; use semver::Version; use serde::de::Error; use serde::ser::SerializeStruct; use serde::{Deserialize, Deserializer, Serialize}; -use std::fmt; -use std::str::FromStr; -use std::{collections::HashMap, path::PathBuf}; +use std::{collections::HashMap, fmt, path::PathBuf, str::FromStr}; use time::Date; use toml_edit::{de, ser, value, Document}; use tracing::{info, warn}; -use crate::channel::{is_beta_toolchain, LATEST, NIGHTLY}; -use crate::constants::{DATE_FORMAT, FUEL_TOOLCHAIN_TOML_FILE}; -use crate::toolchain::{DistToolchainDescription, Toolchain}; -use crate::{ - download::DownloadCfg, file, path::get_fuel_toolchain_toml, target_triple::TargetTriple, -}; - // For composability with other functionality of fuelup, we want to add // additional info to OverrideCfg (representation of 'fuel-toolchain.toml'). // In this case, we want the path to the toml file. More info might be @@ -124,7 +124,7 @@ impl ToolchainOverride { document["toolchain"]["channel"] = value(self.cfg.toolchain.channel.to_string()); if let Some(components) = &self.cfg.components { - for (k, v) in components.iter() { + for (k, v) in components { document["components"][k] = value(v.to_string()); } } diff --git a/tests/check.rs b/tests/check.rs index d2e3d4c06..fcbbe826f 100644 --- a/tests/check.rs +++ b/tests/check.rs @@ -1,8 +1,7 @@ -use anyhow::Result; -use fuelup::target_triple::TargetTriple; - pub mod testcfg; +use anyhow::Result; +use fuelup::target_triple::TargetTriple; use testcfg::FuelupState; #[test] @@ -13,7 +12,7 @@ fn fuelup_check() -> Result<()> { let fuel_core = "fuel-core -"; let fuel_indexer = "fuel-indexer -"; testcfg::setup(FuelupState::Empty, &|cfg| { - let output = cfg.fuelup(&["check"]); + let output = cfg.fuelup(&["check", "--verbose"]); let stripped = strip_ansi_escapes::strip(output.stdout); let stdout = String::from_utf8_lossy(&stripped); assert!(!stdout.contains(&latest)); @@ -24,7 +23,7 @@ fn fuelup_check() -> Result<()> { // Test that only the 'latest' toolchain shows. testcfg::setup(FuelupState::LatestAndCustomInstalled, &|cfg| { - let output = cfg.fuelup(&["check"]); + let output = cfg.fuelup(&["check", "--verbose"]); let stripped = strip_ansi_escapes::strip(output.stdout); let stdout = String::from_utf8_lossy(&stripped); assert!(stdout.contains(&latest)); @@ -34,7 +33,7 @@ fn fuelup_check() -> Result<()> { // Test that toolchain names with '-' inside are parsed correctly. testcfg::setup(FuelupState::Beta1Installed, &|cfg| { - let output = cfg.fuelup(&["check"]); + let output = cfg.fuelup(&["check", "--verbose"]); let stripped = strip_ansi_escapes::strip(output.stdout); let stdout = String::from_utf8_lossy(&stripped); assert!(stdout.contains(&beta_1)); diff --git a/tests/completions.rs b/tests/completions.rs index a6b69dc4e..894874b78 100644 --- a/tests/completions.rs +++ b/tests/completions.rs @@ -1,6 +1,6 @@ -use anyhow::Result; - pub mod testcfg; + +use anyhow::Result; use testcfg::FuelupState; #[test] @@ -9,10 +9,8 @@ fn fuelup_completions() -> Result<()> { let shells = ["zsh", "bash", "fish", "powershell", "elvish"]; for shell in shells { let output = cfg.fuelup(&["completions", "--shell", shell]); - assert!(output.status.success()); } })?; - Ok(()) } diff --git a/tests/component.rs b/tests/component.rs index f76ea831d..9a975791d 100644 --- a/tests/component.rs +++ b/tests/component.rs @@ -1,12 +1,12 @@ +mod expects; +pub mod testcfg; + use anyhow::Result; +use expects::expect_files_exist; use fuelup::{fmt::format_toolchain_with_target, target_triple::TargetTriple}; use indoc::formatdoc; -pub mod testcfg; use testcfg::{FuelupState, ALL_BINS, DATE}; -mod expects; -use expects::expect_files_exist; - #[test] fn fuelup_component_add() -> Result<()> { testcfg::setup(FuelupState::Empty, &|cfg| { diff --git a/tests/default.rs b/tests/default.rs index 109f7086a..88930954e 100644 --- a/tests/default.rs +++ b/tests/default.rs @@ -1,7 +1,7 @@ +pub mod testcfg; + use anyhow::Result; use fuelup::{fmt::format_toolchain_with_target, target_triple::TargetTriple}; - -pub mod testcfg; use testcfg::{FuelupState, CUSTOM_TOOLCHAIN_NAME, DATE}; #[test] @@ -14,7 +14,6 @@ fn fuelup_default_empty() -> Result<()> { assert_eq!(output.stdout, expected_stdout); assert!(!cfg.home.join("settings.toml").exists()); })?; - Ok(()) } @@ -24,10 +23,8 @@ fn fuelup_default() -> Result<()> { testcfg::setup(FuelupState::LatestToolchainInstalled, &|cfg| { let output = cfg.fuelup(&["default"]); let expected_stdout = format!("{latest} (default)\n"); - assert_eq!(output.stdout, expected_stdout); })?; - Ok(()) } @@ -39,15 +36,12 @@ fn fuelup_default_latest_and_custom() -> Result<()> { "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"); - assert_eq!(output.stdout, expected_stdout); })?; - Ok(()) } @@ -65,7 +59,6 @@ fn fuelup_default_uninstalled_toolchain() -> Result<()> { ); assert_eq!(output.stdout, expected_stdout); })?; - Ok(()) } @@ -77,10 +70,8 @@ fn fuelup_default_nightly() -> Result<()> { "Default toolchain set to 'nightly-{}'\n", TargetTriple::from_host().unwrap() ); - assert_eq!(output.stdout, expected_stdout); })?; - Ok(()) } @@ -89,7 +80,6 @@ fn fuelup_default_nightly_and_nightly_date() -> Result<()> { testcfg::setup(FuelupState::NightlyAndNightlyDateInstalled, &|cfg| { let stripped = strip_ansi_escapes::strip(cfg.fuelup(&["default", "nightly"]).stdout); let stdout = String::from_utf8_lossy(&stripped); - let expected_stdout = format!( "Default toolchain set to 'nightly-{}'\n", TargetTriple::from_host().unwrap() @@ -106,7 +96,6 @@ fn fuelup_default_nightly_and_nightly_date() -> Result<()> { ); assert_eq!(stdout, expected_stdout); })?; - Ok(()) } @@ -115,12 +104,9 @@ fn fuelup_default_override() -> Result<()> { testcfg::setup(FuelupState::LatestWithBetaOverride, &|cfg| { let output = cfg.fuelup(&["default"]); let triple = TargetTriple::from_host().unwrap(); - let expected_stdout = format!("beta-1-{triple} (override), latest-{triple} (default)\n"); - assert_eq!(output.stdout, expected_stdout); })?; - Ok(()) } diff --git a/tests/override.rs b/tests/override.rs index 577964f5a..c2b10b240 100644 --- a/tests/override.rs +++ b/tests/override.rs @@ -1,11 +1,11 @@ -use anyhow::Result; -use std::str::FromStr; - pub mod testcfg; + +use anyhow::Result; use fuelup::{ constants::FUEL_TOOLCHAIN_TOML_FILE, toolchain_override::{self, OverrideCfg, ToolchainCfg, ToolchainOverride}, }; +use std::str::FromStr; use testcfg::FuelupState; #[test] @@ -37,6 +37,5 @@ fn check_correct_forc_plugin_called() -> Result<()> { stdout = cfg.exec("forc-wallet", &["--version"]).stdout; assert_eq!(stdout, "forc-wallet 0.2.0\n"); })?; - Ok(()) } diff --git a/tests/self.rs b/tests/self.rs index 834be5d91..368a1b696 100644 --- a/tests/self.rs +++ b/tests/self.rs @@ -1,8 +1,7 @@ +pub mod testcfg; use anyhow::Result; use testcfg::FuelupState; -pub mod testcfg; - #[test] fn test_self_uninstall() -> Result<()> { testcfg::setup(FuelupState::NightlyDateInstalled, &|cfg| { @@ -28,7 +27,6 @@ fn fuelup_self_update() -> Result<()> { let expected_stdout_starts_with = "Fetching binary from"; assert!(output.stdout.starts_with(expected_stdout_starts_with)); })?; - Ok(()) } @@ -39,6 +37,5 @@ fn fuelup_self_update_latest() -> Result<()> { let expected_stdout_starts_with = "Already up to date"; assert!(output.stdout.contains(expected_stdout_starts_with)); })?; - Ok(()) } diff --git a/tests/show.rs b/tests/show.rs index e287cca7b..26d9be405 100644 --- a/tests/show.rs +++ b/tests/show.rs @@ -1,3 +1,5 @@ +pub mod testcfg; + use anyhow::Result; use fuelup::{ constants::FUEL_TOOLCHAIN_TOML_FILE, @@ -6,8 +8,6 @@ use fuelup::{ }; use indoc::formatdoc; use std::str::FromStr; - -pub mod testcfg; use testcfg::FuelupState; #[test] @@ -45,7 +45,7 @@ fn fuelup_show_latest() -> Result<()> { - forc-tx : 0.1.0 - forc-wallet : 0.1.0 fuel-core : 0.1.0 - fuel-core-keygen : not found + fuel-core-keygen : 0.1.0 "# }; assert_eq!(stdout.to_string(), expected_stdout); @@ -88,7 +88,7 @@ fn fuelup_show_and_switch() -> Result<()> { - forc-tx : 0.1.0 - forc-wallet : 0.1.0 fuel-core : 0.1.0 - fuel-core-keygen : not found + fuel-core-keygen : 0.1.0 "# }; assert_eq!(stdout, expected_stdout); @@ -124,7 +124,7 @@ fn fuelup_show_and_switch() -> Result<()> { - forc-tx : 0.2.0 - forc-wallet : 0.2.0 fuel-core : 0.2.0 - fuel-core-keygen : not found + fuel-core-keygen : 0.2.0 "# }; assert_eq!(stdout, expected_stdout); @@ -249,7 +249,7 @@ fn fuelup_show_latest_then_override() -> Result<()> { - forc-tx : 0.1.0 - forc-wallet : 0.1.0 fuel-core : 0.1.0 - fuel-core-keygen : not found + fuel-core-keygen : 0.1.0 "# }; assert_eq!(stdout, expected_stdout); @@ -298,7 +298,7 @@ fn fuelup_show_latest_then_override() -> Result<()> { - forc-tx : 0.2.0 - forc-wallet : 0.2.0 fuel-core : 0.2.0 - fuel-core-keygen : not found + fuel-core-keygen : 0.2.0 "# }; assert_eq!(stdout, expected_stdout); diff --git a/tests/testcfg/mod.rs b/tests/testcfg/mod.rs index 6a29e4611..27c3f8eb5 100644 --- a/tests/testcfg/mod.rs +++ b/tests/testcfg/mod.rs @@ -83,6 +83,7 @@ pub static ALL_BINS: &[&str] = &[ "forc-tx", "forc-wallet", "fuel-core", + "fuel-core-keygen", "fuel-indexer", ]; diff --git a/tests/toolchain.rs b/tests/toolchain.rs index 0c874c4e0..fc9bdc444 100644 --- a/tests/toolchain.rs +++ b/tests/toolchain.rs @@ -1,13 +1,12 @@ +mod expects; +pub mod testcfg; + use anyhow::Result; use chrono::{Duration, Utc}; +use expects::expect_files_exist; use fuelup::{channel, fmt::format_toolchain_with_target, target_triple::TargetTriple}; - -pub mod testcfg; use testcfg::{FuelupState, ALL_BINS, CUSTOM_TOOLCHAIN_NAME, DATE}; -mod expects; -use expects::expect_files_exist; - fn yesterday() -> String { let current_date = Utc::now(); let yesterday = current_date - Duration::days(1); @@ -19,7 +18,6 @@ fn fuelup_toolchain_install_latest() -> Result<()> { testcfg::setup(FuelupState::Empty, &|cfg| { let output = cfg.fuelup(&["toolchain", "install", "latest"]); assert!(output.status.success()); - for entry in cfg.toolchains_dir().read_dir().expect("Could not read dir") { let toolchain_dir = entry.unwrap(); let expected_toolchain_name = @@ -31,7 +29,6 @@ fn fuelup_toolchain_install_latest() -> Result<()> { assert!(toolchain_dir.file_type().unwrap().is_dir()); } })?; - Ok(()) } @@ -40,7 +37,6 @@ fn fuelup_toolchain_install_nightly() -> Result<()> { testcfg::setup(FuelupState::Empty, &|cfg| { let output = cfg.fuelup(&["toolchain", "install", "nightly"]); assert!(output.status.success()); - for entry in cfg.toolchains_dir().read_dir().expect("Could not read dir") { let toolchain_dir = entry.unwrap(); let expected_toolchain_name = @@ -52,7 +48,6 @@ fn fuelup_toolchain_install_nightly() -> Result<()> { assert!(toolchain_dir.file_type().unwrap().is_dir()); } })?; - Ok(()) } @@ -60,7 +55,6 @@ fn fuelup_toolchain_install_nightly() -> Result<()> { fn fuelup_toolchain_install_nightly_date() -> Result<()> { testcfg::setup(FuelupState::Empty, &|cfg| { cfg.fuelup(&["toolchain", "install", "nightly-2022-08-31"]); - for entry in cfg.toolchains_dir().read_dir().expect("Could not read dir") { let toolchain_dir = entry.unwrap(); let expected_toolchain_name = @@ -74,7 +68,6 @@ fn fuelup_toolchain_install_nightly_date() -> Result<()> { expect_files_exist(&toolchain_dir.path().join("bin"), ALL_BINS); } })?; - Ok(()) } @@ -83,9 +76,7 @@ fn fuelup_toolchain_install_malformed_date() -> Result<()> { testcfg::setup(FuelupState::Empty, &|cfg| { let toolchain = "nightly-2022-08-31-"; let output = cfg.fuelup(&["toolchain", "install", toolchain]); - let expected_stdout = format!("Unknown name for toolchain: {toolchain}\n"); - assert!(output.status.success()); assert_eq!(output.stdout, expected_stdout); })?; @@ -100,7 +91,6 @@ fn fuelup_toolchain_install_date_target_allowed() -> Result<()> { let output = cfg.fuelup(&["toolchain", "install", &toolchain]); assert!(output.status.success()); })?; - Ok(()) } @@ -154,13 +144,11 @@ fn fuelup_toolchain_new() -> Result<()> { "New toolchain initialized: {CUSTOM_TOOLCHAIN_NAME} Default toolchain set to '{CUSTOM_TOOLCHAIN_NAME}'\n" ); - assert_eq!(output.stdout, expected_stdout); assert!(cfg.toolchain_bin_dir(CUSTOM_TOOLCHAIN_NAME).is_dir()); let default = cfg.default_toolchain(); assert_eq!(default, Some(CUSTOM_TOOLCHAIN_NAME.to_string())); })?; - Ok(()) } @@ -173,7 +161,6 @@ fn fuelup_toolchain_new_disallowed() -> Result<()> { assert_eq!(output.stderr, expected_stderr); } })?; - Ok(()) } @@ -186,6 +173,5 @@ fn fuelup_toolchain_new_disallowed_with_target() -> Result<()> { let expected_stderr = format!("error: invalid value '{toolchain_name}' for '': Cannot use distributable toolchain name '{toolchain_name}' as a custom toolchain name\n\nFor more information, try '--help'.\n"); assert_eq!(output.stderr, expected_stderr); })?; - Ok(()) } diff --git a/tests/update.rs b/tests/update.rs index 0af95009c..0d844670b 100644 --- a/tests/update.rs +++ b/tests/update.rs @@ -1,7 +1,7 @@ +pub mod testcfg; + use anyhow::Result; use fuelup::target_triple::TargetTriple; - -pub mod testcfg; use testcfg::FuelupState; #[test] diff --git a/tests/upgrade.rs b/tests/upgrade.rs index 6f029110b..96f1332d8 100644 --- a/tests/upgrade.rs +++ b/tests/upgrade.rs @@ -1,8 +1,8 @@ +pub mod testcfg; + use anyhow::Result; use testcfg::FuelupState; -pub mod testcfg; - #[test] fn fuelup_upgrade() -> Result<()> { testcfg::setup(FuelupState::LatestToolchainInstalled, &|cfg| { @@ -10,6 +10,5 @@ fn fuelup_upgrade() -> Result<()> { let expected_stdout_starts_with = "Already up to date"; assert!(output.stdout.contains(expected_stdout_starts_with)); })?; - Ok(()) } diff --git a/tests/version.rs b/tests/version.rs index a5cbb1f0e..d5b7a7995 100644 --- a/tests/version.rs +++ b/tests/version.rs @@ -1,18 +1,15 @@ +pub mod testcfg; + use anyhow::Result; use std::env; - -pub mod testcfg; use testcfg::FuelupState; #[test] fn fuelup_version() -> Result<()> { testcfg::setup(FuelupState::Empty, &|cfg| { let expected_version = format!("fuelup {}\n", clap::crate_version!()); - let stdout = cfg.fuelup(&["--version"]).stdout; - assert_eq!(stdout, expected_version); })?; - Ok(()) }