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::diagnostics::{CheckId, DiagCtx, RunningCheck};
9use crate::walk::walk_no_read;
10
11fn check_unused_files(path: &Path, bless: bool, check: &mut RunningCheck) {
12    let mut rs_files = Vec::<PathBuf>::new();
13    let mut output_files = HashSet::<PathBuf>::new();
14
15    walk_no_read(
16        &[&path.join("mir-opt")],
17        |path, _is_dir| path.file_name() == Some("README.md".as_ref()),
18        &mut |file| {
19            let filepath = file.path();
20            if filepath.extension() == Some("rs".as_ref()) {
21                rs_files.push(filepath.to_owned());
22            } else {
23                output_files.insert(filepath.to_owned());
24            }
25        },
26    );
27
28    for file in rs_files {
29        for bw in [32, 64] {
30            for ps in [PanicStrategy::Unwind, PanicStrategy::Abort] {
31                let mir_opt_test = miropt_test_tools::files_for_miropt_test(&file, bw, ps);
32                for output_file in mir_opt_test.files {
33                    output_files.remove(&output_file.expected_file);
34                }
35            }
36        }
37    }
38
39    for extra in output_files {
40        if !bless {
41            check.error(format!(
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, check: &mut RunningCheck) {
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            && let Some(name) = path.file_name().and_then(|s| s.to_str())
60            && name.contains('-')
61        {
62            if !bless {
63                check.error(format!(
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
77pub fn check(path: &Path, bless: bool, diag_ctx: DiagCtx) {
78    let mut check = diag_ctx.start_check(CheckId::new("mir_opt_tests").path(path));
79
80    check_unused_files(path, bless, &mut check);
81    check_dash_files(path, bless, &mut check);
82}