|
1 | 1 | use std::ffi::OsStr;
|
2 |
| -use walkdir::WalkDir; |
| 2 | +use std::fs; |
| 3 | +use std::path::{Path, PathBuf}; |
3 | 4 |
|
4 |
| -use std::fs::File; |
5 |
| -use std::io::prelude::*; |
| 5 | +use walkdir::WalkDir; |
6 | 6 |
|
7 | 7 | // The maximum length allowed for stderr files.
|
8 | 8 | //
|
9 | 9 | // We limit this because small files are easier to deal with than bigger files.
|
10 |
| -const LIMIT: usize = 200; |
| 10 | +const LENGTH_LIMIT: usize = 200; |
11 | 11 |
|
12 | 12 | pub fn check() {
|
13 |
| - let stderr_files = stderr_files(); |
14 |
| - let exceeding_files = exceeding_stderr_files(stderr_files).collect::<Vec<String>>(); |
| 13 | + let exceeding_files: Vec<_> = exceeding_stderr_files(); |
15 | 14 |
|
16 | 15 | if !exceeding_files.is_empty() {
|
17 |
| - eprintln!("Error: stderr files exceeding limit of {} lines:", LIMIT); |
| 16 | + eprintln!("Error: stderr files exceeding limit of {} lines:", LENGTH_LIMIT); |
18 | 17 | for path in exceeding_files {
|
19 |
| - println!("{}", path); |
| 18 | + println!("{}", path.display()); |
20 | 19 | }
|
21 | 20 | std::process::exit(1);
|
22 | 21 | }
|
23 | 22 | }
|
24 | 23 |
|
25 |
| -fn exceeding_stderr_files(files: impl Iterator<Item = walkdir::DirEntry>) -> impl Iterator<Item = String> { |
26 |
| - files.filter_map(|file| { |
27 |
| - let path = file.path().to_str().expect("Could not convert path to str").to_string(); |
28 |
| - let linecount = count_linenumbers(&path); |
29 |
| - if linecount > LIMIT { |
30 |
| - Some(path) |
31 |
| - } else { |
32 |
| - None |
33 |
| - } |
34 |
| - }) |
35 |
| -} |
36 |
| - |
37 |
| -fn stderr_files() -> impl Iterator<Item = walkdir::DirEntry> { |
| 24 | +fn exceeding_stderr_files() -> Vec<PathBuf> { |
38 | 25 | // We use `WalkDir` instead of `fs::read_dir` here in order to recurse into subdirectories.
|
39 | 26 | WalkDir::new("../tests/ui")
|
40 | 27 | .into_iter()
|
41 |
| - .filter_map(std::result::Result::ok) |
42 |
| - .filter(|f| f.path().extension() == Some(OsStr::new("stderr"))) |
| 28 | + .filter_map(Result::ok) |
| 29 | + .filter_map(|e| { |
| 30 | + let p = e.into_path(); |
| 31 | + if p.extension() == Some(OsStr::new("stderr")) && count_linenumbers(&p) > LENGTH_LIMIT { |
| 32 | + Some(p) |
| 33 | + } else { |
| 34 | + None |
| 35 | + } |
| 36 | + }) |
| 37 | + .collect() |
43 | 38 | }
|
44 | 39 |
|
45 | 40 | #[must_use]
|
46 |
| -fn count_linenumbers(filepath: &str) -> usize { |
47 |
| - if let Ok(mut file) = File::open(filepath) { |
48 |
| - let mut content = String::new(); |
49 |
| - file.read_to_string(&mut content).expect("Failed to read file?"); |
50 |
| - content.lines().count() |
51 |
| - } else { |
52 |
| - 0 |
| 41 | +fn count_linenumbers(filepath: &Path) -> usize { |
| 42 | + match fs::read(filepath) { |
| 43 | + Ok(content) => bytecount::count(&content, b'\n'), |
| 44 | + Err(e) => { |
| 45 | + eprintln!("Failed to read file: {}", e); |
| 46 | + 0 |
| 47 | + }, |
53 | 48 | }
|
54 | 49 | }
|
0 commit comments