|
| 1 | +//! Checks that there are no unpaired `.stderr` or `.stdout` for a test with and without revisions. |
| 2 | +
|
| 3 | +use std::collections::{BTreeMap, BTreeSet}; |
| 4 | +use std::ffi::OsStr; |
| 5 | +use std::path::Path; |
| 6 | + |
| 7 | +use crate::iter_header::*; |
| 8 | +use crate::walk::*; |
| 9 | + |
| 10 | +// Should be kept in sync with `CompareMode` in `src/tools/compiletest/src/common.rs`, |
| 11 | +// as well as `run`. |
| 12 | +const IGNORES: &[&str] = &[ |
| 13 | + "polonius", |
| 14 | + "chalk", |
| 15 | + "split-dwarf", |
| 16 | + "split-dwarf-single", |
| 17 | + "next-solver-coherence", |
| 18 | + "next-solver", |
| 19 | + "run", |
| 20 | +]; |
| 21 | +const EXTENSIONS: &[&str] = &["stdout", "stderr"]; |
| 22 | +const SPECIAL_TEST: &str = "tests/ui/command/need-crate-arg-ignore-tidy.x.rs"; |
| 23 | + |
| 24 | +pub fn check(tests_path: impl AsRef<Path>, bad: &mut bool) { |
| 25 | + // Recurse over subdirectories under `tests/` |
| 26 | + walk_dir(tests_path.as_ref(), filter, &mut |entry| { |
| 27 | + // We are inspecting a folder. Collect the paths to interesting files `.rs`, `.stderr`, |
| 28 | + // `.stdout` under the current folder (shallow). |
| 29 | + let mut files_under_inspection = BTreeSet::new(); |
| 30 | + for sibling in std::fs::read_dir(entry.path()).unwrap() { |
| 31 | + let Ok(sibling) = sibling else { |
| 32 | + continue; |
| 33 | + }; |
| 34 | + |
| 35 | + if sibling.path().is_dir() { |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + let sibling_path = sibling.path(); |
| 40 | + |
| 41 | + let Some(ext) = sibling_path.extension().map(OsStr::to_str).flatten() else { |
| 42 | + continue; |
| 43 | + }; |
| 44 | + |
| 45 | + if ext == "rs" || EXTENSIONS.contains(&ext) { |
| 46 | + files_under_inspection.insert(sibling_path); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + let mut test_info = BTreeMap::new(); |
| 51 | + |
| 52 | + for test in |
| 53 | + files_under_inspection.iter().filter(|f| f.extension().is_some_and(|ext| ext == "rs")) |
| 54 | + { |
| 55 | + if test.ends_with(SPECIAL_TEST) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + let mut expected_revisions = BTreeSet::new(); |
| 60 | + |
| 61 | + let contents = std::fs::read_to_string(test).unwrap(); |
| 62 | + |
| 63 | + // Collect directives. |
| 64 | + iter_header(&contents, &mut |HeaderLine { revision, directive }| { |
| 65 | + // We're trying to *find* `//@ revision: xxx` directives themselves, not revisioned |
| 66 | + // directives. |
| 67 | + if revision.is_some() { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + let directive = directive.trim(); |
| 72 | + |
| 73 | + if directive.starts_with("revisions") { |
| 74 | + let Some((name, value)) = directive.split_once([':', ' ']) else { |
| 75 | + return; |
| 76 | + }; |
| 77 | + |
| 78 | + if name == "revisions" { |
| 79 | + let revs = value.split(' '); |
| 80 | + for rev in revs { |
| 81 | + expected_revisions.insert(rev.to_owned()); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + let Some((test_name, _)) = test.to_str().map(|s| s.split_once('.')).flatten() else { |
| 88 | + continue; |
| 89 | + }; |
| 90 | + |
| 91 | + test_info.insert(test_name.to_string(), (test, expected_revisions)); |
| 92 | + } |
| 93 | + |
| 94 | + // Our test file `foo.rs` has specified no revisions. There should not be any |
| 95 | + // `foo.rev{.stderr,.stdout}` files. rustc-dev-guide says test output files can have names |
| 96 | + // of the form: `test-name.revision.compare_mode.extension`, but our only concern is |
| 97 | + // `test-name.revision` and `extension`. |
| 98 | + for sibling in files_under_inspection.iter().filter(|f| { |
| 99 | + f.extension().map(OsStr::to_str).flatten().is_some_and(|ext| EXTENSIONS.contains(&ext)) |
| 100 | + }) { |
| 101 | + let filename_components = sibling.to_str().unwrap().split('.').collect::<Vec<_>>(); |
| 102 | + let file_prefix = filename_components[0]; |
| 103 | + |
| 104 | + let Some((test_path, expected_revisions)) = test_info.get(file_prefix) else { |
| 105 | + continue; |
| 106 | + }; |
| 107 | + |
| 108 | + match filename_components[..] { |
| 109 | + // Cannot have a revision component, skip. |
| 110 | + [] | [_] => return, |
| 111 | + [_, _] if !expected_revisions.is_empty() => { |
| 112 | + // Found unrevisioned output files for a revisioned test. |
| 113 | + tidy_error!( |
| 114 | + bad, |
| 115 | + "found unrevisioned output file `{}` for a revisioned test `{}`", |
| 116 | + sibling.display(), |
| 117 | + test_path.display(), |
| 118 | + ); |
| 119 | + } |
| 120 | + [_, _] => return, |
| 121 | + [_, found_revision, .., extension] => { |
| 122 | + if !IGNORES.contains(&found_revision) |
| 123 | + && !expected_revisions.contains(found_revision) |
| 124 | + // This is from `//@ stderr-per-bitwidth` |
| 125 | + && !(extension == "stderr" && ["32bit", "64bit"].contains(&found_revision)) |
| 126 | + { |
| 127 | + // Found some unexpected revision-esque component that is not a known |
| 128 | + // compare-mode or expected revision. |
| 129 | + tidy_error!( |
| 130 | + bad, |
| 131 | + "found output file `{}` for unexpected revision `{}` of test `{}`", |
| 132 | + sibling.display(), |
| 133 | + found_revision, |
| 134 | + test_path.display() |
| 135 | + ); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + }); |
| 141 | +} |
| 142 | + |
| 143 | +fn filter(path: &Path) -> bool { |
| 144 | + filter_dirs(path) // ignore certain dirs |
| 145 | + || (path.file_name().is_some_and(|name| name == "auxiliary")) // ignore auxiliary folder |
| 146 | +} |
0 commit comments