Skip to content

Commit

Permalink
Implement --bless for the issues lint
Browse files Browse the repository at this point in the history
  • Loading branch information
workingjubilee committed Feb 18, 2024
1 parent 12641b0 commit cb57c42
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/tools/tidy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn main() {
// Checks over tests.
check!(tests_placement, &root_path);
check!(debug_artifacts, &tests_path);
check!(ui_tests, &tests_path);
check!(ui_tests, &tests_path, bless);
check!(mir_opt_tests, &tests_path, bless);
check!(rustdoc_gui_tests, &tests_path);
check!(rustdoc_css_themes, &librustdoc_path);
Expand Down
39 changes: 32 additions & 7 deletions src/tools/tidy/src/ui_tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! Tidy check to ensure below in UI test directories:
//! - the number of entries in each directory must be less than `ENTRY_LIMIT`
//! - there are no stray `.stderr` files

use ignore::Walk;
use lazy_static::lazy_static;
use regex::Regex;
use std::collections::{HashMap, HashSet};
use std::collections::{BTreeSet, HashMap};
use std::ffi::OsStr;
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};

// FIXME: GitHub's UI truncates file lists that exceed 1000 entries, so these
Expand Down Expand Up @@ -97,14 +97,17 @@ fn check_entries(tests_path: &Path, bad: &mut bool) {
}
}

pub fn check(path: &Path, bad: &mut bool) {
pub fn check(path: &Path, bless: bool, bad: &mut bool) {
check_entries(&path, bad);

// the list of files in ui tests that are allowed to start with `issue-XXXX`
let mut allowed_issue_filenames: HashSet<String> = HashSet::from(
// BTreeSet because we would like a stable ordering so --bless works
let allowed_issue_names = BTreeSet::from(
include!("issues.txt").map(|path| path.replace("/", std::path::MAIN_SEPARATOR_STR)),
);

let mut remaining_issue_names: BTreeSet<String> = allowed_issue_names.clone();

let (ui, ui_fulldeps) = (path.join("ui"), path.join("ui-fulldeps"));
let paths = [ui.as_path(), ui_fulldeps.as_path()];
crate::walk::walk_no_read(&paths, |_, _| false, &mut |entry| {
Expand Down Expand Up @@ -156,7 +159,7 @@ pub fn check(path: &Path, bad: &mut bool) {
if let Some(test_name) = ISSUE_NAME_REGEX.captures(testname) {
// these paths are always relative to the passed `path` and always UTF8
let stripped_path = file_path.strip_prefix(path).unwrap().to_str().unwrap();
if !allowed_issue_filenames.remove(stripped_path) {
if !remaining_issue_names.remove(stripped_path) {
tidy_error!(
bad,
"file `{stripped_path}` must begin with a descriptive name, consider `{{reason}}-issue-{issue_n}.rs`",
Expand All @@ -169,8 +172,30 @@ pub fn check(path: &Path, bad: &mut bool) {
});

// if an excluded file is renamed, it must be removed from this list
if allowed_issue_filenames.len() > 0 {
for file_name in allowed_issue_filenames {
// do this automatically on bless, otherwise issue a tidy error
if bless {
let issues_txt_header = r#"
/*
============================================================
⚠️⚠️⚠️NOTHING SHOULD EVER BE ADDED TO THIS LIST⚠️⚠️⚠️
============================================================
*/
[
"#;
let tidy_src = std::env::current_dir().unwrap().join("src/tools/tidy/src");
// instead of overwriting the file, recreate it and use an "atomic rename"
// so we don't bork things on panic or a contributor using Ctrl+C
let blessed_issues_path = tidy_src.join("issues_blessed.txt");
let mut blessed_issues_txt = fs::File::create(&blessed_issues_path).unwrap();
blessed_issues_txt.write(issues_txt_header.as_bytes()).unwrap();
for filename in allowed_issue_names.difference(&remaining_issue_names) {
write!(blessed_issues_txt, "\"{filename}\",\n").unwrap();
}
write!(blessed_issues_txt, "]\n").unwrap();
let old_issues_path = tidy_src.join("issues.txt");
fs::rename(blessed_issues_path, old_issues_path).unwrap();
} else {
for file_name in remaining_issue_names {
let mut p = PathBuf::from(path);
p.push(file_name);
tidy_error!(
Expand Down

0 comments on commit cb57c42

Please sign in to comment.