Skip to content

Commit 1fb182b

Browse files
Create new CiInfo type in tidy checks to centralize CI related checks
1 parent 6d0c9e2 commit 1fb182b

File tree

3 files changed

+71
-44
lines changed

3 files changed

+71
-44
lines changed

src/tools/tidy/src/lib.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
//! This library contains the tidy lints and exposes it
44
//! to be used by tools.
55
6+
use std::ffi::OsStr;
7+
use std::process::Command;
8+
9+
use build_helper::ci::CiEnv;
10+
use build_helper::git::{GitConfig, get_closest_upstream_commit};
11+
use build_helper::stage0_parser::{Stage0Config, parse_stage0_file};
612
use termcolor::WriteColor;
713

814
macro_rules! static_regex {
@@ -63,6 +69,61 @@ fn tidy_error(args: &str) -> std::io::Result<()> {
6369
Ok(())
6470
}
6571

72+
pub struct CiInfo {
73+
pub git_merge_commit_email: String,
74+
pub nightly_branch: String,
75+
pub base_commit: Option<String>,
76+
pub ci_env: CiEnv,
77+
}
78+
79+
impl CiInfo {
80+
pub fn new(bad: &mut bool) -> Self {
81+
let stage0 = parse_stage0_file();
82+
let Stage0Config { nightly_branch, git_merge_commit_email, .. } = stage0.config;
83+
84+
let mut info = Self {
85+
nightly_branch,
86+
git_merge_commit_email,
87+
ci_env: CiEnv::current(),
88+
base_commit: None,
89+
};
90+
let base_commit = match get_closest_upstream_commit(None, &info.git_config(), info.ci_env) {
91+
Ok(Some(commit)) => Some(commit),
92+
Ok(None) => {
93+
info.error_if_in_ci("no base commit found", bad);
94+
None
95+
}
96+
Err(error) => {
97+
info.error_if_in_ci(&format!("failed to retrieve base commit: {error}"), bad);
98+
None
99+
}
100+
};
101+
info.base_commit = base_commit;
102+
info
103+
}
104+
105+
pub fn git_config(&self) -> GitConfig<'_> {
106+
GitConfig {
107+
nightly_branch: &self.nightly_branch,
108+
git_merge_commit_email: &self.git_merge_commit_email,
109+
}
110+
}
111+
112+
pub fn error_if_in_ci(&self, msg: &str, bad: &mut bool) {
113+
if self.ci_env.is_running_in_ci() {
114+
*bad = true;
115+
eprintln!("tidy check error: {msg}");
116+
} else {
117+
eprintln!("tidy check warning: {msg}. Some checks will be skipped.");
118+
}
119+
}
120+
}
121+
122+
pub fn git_diff<S: AsRef<OsStr>>(base_commit: &str, extra_arg: S) -> Option<String> {
123+
let output = Command::new("git").arg("diff").arg(base_commit).arg(extra_arg).output().ok()?;
124+
Some(String::from_utf8_lossy(&output.stdout).into())
125+
}
126+
66127
pub mod alphabetical;
67128
pub mod bins;
68129
pub mod debug_artifacts;

src/tools/tidy/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ fn main() {
4848
let extra_checks =
4949
cfg_args.iter().find(|s| s.starts_with("--extra-checks=")).map(String::as_str);
5050

51-
let bad = std::sync::Arc::new(AtomicBool::new(false));
51+
let mut bad = false;
52+
let ci_info = CiInfo::new(&mut bad);
53+
let bad = std::sync::Arc::new(AtomicBool::new(bad));
5254

5355
let drain_handles = |handles: &mut VecDeque<ScopedJoinHandle<'_, ()>>| {
5456
// poll all threads for completion before awaiting the oldest one
@@ -110,7 +112,7 @@ fn main() {
110112
check!(rustdoc_css_themes, &librustdoc_path);
111113
check!(rustdoc_templates, &librustdoc_path);
112114
check!(rustdoc_js, &librustdoc_path, &tools_path, &src_path);
113-
check!(rustdoc_json, &src_path);
115+
check!(rustdoc_json, &src_path, &ci_info);
114116
check!(known_bug, &crashes_path);
115117
check!(unknown_revision, &tests_path);
116118

src/tools/tidy/src/rustdoc_json.rs

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,19 @@
11
//! Tidy check to ensure that `FORMAT_VERSION` was correctly updated if `rustdoc-json-types` was
22
//! updated as well.
33
4-
use std::ffi::OsStr;
54
use std::path::Path;
6-
use std::process::Command;
7-
8-
use build_helper::ci::CiEnv;
9-
use build_helper::git::{GitConfig, get_closest_upstream_commit};
10-
use build_helper::stage0_parser::parse_stage0_file;
115

126
const RUSTDOC_JSON_TYPES: &str = "src/rustdoc-json-types";
137

14-
fn git_diff<S: AsRef<OsStr>>(base_commit: &str, extra_arg: S) -> Option<String> {
15-
let output = Command::new("git").arg("diff").arg(base_commit).arg(extra_arg).output().ok()?;
16-
Some(String::from_utf8_lossy(&output.stdout).into())
17-
}
18-
19-
fn error_if_in_ci(ci_env: CiEnv, msg: &str, bad: &mut bool) {
20-
if ci_env.is_running_in_ci() {
21-
*bad = true;
22-
eprintln!("error in `rustdoc_json` tidy check: {msg}");
23-
} else {
24-
eprintln!("{msg}. Skipping `rustdoc_json` tidy check");
25-
}
26-
}
27-
28-
pub fn check(src_path: &Path, bad: &mut bool) {
8+
pub fn check(src_path: &Path, ci_info: &crate::CiInfo, bad: &mut bool) {
299
println!("Checking tidy rustdoc_json...");
30-
let stage0 = parse_stage0_file();
31-
let ci_env = CiEnv::current();
32-
let base_commit = match get_closest_upstream_commit(
33-
None,
34-
&GitConfig {
35-
nightly_branch: &stage0.config.nightly_branch,
36-
git_merge_commit_email: &stage0.config.git_merge_commit_email,
37-
},
38-
ci_env,
39-
) {
40-
Ok(Some(commit)) => commit,
41-
Ok(None) => {
42-
error_if_in_ci(ci_env, "no base commit found", bad);
43-
return;
44-
}
45-
Err(error) => {
46-
error_if_in_ci(ci_env, &format!("failed to retrieve base commit: {error}"), bad);
47-
return;
48-
}
10+
let Some(base_commit) = &ci_info.base_commit else {
11+
eprintln!("No base commit, skipping rustdoc_json check");
12+
return;
4913
};
5014

5115
// First we check that `src/rustdoc-json-types` was modified.
52-
match git_diff(&base_commit, "--name-status") {
16+
match crate::git_diff(&base_commit, "--name-status") {
5317
Some(output) => {
5418
if !output
5519
.lines()
@@ -67,7 +31,7 @@ pub fn check(src_path: &Path, bad: &mut bool) {
6731
}
6832
}
6933
// Then we check that if `FORMAT_VERSION` was updated, the `Latest feature:` was also updated.
70-
match git_diff(&base_commit, src_path.join("rustdoc-json-types")) {
34+
match crate::git_diff(&base_commit, src_path.join("rustdoc-json-types")) {
7135
Some(output) => {
7236
let mut format_version_updated = false;
7337
let mut latest_feature_comment_updated = false;

0 commit comments

Comments
 (0)