From fd406f0f82927aa3a3ce58a4f90e2e28c0de935d Mon Sep 17 00:00:00 2001 From: Gideon <87426140+GideonBear@users.noreply.github.com> Date: Sun, 13 Apr 2025 10:43:08 +0200 Subject: [PATCH] Add `output_changed_message!`, replace some `.expect`s (#1110) --- src/steps/generic.rs | 23 +++++++++++++---------- src/steps/os/archlinux.rs | 7 ++++--- src/steps/os/unix.rs | 21 ++++++++------------- src/utils.rs | 12 ++++++++++++ 4 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/steps/generic.rs b/src/steps/generic.rs index cbfebb4a..04f7c36b 100644 --- a/src/steps/generic.rs +++ b/src/steps/generic.rs @@ -20,12 +20,12 @@ use crate::execution_context::ExecutionContext; use crate::executor::ExecutorOutput; use crate::terminal::{print_separator, shell}; use crate::utils::{check_is_python_2_or_shim, get_require_sudo_string, require, require_option, which, PathExt}; -use crate::Step; use crate::HOME_DIR; use crate::{ error::{SkipStep, StepFailed, TopgradeError}, terminal::print_warning, }; +use crate::{output_changed_message, Step}; #[cfg(target_os = "linux")] pub fn is_wsl() -> Result { @@ -472,8 +472,9 @@ fn run_vscode_compatible(ctx: &ExecutionContext) -> Result Version::parse(&item).map_err(std::convert::Into::into) } None => { - return Err(eyre!(format!( - "The output of `{bin_name} --version` changed, please file an issue to Topgrade: No first line" + return Err(eyre!(output_changed_message!( + &format!("{bin_name} --version"), + "No first line" ))) } }; @@ -481,9 +482,8 @@ fn run_vscode_compatible(ctx: &ExecutionContext) -> Result // Raise any errors in parsing the version // The benefit of handling VSCodium versions so old that the version format is something // unexpected is outweighed by the benefit of failing fast on new breaking versions - let version = version.wrap_err(format!( - "the output of `{bin_name} --version` changed, please file an issue to Topgrade" - ))?; + let version = + version.wrap_err_with(|| output_changed_message!(&format!("{bin_name} --version"), "Invalid version"))?; debug!("Detected {name} version as: {version}"); if version < Version::new(1, 86, 0) { @@ -674,9 +674,12 @@ pub fn run_pip3_update(ctx: &ExecutionContext) -> Result<()> { { Ok(output) => { let stdout = output.stdout.trim(); - stdout - .parse::() - .expect("unexpected output that is not `true` or `false`") + stdout.parse::().wrap_err_with(|| { + output_changed_message!( + "pip config get global.break-system-packages", + "unexpected output that is not `true` or `false`" + ) + })? } // it can fail because this key may not be set // @@ -1344,7 +1347,7 @@ pub fn run_uv(ctx: &ExecutionContext) -> Result<()> { // 2. "0.5.11+1" }; let version = - Version::parse(version_str).expect("the output of `uv --version` changed, please file an issue to Topgrade"); + Version::parse(version_str).wrap_err_with(|| output_changed_message!("uv --version", "Invalid version"))?; if version < Version::new(0, 4, 25) { // For uv before version 0.4.25 (exclusive), the `self` sub-command only diff --git a/src/steps/os/archlinux.rs b/src/steps/os/archlinux.rs index ac0ab8ab..2a62de82 100644 --- a/src/steps/os/archlinux.rs +++ b/src/steps/os/archlinux.rs @@ -3,7 +3,7 @@ use std::ffi::OsString; use std::path::{Path, PathBuf}; use color_eyre::eyre; -use color_eyre::eyre::Result; +use color_eyre::eyre::{Context, Result}; use rust_i18n::t; use walkdir::WalkDir; @@ -12,7 +12,7 @@ use crate::error::TopgradeError; use crate::execution_context::ExecutionContext; use crate::utils::require_option; use crate::utils::which; -use crate::{config, Step}; +use crate::{config, output_changed_message, Step}; fn get_execution_path() -> OsString { let mut path = OsString::from("/usr/bin:"); @@ -285,7 +285,8 @@ impl ArchPackageManager for Aura { // Output will be something like: "aura x.x.x\n" let version_cmd_stdout = version_cmd_output.stdout; let version_str = version_cmd_stdout.trim_start_matches("aura ").trim_end(); - let version = Version::parse(version_str).expect("invalid version"); + let version = Version::parse(version_str) + .wrap_err_with(|| output_changed_message!("aura --version", "invalid version"))?; // Aura, since version 4.0.6, no longer needs sudo. // diff --git a/src/steps/os/unix.rs b/src/steps/os/unix.rs index ec3f136a..aa1ae9f6 100644 --- a/src/steps/os/unix.rs +++ b/src/steps/os/unix.rs @@ -7,7 +7,7 @@ use std::process::Command; use std::{env::var, path::Path}; use crate::command::CommandExt; -use crate::{Step, HOME_DIR}; +use crate::{output_changed_message, Step, HOME_DIR}; use color_eyre::eyre::eyre; use color_eyre::eyre::Context; use color_eyre::eyre::Result; @@ -470,16 +470,10 @@ pub fn run_nix(ctx: &ExecutionContext) -> Result<()> { return Err(eyre!("`nix --version` output was empty")); } - let captures = NIX_VERSION_REGEX.captures(get_version_cmd_first_line_stdout); - let raw_version = match &captures { - None => { - return Err(eyre!( - "`nix --version` output was weird: {get_version_cmd_first_line_stdout:?}\n\ - If the `nix --version` output format changed, please file an issue to Topgrade" - )); - } - Some(captures) => &captures[1], - }; + let captures = NIX_VERSION_REGEX + .captures(get_version_cmd_first_line_stdout) + .ok_or_else(|| eyre!(output_changed_message!("nix --version", "regex did not match")))?; + let raw_version = &captures[1]; let version = Version::parse(raw_version).wrap_err_with(|| format!("Unable to parse Nix version: {raw_version:?}"))?; @@ -653,10 +647,11 @@ pub fn run_asdf(ctx: &ExecutionContext) -> Result<()> { let mut remaining = version_stdout.trim_start_matches('v'); let idx = remaining .find('-') - .expect("the output of `asdf version` changed, please file an issue to Topgrade"); + .ok_or_else(|| eyre!(output_changed_message!("asdf version", "no dash (-) found")))?; // remove the hash part remaining = &remaining[..idx]; - let version = Version::parse(remaining).expect("should be a valid version"); + let version = + Version::parse(remaining).wrap_err_with(|| output_changed_message!("asdf version", "invalid version"))?; if version < Version::new(0, 15, 0) { ctx.run_type() .execute(&asdf) diff --git a/src/utils.rs b/src/utils.rs index 957a8b61..b20e6072 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -282,3 +282,15 @@ pub fn install_color_eyre() -> Result<()> { .display_location_section(true) .install() } + +/// Macro to construct an error message for when the output of a command is unexpected. +#[macro_export] +macro_rules! output_changed_message { + ($command:expr, $message:expr) => { + format!( + "The output of `{}` changed: {}. This is not your fault, this is an issue in Topgrade. Please open an issue at: https://github.com/topgrade-rs/topgrade/issues/new?template=bug_report.md", + $command, + $message, + ) + }; +}