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

Detect path of the test helper module #28

Merged
merged 1 commit into from
Feb 26, 2020
Merged
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
28 changes: 23 additions & 5 deletions src/testsupport.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#![cfg(feature = "test-support")]
use std::env;
use std::process::Command;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::atomic::{AtomicBool, AtomicPtr, Ordering};

use crate::core::mark_initialized;

static TEST_MODE: AtomicBool = AtomicBool::new(false);
static TEST_MODULE: AtomicPtr<String> = AtomicPtr::new(std::ptr::null_mut());

// we need this.
pub use ctor::ctor;
Expand All @@ -27,7 +28,9 @@ macro_rules! enable_test_support {
#[$crate::testsupport::ctor]
#[used]
fn __procspawn_test_support_init() {
$crate::testsupport::enable();
// strip the crate name from the module path
let module_path = std::module_path!().splitn(2, "::").nth(1);
$crate::testsupport::enable(module_path);
}

#[test]
Expand All @@ -37,8 +40,16 @@ macro_rules! enable_test_support {
};
}

pub fn enable() {
TEST_MODE.store(true, Ordering::SeqCst);
pub fn enable(module: Option<&str>) {
if TEST_MODE.swap(true, Ordering::SeqCst) {
panic!("procspawn testmode can only be enabled once");
}

if let Some(module) = module {
let ptr = Box::into_raw(Box::new(module.to_string()));
TEST_MODULE.store(ptr, Ordering::SeqCst);
}

mark_initialized();
}

Expand All @@ -47,9 +58,16 @@ pub struct TestMode {
pub should_silence_stdout: bool,
}

fn test_helper_path() -> String {
match unsafe { TEST_MODULE.load(Ordering::SeqCst).as_ref() } {
Some(module) => format!("{}::procspawn_test_helper", module),
None => "procspawn_test_helper".to_string(),
}
}

pub fn update_command_for_tests(cmd: &mut Command) -> Option<TestMode> {
if TEST_MODE.load(Ordering::SeqCst) {
cmd.arg("procspawn_test_helper");
cmd.arg(test_helper_path());
cmd.arg("--exact");
cmd.arg("--test-threads=1");
cmd.arg("-q");
Expand Down