use toml_edit::{Array, DocumentMut};
const ALLOWLIST_KEYS: [&str; 2] = ["shell_allowlist", "shell_allowlist_extra"];
pub(crate) fn fix_document(doc: &mut DocumentMut) -> Vec<String> {
let mut results = Vec::new();
for key in ALLOWLIST_KEYS {
let Some(item) = doc.get_mut(key) else {
continue;
};
let Some(array) = item.as_array_mut() else {
continue;
};
results.extend(fix_array(array, key));
}
results
}
fn fix_array(array: &mut Array, key: &str) -> Vec<String> {
let invalid: Vec<(usize, String)> = array
.iter()
.enumerate()
.filter_map(|(i, v)| {
let s = v.as_str()?;
(!lean_ctx::core::shell_allowlist::is_valid_allowlist_entry(s))
.then(|| (i, s.to_string()))
})
.collect();
let mut results = Vec::new();
for (idx, entry_text) in invalid.into_iter().rev() {
array.remove(idx);
attach_disabled_comment(array, idx, &entry_text);
results.push(format!(
"disabled {key} entry '{entry_text}' (invalid `*` placement)"
));
}
results
}
fn attach_disabled_comment(array: &mut Array, idx: usize, entry_text: &str) {
let comment = format!(
"\n# lean-ctx doctor --fix: disabled โ \"*\" must be a trailing token, not\n\
# embedded (see `lean-ctx allow --help`)\n\
# \"{entry_text}\"\n"
);
if idx < array.len() {
if let Some(next) = array.get_mut(idx) {
let existing = next
.decor()
.prefix()
.and_then(|r| r.as_str())
.unwrap_or_default()
.to_string();
next.decor_mut().set_prefix(format!("{comment}{existing}"));
}
} else {
let existing = array.trailing().as_str().unwrap_or_default().to_string();
array.set_trailing(format!("{comment}{existing}"));
}
}
pub(crate) fn auto_apply() -> Vec<String> {
let Some(path) = lean_ctx::core::config::Config::path() else {
return Vec::new();
};
let Ok(content) = std::fs::read_to_string(&path) else {
return Vec::new();
};
let Ok(mut doc) = content.parse::<DocumentMut>() else {
return vec![format!(
"FAILED {} (invalid TOML, left untouched)",
path.display()
)];
};
let results = fix_document(&mut doc);
if results.is_empty() {
return results;
}
match std::fs::write(&path, doc.to_string()) {
Ok(()) => results,
Err(e) => vec![format!("FAILED {} ({e})", path.display())],
}
}
#[cfg(test)]
mod tests {
use super::fix_document;
use toml_edit::DocumentMut;
fn run(toml: &str) -> (Vec<String>, String) {
let mut doc = toml
.parse::<DocumentMut>()
.expect("input must be valid TOML");
let results = fix_document(&mut doc);
let out = doc.to_string();
out.parse::<DocumentMut>()
.unwrap_or_else(|e| panic!("fix produced invalid TOML: {e}\n---\n{out}"));
(results, out)
}
fn live_values(out: &str, key: &str) -> Vec<String> {
out.parse::<DocumentMut>()
.unwrap()
.get(key)
.and_then(|i| i.as_array().cloned())
.map(|a| {
a.iter()
.filter_map(|v| v.as_str().map(str::to_string))
.collect()
})
.unwrap_or_default()
}
#[test]
fn no_invalid_entries_is_a_no_op() {
let (results, out) = run(r#"shell_allowlist_extra = ["git", "cargo"]"#);
assert!(results.is_empty(), "{results:?}");
assert_eq!(
live_values(&out, "shell_allowlist_extra"),
vec!["git", "cargo"]
);
}
#[test]
fn single_item_array_invalid_entry_becomes_trailing_comment() {
let (results, out) = run(r#"shell_allowlist_extra = ["git * push"]"#);
assert_eq!(results.len(), 1);
assert!(results[0].contains("git * push"));
{
let found = &live_values(&out, "shell_allowlist_extra");
assert!(found.is_empty(), "{found:?}");
}
assert!(out.contains("lean-ctx doctor --fix: disabled"));
assert!(out.contains(r#"# "git * push""#));
}
#[test]
fn compact_array_middle_item_invalid() {
let (results, out) = run(r#"shell_allowlist_extra = ["git", "git * push", "cargo"]"#);
assert_eq!(results.len(), 1);
assert_eq!(
live_values(&out, "shell_allowlist_extra"),
vec!["git", "cargo"],
"the two valid neighbors must survive, in order: {out}"
);
assert!(out.contains(r#"# "git * push""#));
}
#[test]
fn compact_array_first_item_invalid() {
let (results, out) = run(r#"shell_allowlist_extra = ["*", "git", "cargo"]"#);
assert_eq!(results.len(), 1);
assert_eq!(
live_values(&out, "shell_allowlist_extra"),
vec!["git", "cargo"]
);
assert!(out.contains("# \"*\""));
}
#[test]
fn compact_array_last_item_invalid() {
let (results, out) = run(r#"shell_allowlist_extra = ["git", "cargo", "* *"]"#);
assert_eq!(results.len(), 1);
assert_eq!(
live_values(&out, "shell_allowlist_extra"),
vec!["git", "cargo"]
);
assert!(out.contains("# \"* *\""));
}
#[test]
fn pretty_array_middle_item_invalid() {
let input = "shell_allowlist_extra = [\n \"git status\",\n \"git * push\",\n \"cargo\",\n]\n";
let (results, out) = run(input);
assert_eq!(results.len(), 1);
assert_eq!(
live_values(&out, "shell_allowlist_extra"),
vec!["git status", "cargo"]
);
assert!(out.contains(r#"# "git * push""#));
assert!(out.contains("\"git status\""));
assert!(out.contains("\"cargo\""));
}
#[test]
fn pretty_array_last_item_invalid() {
let input =
"shell_allowlist_extra = [\n \"git status\",\n \"cargo\",\n \"* extra\",\n]\n";
let (results, out) = run(input);
assert_eq!(results.len(), 1);
assert_eq!(
live_values(&out, "shell_allowlist_extra"),
vec!["git status", "cargo"]
);
assert!(out.contains(r#"# "* extra""#));
}
#[test]
fn pretty_array_first_item_invalid() {
let input =
"shell_allowlist_extra = [\n \"* first\",\n \"git status\",\n \"cargo\",\n]\n";
let (results, out) = run(input);
assert_eq!(results.len(), 1);
assert_eq!(
live_values(&out, "shell_allowlist_extra"),
vec!["git status", "cargo"]
);
assert!(out.contains(r#"# "* first""#));
}
#[test]
fn multiple_invalid_entries_in_one_array_all_get_fixed() {
let (results, out) =
run(r#"shell_allowlist_extra = ["*", "git status", "git * push", "cargo", "* *"]"#);
assert_eq!(results.len(), 3, "got: {results:?}");
assert_eq!(
live_values(&out, "shell_allowlist_extra"),
vec!["git status", "cargo"],
"all three invalid entries removed, both valid ones survive in order: {out}"
);
for needle in [r#"# "*""#, r#"# "git * push""#, r#"# "* *""#] {
assert!(out.contains(needle), "missing {needle}: {out}");
}
}
#[test]
fn catches_bare_embedded_and_repeated_star() {
let (results, out) =
run(r#"shell_allowlist_extra = ["*", "git * push", "* *", "git status *"]"#);
assert_eq!(results.len(), 3, "got: {results:?}");
assert_eq!(
live_values(&out, "shell_allowlist_extra"),
vec!["git status *"],
"only the validly-placed trailing '*' entry survives: {out}"
);
}
#[test]
fn both_allowlist_keys_are_fixed_independently() {
let (results, out) = run("shell_allowlist = [\"git\", \"git * push\"]\n\
shell_allowlist_extra = [\"cargo\", \"*\"]\n");
assert_eq!(results.len(), 2, "got: {results:?}");
assert_eq!(live_values(&out, "shell_allowlist"), vec!["git"]);
assert_eq!(live_values(&out, "shell_allowlist_extra"), vec!["cargo"]);
}
#[test]
fn second_run_is_a_no_op_idempotent() {
let (first_results, first_out) =
run(r#"shell_allowlist_extra = ["git", "git * push", "cargo"]"#);
assert_eq!(first_results.len(), 1);
let mut doc = first_out.parse::<DocumentMut>().unwrap();
let second_results = fix_document(&mut doc);
assert!(
second_results.is_empty(),
"re-running must not re-flag an already-fixed entry: {second_results:?}"
);
assert_eq!(
doc.to_string(),
first_out,
"second run must not change the document"
);
}
}