fix(xtask): recognize documented launcher switch reads

This commit is contained in:
Tommie Hansen
2026-12-04 23:38:41 +01:00
parent b581f7e0a8
commit f289c1e6a6

View File

@ -2,9 +2,9 @@
//!
//! A switch is read through a string literal of its whole name
//! (`std::env::var("VICTORY_X")`, `crate::switch::on("VICTORY_X")`,
//! `crate::ab::pick("VICTORY_X")`), so a name the Markdown files give must
//! appear quoted in at least one Rust file. xtask's own files are left out
//! of the search: this module's tests name switches of their own.
//! `crate::ab::pick("VICTORY_X")`). The launcher also reads switches through
//! braced shell parameter expansions in run.sh. xtask's own files are left
//! out of the search: this module's tests name switches of their own.
use std::collections::BTreeSet;
@ -29,13 +29,28 @@ fn names(text: &str) -> BTreeSet<String> {
found
}
/// The names of `documented` that no text of `sources` holds in quotes.
fn unread<'a>(documented: &'a BTreeSet<String>, sources: &[String]) -> Vec<&'a str> {
/// Recognize the launcher's braced reads, including its default-value form.
/// This is a textual check like the Rust literal check, not a shell parser.
fn shell_reads(source: &str, name: &str) -> bool {
source
.lines()
.filter(|line| !line.trim_start().starts_with('#'))
.flat_map(|line| line.split("${").skip(1))
.filter_map(|tail| tail.strip_prefix(name))
.any(|tail| tail.starts_with('}') || tail.starts_with(":-"))
}
/// The documented names absent from both Rust literals and launcher reads.
fn unread<'a>(
documented: &'a BTreeSet<String>,
sources: &[String],
launcher: &str,
) -> Vec<&'a str> {
documented
.iter()
.filter(|name| {
let quoted = format!("\"{name}\"");
!sources.iter().any(|s| s.contains(&quoted))
!sources.iter().any(|s| s.contains(&quoted)) && !shell_reads(launcher, name)
})
.map(String::as_str)
.collect()
@ -63,7 +78,12 @@ fn documented_switches_are_read() {
!documented.is_empty(),
"no VICTORY_* names found in the Markdown files"
);
let dead = unread(&documented, &sources);
let launcher = if files.iter().any(|path| path == "run.sh") {
std::fs::read_to_string(root.join("run.sh")).expect("read launcher")
} else {
String::new()
};
let dead = unread(&documented, &sources, &launcher);
let report: Vec<String> = dead
.iter()
.map(|name| {
@ -77,7 +97,7 @@ fn documented_switches_are_read() {
.collect();
assert!(
dead.is_empty(),
"switches the documents name but no Rust source reads: {}",
"switches the documents name but no Rust source or launcher reads: {}",
report.join("; ")
);
}
@ -100,5 +120,25 @@ fn a_longer_name_does_not_read_a_shorter_one() {
"std::env::var_os(\"VICTORY_LATE_WAITS\")".to_owned(),
"on(\"VICTORY_WIDE_BACK\")".to_owned(),
];
assert_eq!(unread(&documented, &sources), ["VICTORY_LATE_WAIT"]);
assert_eq!(unread(&documented, &sources, ""), ["VICTORY_LATE_WAIT"]);
}
#[test]
fn launcher_reads_whole_switches_without_counting_comment_only_lines() {
let documented = names(
"VICTORY_GPU_GUARD VICTORY_GPU_GUARD_PERCENT VICTORY_RAM_GUARD_STALL \
VICTORY_STEAMDECK VICTORY_UNUSED VICTORY_GPU VICTORY_ASSIGNED",
);
let launcher = r#"
# ${VICTORY_UNUSED:-0}
VICTORY_ASSIGNED=1
if [ "${VICTORY_GPU_GUARD:-0}" = 1 ] || [ "${VICTORY_STEAMDECK}" = 1 ]; then
guard=${VICTORY_GPU_GUARD_PERCENT:-85}
stall=${VICTORY_RAM_GUARD_STALL:-${RAM_GUARD_STALL:-40}}
fi
"#;
assert_eq!(
unread(&documented, &[], launcher),
["VICTORY_ASSIGNED", "VICTORY_GPU", "VICTORY_UNUSED"]
);
}