mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-02 20:18:38 -05:00
xtask: test-lint decodes escapes in #[path] values
A plain #[path = "\x63hild.rs"] after #[cfg_attr(windows, path = "child.rs")] loads child.rs on Linux and FreeBSD, but test-lint compared the undecoded spelling with the file name, dropped that route and reported a test in child.rs as never-run. The plain attribute and the path argument of cfg_attr now decode the escapes of an ordinary string literal with the same unescape the cfg values use. Raw string literals are read as written.
This commit is contained in:
1 parent
d94c7477c5
commit
859a6b4dd3
1 file changed
+37
-6
+37
-6
@@ -4820,7 +4820,8 @@ fn path_overrides<'a>(sources: impl IntoIterator<Item = (&'a str, &'a str)>) ->
|
||||
}
|
||||
|
||||
/// The string value of a `#[path = "…"]` attribute in `m[from..to]`, written as an ordinary or a
|
||||
/// raw string literal (`#[path = r"shared.rs"]`, `#[path = r#"shared.rs"#]`).
|
||||
/// raw string literal (`#[path = r"shared.rs"]`, `#[path = r#"shared.rs"#]`). The escapes of an
|
||||
/// ordinary literal are decoded (`"\x63hild.rs"` names `child.rs`); a raw literal is read as written.
|
||||
fn path_attr(m: &[u8], src: &[u8], from: usize, to: usize) -> Option<String> {
|
||||
words(m, b"path", from, to).into_iter().find_map(|at| {
|
||||
let attr = m[..at].trim_ascii_end().strip_suffix(b"[").is_some_and(|b| b.trim_ascii_end().ends_with(b"#"));
|
||||
@@ -4829,7 +4830,8 @@ fn path_attr(m: &[u8], src: &[u8], from: usize, to: usize) -> Option<String> {
|
||||
if !attr || m.get(eq) != Some(&b'=') {
|
||||
return None;
|
||||
}
|
||||
let hashes = if m.get(q) == Some(&b'r') {
|
||||
let raw = m.get(q) == Some(&b'r');
|
||||
let hashes = if raw {
|
||||
let h = m[q + 1..].iter().take_while(|&&c| c == b'#').count();
|
||||
q += 1 + h;
|
||||
h
|
||||
@@ -4841,13 +4843,17 @@ fn path_attr(m: &[u8], src: &[u8], from: usize, to: usize) -> Option<String> {
|
||||
}
|
||||
let close: Vec<u8> = std::iter::once(b'"').chain(std::iter::repeat_n(b'#', hashes)).collect();
|
||||
let end = find(m, &close, q + 1)?;
|
||||
Some(String::from_utf8_lossy(&src[q + 1..end]).into_owned())
|
||||
let body = String::from_utf8_lossy(&src[q + 1..end]);
|
||||
Some(if raw { body.into_owned() } else { unescape(&body) })
|
||||
})
|
||||
}
|
||||
|
||||
/// The value of a whitespace-free `path=<literal>` argument, for an ordinary or a raw string literal.
|
||||
fn path_arg(arg: &str) -> Option<&str> {
|
||||
whole_literal(arg.strip_prefix("path=")?).map(|l| &l[1..l.len() - 1])
|
||||
/// The escapes of an ordinary literal are decoded, as in [`path_attr`].
|
||||
fn path_arg(arg: &str) -> Option<String> {
|
||||
let lit = arg.strip_prefix("path=")?;
|
||||
let body = whole_literal(lit).map(|l| &l[1..l.len() - 1])?;
|
||||
Some(if lit.starts_with('"') { unescape(body) } else { body.to_owned() })
|
||||
}
|
||||
|
||||
/// The `#[cfg_attr(<condition>, path = "…")]` overrides in `m[from..to]`, each as its condition and
|
||||
@@ -4867,7 +4873,7 @@ fn cfg_attr_paths(m: &[u8], src: &[u8], from: usize, to: usize) -> Vec<(String,
|
||||
for cut in cuts[1..].iter().copied().chain([text.len()]) {
|
||||
let arg = text.get(start..cut).unwrap_or("");
|
||||
if let Some(rel) = path_arg(arg) {
|
||||
out.push((text[..first].to_owned(), rel.to_owned()));
|
||||
out.push((text[..first].to_owned(), rel));
|
||||
}
|
||||
start = cut + 1;
|
||||
}
|
||||
@@ -7851,6 +7857,10 @@ mod tests {
|
||||
assert_eq!(cfg_attr_paths(&mask(src), src.as_bytes(), 0, src.len()), [("unix".to_owned(), "a,b.rs".to_owned())]);
|
||||
let raw = "#[cfg_attr(unix, path = r#\"a.rs\"#)]\nmod x;\n";
|
||||
assert_eq!(cfg_attr_paths(&mask(raw), raw.as_bytes(), 0, raw.len()), [("unix".to_owned(), "a.rs".to_owned())]);
|
||||
let escaped = "#[cfg_attr(unix, path = \"\\x61.rs\")]\nmod x;\n";
|
||||
assert_eq!(cfg_attr_paths(&mask(escaped), escaped.as_bytes(), 0, escaped.len()), [("unix".to_owned(), "a.rs".to_owned())]);
|
||||
let raw_escape = "#[cfg_attr(unix, path = r\"\\x61.rs\")]\nmod x;\n";
|
||||
assert_eq!(cfg_attr_paths(&mask(raw_escape), raw_escape.as_bytes(), 0, raw_escape.len()), [("unix".to_owned(), "\\x61.rs".to_owned())]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -7873,6 +7883,27 @@ mod tests {
|
||||
assert_eq!(hits_in(&shared, test, &ctx), [], "the raw-string route runs it on Linux");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn path_attributes_decode_escapes_in_ordinary_strings() {
|
||||
let escaped = "#[path = \"\\x63hild.rs\"]\nmod c;\n";
|
||||
assert_eq!(path_attr(&mask(escaped), escaped.as_bytes(), 0, escaped.len()).as_deref(), Some("child.rs"));
|
||||
let raw = "#[path = r\"\\x63hild.rs\"]\nmod c;\n";
|
||||
assert_eq!(path_attr(&mask(raw), raw.as_bytes(), 0, raw.len()).as_deref(), Some("\\x63hild.rs"), "a raw string keeps its backslash");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_escaped_plain_path_after_a_cfg_attr_path_is_the_fallback_route() {
|
||||
// Linux loads `child.rs` through the plain attribute, the fallback of the `cfg_attr` override.
|
||||
let files: HashMap<&str, &str> =
|
||||
[("crates/x/src/lib.rs", "#[cfg_attr(windows, path = \"child.rs\")]\n#[path = \"\\x63hild.rs\"]\nmod c;\n")].into_iter().collect();
|
||||
let read = |p: &str| files.get(p).map(|s| (*s).to_owned());
|
||||
let overrides = path_overrides(files.iter().map(|(p, s)| (*p, *s)));
|
||||
let child = "crates/x/src/child.rs".to_owned();
|
||||
let ctx = Ctx { module_cfgs: module_cfgs(std::slice::from_ref(&child), &read, &overrides), ..ctx() };
|
||||
let test = "#[test]\nfn t() {\n assert_eq!(f(), 1);\n}\n";
|
||||
assert_eq!(hits_in(&child, test, &ctx), [], "the plain attribute's route runs it on Linux");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_module_loaded_by_two_roots_runs_if_either_runs_it() {
|
||||
let files: HashMap<&str, &str> = [
|
||||
|
||||
Reference in new issue
Block a user