Skip to content

Commit c3087c8

Browse files
committed
WIP: use ui_test dependency builder for test dependencies
1 parent dff9f9f commit c3087c8

File tree

4 files changed

+29
-98
lines changed

4 files changed

+29
-98
lines changed

Cargo.toml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ itertools = "0.12"
4545
pulldown-cmark = { version = "0.11", default-features = false, features = ["html"] }
4646
askama = { version = "0.14", default-features = false, features = ["alloc", "config", "derive"] }
4747

48-
# UI test dependencies
49-
if_chain = "1.0"
50-
quote = "1.0.25"
51-
syn = { version = "2.0", features = ["full"] }
52-
futures = "0.3"
53-
parking_lot = "0.12"
54-
tokio = { version = "1", features = ["io-util"] }
55-
5648
[build-dependencies]
5749
rustc_tools_util = { path = "rustc_tools_util", version = "0.4.2" }
5850

clippy_test_deps/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "clippy_test_deps"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# Add dependencies here to make them available in ui tests.
7+
8+
[dependencies]
9+
if_chain = "1.0"
10+
quote = "1.0.25"
11+
syn = { version = "2.0", features = ["full"] }
12+
futures = "0.3"
13+
parking_lot = "0.12"
14+
tokio = { version = "1", features = ["io-util"] }
15+
itertools = "0.12"

clippy_test_deps/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}

tests/compile-test.rs

Lines changed: 13 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -16,110 +16,25 @@ use test_utils::IS_RUSTC_TEST_SUITE;
1616
use ui_test::custom_flags::Flag;
1717
use ui_test::custom_flags::edition::Edition;
1818
use ui_test::custom_flags::rustfix::RustfixMode;
19+
use ui_test::dependencies::DependencyBuilder;
1920
use ui_test::spanned::Spanned;
2021
use ui_test::{Args, CommandBuilder, Config, Match, error_on_output_conflict};
2122

22-
use std::collections::{BTreeMap, HashMap};
23+
use std::collections::HashMap;
2324
use std::env::{self, set_var, var_os};
2425
use std::ffi::{OsStr, OsString};
2526
use std::fmt::Write;
2627
use std::path::{Path, PathBuf};
2728
use std::sync::mpsc::{Sender, channel};
2829
use std::{fs, iter, thread};
2930

30-
// Test dependencies may need an `extern crate` here to ensure that they show up
31-
// in the depinfo file (otherwise cargo thinks they are unused)
32-
extern crate futures;
33-
extern crate if_chain;
34-
extern crate itertools;
35-
extern crate parking_lot;
36-
extern crate quote;
37-
extern crate syn;
38-
extern crate tokio;
39-
4031
mod test_utils;
4132

42-
/// All crates used in UI tests are listed here
43-
static TEST_DEPENDENCIES: &[&str] = &[
44-
"clippy_config",
45-
"clippy_lints",
46-
"clippy_utils",
47-
"futures",
48-
"if_chain",
49-
"itertools",
50-
"parking_lot",
51-
"quote",
52-
"regex",
53-
"serde_derive",
54-
"serde",
55-
"syn",
56-
"tokio",
57-
];
58-
59-
/// Produces a string with an `--extern` flag for all UI test crate
60-
/// dependencies.
61-
///
62-
/// The dependency files are located by parsing the depinfo file for this test
63-
/// module. This assumes the `-Z binary-dep-depinfo` flag is enabled. All test
64-
/// dependencies must be added to Cargo.toml at the project root. Test
65-
/// dependencies that are not *directly* used by this test module require an
66-
/// `extern crate` declaration.
67-
fn extern_flags() -> Vec<String> {
68-
let current_exe_depinfo = {
69-
let mut path = env::current_exe().unwrap();
70-
path.set_extension("d");
71-
fs::read_to_string(path).unwrap()
72-
};
73-
let mut crates = BTreeMap::<&str, &str>::new();
74-
for line in current_exe_depinfo.lines() {
75-
// each dependency is expected to have a Makefile rule like `/path/to/crate-hash.rlib:`
76-
let parse_name_path = || {
77-
if line.starts_with(char::is_whitespace) {
78-
return None;
79-
}
80-
let path_str = line.strip_suffix(':')?;
81-
let path = Path::new(path_str);
82-
if !matches!(path.extension()?.to_str()?, "rlib" | "so" | "dylib" | "dll") {
83-
return None;
84-
}
85-
let (name, _hash) = path.file_stem()?.to_str()?.rsplit_once('-')?;
86-
// the "lib" prefix is not present for dll files
87-
let name = name.strip_prefix("lib").unwrap_or(name);
88-
Some((name, path_str))
89-
};
90-
if let Some((name, path)) = parse_name_path()
91-
&& TEST_DEPENDENCIES.contains(&name)
92-
{
93-
// A dependency may be listed twice if it is available in sysroot,
94-
// and the sysroot dependencies are listed first. As of the writing,
95-
// this only seems to apply to if_chain.
96-
crates.insert(name, path);
97-
}
98-
}
99-
let not_found: Vec<&str> = TEST_DEPENDENCIES
100-
.iter()
101-
.copied()
102-
.filter(|n| !crates.contains_key(n))
103-
.collect();
104-
assert!(
105-
not_found.is_empty(),
106-
"dependencies not found in depinfo: {not_found:?}\n\
107-
help: Make sure the `-Z binary-dep-depinfo` rust flag is enabled\n\
108-
help: Try adding to dev-dependencies in Cargo.toml\n\
109-
help: Be sure to also add `extern crate ...;` to tests/compile-test.rs",
110-
);
111-
crates
112-
.into_iter()
113-
.map(|(name, path)| format!("--extern={name}={path}"))
114-
.collect()
115-
}
116-
11733
// whether to run internal tests or not
11834
const RUN_INTERNAL_TESTS: bool = cfg!(feature = "internal");
11935

12036
struct TestContext {
12137
args: Args,
122-
extern_flags: Vec<String>,
12338
diagnostic_collector: Option<DiagnosticCollector>,
12439
collector_thread: Option<thread::JoinHandle<()>>,
12540
}
@@ -134,7 +49,6 @@ impl TestContext {
13449
.unzip();
13550
Self {
13651
args,
137-
extern_flags: extern_flags(),
13852
diagnostic_collector,
13953
collector_thread,
14054
}
@@ -158,6 +72,17 @@ impl TestContext {
15872
};
15973
let defaults = config.comment_defaults.base();
16074
defaults.set_custom("edition", Edition("2024".into()));
75+
defaults.set_custom(
76+
"dependencies",
77+
DependencyBuilder {
78+
program: CommandBuilder {
79+
..CommandBuilder::cargo()
80+
},
81+
crate_manifest_path: Path::new("clippy_test_deps").join("Cargo.toml"),
82+
build_std: None,
83+
bless_lockfile: self.args.bless,
84+
},
85+
);
16186
defaults.exit_status = None.into();
16287
if mandatory_annotations {
16388
defaults.require_annotations = Some(Spanned::dummy(true)).into();
@@ -182,12 +107,10 @@ impl TestContext {
182107
"-Zui-testing",
183108
"-Zdeduplicate-diagnostics=no",
184109
"-Dwarnings",
185-
&format!("-Ldependency={}", deps_path.display()),
186110
]
187111
.map(OsString::from),
188112
);
189113

190-
config.program.args.extend(self.extern_flags.iter().map(OsString::from));
191114
// Prevent rustc from creating `rustc-ice-*` files the console output is enough.
192115
config.program.envs.push(("RUSTC_ICE".into(), Some("0".into())));
193116

0 commit comments

Comments
 (0)