Skip to content

Commit a6c59fe

Browse files
authored
Merge pull request #913 from bsilver8192/gen-tests
Add tests for `gen`
2 parents 3bc76f5 + 9af5e84 commit a6c59fe

File tree

4 files changed

+90
-12
lines changed

4 files changed

+90
-12
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/cmd/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ miette = { version="4.3", features=["fancy"]}
3232
[dev-dependencies]
3333
assert_cmd = "1.0.3"
3434
tempdir = "0.3.7"
35+
autocxx-integration-tests = { path = "../../integration-tests", version="0.1" }
36+
# This is necessary for building the projects created
37+
# by the trybuild test system...
38+
autocxx = { path="../.." }
39+
cxx = "1.0.54"

gen/cmd/tests/cmd_test.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@
99
use std::{convert::TryInto, fs::File, io::Write, path::Path};
1010

1111
use assert_cmd::Command;
12+
use autocxx_integration_tests::build_from_folder;
1213
use tempdir::TempDir;
1314

14-
static MAIN_RS: &str = include_str!("../../../demo/src/main.rs");
15+
static MAIN_RS: &str = concat!(
16+
include_str!("../../../demo/src/main.rs"),
17+
"#[link(name = \"autocxx-demo\")]\nextern \"C\" {}"
18+
);
1519
static INPUT_H: &str = include_str!("../../../demo/src/input.h");
1620
static BLANK: &str = "// Blank autocxx placeholder";
1721

22+
const KEEP_TEMPDIRS: bool = false;
23+
1824
#[test]
1925
fn test_help() -> Result<(), Box<dyn std::error::Error>> {
2026
let mut cmd = Command::cargo_bin("autocxx-gen")?;
@@ -64,7 +70,21 @@ where
6470
#[test]
6571
fn test_gen() -> Result<(), Box<dyn std::error::Error>> {
6672
let tmp_dir = TempDir::new("example")?;
67-
base_test(&tmp_dir, |_| {})
73+
base_test(&tmp_dir, |_| {})?;
74+
File::create(tmp_dir.path().join("cxx.h"))
75+
.and_then(|mut cxx_h| cxx_h.write_all(autocxx_engine::HEADER.as_bytes()))?;
76+
std::env::set_var("OUT_DIR", tmp_dir.path().to_str().unwrap());
77+
let r = build_from_folder(
78+
tmp_dir.path(),
79+
&tmp_dir.path().join("demo/main.rs"),
80+
vec![tmp_dir.path().join("autocxx-ffi-default-gen.rs")],
81+
&["gen0.cc"],
82+
);
83+
if KEEP_TEMPDIRS {
84+
println!("Tempdir: {:?}", tmp_dir.into_path().to_str());
85+
}
86+
r.unwrap();
87+
Ok(())
6888
}
6989

7090
#[test]
@@ -104,6 +124,18 @@ fn test_gen_fixed_num() -> Result<(), Box<dyn std::error::Error>> {
104124
assert_not_contentful(&tmp_dir, "gen2.h");
105125
assert_contentful(&tmp_dir, "cxxgen.h");
106126
assert_contentful(&tmp_dir, "gen.complete.rs");
127+
File::create(tmp_dir.path().join("cxx.h"))
128+
.and_then(|mut cxx_h| cxx_h.write_all(autocxx_engine::HEADER.as_bytes()))?;
129+
let r = build_from_folder(
130+
tmp_dir.path(),
131+
&tmp_dir.path().join("gen.complete.rs"),
132+
vec![],
133+
&["gen0.cc"],
134+
);
135+
if KEEP_TEMPDIRS {
136+
println!("Tempdir: {:?}", tmp_dir.into_path().to_str());
137+
}
138+
r.unwrap();
107139
Ok(())
108140
}
109141

integration-tests/src/lib.rs

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use std::{
1515
sync::Mutex,
1616
};
1717

18-
use autocxx_engine::{Builder, BuilderContext, BuilderError, RebuildDependencyRecorder, HEADER};
18+
use autocxx_engine::{
19+
Builder, BuilderBuild, BuilderContext, BuilderError, RebuildDependencyRecorder, HEADER,
20+
};
1921
use log::info;
2022
use once_cell::sync::OnceCell;
2123
use proc_macro2::{Span, TokenStream};
@@ -38,6 +40,50 @@ pub fn doctest(
3840
do_run_test_manual(cxx_code, header_code, rust_code, None, None)
3941
}
4042

43+
fn configure_builder(b: &mut BuilderBuild) -> &mut BuilderBuild {
44+
let target = rust_info::get().target_triple.unwrap();
45+
b.host(&target)
46+
.target(&target)
47+
.opt_level(1)
48+
.flag("-std=c++14") // For clang
49+
.flag_if_supported("/GX") // Enable C++ exceptions for msvc
50+
}
51+
52+
/// API to test building pre-generated files.
53+
pub fn build_from_folder(
54+
folder: &Path,
55+
main_rs_file: &Path,
56+
generated_rs_files: Vec<PathBuf>,
57+
cpp_files: &[&str],
58+
) -> Result<(), TestError> {
59+
let target_dir = folder.join("target");
60+
std::fs::create_dir(&target_dir).unwrap();
61+
let mut b = BuilderBuild::new();
62+
for cpp_file in cpp_files.iter() {
63+
b.file(folder.join(cpp_file));
64+
}
65+
configure_builder(&mut b)
66+
.out_dir(&target_dir)
67+
.include(folder)
68+
.include(folder.join("demo"))
69+
.try_compile("autocxx-demo")
70+
.map_err(TestError::CppBuild)?;
71+
// use the trybuild crate to build the Rust file.
72+
let r = get_builder().lock().unwrap().build(
73+
&target_dir,
74+
"autocxx-demo",
75+
&folder,
76+
&["input.h", "cxx.h"],
77+
&main_rs_file,
78+
generated_rs_files,
79+
);
80+
if r.is_err() {
81+
return Err(TestError::RsBuild); // details of Rust panic are a bit messy to include, and
82+
// not important at the moment.
83+
}
84+
Ok(())
85+
}
86+
4187
fn get_builder() -> &'static Mutex<LinkableTryBuilder> {
4288
static INSTANCE: OnceCell<Mutex<LinkableTryBuilder>> = OnceCell::new();
4389
INSTANCE.get_or_init(|| Mutex::new(LinkableTryBuilder::new()))
@@ -375,8 +421,6 @@ pub fn do_run_test_manual(
375421
}
376422
}
377423

378-
let target = rust_info::get().target_triple.unwrap();
379-
380424
if !cxx_code.is_empty() {
381425
// Step 4: Write the C++ code snippet to a .cc file, along with a #include
382426
// of the header emitted in step 5.
@@ -385,13 +429,7 @@ pub fn do_run_test_manual(
385429
b.file(cxx_path);
386430
}
387431

388-
let b = b
389-
.out_dir(&target_dir)
390-
.host(&target)
391-
.target(&target)
392-
.opt_level(1)
393-
.flag("-std=c++14") // For clang
394-
.flag_if_supported("/GX"); // Enable C++ exceptions for msvc
432+
let b = configure_builder(&mut b).out_dir(&target_dir);
395433
let b = if let Some(builder_modifier) = builder_modifier {
396434
builder_modifier.modify_cc_builder(b)
397435
} else {

0 commit comments

Comments
 (0)