Skip to content

Commit

Permalink
fix rye config --show-path (astral-sh#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsp authored Feb 21, 2024
1 parent 2b6c270 commit 33454c4
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ that were not yet released.

_Unreleased_

- Fixed `rye config --show-path` abort with an error. #706

- Bumped `uv` to 0.1.6. #719

- Bumped `ruff` to 0.2.2. #700
Expand Down
23 changes: 12 additions & 11 deletions rye/src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::sync::Arc;
use anyhow::bail;
use anyhow::Context;
use anyhow::Error;
use clap::Args as ClapArgs;
use clap::Parser;
use clap::ValueEnum;
use serde::Serialize;
Expand All @@ -31,19 +30,20 @@ enum Format {
#[derive(Parser, Debug)]
#[command(arg_required_else_help(true))]
pub struct Args {
#[command(flatten)]
action: ActionArgs,
/// Print the path to the config.
#[arg(long, conflicts_with = "format")]
#[arg(long)]
show_path: bool,

#[command(flatten)]
action: Action,
}

#[derive(Parser, Debug)]
#[group(required = false, multiple = true, conflicts_with = "show_path")]
pub struct Action {
/// Request parseable output format rather than lines.
#[arg(long)]
format: Option<Format>,
}

#[derive(ClapArgs, Debug)]
#[group(required = true, multiple = true)]
pub struct ActionArgs {
/// Reads a config key
#[arg(long)]
get: Vec<String>,
Expand All @@ -60,6 +60,7 @@ pub struct ActionArgs {
#[arg(long)]
unset: Vec<String>,
}

pub fn execute(cmd: Args) -> Result<(), Error> {
let mut config = Config::current();
let doc = Arc::make_mut(&mut config).doc_mut();
Expand All @@ -80,7 +81,7 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
}

let val = ptr.and_then(|x| x.as_value());
match cmd.format {
match cmd.action.format {
None => {
read_as_string.push(value_to_string(val));
}
Expand Down Expand Up @@ -171,7 +172,7 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
config.save()?;
}

match cmd.format {
match cmd.action.format {
None => {
for line in read_as_string {
echo!("{}", line);
Expand Down
2 changes: 2 additions & 0 deletions rye/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub const INSTA_FILTERS: &[(&str, &str)] = &[
r"(\b[A-Z]:)?[\\/].*?[\\/]\.rye-tests---[^\\/]+[\\/]",
"[TEMP_PATH]/",
),
// home
(r"(\b[A-Z]:)?[\\/].*?[\\/]rye-test-home", "[RYE_HOME]"),
// macos temp folder
(r"/var/folders/\S+?/T/\S+", "[TEMP_FILE]"),
// linux temp folders
Expand Down
111 changes: 111 additions & 0 deletions rye/tests/test_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use crate::common::{rye_cmd_snapshot, Space};

mod common;

#[test]
fn test_config_empty() {
let space = Space::new();
rye_cmd_snapshot!(space.rye_cmd().arg("config"), @r###"
success: false
exit_code: 2
----- stdout -----
----- stderr -----
Reads or modifies the global `config.toml` file
Usage: rye config [OPTIONS]
Options:
--show-path Print the path to the config
--format <FORMAT> Request parseable output format rather than lines [possible values:
json]
--get <GET> Reads a config key
--set <SET> Sets a config key to a string
--set-int <SET_INT> Sets a config key to an integer
--set-bool <SET_BOOL> Sets a config key to a bool
--unset <UNSET> Remove a config key
-h, --help Print help (see more with '--help')
"###);
}

#[test]
fn test_config_show_path() {
let space = Space::new();
rye_cmd_snapshot!(space.rye_cmd().arg("config").arg("--show-path"), @r###"
success: true
exit_code: 0
----- stdout -----
[RYE_HOME]/config.toml
----- stderr -----
"###);
}

#[test]
fn test_config_incompatible_format_and_show_path() {
let space = Space::new();
rye_cmd_snapshot!(space.rye_cmd().arg("config").arg("--show-path").arg("--format=json"), @r###"
success: false
exit_code: 2
----- stdout -----
----- stderr -----
error: an argument cannot be used with one or more of the other specified arguments
"###);
}

#[test]
fn test_config_get_set_multiple() {
let space = Space::new();
rye_cmd_snapshot!(space.rye_cmd()
.arg("config")
.arg("--set")
.arg("[email protected]")
.arg("--set-bool")
.arg("behavior.use-uv=true"),
@r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
"###);

rye_cmd_snapshot!(space.rye_cmd()
.arg("config")
.arg("--get")
.arg("default.toolchain")
.arg("--get")
.arg("behavior.use-uv")
.arg("--format=json"),
@r###"
success: true
exit_code: 0
----- stdout -----
{
"behavior.use-uv": true,
"default.toolchain": "[email protected]"
}
----- stderr -----
"###);
}

#[test]
// This test ensure that --show-path is not compatible with any other action
fn test_config_show_path_and_any_action() {
let space = Space::new();
rye_cmd_snapshot!(space.rye_cmd()
.arg("config")
.arg("--set")
.arg("[email protected]")
.arg("--show-path"),
@r###"
success: false
exit_code: 2
----- stdout -----
----- stderr -----
error: an argument cannot be used with one or more of the other specified arguments
"###);
}

0 comments on commit 33454c4

Please sign in to comment.