Skip to content

Commit 50fcf47

Browse files
committed
Correctly test integration tests on the target and fix proc macro aux builds
1 parent fc8cea3 commit 50fcf47

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

src/lib.rs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,44 @@ pub fn default_any_file_filter(path: &Path, config: &Config) -> bool {
170170

171171
/// The default per-file config used by `run_tests`.
172172
pub fn default_per_file_config(config: &mut Config, _path: &Path, file_contents: &[u8]) {
173-
// Heuristic:
174-
// * if the file contains `#[test]`, automatically pass `--cfg test`.
175-
// * if the file does not contain `fn main()` or `#[start]`, automatically pass `--crate-type=lib`.
176-
// This avoids having to spam `fn main() {}` in almost every test.
173+
config.program.args.push(
174+
match crate_type(file_contents) {
175+
CrateType::ProcMacro => "--crate-type=proc-macro",
176+
CrateType::Test => "--test",
177+
CrateType::Bin => return,
178+
CrateType::Lib => "--crate-type=lib",
179+
}
180+
.into(),
181+
)
182+
}
183+
184+
/// The kind of crate we're building here. Corresponds to `--crate-type` flags of rustc
185+
pub enum CrateType {
186+
/// A proc macro
187+
ProcMacro,
188+
/// A file containing unit tests
189+
Test,
190+
/// A binary file containing a main function or start function
191+
Bin,
192+
/// A library crate
193+
Lib,
194+
}
195+
196+
/// Heuristic:
197+
/// * if the file contains `#[test]`, automatically pass `--cfg test`.
198+
/// * if the file does not contain `fn main()` or `#[start]`, automatically pass `--crate-type=lib`.
199+
/// This avoids having to spam `fn main() {}` in almost every test.
200+
pub fn crate_type(file_contents: &[u8]) -> CrateType {
177201
if file_contents.find(b"#[proc_macro").is_some() {
178-
config.program.args.push("--crate-type=proc-macro".into())
202+
CrateType::ProcMacro
179203
} else if file_contents.find(b"#[test]").is_some() {
180-
config.program.args.push("--test".into());
204+
CrateType::Test
181205
} else if file_contents.find(b"fn main()").is_none()
182206
&& file_contents.find(b"#[start]").is_none()
183207
{
184-
config.program.args.push("--crate-type=lib".into());
208+
CrateType::Lib
209+
} else {
210+
CrateType::Bin
185211
}
186212
}
187213

@@ -583,6 +609,12 @@ fn build_aux(
583609

584610
default_per_file_config(&mut config, aux_file, &file_contents);
585611

612+
match crate_type(&file_contents) {
613+
// Proc macros must be run on the host
614+
CrateType::ProcMacro => config.target = config.host.clone(),
615+
CrateType::Test | CrateType::Bin | CrateType::Lib => {}
616+
}
617+
586618
// Put aux builds into a separate directory per path so that multiple aux files
587619
// from different directories (but with the same file name) don't collide.
588620
let relative = strip_path_prefix(aux_file.parent().unwrap(), &config.out_dir);

tests/integration.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ fn main() -> Result<()> {
1313
let args = Args::test()?;
1414
config.with_args(&args);
1515

16-
if let Ok(target) = std::env::var("UITEST_TEST_TARGET") {
17-
config.target = Some(target);
18-
config.output_conflict_handling = OutputConflictHandling::Ignore;
19-
}
20-
2116
config.program.args = vec![
2217
"test".into(),
2318
"--color".into(),

tests/integrations/basic/tests/ui_tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ fn main() -> ui_test::color_eyre::Result<()> {
1717
config.stderr_filter(r"[^ ]*/\.?cargo/registry/.*/", "$$CARGO_REGISTRY");
1818
config.path_stderr_filter(&std::path::Path::new(path), "$DIR");
1919

20+
if let Ok(target) = std::env::var("UITEST_TEST_TARGET") {
21+
config.target = Some(target);
22+
config.output_conflict_handling = OutputConflictHandling::Ignore;
23+
}
24+
2025
// hide binaries generated for successfully passing tests
2126
let tmp_dir = tempfile::tempdir_in(path)?;
2227
let tmp_dir = tmp_dir.path();

0 commit comments

Comments
 (0)