Factor out common code in interface tests.

Replacing `mk_session` with `sess_and_cfgs`, which does a bit more of
the shared stuff -- the option parsing and the `build_configuration`
call.
This commit is contained in:
Nicholas Nethercote 2024-03-21 14:06:15 +11:00
parent ccfcd950b3
commit ff2e4ed1f1

View File

@ -25,12 +25,19 @@ use std::num::NonZero;
use std::path::{Path, PathBuf};
use std::sync::Arc;
fn mk_session(matches: getopts::Matches) -> (Session, Cfg) {
fn sess_and_cfg<F>(args: &[&'static str], f: F)
where
F: FnOnce(Session, Cfg),
{
let mut early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
early_dcx.initialize_checked_jobserver();
let registry = registry::Registry::new(&[]);
let matches = optgroups().parse(args).unwrap();
let sessopts = build_session_options(&mut early_dcx, &matches);
let sysroot = filesearch::materialize_sysroot(sessopts.maybe_sysroot.clone());
let target = rustc_session::config::build_target_config(&early_dcx, &sessopts, &sysroot);
rustc_span::create_default_session_globals_then(|| {
let temps_dir = sessopts.unstable_opts.temps_dir.as_deref().map(PathBuf::from);
let io = CompilerIO {
input: Input::Str { name: FileName::Custom(String::new()), input: String::new() },
@ -38,17 +45,12 @@ fn mk_session(matches: getopts::Matches) -> (Session, Cfg) {
output_file: None,
temps_dir,
};
let sysroot = filesearch::materialize_sysroot(sessopts.maybe_sysroot.clone());
let target = rustc_session::config::build_target_config(&early_dcx, &sessopts, &sysroot);
let sess = build_session(
early_dcx,
sessopts,
io,
None,
registry,
registry::Registry::new(&[]),
vec![],
Default::default(),
None,
@ -60,7 +62,9 @@ fn mk_session(matches: getopts::Matches) -> (Session, Cfg) {
Default::default(),
);
let cfg = parse_cfg(&sess.dcx(), matches.opt_strs("cfg"));
(sess, cfg)
let cfg = build_configuration(&sess, cfg);
f(sess, cfg)
});
}
fn new_public_extern_entry<S, I>(locations: I) -> ExternEntry
@ -125,21 +129,15 @@ fn assert_non_crate_hash_different(x: &Options, y: &Options) {
// When the user supplies --test we should implicitly supply --cfg test
#[test]
fn test_switch_implies_cfg_test() {
rustc_span::create_default_session_globals_then(|| {
let matches = optgroups().parse(&["--test".to_string()]).unwrap();
let (sess, cfg) = mk_session(matches);
let cfg = build_configuration(&sess, cfg);
sess_and_cfg(&["--test"], |_sess, cfg| {
assert!(cfg.contains(&(sym::test, None)));
});
})
}
// When the user supplies --test and --cfg test, don't implicitly add another --cfg test
#[test]
fn test_switch_implies_cfg_test_unless_cfg_test() {
rustc_span::create_default_session_globals_then(|| {
let matches = optgroups().parse(&["--test".to_string(), "--cfg=test".to_string()]).unwrap();
let (sess, cfg) = mk_session(matches);
let cfg = build_configuration(&sess, cfg);
sess_and_cfg(&["--test", "--cfg=test"], |_sess, cfg| {
let mut test_items = cfg.iter().filter(|&&(name, _)| name == sym::test);
assert!(test_items.next().is_some());
assert!(test_items.next().is_none());
@ -148,22 +146,15 @@ fn test_switch_implies_cfg_test_unless_cfg_test() {
#[test]
fn test_can_print_warnings() {
rustc_span::create_default_session_globals_then(|| {
let matches = optgroups().parse(&["-Awarnings".to_string()]).unwrap();
let (sess, _) = mk_session(matches);
sess_and_cfg(&["-Awarnings"], |sess, _cfg| {
assert!(!sess.dcx().can_emit_warnings());
});
rustc_span::create_default_session_globals_then(|| {
let matches =
optgroups().parse(&["-Awarnings".to_string(), "-Dwarnings".to_string()]).unwrap();
let (sess, _) = mk_session(matches);
sess_and_cfg(&["-Awarnings", "-Dwarnings"], |sess, _cfg| {
assert!(sess.dcx().can_emit_warnings());
});
rustc_span::create_default_session_globals_then(|| {
let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap();
let (sess, _) = mk_session(matches);
sess_and_cfg(&["-Adead_code"], |sess, _cfg| {
assert!(sess.dcx().can_emit_warnings());
});
}