Skip to content

Commit c3b6733

Browse files
committed
Make compile tests much more robust
1 parent 6a17afe commit c3b6733

File tree

2 files changed

+65
-57
lines changed

2 files changed

+65
-57
lines changed

compile-tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ repository = "https://github.com/madsmtm/objc2"
88
license = "MIT"
99

1010
[dependencies]
11-
objc2 = { path = "../objc2" }
1211
compiletest_rs = "0.7"
12+
cargo_metadata = "0.14"

compile-tests/src/main.rs

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,86 @@
1+
use cargo_metadata::camino::Utf8PathBuf;
2+
use cargo_metadata::Message;
13
use std::env;
24
use std::io;
3-
use std::path::{Path, PathBuf};
5+
use std::path::Path;
6+
use std::process::Command;
7+
use std::process::Stdio;
48

59
use compiletest_rs::{common::Mode, run_tests, Config};
610

7-
// Environment variables created in build script
8-
const TARGET: &'static str = env!("BUILD_TARGET");
9-
const PROFILE: &'static str = env!("BUILD_PROFILE");
10-
11-
fn find_dependency(deps_dir: &Path, dep: &str) -> io::Result<PathBuf> {
12-
// Find lib[dep]-XYZ.rlib
13-
// Yes. This is ugly.
14-
let mut rlib = None;
15-
for dir in deps_dir.read_dir()? {
16-
let path = dir?.path();
17-
if path
18-
.file_name()
19-
.unwrap()
20-
.to_str()
21-
.unwrap()
22-
.starts_with(&format!("lib{}-", dep))
23-
&& path.extension().map_or(false, |ext| ext == "rlib")
24-
{
25-
if let Some(rlib) = rlib {
26-
panic!("Found multiple rlibs: {:?}, {:?}", rlib, path);
27-
} else {
28-
rlib = Some(path);
29-
}
30-
}
31-
}
32-
Ok(rlib.expect("Found no rlib"))
11+
fn get_rlib<'a>(filenames: impl IntoIterator<Item = &'a Utf8PathBuf>) -> &'a Utf8PathBuf {
12+
filenames
13+
.into_iter()
14+
.find(|name| name.extension() == Some("rlib"))
15+
.expect("An rlib")
3316
}
3417

35-
fn run(src: &'static str, mode: Mode) {
36-
let mut config = Config::default();
37-
38-
// ../target
39-
let target_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
40-
.parent()
41-
.unwrap()
42-
.join("target");
18+
fn main() -> io::Result<()> {
19+
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
4320

44-
// [/TARGET]/debug/deps
45-
let deps_dir = if target_dir.join(TARGET).exists() {
46-
target_dir.join(TARGET)
47-
} else {
48-
target_dir
49-
}
50-
.join(PROFILE)
51-
.join("deps");
21+
// Build objc2
22+
let result = Command::new("cargo")
23+
.current_dir(&manifest_dir.parent().unwrap())
24+
.arg("build")
25+
.arg("-pobjc2")
26+
.arg("--message-format=json-render-diagnostics")
27+
.args(std::env::args().skip(1))
28+
.stdout(Stdio::piped())
29+
.output()?;
5230

53-
let dir = find_dependency(&deps_dir, "objc2").unwrap();
31+
// Extract metadata from build
32+
let artifacts: Vec<_> = cargo_metadata::Message::parse_stream(&*result.stdout)
33+
.filter_map(|message| {
34+
if let Message::CompilerArtifact(artifact) = message.unwrap() {
35+
if artifact.target.kind == ["lib"] && !artifact.profile.test {
36+
return Some(artifact);
37+
}
38+
}
39+
None
40+
})
41+
.collect();
42+
let dep_dir = get_rlib(&artifacts[0].filenames).parent().unwrap();
43+
let flags = artifacts
44+
.iter()
45+
.map(|artifact| {
46+
format!(
47+
" --extern {name}={rlib}",
48+
name = artifact.target.name,
49+
rlib = get_rlib(&artifact.filenames),
50+
)
51+
})
52+
.collect::<String>();
5453

55-
config.mode = mode;
56-
config.src_base = PathBuf::from(format!("{}/{}", env!("CARGO_MANIFEST_DIR"), src));
54+
let mut config = Config::default();
5755
config.target_rustcflags = Some(format!(
58-
"-L dependency={} --extern objc2={}",
59-
deps_dir.display(),
60-
dir.display()
56+
"-L dependency={dep}{flags}",
57+
dep = dep_dir,
58+
flags = flags,
6159
));
6260
config.llvm_filecheck = Some(
6361
env::var("FILECHECK")
6462
.unwrap_or("FileCheck".to_string())
6563
.into(),
6664
);
6765
config.edition = Some("2018".into());
68-
config.verbose = true;
66+
config.verbose = matches!(
67+
std::env::var("CARGO_TERM_VERBOSE").as_deref(),
68+
Ok("true" | "1")
69+
) || std::env::args().any(|val| val == "--verbose" || val == "-v");
6970

71+
// Run UI tests
72+
config.src_base = manifest_dir.join("ui");
73+
config.mode = Mode::Ui;
74+
run_tests(&config);
75+
config.mode = Mode::CompileFail;
76+
run_tests(&config);
77+
78+
// Run Codegen tests
79+
config.src_base = manifest_dir.join("codegen");
80+
config.mode = Mode::RunPass;
81+
run_tests(&config);
82+
config.mode = Mode::Codegen;
7083
run_tests(&config);
71-
}
7284

73-
fn main() {
74-
run("ui", Mode::Ui);
75-
run("ui", Mode::CompileFail);
76-
run("codegen", Mode::Codegen);
77-
run("codegen", Mode::RunPass);
85+
Ok(())
7886
}

0 commit comments

Comments
 (0)