Skip to content

Commit e1fe7f2

Browse files
committed
Reinstate ci_rustc_if_unchanged tests
This time without the dummy commit.
1 parent 307dca8 commit e1fe7f2

File tree

4 files changed

+139
-6
lines changed

4 files changed

+139
-6
lines changed

src/bootstrap/src/core/builder/tests.rs

+122
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
use std::env::VarError;
12
use std::{panic, thread};
23

4+
use build_helper::stage0_parser::parse_stage0_file;
35
use llvm::prebuilt_llvm_config;
46

57
use super::*;
68
use crate::Flags;
79
use crate::core::build_steps::doc::DocumentationFormat;
810
use crate::core::config::Config;
11+
use crate::utils::tests::git::{GitCtx, git_test};
912

1013
static TEST_TRIPLE_1: &str = "i686-unknown-haiku";
1114
static TEST_TRIPLE_2: &str = "i686-unknown-hurd-gnu";
@@ -238,6 +241,125 @@ fn alias_and_path_for_library() {
238241
);
239242
}
240243

244+
#[test]
245+
fn ci_rustc_if_unchanged_invalidate_on_compiler_changes() {
246+
git_test(|ctx| {
247+
prepare_rustc_checkout(ctx);
248+
ctx.create_upstream_merge(&["compiler/bar"]);
249+
// This change should invalidate download-ci-rustc
250+
ctx.create_nonupstream_merge(&["compiler/foo"]);
251+
252+
let config = parse_config_download_rustc_at(ctx.get_path(), "if-unchanged");
253+
assert_eq!(config.download_rustc_commit, None);
254+
});
255+
}
256+
257+
#[test]
258+
fn ci_rustc_if_unchanged_invalidate_on_library_changes_in_ci() {
259+
git_test(|ctx| {
260+
prepare_rustc_checkout(ctx);
261+
ctx.create_upstream_merge(&["compiler/bar"]);
262+
// This change should invalidate download-ci-rustc
263+
ctx.create_nonupstream_merge(&["library/foo"]);
264+
265+
with_mocked_ci(|| {
266+
let config = parse_config_download_rustc_at(ctx.get_path(), "if-unchanged");
267+
assert_eq!(config.download_rustc_commit, None);
268+
});
269+
});
270+
}
271+
272+
#[test]
273+
fn ci_rustc_if_unchanged_do_not_invalidate_on_library_changes_outside_ci() {
274+
git_test(|ctx| {
275+
prepare_rustc_checkout(ctx);
276+
let sha = ctx.create_upstream_merge(&["compiler/bar"]);
277+
// This change should not invalidate download-ci-rustc
278+
ctx.create_nonupstream_merge(&["library/foo"]);
279+
280+
with_mocked_outside_ci(|| {
281+
let config = parse_config_download_rustc_at(ctx.get_path(), "if-unchanged");
282+
assert_eq!(config.download_rustc_commit, Some(sha));
283+
});
284+
});
285+
}
286+
287+
#[test]
288+
fn ci_rustc_if_unchanged_do_not_invalidate_on_tool_changes() {
289+
git_test(|ctx| {
290+
prepare_rustc_checkout(ctx);
291+
let sha = ctx.create_upstream_merge(&["compiler/bar"]);
292+
// This change should not invalidate download-ci-rustc
293+
ctx.create_nonupstream_merge(&["src/tools/foo"]);
294+
295+
let config = parse_config_download_rustc_at(ctx.get_path(), "if-unchanged");
296+
assert_eq!(config.download_rustc_commit, Some(sha));
297+
});
298+
}
299+
300+
/// Temporarily mocks the current environment to make it look like we're in CI.
301+
/// If we are actually in CI, nothing changes.
302+
fn with_mocked_ci<F, R>(func: F) -> R
303+
where
304+
F: FnOnce() -> R,
305+
{
306+
if let Err(VarError::NotPresent) = std::env::var("GITHUB_ACTIONS") {
307+
// SAFETY: bootstrap tests are sequential
308+
unsafe { std::env::set_var("GITHUB_ACTIONS", "true") };
309+
310+
let ret = func();
311+
312+
// SAFETY: bootstrap tests are sequential
313+
unsafe { std::env::remove_var("GITHUB_ACTIONS") };
314+
ret
315+
} else {
316+
func()
317+
}
318+
}
319+
320+
/// Temporarily mocks the current environment to make it look like we're **NOT** in CI.
321+
/// If we are actually not in CI, nothing changes.
322+
fn with_mocked_outside_ci<F, R>(func: F) -> R
323+
where
324+
F: FnOnce() -> R,
325+
{
326+
if let Ok(var) = std::env::var("GITHUB_ACTIONS") {
327+
// SAFETY: bootstrap tests are sequential
328+
unsafe { std::env::remove_var("GITHUB_ACTIONS") };
329+
330+
let ret = func();
331+
332+
// SAFETY: bootstrap tests are sequential
333+
unsafe { std::env::set_var("GITHUB_ACTIONS", var) };
334+
ret
335+
} else {
336+
func()
337+
}
338+
}
339+
340+
/// Prepares the given directory so that it looks like a rustc checkout.
341+
/// Also configures `GitCtx` to use the correct merge bot e-mail for upstream merge commits.
342+
fn prepare_rustc_checkout(ctx: &mut GitCtx) {
343+
ctx.merge_bot_email =
344+
format!("Merge bot <{}>", parse_stage0_file().config.git_merge_commit_email);
345+
ctx.write("src/ci/channel", "nightly");
346+
ctx.commit();
347+
}
348+
349+
/// Parses a Config directory from `path`, with the given value of `download_rustc`.
350+
fn parse_config_download_rustc_at(path: &Path, download_rustc: &str) -> Config {
351+
Config::parse_inner(
352+
Flags::parse(&[
353+
"build".to_owned(),
354+
"--dry-run".to_owned(),
355+
format!("--set=rust.download-rustc='{download_rustc}'"),
356+
"--src".to_owned(),
357+
path.to_str().unwrap().to_owned(),
358+
]),
359+
|&_| Ok(Default::default()),
360+
)
361+
}
362+
241363
mod defaults {
242364
use pretty_assertions::assert_eq;
243365

src/bootstrap/src/core/config/config.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -3007,6 +3007,7 @@ impl Config {
30073007
llvm_assertions: bool,
30083008
) -> Option<String> {
30093009
if !is_download_ci_available(&self.build.triple, llvm_assertions) {
3010+
eprintln!("DOWNLOAD NOT AVAILABLE");
30103011
return None;
30113012
}
30123013

@@ -3045,17 +3046,21 @@ impl Config {
30453046
allowed_paths.push(":!library");
30463047
}
30473048

3049+
eprintln!("{:?}", self.rust_info);
3050+
eprintln!("{:?}", self.src);
3051+
eprintln!("{:?}", self.git_config());
30483052
let commit = if self.rust_info.is_managed_git_subrepository() {
30493053
// Look for a version to compare to based on the current commit.
30503054
// Only commits merged by bors will have CI artifacts.
30513055
let freshness = self.check_path_modifications(&allowed_paths);
3052-
self.verbose(|| {
3053-
eprintln!("rustc freshness: {freshness:?}");
3054-
});
3056+
// self.verbose(|| {
3057+
eprintln!("rustc freshness: {freshness:?}");
3058+
// });
30553059
match freshness {
30563060
PathFreshness::LastModifiedUpstream { upstream } => upstream,
30573061
PathFreshness::HasLocalModifications { upstream } => {
30583062
if if_unchanged {
3063+
eprintln!("LOCAL MODIFICATIONS IF UNCHANGED");
30593064
return None;
30603065
}
30613066

@@ -3064,6 +3069,7 @@ impl Config {
30643069
eprintln!(
30653070
"`rustc.download-ci` functionality will be skipped as artifacts are not available."
30663071
);
3072+
eprintln!("WE ARE ON CI");
30673073
return None;
30683074
}
30693075

@@ -3085,6 +3091,7 @@ impl Config {
30853091
"WARN: `rust.debug-assertions = true` will prevent downloading CI rustc as alt CI \
30863092
rustc is not currently built with debug assertions."
30873093
);
3094+
eprintln!("REQUESTING DEBUG ASSERTIONS");
30883095
return None;
30893096
}
30903097

src/bootstrap/src/utils/channel.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use super::helpers;
1212
use crate::Build;
1313
use crate::utils::helpers::{start_process, t};
1414

15-
#[derive(Clone, Default)]
15+
#[derive(Clone, Default, Debug)]
1616
pub enum GitInfo {
1717
/// This is not a git repository.
1818
#[default]
@@ -26,7 +26,7 @@ pub enum GitInfo {
2626
RecordedForTarball(Info),
2727
}
2828

29-
#[derive(Clone)]
29+
#[derive(Clone, Debug)]
3030
pub struct Info {
3131
pub commit_date: String,
3232
pub sha: String,

src/bootstrap/src/utils/tests/git.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,17 @@ impl GitCtx {
8181
}
8282

8383
pub fn modify(&self, path: &str) {
84+
self.write(path, "line");
85+
}
86+
87+
pub fn write(&self, path: &str, data: &str) {
8488
use std::io::Write;
8589

8690
let path = self.dir.path().join(path);
8791
std::fs::create_dir_all(&path.parent().unwrap()).unwrap();
8892

8993
let mut file = OpenOptions::new().create(true).append(true).open(path).unwrap();
90-
writeln!(file, "line").unwrap();
94+
writeln!(file, "{data}").unwrap();
9195
}
9296

9397
pub fn commit(&self) -> String {

0 commit comments

Comments
 (0)