Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rye config --show-path #706

Merged
merged 7 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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("default.toolchain=cpython@3.12")
.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": "cpython@3.12"
}

----- 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("default.toolchain=cpython@3.12")
.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
"###);
}