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: also use XDG_CONFIG_HOME on MacOS #3466

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 3 additions & 4 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ use crate::command_error::{
};
use crate::commit_templater::{CommitTemplateLanguage, CommitTemplateLanguageExtension};
use crate::config::{
new_config_path, AnnotatedValue, CommandNameAndArgs, ConfigSource, LayeredConfigs,
new_or_existing_config_path, AnnotatedValue, CommandNameAndArgs, ConfigSource, LayeredConfigs,
};
use crate::formatter::{FormatRecorder, Formatter, PlainTextFormatter};
use crate::git_util::{
Expand Down Expand Up @@ -1984,9 +1984,8 @@ pub fn get_new_config_file_path(
) -> Result<PathBuf, CommandError> {
let edit_path = match config_source {
// TODO(#531): Special-case for editors that can't handle viewing directories?
ConfigSource::User => {
new_config_path()?.ok_or_else(|| user_error("No repo config path found to edit"))?
}
ConfigSource::User => new_or_existing_config_path()?
.ok_or_else(|| user_error("No repo config path found to edit"))?,
ConfigSource::Repo => command.workspace_loader()?.repo_path().join("config.toml"),
_ => {
return Err(user_error(format!(
Expand Down
86 changes: 47 additions & 39 deletions cli/src/config.rs
ckoehler marked this conversation as resolved.
Show resolved Hide resolved
ckoehler marked this conversation as resolved.
Show resolved Hide resolved
ckoehler marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl ConfigEnv {
}
}

fn new_config_path(self) -> Result<Option<PathBuf>, ConfigError> {
fn new_or_existing_config_path(self) -> Result<Option<PathBuf>, ConfigError> {
match self.config_path()? {
ConfigPath::Existing(path) => Ok(Some(path)),
ConfigPath::New(path) => {
Expand All @@ -322,8 +322,8 @@ pub fn existing_config_path() -> Result<Option<PathBuf>, ConfigError> {
/// tries to guess a reasonable new location for it. If a path to a new config
/// file is returned, the parent directory may be created as a result of this
/// call.
pub fn new_config_path() -> Result<Option<PathBuf>, ConfigError> {
ConfigEnv::new().new_config_path()
pub fn new_or_existing_config_path() -> Result<Option<PathBuf>, ConfigError> {
ConfigEnv::new().new_or_existing_config_path()
}

/// Environment variables that should be overridden by config values
Expand Down Expand Up @@ -765,60 +765,62 @@ mod tests {
home_dir: Some("home".into()),
..Default::default()
},
want: Want::ExistingAndNew("home/.jjconfig.toml"),
want: Want::Existing("home/.jjconfig.toml"),
}
.run()
}

#[test]
fn test_config_path_home_dir_new() -> anyhow::Result<()> {
fn test_config_path_home_dir_doesnt_create_new() -> anyhow::Result<()> {
TestCase {
files: vec![],
cfg: ConfigEnv {
home_dir: Some("home".into()),
..Default::default()
},
want: Want::New("home/.jjconfig.toml"),
want: Want::None,
}
.run()
}

#[test]
fn test_config_path_config_dir_existing() -> anyhow::Result<()> {
fn test_config_path_config_dir_new() -> anyhow::Result<()> {
TestCase {
files: vec!["config/jj/config.toml"],
files: vec![],
cfg: ConfigEnv {
config_dir: Some("config".into()),
home_dir: Some("home".into()),
..Default::default()
},
want: Want::ExistingAndNew("config/jj/config.toml"),
want: Want::ExistingOrNew("config/jj/config.toml"),
}
.run()
}

#[test]
fn test_config_path_config_dir_new() -> anyhow::Result<()> {
fn test_config_path_config_dir_existing() -> anyhow::Result<()> {
TestCase {
files: vec![],
files: vec!["config/jj/config.toml"],
cfg: ConfigEnv {
config_dir: Some("config".into()),
..Default::default()
},
want: Want::New("config/jj/config.toml"),
want: Want::Existing("config/jj/config.toml"),
}
.run()
}

#[test]
fn test_config_path_new_prefer_config_dir() -> anyhow::Result<()> {
// if no config exists, make one in the config dir location
fn test_config_path_new_default_to_config_dir() -> anyhow::Result<()> {
TestCase {
files: vec![],
cfg: ConfigEnv {
config_dir: Some("config".into()),
home_dir: Some("home".into()),
..Default::default()
},
want: Want::New("config/jj/config.toml"),
want: Want::ExistingOrNew("config/jj/config.toml"),
}
.run()
}
Expand All @@ -831,20 +833,20 @@ mod tests {
jj_config: Some("custom.toml".into()),
..Default::default()
},
want: Want::ExistingAndNew("custom.toml"),
want: Want::Existing("custom.toml"),
}
.run()
}

#[test]
fn test_config_path_jj_config_new() -> anyhow::Result<()> {
fn test_config_path_jj_config_doesnt_create_new() -> anyhow::Result<()> {
TestCase {
files: vec![],
cfg: ConfigEnv {
jj_config: Some("custom.toml".into()),
..Default::default()
},
want: Want::New("custom.toml"),
want: Want::None,
}
.run()
}
Expand All @@ -858,7 +860,7 @@ mod tests {
config_dir: Some("config".into()),
..Default::default()
},
want: Want::ExistingAndNew("config/jj/config.toml"),
want: Want::Existing("config/jj/config.toml"),
}
.run()
}
Expand All @@ -872,7 +874,7 @@ mod tests {
config_dir: Some("config".into()),
..Default::default()
},
want: Want::ExistingAndNew("home/.jjconfig.toml"),
want: Want::Existing("home/.jjconfig.toml"),
}
.run()
}
Expand Down Expand Up @@ -901,7 +903,7 @@ mod tests {
Err(ConfigError::AmbiguousSource(_, _))
);
assert_matches!(
cfg.clone().new_config_path(),
cfg.clone().new_or_existing_config_path(),
Err(ConfigError::AmbiguousSource(_, _))
);
Ok(())
Expand All @@ -921,8 +923,8 @@ mod tests {

enum Want {
None,
New(&'static str),
ExistingAndNew(&'static str),
Existing(&'static str),
ExistingOrNew(&'static str),
}

struct TestCase {
Expand Down Expand Up @@ -958,25 +960,31 @@ mod tests {
}
};

let (want_existing, want_new) = match self.want {
Want::None => (None, None),
Want::New(want) => (None, Some(want)),
Want::ExistingAndNew(want) => (Some(want), Some(want)),
match self.want {
Want::None => {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't it remove the assertion for Want::None case?

I don't remember the original expectation of these tests, but it doesn't make sense to run a test with no assertion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing that out, you're exactly right! The Want::None test doesn't really make sense, because we need to either call the function that gives us a config path without creating it or with creating it.

What we want is a way to tell the test to run one or the other, and what to expect out of each. I'm changing it accordingly.

Copy link
Member

Choose a reason for hiding this comment

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

It looks like this was added in #1861, so maybe @qfel can clarify?

Copy link
Contributor

Choose a reason for hiding this comment

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

It has been a while, so I don't remember well, but..

It looks like Want::None used to declare both new_config_path and existing_config_path were expected to return None. In this PR test_config_path_home_dir_doesnt_create_new uses Want::None. What do you want to assert for that test? That both existing_config_path and new_or_existing_config_path return None, or something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't understand the behavior too well before. In this case it should be None with existing_config_path and Some("<path>") with new_or_existing_config_path.

Want::Existing(want) => {
check(
"existing_config_path",
ConfigEnv::existing_config_path,
Some(want),
)?;
}
Want::ExistingOrNew(want) => {
let got = check(
"new_or_existing_config_path",
ConfigEnv::new_or_existing_config_path,
Some(want),
)?;
if let Some(path) = got {
if !Path::new(&path).is_file() {
return Err(anyhow!(
"new_config_path returned {path:?} which is not a file"
));
}
}
}
};

check(
"existing_config_path",
ConfigEnv::existing_config_path,
want_existing,
)?;
let got = check("new_config_path", ConfigEnv::new_config_path, want_new)?;
if let Some(path) = got {
if !Path::new(&path).is_file() {
return Err(anyhow!(
"new_config_path returned {path:?} which is not a file"
));
}
}
Ok(())
}
}
Expand Down