summaryrefslogtreecommitdiffstats
path: root/vendor/sysinfo-0.26.7/tests/code_checkers/utils.rs
blob: 893201961ba54260e117c163b9f3d4221966b142 (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
// Take a look at the license at the top of the repository in the LICENSE file.

use std::fs::{self, File};
use std::io::Read;
use std::path::Path;

pub struct TestResult {
    pub nb_tests: usize,
    pub nb_errors: usize,
}

impl std::ops::AddAssign for TestResult {
    fn add_assign(&mut self, other: Self) {
        self.nb_tests += other.nb_tests;
        self.nb_errors += other.nb_errors;
    }
}

pub fn read_dirs<P: AsRef<Path>, F: FnMut(&Path, &str)>(dirs: &[P], callback: &mut F) {
    for dir in dirs {
        read_dir(dir, callback);
    }
}

fn read_dir<P: AsRef<Path>, F: FnMut(&Path, &str)>(dir: P, callback: &mut F) {
    for entry in fs::read_dir(dir).expect("read_dir failed") {
        let entry = entry.expect("entry failed");
        let path = entry.path();
        if path.is_dir() {
            read_dir(path, callback);
        } else {
            let content = read_file(&path);
            callback(&path, &content);
        }
    }
}

fn read_file<P: AsRef<Path>>(p: P) -> String {
    let mut f = File::open(p).expect("read_file::open failed");
    let mut content =
        String::with_capacity(f.metadata().map(|m| m.len() as usize + 1).unwrap_or(0));
    f.read_to_string(&mut content)
        .expect("read_file::read_to_end failed");
    content
}

pub fn show_error(p: &Path, err: &str) {
    eprintln!("=> [{}]: {err}", p.display());
}