Skip to content

Commit

Permalink
[proptest] Fix WebAssembly support
Browse files Browse the repository at this point in the history
Hides a few paths, that fail at runtime on wasm32-unknown-unknown, under conditional compilation.

Fixes #137.
  • Loading branch information
RReverser authored and matthew-russo committed Dec 8, 2024
1 parent d179a50 commit 426a9f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 58 deletions.
44 changes: 4 additions & 40 deletions proptest/src/test_runner/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,21 @@
use crate::std_facade::Box;
use core::u32;

#[cfg(feature = "std")]
use std::env;
#[cfg(feature = "std")]
use std::ffi::OsString;
#[cfg(feature = "std")]
use std::fmt;
#[cfg(feature = "std")]
use std::str::FromStr;

use crate::test_runner::result_cache::{noop_result_cache, ResultCache};
use crate::test_runner::rng::RngAlgorithm;
use crate::test_runner::FailurePersistence;
#[cfg(feature = "std")]
use crate::test_runner::FileFailurePersistence;

#[cfg(feature = "std")]
const CASES: &str = "PROPTEST_CASES";
#[cfg(feature = "std")]
const MAX_LOCAL_REJECTS: &str = "PROPTEST_MAX_LOCAL_REJECTS";
#[cfg(feature = "std")]
const MAX_GLOBAL_REJECTS: &str = "PROPTEST_MAX_GLOBAL_REJECTS";
#[cfg(feature = "std")]
const MAX_FLAT_MAP_REGENS: &str = "PROPTEST_MAX_FLAT_MAP_REGENS";
#[cfg(feature = "std")]
const MAX_SHRINK_TIME: &str = "PROPTEST_MAX_SHRINK_TIME";
#[cfg(feature = "std")]
const MAX_SHRINK_ITERS: &str = "PROPTEST_MAX_SHRINK_ITERS";
#[cfg(feature = "std")]
const MAX_DEFAULT_SIZE_RANGE: &str = "PROPTEST_MAX_DEFAULT_SIZE_RANGE";
#[cfg(feature = "fork")]
const FORK: &str = "PROPTEST_FORK";
#[cfg(feature = "timeout")]
const TIMEOUT: &str = "PROPTEST_TIMEOUT";
#[cfg(feature = "std")]
const VERBOSE: &str = "PROPTEST_VERBOSE";
#[cfg(feature = "std")]
const RNG_ALGORITHM: &str = "PROPTEST_RNG_ALGORITHM";
#[cfg(feature = "std")]
const DISABLE_FAILURE_PERSISTENCE: &str =
"PROPTEST_DISABLE_FAILURE_PERSISTENCE";

/// Override the config fields from environment variables, if any are set.
/// Without the `std` feature this function returns config unchanged.
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_arch = "wasm32")))]
pub fn contextualize_config(mut result: Config) -> Config {
use std::env;
use std::ffi::OsString;
use std::fmt;
use std::str::FromStr;

const CASES: &str = "PROPTEST_CASES";

const MAX_LOCAL_REJECTS: &str = "PROPTEST_MAX_LOCAL_REJECTS";
const MAX_GLOBAL_REJECTS: &str = "PROPTEST_MAX_GLOBAL_REJECTS";
const MAX_FLAT_MAP_REGENS: &str = "PROPTEST_MAX_FLAT_MAP_REGENS";
Expand Down Expand Up @@ -182,7 +146,7 @@ pub fn contextualize_config(mut result: Config) -> Config {
}

/// Without the `std` feature this function returns config unchanged.
#[cfg(not(feature = "std"))]
#[cfg(not(all(feature = "std", not(target_arch = "wasm32"))))]
pub fn contextualize_config(result: Config) -> Config {
result
}
Expand Down Expand Up @@ -218,7 +182,7 @@ fn default_default_config() -> Config {
lazy_static! {
static ref DEFAULT_CONFIG: Config = {
let mut default_config = default_default_config();
default_config.failure_persistence = Some(Box::new(FileFailurePersistence::default()));
default_config.failure_persistence = Some(Box::new(crate::test_runner::FileFailurePersistence::default()));
contextualize_config(default_config)
};
}
Expand Down
29 changes: 11 additions & 18 deletions proptest/src/test_runner/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,7 @@ where
F: Fn(V) -> TestCaseResult,
R: Iterator<Item = TestCaseResult>,
{
use std::time;

#[cfg(feature = "timeout")]
let timeout = runner.config.timeout();

if let Some(result) = replay_from_fork.next() {
Expand All @@ -250,7 +249,8 @@ where
return result.clone().map(|_| TestCaseOk::CacheHitSuccess);
}

let time_start = time::Instant::now();
#[cfg(feature = "timeout")]
let time_start = std::time::Instant::now();

let mut result = unwrap_or!(
panic::catch_unwind(AssertUnwindSafe(|| test(case))),
Expand All @@ -263,6 +263,7 @@ where
// If there is a timeout and we exceeded it, fail the test here so we get
// consistent behaviour. (The parent process cannot precisely time the test
// cases itself.)
#[cfg(feature = "timeout")]
if timeout > 0 && result.is_ok() {
let elapsed = time_start.elapsed();
let elapsed_millis = elapsed.as_secs() as u32 * 1000
Expand Down Expand Up @@ -772,35 +773,27 @@ impl TestRunner {
return None
}

#[cfg(feature = "std")]
use std::time;

#[cfg(all(feature = "std", not(target_arch = "wasm32")))]
let start_time = std::time::Instant::now();
let mut last_failure = None;
let mut iterations = 0;
#[cfg(feature = "std")]
let start_time = time::Instant::now();

verbose_message!(self, TRACE, "Starting shrinking");

if case.simplify() {
loop {
#[cfg(feature = "std")]
let timed_out = if self.config.max_shrink_time > 0 {
let mut timed_out: Option<u64> = None;
#[cfg(all(feature = "std", not(target_arch = "wasm32")))]
if self.config.max_shrink_time > 0 {
let elapsed = start_time.elapsed();
let elapsed_ms = elapsed
.as_secs()
.saturating_mul(1000)
.saturating_add(elapsed.subsec_millis().into());
if elapsed_ms > self.config.max_shrink_time as u64 {
Some(elapsed_ms)
} else {
None
timed_out = Some(elapsed_ms);
}
} else {
None
};
#[cfg(not(feature = "std"))]
let timed_out: Option<u64> = None;
}

let bail = if iterations >= self.config.max_shrink_iters() {
#[cfg(feature = "std")]
Expand Down

0 comments on commit 426a9f0

Please sign in to comment.