Skip to content

Commit 6ca2a0d

Browse files
committed
Extend ci_rustc_if_unchanged tests
1 parent f414afb commit 6ca2a0d

File tree

2 files changed

+77
-32
lines changed

2 files changed

+77
-32
lines changed

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

Lines changed: 72 additions & 31 deletions
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";
@@ -239,42 +242,80 @@ fn alias_and_path_for_library() {
239242
}
240243

241244
#[test]
242-
fn ci_rustc_if_unchanged_logic() {
243-
let config = Config::parse_inner(
244-
Flags::parse(&[
245-
"build".to_owned(),
246-
"--dry-run".to_owned(),
247-
"--set=rust.download-rustc='if-unchanged'".to_owned(),
248-
]),
249-
|&_| Ok(Default::default()),
250-
);
251-
252-
let build = Build::new(config.clone());
253-
let builder = Builder::new(&build);
254-
255-
if config.out.exists() {
256-
fs::remove_dir_all(&config.out).unwrap();
257-
}
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", true);
253+
assert_eq!(config.download_rustc_commit, None);
254+
});
255+
}
258256

259-
builder.run_step_descriptions(&Builder::get_step_descriptions(config.cmd.kind()), &[]);
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+
let config = parse_config_download_rustc_at(ctx.get_path(), "if-unchanged", true);
266+
assert_eq!(config.download_rustc_commit, None);
267+
});
268+
}
260269

261-
// Make sure "if-unchanged" logic doesn't try to use CI rustc while there are changes
262-
// in compiler and/or library.
263-
if config.download_rustc_commit.is_some() {
264-
let mut paths = vec!["compiler"];
270+
#[test]
271+
fn ci_rustc_if_unchanged_do_not_invalidate_on_library_changes_outside_ci() {
272+
git_test(|ctx| {
273+
prepare_rustc_checkout(ctx);
274+
let sha = ctx.create_upstream_merge(&["compiler/bar"]);
275+
// This change should not invalidate download-ci-rustc
276+
ctx.create_nonupstream_merge(&["library/foo"]);
277+
278+
let config = parse_config_download_rustc_at(ctx.get_path(), "if-unchanged", false);
279+
assert_eq!(config.download_rustc_commit, Some(sha));
280+
});
281+
}
265282

266-
// Handle library tree the same way as in `Config::download_ci_rustc_commit`.
267-
if builder.config.is_running_on_ci {
268-
paths.push("library");
269-
}
283+
#[test]
284+
fn ci_rustc_if_unchanged_do_not_invalidate_on_tool_changes() {
285+
git_test(|ctx| {
286+
prepare_rustc_checkout(ctx);
287+
let sha = ctx.create_upstream_merge(&["compiler/bar"]);
288+
// This change should not invalidate download-ci-rustc
289+
ctx.create_nonupstream_merge(&["src/tools/foo"]);
290+
291+
let config = parse_config_download_rustc_at(ctx.get_path(), "if-unchanged", true);
292+
assert_eq!(config.download_rustc_commit, Some(sha));
293+
});
294+
}
270295

271-
let has_changes = config.has_changes_from_upstream(&paths);
296+
/// Prepares the given directory so that it looks like a rustc checkout.
297+
/// Also configures `GitCtx` to use the correct merge bot e-mail for upstream merge commits.
298+
fn prepare_rustc_checkout(ctx: &mut GitCtx) {
299+
ctx.merge_bot_email =
300+
format!("Merge bot <{}>", parse_stage0_file().config.git_merge_commit_email);
301+
ctx.write("src/ci/channel", "nightly");
302+
ctx.commit();
303+
}
272304

273-
assert!(
274-
!has_changes,
275-
"CI-rustc can't be used with 'if-unchanged' while there are changes in compiler and/or library."
276-
);
277-
}
305+
/// Parses a Config directory from `path`, with the given value of `download_rustc`.
306+
fn parse_config_download_rustc_at(path: &Path, download_rustc: &str, ci: bool) -> Config {
307+
Config::parse_inner(
308+
Flags::parse(&[
309+
"build".to_owned(),
310+
"--dry-run".to_owned(),
311+
"--ci".to_owned(),
312+
if ci { "true" } else { "false" }.to_owned(),
313+
format!("--set=rust.download-rustc='{download_rustc}'"),
314+
"--src".to_owned(),
315+
path.to_str().unwrap().to_owned(),
316+
]),
317+
|&_| Ok(Default::default()),
318+
)
278319
}
279320

280321
mod defaults {

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

Lines changed: 5 additions & 1 deletion
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)