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