Skip to content

Commit c34587e

Browse files
authored
Rollup merge of #147506 - Zalathar:isolate, r=jieyouxu
compiletest: Isolate public APIs and minimize public surface area As part of my ongoing efforts to improve directive parsing, I would like to be able to make internal changes without worrying about whether they're going to break the rustdoc-gui-test tool. That tool currently uses compiletest as a dependency to help with directive parsing. This PR therefore isolates all of compiletest's public APIs into two dedicated modules, one used by rustdoc-gui-test, and one used by the compiletest binary in `compiletest/src/bin/main.rs`. All other modules (and crate-root items) are then made non-`pub` to achieve the API isolation. Doing so reveals some unused items, which have been removed. (To reduce the amount of immediate textual churn, this PR does not comprehensively replace `pub` with `pub(crate)` throughout the whole crate; that could be done in a follow-up PR.) --- Ideally, rustdoc-gui-test would not depend on compiletest at all, but properly fixing that is out of scope for this PR. - #143827 r? jieyouxu
2 parents 1201c90 + ce4699d commit c34587e

File tree

11 files changed

+190
-165
lines changed

11 files changed

+190
-165
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4805,7 +4805,6 @@ name = "rustdoc-gui-test"
48054805
version = "0.1.0"
48064806
dependencies = [
48074807
"build_helper",
4808-
"camino",
48094808
"compiletest",
48104809
"getopts",
48114810
"walkdir",

src/bootstrap/src/core/build_steps/test.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,9 +1974,6 @@ HELP: You can add it into `bootstrap.toml` in `rust.codegen-backends = [{name:?}
19741974
} else if mode == "rustdoc-js" {
19751975
panic!("need nodejs to run rustdoc-js suite");
19761976
}
1977-
if let Some(ref npm) = builder.config.npm {
1978-
cmd.arg("--npm").arg(npm);
1979-
}
19801977
if builder.config.rust_optimize_tests {
19811978
cmd.arg("--optimize-tests");
19821979
}
Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,3 @@
1-
use std::env;
2-
use std::io::IsTerminal;
3-
use std::sync::Arc;
4-
5-
use compiletest::{early_config_check, parse_config, run_tests};
6-
71
fn main() {
8-
tracing_subscriber::fmt::init();
9-
10-
// colored checks stdout by default, but for some reason only stderr is a terminal.
11-
// compiletest *does* print many things to stdout, but it doesn't really matter.
12-
if std::io::stderr().is_terminal()
13-
&& matches!(std::env::var("NO_COLOR").as_deref(), Err(_) | Ok("0"))
14-
{
15-
colored::control::set_override(true);
16-
}
17-
18-
let config = Arc::new(parse_config(env::args().collect()));
19-
20-
early_config_check(&config);
21-
22-
run_tests(config);
2+
compiletest::cli::main();
233
}

src/tools/compiletest/src/cli.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//! Isolates the APIs used by `bin/main.rs`, to help minimize the surface area
2+
//! of public exports from the compiletest library crate.
3+
4+
use std::env;
5+
use std::io::IsTerminal;
6+
use std::sync::Arc;
7+
8+
use crate::{early_config_check, parse_config, run_tests};
9+
10+
pub fn main() {
11+
tracing_subscriber::fmt::init();
12+
13+
// colored checks stdout by default, but for some reason only stderr is a terminal.
14+
// compiletest *does* print many things to stdout, but it doesn't really matter.
15+
if std::io::stderr().is_terminal()
16+
&& matches!(std::env::var("NO_COLOR").as_deref(), Err(_) | Ok("0"))
17+
{
18+
colored::control::set_override(true);
19+
}
20+
21+
let config = Arc::new(parse_config(env::args().collect()));
22+
23+
early_config_check(&config);
24+
25+
run_tests(config);
26+
}

src/tools/compiletest/src/common.rs

Lines changed: 2 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,6 @@ pub struct Config {
631631

632632
/// Path to a NodeJS executable. Used for JS doctests, emscripten and WASM tests.
633633
pub nodejs: Option<String>,
634-
/// Path to a npm executable. Used for rustdoc GUI tests.
635-
pub npm: Option<String>,
636634

637635
/// Whether to rerun tests even if the inputs are unchanged.
638636
pub force_rerun: bool,
@@ -686,110 +684,6 @@ pub struct Config {
686684
}
687685

688686
impl Config {
689-
/// Incomplete config intended for `src/tools/rustdoc-gui-test` **only** as
690-
/// `src/tools/rustdoc-gui-test` wants to reuse `compiletest`'s directive -> test property
691-
/// handling for `//@ {compile,run}-flags`, do not use for any other purpose.
692-
///
693-
/// FIXME(#143827): this setup feels very hacky. It so happens that `tests/rustdoc-gui/`
694-
/// **only** uses `//@ {compile,run}-flags` for now and not any directives that actually rely on
695-
/// info that is assumed available in a fully populated [`Config`].
696-
pub fn incomplete_for_rustdoc_gui_test() -> Config {
697-
// FIXME(#143827): spelling this out intentionally, because this is questionable.
698-
//
699-
// For instance, `//@ ignore-stage1` will not work at all.
700-
Config {
701-
mode: TestMode::Rustdoc,
702-
// E.g. this has no sensible default tbh.
703-
suite: TestSuite::Ui,
704-
705-
// Dummy values.
706-
edition: Default::default(),
707-
bless: Default::default(),
708-
fail_fast: Default::default(),
709-
compile_lib_path: Utf8PathBuf::default(),
710-
run_lib_path: Utf8PathBuf::default(),
711-
rustc_path: Utf8PathBuf::default(),
712-
cargo_path: Default::default(),
713-
stage0_rustc_path: Default::default(),
714-
query_rustc_path: Default::default(),
715-
rustdoc_path: Default::default(),
716-
coverage_dump_path: Default::default(),
717-
python: Default::default(),
718-
jsondocck_path: Default::default(),
719-
jsondoclint_path: Default::default(),
720-
llvm_filecheck: Default::default(),
721-
llvm_bin_dir: Default::default(),
722-
run_clang_based_tests_with: Default::default(),
723-
src_root: Utf8PathBuf::default(),
724-
src_test_suite_root: Utf8PathBuf::default(),
725-
build_root: Utf8PathBuf::default(),
726-
build_test_suite_root: Utf8PathBuf::default(),
727-
sysroot_base: Utf8PathBuf::default(),
728-
stage: Default::default(),
729-
stage_id: String::default(),
730-
debugger: Default::default(),
731-
run_ignored: Default::default(),
732-
with_rustc_debug_assertions: Default::default(),
733-
with_std_debug_assertions: Default::default(),
734-
filters: Default::default(),
735-
skip: Default::default(),
736-
filter_exact: Default::default(),
737-
force_pass_mode: Default::default(),
738-
run: Default::default(),
739-
runner: Default::default(),
740-
host_rustcflags: Default::default(),
741-
target_rustcflags: Default::default(),
742-
rust_randomized_layout: Default::default(),
743-
optimize_tests: Default::default(),
744-
target: Default::default(),
745-
host: Default::default(),
746-
cdb: Default::default(),
747-
cdb_version: Default::default(),
748-
gdb: Default::default(),
749-
gdb_version: Default::default(),
750-
lldb_version: Default::default(),
751-
llvm_version: Default::default(),
752-
system_llvm: Default::default(),
753-
android_cross_path: Default::default(),
754-
adb_path: Default::default(),
755-
adb_test_dir: Default::default(),
756-
adb_device_status: Default::default(),
757-
lldb_python_dir: Default::default(),
758-
verbose: Default::default(),
759-
color: Default::default(),
760-
remote_test_client: Default::default(),
761-
compare_mode: Default::default(),
762-
rustfix_coverage: Default::default(),
763-
has_html_tidy: Default::default(),
764-
has_enzyme: Default::default(),
765-
channel: Default::default(),
766-
git_hash: Default::default(),
767-
cc: Default::default(),
768-
cxx: Default::default(),
769-
cflags: Default::default(),
770-
cxxflags: Default::default(),
771-
ar: Default::default(),
772-
target_linker: Default::default(),
773-
host_linker: Default::default(),
774-
llvm_components: Default::default(),
775-
nodejs: Default::default(),
776-
npm: Default::default(),
777-
force_rerun: Default::default(),
778-
only_modified: Default::default(),
779-
target_cfgs: Default::default(),
780-
builtin_cfg_names: Default::default(),
781-
supported_crate_types: Default::default(),
782-
nocapture: Default::default(),
783-
nightly_branch: Default::default(),
784-
git_merge_commit_email: Default::default(),
785-
profiler_runtime: Default::default(),
786-
diff_command: Default::default(),
787-
minicore_path: Default::default(),
788-
default_codegen_backend: CodegenBackend::Llvm,
789-
override_codegen_backend: None,
790-
}
791-
}
792-
793687
/// FIXME: this run scheme is... confusing.
794688
pub fn run_enabled(&self) -> bool {
795689
self.run.unwrap_or_else(|| {
@@ -825,7 +719,8 @@ impl Config {
825719
self.target_cfg().abi == abi
826720
}
827721

828-
pub fn matches_family(&self, family: &str) -> bool {
722+
#[cfg_attr(not(test), expect(dead_code, reason = "only used by tests for `ignore-{family}`"))]
723+
pub(crate) fn matches_family(&self, family: &str) -> bool {
829724
self.target_cfg().families.iter().any(|f| f == family)
830725
}
831726

src/tools/compiletest/src/directives.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,6 @@ pub struct TestProps {
198198
pub filecheck_flags: Vec<String>,
199199
/// Don't automatically insert any `--check-cfg` args
200200
pub no_auto_check_cfg: bool,
201-
/// Run tests which require enzyme being build
202-
pub has_enzyme: bool,
203201
/// Build and use `minicore` as `core` stub for `no_core` tests in cross-compilation scenarios
204202
/// that don't otherwise want/need `-Z build-std`.
205203
pub add_core_stubs: bool,
@@ -314,7 +312,6 @@ impl TestProps {
314312
llvm_cov_flags: vec![],
315313
filecheck_flags: vec![],
316314
no_auto_check_cfg: false,
317-
has_enzyme: false,
318315
add_core_stubs: false,
319316
core_stubs_compile_flags: vec![],
320317
dont_require_annotations: Default::default(),

src/tools/compiletest/src/lib.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
#[cfg(test)]
44
mod tests;
55

6-
pub mod common;
6+
pub mod cli;
7+
mod common;
78
mod debuggers;
8-
pub mod diagnostics;
9-
pub mod directives;
10-
pub mod edition;
11-
pub mod errors;
9+
mod diagnostics;
10+
mod directives;
11+
mod edition;
12+
mod errors;
1213
mod executor;
1314
mod json;
1415
mod output_capture;
1516
mod panic_hook;
1617
mod raise_fd_limit;
1718
mod read2;
18-
pub mod runtest;
19-
pub mod util;
19+
mod runtest;
20+
pub mod rustdoc_gui_test;
21+
mod util;
2022

2123
use core::panic;
2224
use std::collections::HashSet;
@@ -48,7 +50,7 @@ use crate::executor::{CollectedTest, ColorConfig};
4850
/// The config mostly reflects command-line arguments, but there might also be
4951
/// some code here that inspects environment variables or even runs executables
5052
/// (e.g. when discovering debugger versions).
51-
pub fn parse_config(args: Vec<String>) -> Config {
53+
fn parse_config(args: Vec<String>) -> Config {
5254
let mut opts = Options::new();
5355
opts.reqopt("", "compile-lib-path", "path to host shared libraries", "PATH")
5456
.reqopt("", "run-lib-path", "path to target shared libraries", "PATH")
@@ -462,7 +464,6 @@ pub fn parse_config(args: Vec<String>) -> Config {
462464
host_linker: matches.opt_str("host-linker"),
463465
llvm_components: matches.opt_str("llvm-components").unwrap(),
464466
nodejs: matches.opt_str("nodejs"),
465-
npm: matches.opt_str("npm"),
466467

467468
force_rerun: matches.opt_present("force-rerun"),
468469

@@ -486,22 +487,15 @@ pub fn parse_config(args: Vec<String>) -> Config {
486487
}
487488
}
488489

489-
pub fn opt_str(maybestr: &Option<String>) -> &str {
490-
match *maybestr {
491-
None => "(none)",
492-
Some(ref s) => s,
493-
}
494-
}
495-
496-
pub fn opt_str2(maybestr: Option<String>) -> String {
490+
fn opt_str2(maybestr: Option<String>) -> String {
497491
match maybestr {
498492
None => "(none)".to_owned(),
499493
Some(s) => s,
500494
}
501495
}
502496

503497
/// Called by `main` after the config has been parsed.
504-
pub fn run_tests(config: Arc<Config>) {
498+
fn run_tests(config: Arc<Config>) {
505499
debug!(?config, "run_tests");
506500

507501
panic_hook::install_panic_hook();
@@ -639,7 +633,7 @@ impl TestCollector {
639633
/// FIXME(Zalathar): Now that we no longer rely on libtest, try to overhaul
640634
/// test discovery to take into account the filters/tests specified on the
641635
/// command-line, instead of having to enumerate everything.
642-
pub(crate) fn collect_and_make_tests(config: Arc<Config>) -> Vec<CollectedTest> {
636+
fn collect_and_make_tests(config: Arc<Config>) -> Vec<CollectedTest> {
643637
debug!("making tests from {}", config.src_test_suite_root);
644638
let common_inputs_stamp = common_inputs_stamp(&config);
645639
let modified_tests =
@@ -851,7 +845,7 @@ fn collect_tests_from_dir(
851845
}
852846

853847
/// Returns true if `file_name` looks like a proper test file name.
854-
pub fn is_test(file_name: &str) -> bool {
848+
fn is_test(file_name: &str) -> bool {
855849
if !file_name.ends_with(".rs") {
856850
return false;
857851
}
@@ -1131,7 +1125,7 @@ fn check_for_overlapping_test_paths(found_path_stems: &HashSet<Utf8PathBuf>) {
11311125
}
11321126
}
11331127

1134-
pub fn early_config_check(config: &Config) {
1128+
fn early_config_check(config: &Config) {
11351129
if !config.has_html_tidy && config.mode == TestMode::Rustdoc {
11361130
warning!("`tidy` (html-tidy.org) is not installed; diffs will not be generated");
11371131
}

0 commit comments

Comments
 (0)