Skip to content

Commit 63be8ba

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

File tree

4 files changed

+143
-6
lines changed

4 files changed

+143
-6
lines changed

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

+126
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,129 @@ fn alias_and_path_for_library() {
238241
);
239242
}
240243

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

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)