tidy/
mir_opt_tests.rs

1//! Tidy check to ensure that mir opt directories do not have stale files or dashes in file names
2
3use std::collections::HashSet;
4use std::path::{Path, PathBuf};
5
6use miropt_test_tools::PanicStrategy;
7
8use crate::walk::walk_no_read;
9
10fn check_unused_files(path: &Path, bless: bool, bad: &mut bool) {
11    let mut rs_files = Vec::<PathBuf>::new();
12    let mut output_files = HashSet::<PathBuf>::new();
13
14    walk_no_read(
15        &[&path.join("mir-opt")],
16        |path, _is_dir| path.file_name() == Some("README.md".as_ref()),
17        &mut |file| {
18            let filepath = file.path();
19            if filepath.extension() == Some("rs".as_ref()) {
20                rs_files.push(filepath.to_owned());
21            } else {
22                output_files.insert(filepath.to_owned());
23            }
24        },
25    );
26
27    for file in rs_files {
28        for bw in [32, 64] {
29            for ps in [PanicStrategy::Unwind, PanicStrategy::Abort] {
30                let mir_opt_test = miropt_test_tools::files_for_miropt_test(&file, bw, ps);
31                for output_file in mir_opt_test.files {
32                    output_files.remove(&output_file.expected_file);
33                }
34            }
35        }
36    }
37
38    for extra in output_files {
39        if !bless {
40            tidy_error!(
41                bad,
42                "the following output file is not associated with any mir-opt test, you can remove it: {}",
43                extra.display()
44            );
45        } else {
46            let _ = std::fs::remove_file(extra);
47        }
48    }
49}
50
51fn check_dash_files(path: &Path, bless: bool, bad: &mut bool) {
52    for file in walkdir::WalkDir::new(&path.join("mir-opt"))
53        .into_iter()
54        .filter_map(Result::ok)
55        .filter(|e| e.file_type().is_file())
56    {
57        let path = file.path();
58        if path.extension() == Some("rs".as_ref()) {
59            if let Some(name) = path.file_name().and_then(|s| s.to_str()) {
60                if name.contains('-') {
61                    if !bless {
62                        tidy_error!(
63                            bad,
64                            "mir-opt test files should not have dashes in them: {}",
65                            path.display()
66                        );
67                    } else {
68                        let new_name = name.replace('-', "_");
69                        let mut new_path = path.to_owned();
70                        new_path.set_file_name(new_name);
71                        let _ = std::fs::rename(path, new_path);
72                    }
73                }
74            }
75        }
76    }
77}
78
79pub fn check(path: &Path, bless: bool, bad: &mut bool) {
80    check_unused_files(path, bless, bad);
81    check_dash_files(path, bless, bad);
82}