Skip to content

Commit

Permalink
fix: incorrect config_root for project/.mise/config.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
roele committed Jan 14, 2025
1 parent 97fee36 commit f31e6ad
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/config/config_file/mise_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub struct EnvList(pub(crate) Vec<EnvDirective>);
impl MiseToml {
pub fn init(path: &Path) -> Self {
let mut context = BASE_CONTEXT.clone();
context.insert("config_root", path.parent().unwrap().to_str().unwrap());
context.insert("config_root", get_config_root(path).to_str().unwrap());
Self {
path: path.to_path_buf(),
context,
Expand Down Expand Up @@ -514,6 +514,30 @@ impl ConfigFile for MiseToml {
}
}

/// Returns the root path of the config directory.
/// If the config directory is nested, this will return the top-most directory.
/// For example
/// ```text
/// /src/myproj/mise.toml -> /src/myproj
/// /src/myproj/mise/config.toml -> /src/myproj
/// /src/myproj/.mise/config.toml -> /src/myproj
/// /src/myproj/.config/mise/config.toml -> /src/myproj
/// /src/myproj/.config/mise/conf.d/a.toml -> /src/myproj
/// ```
fn get_config_root(path: &Path) -> PathBuf {
let is_config_dir = |p: &Path| {
p.ends_with("mise")
|| p.ends_with(".mise")
|| p.ends_with(".config")
|| p.ends_with("conf.d")
};
let mut config_root = path.to_path_buf();
while is_config_dir(&config_root) {
config_root.pop();
}
config_root
}

/// Returns a [`toml_edit::Key`] from the given `key`.
/// Preserves any surrounding whitespace (e.g. comments) if the key already exists in the provided [`toml_edit::Table`].
fn get_key_with_decor(table: &toml_edit::Table, key: &str) -> Key {
Expand Down Expand Up @@ -1813,6 +1837,23 @@ mod tests {
");
}

#[test]
fn test_get_config_root() {
let cases = vec![
("/src/myproj/mise.toml", "/src/myproj"),
("/src/myproj/mise/config.toml", "/src/myproj"),
("/src/myproj/.mise/config.toml", "/src/myproj"),
("/src/myproj/.config/mise/config.toml", "/src/myproj"),
("/src/myproj/.config/mise/conf.d/a.toml", "/src/myproj"),
];

for (input, expected) in cases {
let input_path = Path::new(input);
let expected_path = Path::new(expected);
assert_eq!(get_config_root(input_path.parent().unwrap()), expected_path);
}
}

fn parse(s: String) -> MiseToml {
let p = CWD.as_ref().unwrap().join(".test.mise.toml");
file::write(&p, s).unwrap();
Expand Down

0 comments on commit f31e6ad

Please sign in to comment.