]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/ui_tests.rs
ba20c77a7d16338fbbed672232e4c2c6bf122ef1
[rust.git] / src / tools / tidy / src / ui_tests.rs
1 //! Tidy check to ensure below in UI test directories:
2 //! - the number of entries in each directory must be less than `ENTRY_LIMIT`
3 //! - there are no stray `.stderr` files
4
5 use ignore::Walk;
6 use ignore::WalkBuilder;
7 use std::fs;
8 use std::path::Path;
9
10 const ENTRY_LIMIT: usize = 1000;
11 // FIXME: The following limits should be reduced eventually.
12 const ROOT_ENTRY_LIMIT: usize = 939;
13 const ISSUES_ENTRY_LIMIT: usize = 2000;
14
15 fn check_entries(path: &Path, bad: &mut bool) {
16     for dir in Walk::new(&path.join("ui")) {
17         if let Ok(entry) = dir {
18             if entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false) {
19                 let dir_path = entry.path();
20                 // Use special values for these dirs.
21                 let is_root = path.join("ui") == dir_path;
22                 let is_issues_dir = path.join("ui/issues") == dir_path;
23                 let limit = if is_root {
24                     ROOT_ENTRY_LIMIT
25                 } else if is_issues_dir {
26                     ISSUES_ENTRY_LIMIT
27                 } else {
28                     ENTRY_LIMIT
29                 };
30
31                 let count = WalkBuilder::new(&dir_path)
32                     .max_depth(Some(1))
33                     .build()
34                     .into_iter()
35                     .collect::<Vec<_>>()
36                     .len()
37                     - 1; // remove the dir itself
38
39                 if count > limit {
40                     tidy_error!(
41                         bad,
42                         "following path contains more than {} entries, \
43                             you should move the test to some relevant subdirectory (current: {}): {}",
44                         limit,
45                         count,
46                         dir_path.display()
47                     );
48                 }
49             }
50         }
51     }
52 }
53
54 pub fn check(path: &Path, bad: &mut bool) {
55     check_entries(&path, bad);
56     for path in &[&path.join("ui"), &path.join("ui-fulldeps")] {
57         crate::walk::walk_no_read(path, &mut |_| false, &mut |entry| {
58             let file_path = entry.path();
59             if let Some(ext) = file_path.extension() {
60                 if ext == "stderr" || ext == "stdout" {
61                     // Test output filenames have one of the formats:
62                     // ```
63                     // $testname.stderr
64                     // $testname.$mode.stderr
65                     // $testname.$revision.stderr
66                     // $testname.$revision.$mode.stderr
67                     // ```
68                     //
69                     // For now, just make sure that there is a corresponding
70                     // `$testname.rs` file.
71                     //
72                     // NB: We do not use file_stem() as some file names have multiple `.`s and we
73                     // must strip all of them.
74                     let testname =
75                         file_path.file_name().unwrap().to_str().unwrap().split_once('.').unwrap().0;
76                     if !file_path.with_file_name(testname).with_extension("rs").exists() {
77                         tidy_error!(bad, "Stray file with UI testing output: {:?}", file_path);
78                     }
79
80                     if let Ok(metadata) = fs::metadata(file_path) {
81                         if metadata.len() == 0 {
82                             tidy_error!(bad, "Empty file with UI testing output: {:?}", file_path);
83                         }
84                     }
85                 }
86             }
87         });
88     }
89 }