Don’t rely on OPT_LEVEL == 0 as indicator…

for whether test helpers are necessary, because otherwise `cargo test
--release` won’t work.
This commit is contained in:
Simonas Kazlauskas
2017-12-07 21:24:03 +02:00
parent b155d1e187
commit 77c7b40ba7
3 changed files with 66 additions and 67 deletions
-23
View File
@@ -20,27 +20,4 @@ fn main(){
::std::process::exit(0xfc);
}
}
maybe_test_helpers();
}
fn maybe_test_helpers() {
if env::var("OPT_LEVEL").ok().and_then(|v| v.parse().ok()).unwrap_or(0u64) != 0 {
// certainly not for testing, just skip.
return;
}
let mut outpath = if let Some(od) = env::var_os("OUT_DIR") { od } else { return };
let target = if let Some(t) = env::var_os("TARGET") { t } else { return };
let rustc = env::var_os("RUSTC").unwrap_or_else(|| { "rustc".into() });
outpath.push("/libtest_helpers.dll"); // extension for windows required, POSIX does not care.
let _ = ::std::process::Command::new(rustc)
.arg("src/test_helpers.rs")
.arg("-o")
.arg(outpath)
.arg("-O")
.arg("--target")
.arg(target)
.output();
// Ignore the failures here. We do not want failures of this thing to inhibit people from
// building and using the library. Might make it hard to debug why tests fail in case this
// library does not get built, though.
}
+66
View File
@@ -3,8 +3,25 @@ use libloading::{Symbol, Library};
const LIBPATH: &'static str = concat!(env!("OUT_DIR"), "/libtest_helpers.dll");
fn make_helpers() {
static ONCE: ::std::sync::Once = ::std::sync::ONCE_INIT;
ONCE.call_once(|| {
let mut outpath = String::from(if let Some(od) = option_env!("OUT_DIR") { od } else { return });
let rustc = option_env!("RUSTC").unwrap_or_else(|| { "rustc".into() });
outpath.push_str(&"/libtest_helpers.dll"); // extension for windows required, POSIX does not care.
let _ = ::std::process::Command::new(rustc)
.arg("src/test_helpers.rs")
.arg("-o")
.arg(outpath)
.arg("-O")
.output()
.expect("could not compile the test helpers!");
});
}
#[test]
fn test_id_u32() {
make_helpers();
let lib = Library::new(LIBPATH).unwrap();
unsafe {
let f: Symbol<unsafe extern fn(u32) -> u32> = lib.get(b"test_identity_u32\0").unwrap();
@@ -23,6 +40,7 @@ struct S {
#[test]
fn test_id_struct() {
make_helpers();
let lib = Library::new(LIBPATH).unwrap();
unsafe {
let f: Symbol<unsafe extern fn(S) -> S> = lib.get(b"test_identity_struct\0").unwrap();
@@ -32,6 +50,7 @@ fn test_id_struct() {
#[test]
fn test_0_no_0() {
make_helpers();
let lib = Library::new(LIBPATH).unwrap();
unsafe {
let f: Symbol<unsafe extern fn(S) -> S> = lib.get(b"test_identity_struct\0").unwrap();
@@ -47,6 +66,7 @@ fn wrong_name_fails() {
#[test]
fn missing_symbol_fails() {
make_helpers();
let lib = Library::new(LIBPATH).unwrap();
unsafe {
lib.get::<*mut ()>(b"test_does_not_exist").err().unwrap();
@@ -56,6 +76,7 @@ fn missing_symbol_fails() {
#[test]
fn interior_null_fails() {
make_helpers();
let lib = Library::new(LIBPATH).unwrap();
unsafe {
lib.get::<*mut ()>(b"test_does\0_not_exist").err().unwrap();
@@ -66,6 +87,7 @@ fn interior_null_fails() {
#[test]
#[should_panic]
fn test_incompatible_type() {
make_helpers();
let lib = Library::new(LIBPATH).unwrap();
unsafe {
let _ = lib.get::<()>(b"test_identity_u32\0");
@@ -75,6 +97,7 @@ fn test_incompatible_type() {
#[test]
#[should_panic]
fn test_incompatible_type_named_fn() {
make_helpers();
unsafe fn get<'a, T>(l: &'a Library, _: T) -> libloading::Result<Symbol<'a, T>> {
l.get::<T>(b"test_identity_u32\0")
}
@@ -83,3 +106,46 @@ fn test_incompatible_type_named_fn() {
let _ = get(&lib, test_incompatible_type_named_fn);
}
}
#[test]
fn test_static_u32() {
make_helpers();
let lib = Library::new(LIBPATH).unwrap();
unsafe {
let var: Symbol<*mut u32> = lib.get(b"TEST_STATIC_U32\0").unwrap();
**var = 42;
let help: Symbol<unsafe extern fn() -> u32> = lib.get(b"test_get_static_u32\0").unwrap();
assert_eq!(42, help());
}
}
#[test]
fn test_static_ptr() {
make_helpers();
let lib = Library::new(LIBPATH).unwrap();
unsafe {
let var: Symbol<*mut *mut ()> = lib.get(b"TEST_STATIC_PTR\0").unwrap();
**var = *var as *mut _;
let works: Symbol<unsafe extern fn() -> bool> =
lib.get(b"test_check_static_ptr\0").unwrap();
assert!(works());
}
}
#[cfg(any(windows, target_os="linux"))]
#[cfg(test_nightly)]
#[test]
fn test_tls_static() {
make_helpers();
let lib = Library::new(LIBPATH).unwrap();
unsafe {
let var: Symbol<*mut u32> = lib.get(b"TEST_THREAD_LOCAL\0").unwrap();
**var = 84;
let help: Symbol<unsafe extern fn() -> u32> = lib.get(b"test_get_thread_local\0").unwrap();
assert_eq!(84, help());
}
::std::thread::spawn(move || unsafe {
let help: Symbol<unsafe extern fn() -> u32> = lib.get(b"test_get_thread_local\0").unwrap();
assert_eq!(0, help());
}).join().unwrap();
}
-44
View File
@@ -1,44 +0,0 @@
extern crate libloading;
use libloading::{Symbol, Library};
const LIBPATH: &'static str = concat!(env!("OUT_DIR"), "/libtest_helpers.dll");
#[test]
fn test_static_u32() {
let lib = Library::new(LIBPATH).unwrap();
unsafe {
let var: Symbol<*mut u32> = lib.get(b"TEST_STATIC_U32\0").unwrap();
**var = 42;
let help: Symbol<unsafe extern fn() -> u32> = lib.get(b"test_get_static_u32\0").unwrap();
assert_eq!(42, help());
}
}
#[test]
fn test_static_ptr() {
let lib = Library::new(LIBPATH).unwrap();
unsafe {
let var: Symbol<*mut *mut ()> = lib.get(b"TEST_STATIC_PTR\0").unwrap();
**var = *var as *mut _;
let works: Symbol<unsafe extern fn() -> bool> =
lib.get(b"test_check_static_ptr\0").unwrap();
assert!(works());
}
}
#[cfg(any(windows, target_os="linux"))]
#[cfg(test_nightly)]
#[test]
fn test_tls_static() {
let lib = Library::new(LIBPATH).unwrap();
unsafe {
let var: Symbol<*mut u32> = lib.get(b"TEST_THREAD_LOCAL\0").unwrap();
**var = 84;
let help: Symbol<unsafe extern fn() -> u32> = lib.get(b"test_get_thread_local\0").unwrap();
assert_eq!(84, help());
}
::std::thread::spawn(move || unsafe {
let help: Symbol<unsafe extern fn() -> u32> = lib.get(b"test_get_thread_local\0").unwrap();
assert_eq!(0, help());
}).join().unwrap();
}