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

config: set $LESS and $LESSCHARSET even if $PAGER is set #3657

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ pub enum CommandNameAndArgs {
Structured {
#[serde(default)]
env: HashMap<String, String>,
#[serde(default)]
env_default: HashMap<String, String>,
command: NonEmptyCommandArgsVec,
},
}
Expand All @@ -469,6 +471,7 @@ impl CommandNameAndArgs {
}
CommandNameAndArgs::Structured {
env: _,
env_default: _,
command: cmd,
} => (Cow::Borrowed(&cmd.0[0]), Cow::Borrowed(&cmd.0[1..])),
}
Expand All @@ -478,7 +481,15 @@ impl CommandNameAndArgs {
pub fn to_command(&self) -> Command {
let (name, args) = self.split_name_and_args();
let mut cmd = Command::new(name.as_ref());
if let CommandNameAndArgs::Structured { env, .. } = self {
if let CommandNameAndArgs::Structured {
env_default, env, ..
} = self
{
for (key, val) in env_default {
if std::env::var(key).is_err() {
cmd.env(key, val);
}
}
cmd.envs(env);
}
cmd.args(args.as_ref());
Expand All @@ -498,8 +509,15 @@ impl fmt::Display for CommandNameAndArgs {
CommandNameAndArgs::String(s) => write!(f, "{s}"),
// TODO: format with shell escapes
CommandNameAndArgs::Vec(a) => write!(f, "{}", a.0.join(" ")),
CommandNameAndArgs::Structured { env, command } => {
for (k, v) in env.iter() {
CommandNameAndArgs::Structured {
env_default,
env,
command,
} => {
for (k, v) in env_default {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: Perhaps k.=v would work here? Or k~=v? Or (k=v)?

write!(f, "{k}={v} ")?;
}
for (k, v) in env {
write!(f, "{k}={v} ")?;
}
write!(f, "{}", command.0.join(" "))
Expand Down Expand Up @@ -540,6 +558,7 @@ empty_string = ""
"array" = ["emacs", "-nw"]
"string" = "emacs -nw"
structured = { command = ["emacs", "-nw"] }
with_env_default = { env_default = { KEY1 = "value1", KEY2 = "value2" }, command = ["emacs", "-nw"] }
with_env = { env = { KEY1 = "value1", KEY2 = "value2" }, command = ["emacs", "-nw"] }
"#;
let config = config::Config::builder()
Expand Down Expand Up @@ -582,6 +601,7 @@ with_env = { env = { KEY1 = "value1", KEY2 = "value2" }, command = ["emacs", "-n
assert_eq!(
command_args,
CommandNameAndArgs::Structured {
env_default: hashmap! {},
env: hashmap! {},
command: NonEmptyCommandArgsVec(["emacs", "-nw",].map(|s| s.to_owned()).to_vec())
}
Expand All @@ -590,10 +610,24 @@ with_env = { env = { KEY1 = "value1", KEY2 = "value2" }, command = ["emacs", "-n
assert_eq!(name, "emacs");
assert_eq!(args, ["-nw"].as_ref());

let command_args: CommandNameAndArgs = config.get("with_env_default").unwrap();
Copy link
Collaborator

@ilyagr ilyagr May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: I was going to suggest adding a test with https://doc.rust-lang.org/std/env/fn.set_var.html here, but I'm not so sure after reading the safety warning there. cargo-nextest is, in principle, multithreaded.

You could also set up a test that sets some variable that very likely exists, like PATH or CARGO. I guess to make the test reliable, you could check if that variable is set first, and skip the test if it isn't.

assert_eq!(
command_args,
CommandNameAndArgs::Structured {
env_default: hashmap! {
"KEY1".to_string() => "value1".to_string(),
"KEY2".to_string() => "value2".to_string(),
},
env: hashmap! {},
command: NonEmptyCommandArgsVec(["emacs", "-nw",].map(|s| s.to_owned()).to_vec())
}
);

let command_args: CommandNameAndArgs = config.get("with_env").unwrap();
assert_eq!(
command_args,
CommandNameAndArgs::Structured {
env_default: hashmap! {},
env: hashmap! {
"KEY1".to_string() => "value1".to_string(),
"KEY2".to_string() => "value2".to_string(),
Expand Down