From 72163c3576a1f2f53034ee4fb74c282b3a22283f Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Thu, 16 May 2024 00:33:43 -0700 Subject: [PATCH] address PR comments --- README.md | 4 ++-- src/search.rs | 16 +++++----------- tests/global.rs | 40 ++++++++++++++++++++++++++++++++++++++++ tests/lib.rs | 1 + 4 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 tests/global.rs diff --git a/README.md b/README.md index 6e3f9eab35..358dcd3e73 100644 --- a/README.md +++ b/README.md @@ -3188,8 +3188,8 @@ below. You can put recipes that are used across many projects in this global justfile, and easily invoke them from any directory. `just` will search for a global justfile in the following locations, in this order, stopping after it finds a `justfile` in any location: -- `$XDG_CONFIG_HOME/just/global.just` -- `$HOME/.config/just/global.just` +- `$XDG_CONFIG_HOME/just/justfile` +- `$HOME/.config/just/justfile` - `$HOME/.justfile` - `$HOME/justfile` diff --git a/src/search.rs b/src/search.rs index d20d7ff6c4..7c03d73270 100644 --- a/src/search.rs +++ b/src/search.rs @@ -11,19 +11,13 @@ pub(crate) struct Search { impl Search { fn candidate_global_justfiles() -> Vec { - // Just will search for a global justfile in `$XDG_CONFIG_HOME/just/global.just`, - // `$HOME/.justfile`, `$HOME/justfile`, in that order. + // Just will search for a global justfile in `$XDG_CONFIG_HOME/just/justfile`, + // `$HOME/.config/just/justfile`, `$HOME/.justfile`, `$HOME/justfile`, in that order. let mut global_candidate_paths = vec![]; - // See https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables - let xdg_config_home = if let Ok(config_dir) = std::env::var("XDG_CONFIG_HOME") { - Some(PathBuf::from(config_dir)) - } else { - dirs::home_dir().map(|home_dir| home_dir.join(".config")) - }; - - if let Some(config_dir) = xdg_config_home { - global_candidate_paths.push(config_dir.join("just").join("global.just")); + let config_dir = dirs::config_dir().or_else(|| dirs::home_dir().map(|d| d.join(".config"))); + if let Some(config_dir) = config_dir { + global_candidate_paths.push(config_dir.join("just").join(JUSTFILE_NAMES[0])); } if let Some(home_dir) = dirs::home_dir() { diff --git a/tests/global.rs b/tests/global.rs new file mode 100644 index 0000000000..8c49f02563 --- /dev/null +++ b/tests/global.rs @@ -0,0 +1,40 @@ +use super::*; + +#[test] +fn test_global_justfile() { + let tmp = temptree! { + just: { + justfile: "default:\n echo 'foo'", + + } + }; + + let xdg_config_path = tmp.path(); + + let output = Command::new(executable_path("just")) + .env("XDG_CONFIG_HOME", xdg_config_path.display().to_string()) + .args(["--global"]) + .output() + .expect("just invocation failed"); + + let expected_status = 0; + let expected_stdout = "foo\n"; + + let mut failure = false; + + let status = output.status.code().unwrap(); + if status != expected_status { + println!("bad status: {status} != {expected_status}"); + failure = true; + } + + let stdout = str::from_utf8(&output.stdout).unwrap(); + if stdout != expected_stdout { + println!("bad stdout:\ngot:\n{stdout:?}\n\nexpected:\n{expected_stdout:?}"); + failure = true; + } + + if failure { + panic!("test failed"); + } +} diff --git a/tests/lib.rs b/tests/lib.rs index 26c9d12581..fe23dc4c86 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -58,6 +58,7 @@ mod export; mod fallback; mod fmt; mod functions; +mod global; mod ignore_comments; mod imports; mod init;