From 99d09b516f271fc6e2ec0c62342ecdaa1cef0618 Mon Sep 17 00:00:00 2001 From: Martijn Visser Date: Thu, 25 Jul 2024 16:10:45 +0200 Subject: [PATCH] Support overriding an override Fixes #987. Now we get a stderr message on `override add`, which indicates if an override is changed, new, or already exists. If the channel is not installed, it gives the same error as julialauncher. Please check carefully since I'm new to Rust. Especially not sure if the `iter_mut` pattern is idiomatic. --- src/command_override.rs | 44 +++++++++++++++++++++++++++------- tests/command_override_test.rs | 31 +++++++++++++++++++++++- 2 files changed, 65 insertions(+), 10 deletions(-) diff --git a/src/command_override.rs b/src/command_override.rs index ae9c0092..fe1440e1 100644 --- a/src/command_override.rs +++ b/src/command_override.rs @@ -66,7 +66,11 @@ pub fn run_command_override_set( .with_context(|| "`override set` command failed to load configuration data.")?; if !config_file.data.installed_channels.contains_key(&channel) { - bail!("'{}' channel does not exist.", &channel); + bail!( + "'{}' is not installed. Please run `juliaup add {}` to install channel or version.", + &channel, + &channel + ); } let path = match path { @@ -75,25 +79,47 @@ pub fn run_command_override_set( } .canonicalize()?; - if config_file + if let Some(i) = config_file .data .overrides - .iter() - .any(|i| i.path == path.to_string_lossy()) + .iter_mut() + .find(|i| i.path == path.to_string_lossy()) { - bail!( - "'{}' path already has an override configured.", - path.to_string_lossy() - ); + if i.channel == channel { + eprintln!( + "Override already set to '{}' for '{}'.", + channel, + path.to_string_lossy() + ); + return Ok(()); + } else { + eprintln!( + "Override changed from '{}' to '{}' for '{}'.", + i.channel, + channel, + path.to_string_lossy() + ); + i.channel = channel.clone(); + + save_config_db(&mut config_file).with_context(|| { + "Failed to save configuration file from `override set` command." + })?; + return Ok(()); + } } config_file.data.overrides.push(JuliaupOverride { path: path.to_string_lossy().to_string(), channel: channel.clone(), }); + eprintln!( + "Override set to '{}' for '{}'.", + channel, + path.to_string_lossy() + ); save_config_db(&mut config_file) - .with_context(|| "Failed to save configuration file from `override add` command.")?; + .with_context(|| "Failed to save configuration file from `override set` command.")?; Ok(()) } diff --git a/tests/command_override_test.rs b/tests/command_override_test.rs index 22c1d948..27d0a0e2 100644 --- a/tests/command_override_test.rs +++ b/tests/command_override_test.rs @@ -1,5 +1,6 @@ use assert_cmd::Command; use predicates::prelude::PredicateBooleanExt; +use predicates::str::starts_with; #[test] fn command_override_status_test() { @@ -83,6 +84,32 @@ fn command_override_cur_dir_test() { .success() .stdout("1.6.7"); + Command::cargo_bin("juliaup") + .unwrap() + .arg("override") + .arg("set") + .arg("1.6.7") + .env("JULIA_DEPOT_PATH", depot_dir.path()) + .env("JULIAUP_DEPOT_PATH", depot_dir.path()) + .current_dir(&or_dir) + .assert() + .success() + .stdout("") + .stderr(starts_with("Override set to '1.6.7'")); + + Command::cargo_bin("juliaup") + .unwrap() + .arg("override") + .arg("set") + .arg("1.6.7") + .env("JULIA_DEPOT_PATH", depot_dir.path()) + .env("JULIAUP_DEPOT_PATH", depot_dir.path()) + .current_dir(&or_dir) + .assert() + .success() + .stdout("") + .stderr(starts_with("Override already set to '1.6.7'")); + Command::cargo_bin("juliaup") .unwrap() .arg("override") @@ -92,7 +119,9 @@ fn command_override_cur_dir_test() { .env("JULIAUP_DEPOT_PATH", depot_dir.path()) .current_dir(&or_dir) .assert() - .success(); + .success() + .stdout("") + .stderr(starts_with("Override changed from '1.6.7' to '1.8.5'")); Command::cargo_bin("julia") .unwrap()