about summary refs log tree commit diff
path: root/clippy_dev/src/stderr_length_check.rs
blob: 041ee6911377780697c5d5d065459df0595583c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};

use walkdir::WalkDir;

// The maximum length allowed for stderr files.
//
// We limit this because small files are easier to deal with than bigger files.
const LENGTH_LIMIT: usize = 200;

pub fn check() {
    let exceeding_files: Vec<_> = exceeding_stderr_files();

    if !exceeding_files.is_empty() {
        eprintln!("Error: stderr files exceeding limit of {} lines:", LENGTH_LIMIT);
        for path in exceeding_files {
            println!("{}", path.display());
        }
        std::process::exit(1);
    }
}

fn exceeding_stderr_files() -> Vec<PathBuf> {
    // We use `WalkDir` instead of `fs::read_dir` here in order to recurse into subdirectories.
    WalkDir::new("../tests/ui")
        .into_iter()
        .filter_map(Result::ok)
        .filter_map(|e| {
            let p = e.into_path();
            if p.extension() == Some(OsStr::new("stderr")) && count_linenumbers(&p) > LENGTH_LIMIT {
                Some(p)
            } else {
                None
            }
        })
        .collect()
}

#[must_use]
fn count_linenumbers(filepath: &Path) -> usize {
    match fs::read(filepath) {
        Ok(content) => bytecount::count(&content, b'\n'),
        Err(e) => {
            eprintln!("Failed to read file: {}", e);
            0
        },
    }
}