Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for gen #913

Merged
merged 3 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions gen/cmd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ miette = { version="4.3", features=["fancy"]}
[dev-dependencies]
assert_cmd = "1.0.3"
tempdir = "0.3.7"
autocxx-integration-tests = { path = "../../integration-tests", version="0.1" }
# This is necessary for building the projects created
# by the trybuild test system...
autocxx = { path="../.." }
cxx = "1.0.54"
36 changes: 34 additions & 2 deletions gen/cmd/tests/cmd_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@
use std::{convert::TryInto, fs::File, io::Write, path::Path};

use assert_cmd::Command;
use autocxx_integration_tests::build_from_folder;
use tempdir::TempDir;

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

const KEEP_TEMPDIRS: bool = false;

#[test]
fn test_help() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("autocxx-gen")?;
Expand Down Expand Up @@ -64,7 +70,21 @@ where
#[test]
fn test_gen() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = TempDir::new("example")?;
base_test(&tmp_dir, |_| {})
base_test(&tmp_dir, |_| {})?;
File::create(tmp_dir.path().join("cxx.h"))
.and_then(|mut cxx_h| cxx_h.write_all(autocxx_engine::HEADER.as_bytes()))?;
std::env::set_var("OUT_DIR", tmp_dir.path().to_str().unwrap());
let r = build_from_folder(
tmp_dir.path(),
&tmp_dir.path().join("demo/main.rs"),
vec![tmp_dir.path().join("autocxx-ffi-default-gen.rs")],
&["gen0.cc"],
);
if KEEP_TEMPDIRS {
println!("Tempdir: {:?}", tmp_dir.into_path().to_str());
}
r.unwrap();
Ok(())
}

#[test]
Expand Down Expand Up @@ -104,6 +124,18 @@ fn test_gen_fixed_num() -> Result<(), Box<dyn std::error::Error>> {
assert_not_contentful(&tmp_dir, "gen2.h");
assert_contentful(&tmp_dir, "cxxgen.h");
assert_contentful(&tmp_dir, "gen.complete.rs");
File::create(tmp_dir.path().join("cxx.h"))
.and_then(|mut cxx_h| cxx_h.write_all(autocxx_engine::HEADER.as_bytes()))?;
let r = build_from_folder(
tmp_dir.path(),
&tmp_dir.path().join("gen.complete.rs"),
vec![],
&["gen0.cc"],
);
if KEEP_TEMPDIRS {
println!("Tempdir: {:?}", tmp_dir.into_path().to_str());
}
r.unwrap();
Ok(())
}

Expand Down
58 changes: 48 additions & 10 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use std::{
sync::Mutex,
};

use autocxx_engine::{Builder, BuilderContext, BuilderError, RebuildDependencyRecorder, HEADER};
use autocxx_engine::{
Builder, BuilderBuild, BuilderContext, BuilderError, RebuildDependencyRecorder, HEADER,
};
use log::info;
use once_cell::sync::OnceCell;
use proc_macro2::{Span, TokenStream};
Expand All @@ -38,6 +40,50 @@ pub fn doctest(
do_run_test_manual(cxx_code, header_code, rust_code, None, None)
}

fn configure_builder(b: &mut BuilderBuild) -> &mut BuilderBuild {
let target = rust_info::get().target_triple.unwrap();
b.host(&target)
.target(&target)
.opt_level(1)
.flag("-std=c++14") // For clang
.flag_if_supported("/GX") // Enable C++ exceptions for msvc
}

/// API to test building pre-generated files.
pub fn build_from_folder(
folder: &Path,
main_rs_file: &Path,
generated_rs_files: Vec<PathBuf>,
cpp_files: &[&str],
) -> Result<(), TestError> {
let target_dir = folder.join("target");
std::fs::create_dir(&target_dir).unwrap();
let mut b = BuilderBuild::new();
for cpp_file in cpp_files.iter() {
b.file(folder.join(cpp_file));
}
configure_builder(&mut b)
.out_dir(&target_dir)
.include(folder)
.include(folder.join("demo"))
.try_compile("autocxx-demo")
.map_err(TestError::CppBuild)?;
// use the trybuild crate to build the Rust file.
let r = get_builder().lock().unwrap().build(
&target_dir,
"autocxx-demo",
&folder,
&["input.h", "cxx.h"],
&main_rs_file,
generated_rs_files,
);
if r.is_err() {
return Err(TestError::RsBuild); // details of Rust panic are a bit messy to include, and
// not important at the moment.
}
Ok(())
}

fn get_builder() -> &'static Mutex<LinkableTryBuilder> {
static INSTANCE: OnceCell<Mutex<LinkableTryBuilder>> = OnceCell::new();
INSTANCE.get_or_init(|| Mutex::new(LinkableTryBuilder::new()))
Expand Down Expand Up @@ -375,8 +421,6 @@ pub fn do_run_test_manual(
}
}

let target = rust_info::get().target_triple.unwrap();

if !cxx_code.is_empty() {
// Step 4: Write the C++ code snippet to a .cc file, along with a #include
// of the header emitted in step 5.
Expand All @@ -385,13 +429,7 @@ pub fn do_run_test_manual(
b.file(cxx_path);
}

let b = b
.out_dir(&target_dir)
.host(&target)
.target(&target)
.opt_level(1)
.flag("-std=c++14") // For clang
.flag_if_supported("/GX"); // Enable C++ exceptions for msvc
let b = configure_builder(&mut b).out_dir(&target_dir);
let b = if let Some(builder_modifier) = builder_modifier {
builder_modifier.modify_cc_builder(b)
} else {
Expand Down