Skip to content

Commit f4de904

Browse files
committed
Auto merge of #4100 - phansch:add_stderr_length_check, r=flip1995
Add a stderr file length check to clippy_dev This adds a check to `clippy_dev` that enforces a maximum line count for `stderr` files. CI will fail if the line count is exceeded. It's currently set to `320` lines. Ideally this would be implemented in `compiletest-rs` but there are plans to move Rust's `compiletest` into the `compiletest-rs` repository and I don't want to do the work in `compiletest` twice. However, I also don't want to wait until the move is done, so I added the check to `clippy_dev` until it makes sense to add it to compiletest-rs. cc #2038
2 parents 0a59a78 + 619a290 commit f4de904

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

ci/base-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export CARGO_TARGET_DIR=`pwd`/target/
2323

2424
# Perform various checks for lint registration
2525
./util/dev update_lints --check
26+
./util/dev --limit-stderr-length
2627
cargo +nightly fmt --all -- --check
2728

2829
# Check running clippy-driver without cargo

clippy_dev/src/main.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ extern crate clap;
22
extern crate clippy_dev;
33
extern crate regex;
44

5-
use clap::{App, AppSettings, Arg, SubCommand};
5+
use clap::{App, Arg, SubCommand};
66
use clippy_dev::*;
7+
mod stderr_length_check;
78

89
#[derive(PartialEq)]
910
enum UpdateMode {
@@ -13,7 +14,6 @@ enum UpdateMode {
1314

1415
fn main() {
1516
let matches = App::new("Clippy developer tooling")
16-
.setting(AppSettings::SubcommandRequiredElseHelp)
1717
.subcommand(
1818
SubCommand::with_name("update_lints")
1919
.about("Updates lint registration and information from the source code")
@@ -36,8 +36,16 @@ fn main() {
3636
.help("Checks that util/dev update_lints has been run. Used on CI."),
3737
),
3838
)
39+
.arg(
40+
Arg::with_name("limit-stderr-length")
41+
.long("limit-stderr-length")
42+
.help("Ensures that stderr files do not grow longer than a certain amount of lines."),
43+
)
3944
.get_matches();
4045

46+
if matches.is_present("limit-stderr-length") {
47+
stderr_length_check::check();
48+
}
4149
if let Some(matches) = matches.subcommand_matches("update_lints") {
4250
if matches.is_present("print-only") {
4351
print_lints();

clippy_dev/src/stderr_length_check.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use std::ffi::OsStr;
2+
use walkdir::WalkDir;
3+
4+
use std::fs::File;
5+
use std::io::prelude::*;
6+
7+
// The maximum length allowed for stderr files.
8+
//
9+
// We limit this because small files are easier to deal with than bigger files.
10+
const LIMIT: usize = 320;
11+
12+
pub fn check() {
13+
let stderr_files = stderr_files();
14+
let exceeding_files = exceeding_stderr_files(stderr_files).collect::<Vec<String>>();
15+
16+
if !exceeding_files.is_empty() {
17+
eprintln!("Error: stderr files exceeding limit of {} lines:", LIMIT);
18+
for path in exceeding_files {
19+
println!("{}", path);
20+
}
21+
std::process::exit(1);
22+
}
23+
}
24+
25+
fn exceeding_stderr_files(files: impl Iterator<Item = walkdir::DirEntry>) -> impl Iterator<Item = String> {
26+
files
27+
.filter_map(|file| {
28+
let path = file.path().to_str().expect("Could not convert path to str").to_string();
29+
let linecount = count_linenumbers(&path);
30+
if linecount > LIMIT {
31+
Some(path)
32+
} else {
33+
None
34+
}
35+
})
36+
}
37+
38+
fn stderr_files() -> impl Iterator<Item = walkdir::DirEntry> {
39+
// We use `WalkDir` instead of `fs::read_dir` here in order to recurse into subdirectories.
40+
WalkDir::new("../tests/ui")
41+
.into_iter()
42+
.filter_map(std::result::Result::ok)
43+
.filter(|f| f.path().extension() == Some(OsStr::new("stderr")))
44+
}
45+
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
53+
}
54+
}

0 commit comments

Comments
 (0)