use crate::common::*;
#[test]
fn no_message_literal_in_the_workspace_carries_a_run_of_collapsed_indentation() {
let defective = "fn f() { err(\"packing paths needs a container (`-o bundle.tar`, or --format tar)\"); }";
let found = rust_string_literals(defective);
assert_eq!(
found.len(),
1,
"the scanner must find the literal: {found:?}"
);
assert!(
first_space_run(&found[0].1).is_some_and(|at| !is_column_label(&found[0].1[..at])),
"the scanner must flag the historical defect: {:?}",
found[0].1
);
let continued = "fn f() { err(\"packing paths needs a container \\\n (`-o bundle.tar`, or --format tar)\"); }";
let found = rust_string_literals(continued);
assert_eq!(found.len(), 1);
assert_eq!(
found[0].1, "packing paths needs a container (`-o bundle.tar`, or --format tar)",
"a `\\`-continued literal carries no indentation, and the scanner must apply that \
rule rather than report the source text"
);
assert!(first_space_run(&found[0].1).is_none());
let aligned = "fn f() { println!(\"format: {}\", x); }";
let found = rust_string_literals(aligned);
let at = first_space_run(&found[0].1).expect("the column really is three spaces");
assert!(
is_column_label(&found[0].1[..at]),
"a deliberate column label must not be reported as a collapse"
);
let colon_prose = "fn f() { err(\"stuffr cannot do this: try force\"); }";
let found = rust_string_literals(colon_prose);
assert_eq!(found.len(), 1);
let at = first_space_run(&found[0].1).expect("the run is there");
assert!(
!is_column_label(&found[0].1[..at]),
"a prose clause ending in a colon is not a column label: {:?}",
found[0].1
);
assert!(is_column_label("MemAvailable:"));
assert!(!is_column_label("two words:"));
let quoted_char = "fn f() { let q = '\"'; err(\"a collapse\"); }";
let found = rust_string_literals(quoted_char);
assert_eq!(
found.iter().map(|(_, t)| t.as_str()).collect::<Vec<_>>(),
vec!["a collapse"],
"a `\"` inside a char literal must not desynchronise the scan"
);
use std::path::Path;
assert!(is_verbatim_reference_messages(Path::new(
"crates/stuffr-cli/src/compat/bzip2/messages.rs"
)));
assert!(!is_verbatim_reference_messages(Path::new(
"crates/stuffr-cli/src/compat/bzip2/run.rs"
)));
assert!(!is_verbatim_reference_messages(Path::new(
"crates/stuffr-cli/src/messages.rs"
)));
assert!(!is_verbatim_reference_messages(Path::new(
"crates/stuffr-cli/src/compat/messages.rs"
)));
assert!(!is_verbatim_reference_messages(Path::new(
"crates/stuffr-cli/src/compat/bzip2/xmessages.rs"
)));
assert!(is_verbatim_reference_messages(Path::new(
"crates/stuffr-cli/src/compat/gzip/gnu/messages.rs"
)));
assert!(is_verbatim_reference_messages(Path::new(
"crates/stuffr-cli/src/compat/gzip/bsd/messages.rs"
)));
assert!(!is_verbatim_reference_messages(Path::new(
"crates/stuffr-cli/src/compat/gzip/bsd/run.rs"
)));
assert!(!is_verbatim_reference_messages(Path::new(
"crates/stuffr-cli/src/compat/gzip/gnu/run.rs"
)));
assert!(!is_verbatim_reference_messages(Path::new(
"crates/stuffr-cli/src/compat/gzip/gnu/x/messages.rs"
)));
assert!(is_verbatim_reference_messages(Path::new(
"crates/stuffr-cli/src/compat/compress/apple/messages.rs"
)));
assert!(!is_verbatim_reference_messages(Path::new(
"crates/stuffr-cli/src/compat/compress/apple/run.rs"
)));
assert!(is_verbatim_reference_messages(Path::new(
"crates/stuffr-cli/src/compat/compress/ncompress/messages.rs"
)));
assert!(!is_verbatim_reference_messages(Path::new(
"crates/stuffr-cli/src/compat/compress/ncompress/run.rs"
)));
let files = workspace_source_files();
assert!(
files.len() > 20,
"the walk found only {} source files, which is not this workspace: {files:?}",
files.len()
);
assert!(
files.iter().any(|p| p.ends_with("stuffr-cli/src/main.rs")),
"the walk must reach the crate every user-facing message is printed from"
);
let mut scanned = 0usize;
let mut offenders = Vec::new();
let mut exempted = 0usize;
for file in &files {
if is_verbatim_reference_messages(file) {
exempted += 1;
continue;
}
let src = std::fs::read_to_string(file).unwrap();
for (line, text) in rust_string_literals(&src) {
scanned += 1;
let Some(at) = first_space_run(&text) else {
continue;
};
if is_column_label(&text[..at]) {
continue;
}
offenders.push(format!("{}:{line}: {text:?}", file.display()));
}
}
assert!(
scanned > 1000,
"only {scanned} literals scanned, which is not this workspace"
);
assert!(
exempted >= 1,
"the bzip2 message table was not found, so the exemption matched nothing"
);
assert!(
offenders.is_empty(),
"a string literal carries a run of three or more spaces, which reaches the user as \
collapsed continuation indentation mid-sentence. Re-join the sentence and end each \
continued line with `\\` (which strips the newline AND the next line's indentation); \
a deliberate column is written `label: value`. Offenders:\n{}",
offenders.join("\n")
);
}