use std::collections::{BTreeSet, HashMap};
use std::ffi::OsStr;
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use ignore::Walk;
const ENTRY_LIMIT: u32 = 901;
const ISSUES_ENTRY_LIMIT: u32 = 1673;
const EXPECTED_TEST_FILE_EXTENSIONS: &[&str] = &[
"rs", "stderr", "svg", "stdout", "fixed", "md", "ftl", ];
const EXTENSION_EXCEPTION_PATHS: &[&str] = &[
"tests/ui/asm/named-asm-labels.s", "tests/ui/codegen/mismatched-data-layout.json", "tests/ui/check-cfg/my-awesome-platform.json", "tests/ui/argfile/commandline-argfile-badutf8.args", "tests/ui/argfile/commandline-argfile.args", "tests/ui/crate-loading/auxiliary/libfoo.rlib", "tests/ui/include-macros/data.bin", "tests/ui/include-macros/file.txt", "tests/ui/macros/macro-expanded-include/file.txt", "tests/ui/macros/not-utf8.bin", "tests/ui/macros/syntax-extension-source-utils-files/includeme.fragment", "tests/ui/proc-macro/auxiliary/included-file.txt", "tests/ui/unpretty/auxiliary/data.txt", "tests/ui/invalid/foo.natvis.xml", "tests/ui/sanitizer/dataflow-abilist.txt", "tests/ui/shell-argfiles/shell-argfiles.args", "tests/ui/shell-argfiles/shell-argfiles-badquotes.args", "tests/ui/shell-argfiles/shell-argfiles-via-argfile-shell.args", "tests/ui/shell-argfiles/shell-argfiles-via-argfile.args", "tests/ui/std/windows-bat-args1.bat", "tests/ui/std/windows-bat-args2.bat", "tests/ui/std/windows-bat-args3.bat", ];
fn check_entries(tests_path: &Path, bad: &mut bool) {
let mut directories: HashMap<PathBuf, u32> = HashMap::new();
for dir in Walk::new(&tests_path.join("ui")) {
if let Ok(entry) = dir {
let parent = entry.path().parent().unwrap().to_path_buf();
*directories.entry(parent).or_default() += 1;
}
}
let (mut max, mut max_issues) = (0, 0);
for (dir_path, count) in directories {
let is_issues_dir = tests_path.join("ui/issues") == dir_path;
let (limit, maxcnt) = if is_issues_dir {
(ISSUES_ENTRY_LIMIT, &mut max_issues)
} else {
(ENTRY_LIMIT, &mut max)
};
*maxcnt = (*maxcnt).max(count);
if count > limit {
tidy_error!(
bad,
"following path contains more than {} entries, \
you should move the test to some relevant subdirectory (current: {}): {}",
limit,
count,
dir_path.display()
);
}
}
if ISSUES_ENTRY_LIMIT > max_issues {
tidy_error!(
bad,
"`ISSUES_ENTRY_LIMIT` is too high (is {ISSUES_ENTRY_LIMIT}, should be {max_issues})"
);
}
}
pub fn check(root_path: &Path, bless: bool, bad: &mut bool) {
let issues_txt_header = r#"============================================================
⚠️⚠️⚠️NOTHING SHOULD EVER BE ADDED TO THIS LIST⚠️⚠️⚠️
============================================================
"#;
let path = &root_path.join("tests");
check_entries(&path, bad);
let mut prev_line = "";
let mut is_sorted = true;
let allowed_issue_names: BTreeSet<_> = include_str!("issues.txt")
.strip_prefix(issues_txt_header)
.unwrap()
.lines()
.map(|line| {
if prev_line > line {
is_sorted = false;
}
prev_line = line;
line
})
.collect();
if !is_sorted && !bless {
tidy_error!(
bad,
"`src/tools/tidy/src/issues.txt` is not in order, mostly because you modified it manually,
please only update it with command `x test tidy --bless`"
);
}
let mut remaining_issue_names: BTreeSet<&str> = allowed_issue_names.clone();
let (ui, ui_fulldeps) = (path.join("ui"), path.join("ui-fulldeps"));
let paths = [ui.as_path(), ui_fulldeps.as_path()];
crate::walk::walk_no_read(&paths, |_, _| false, &mut |entry| {
let file_path = entry.path();
if let Some(ext) = file_path.extension().and_then(OsStr::to_str) {
if !(EXPECTED_TEST_FILE_EXTENSIONS.contains(&ext)
|| EXTENSION_EXCEPTION_PATHS.iter().any(|path| file_path.ends_with(path)))
{
tidy_error!(bad, "file {} has unexpected extension {}", file_path.display(), ext);
}
let testname =
file_path.file_name().unwrap().to_str().unwrap().split_once('.').unwrap().0;
if ext == "stderr" || ext == "stdout" || ext == "fixed" {
if !file_path.with_file_name(testname).with_extension("rs").exists()
&& !testname.contains("ignore-tidy")
{
tidy_error!(bad, "Stray file with UI testing output: {:?}", file_path);
}
if let Ok(metadata) = fs::metadata(file_path) {
if metadata.len() == 0 {
tidy_error!(bad, "Empty file with UI testing output: {:?}", file_path);
}
}
}
if ext == "rs" {
if let Some(test_name) = static_regex!(r"^issues?[-_]?(\d{3,})").captures(testname)
{
let stripped_path = file_path
.strip_prefix(path)
.unwrap()
.to_str()
.unwrap()
.replace(std::path::MAIN_SEPARATOR_STR, "/");
if !remaining_issue_names.remove(stripped_path.as_str()) {
tidy_error!(
bad,
"file `tests/{stripped_path}` must begin with a descriptive name, consider `{{reason}}-issue-{issue_n}.rs`",
issue_n = &test_name[1],
);
}
}
}
}
});
if bless && (!remaining_issue_names.is_empty() || !is_sorted) {
let tidy_src = root_path.join("src/tools/tidy/src");
let blessed_issues_path = tidy_src.join("issues_blessed.txt");
let mut blessed_issues_txt = fs::File::create(&blessed_issues_path).unwrap();
blessed_issues_txt.write(issues_txt_header.as_bytes()).unwrap();
for filename in allowed_issue_names.difference(&remaining_issue_names) {
writeln!(blessed_issues_txt, "{filename}").unwrap();
}
let old_issues_path = tidy_src.join("issues.txt");
fs::rename(blessed_issues_path, old_issues_path).unwrap();
} else {
for file_name in remaining_issue_names {
let mut p = PathBuf::from(path);
p.push(file_name);
tidy_error!(
bad,
"file `{}` no longer exists and should be removed from the exclusions in `src/tools/tidy/src/issues.txt`",
p.display()
);
}
}
}