]> git.proxmox.com Git - rustc.git/blob - vendor/sysinfo/tests/code_checkers/utils.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / vendor / sysinfo / tests / code_checkers / utils.rs
1 // Take a look at the license at the top of the repository in the LICENSE file.
2
3 use std::fs::{self, File};
4 use std::io::Read;
5 use std::path::Path;
6
7 pub struct TestResult {
8 pub nb_tests: usize,
9 pub nb_errors: usize,
10 }
11
12 impl std::ops::AddAssign for TestResult {
13 fn add_assign(&mut self, other: Self) {
14 self.nb_tests += other.nb_tests;
15 self.nb_errors += other.nb_errors;
16 }
17 }
18
19 pub fn read_dirs<P: AsRef<Path>, F: FnMut(&Path, &str)>(dirs: &[P], callback: &mut F) {
20 for dir in dirs {
21 read_dir(dir, callback);
22 }
23 }
24
25 fn read_dir<P: AsRef<Path>, F: FnMut(&Path, &str)>(dir: P, callback: &mut F) {
26 for entry in fs::read_dir(dir).expect("read_dir failed") {
27 let entry = entry.expect("entry failed");
28 let path = entry.path();
29 if path.is_dir() {
30 read_dir(path, callback);
31 } else {
32 let content = read_file(&path);
33 callback(&path, &content);
34 }
35 }
36 }
37
38 fn read_file<P: AsRef<Path>>(p: P) -> String {
39 let mut f = File::open(p).expect("read_file::open failed");
40 let mut content =
41 String::with_capacity(f.metadata().map(|m| m.len() as usize + 1).unwrap_or(0));
42 f.read_to_string(&mut content)
43 .expect("read_file::read_to_end failed");
44 content
45 }
46
47 pub fn show_error(p: &Path, err: &str) {
48 eprintln!("=> [{}]: {}", p.display(), err);
49 }