1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
//-
// Copyright 2017, 2018, 2019 The proptest developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use crate::std_facade::Box;
use core::u32;
use crate::test_runner::result_cache::{noop_result_cache, ResultCache};
use crate::test_runner::rng::RngAlgorithm;
use crate::test_runner::FailurePersistence;
/// Override the config fields from environment variables, if any are set.
/// Without the `std` feature this function returns config unchanged.
#[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";
const MAX_SHRINK_TIME: &str = "PROPTEST_MAX_SHRINK_TIME";
const MAX_SHRINK_ITERS: &str = "PROPTEST_MAX_SHRINK_ITERS";
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";
const VERBOSE: &str = "PROPTEST_VERBOSE";
const RNG_ALGORITHM: &str = "PROPTEST_RNG_ALGORITHM";
const DISABLE_FAILURE_PERSISTENCE: &str =
"PROPTEST_DISABLE_FAILURE_PERSISTENCE";
fn parse_or_warn<T: FromStr + fmt::Display>(
src: &OsString,
dst: &mut T,
typ: &str,
var: &str,
) {
if let Some(src) = src.to_str() {
if let Ok(value) = src.parse() {
*dst = value;
} else {
eprintln!(
"proptest: The env-var {}={} can't be parsed as {}, \
using default of {}.",
var, src, typ, *dst
);
}
} else {
eprintln!(
"proptest: The env-var {} is not valid, using \
default of {}.",
var, *dst
);
}
}
for (var, value) in
env::vars_os().filter_map(|(k, v)| k.into_string().ok().map(|k| (k, v)))
{
let var = var.as_str();
#[cfg(feature = "fork")]
if var == FORK {
parse_or_warn(&value, &mut result.fork, "bool", FORK);
continue;
}
#[cfg(feature = "timeout")]
if var == TIMEOUT {
parse_or_warn(&value, &mut result.timeout, "timeout", TIMEOUT);
continue;
}
if var == CASES {
parse_or_warn(&value, &mut result.cases, "u32", CASES);
} else if var == MAX_LOCAL_REJECTS {
parse_or_warn(
&value,
&mut result.max_local_rejects,
"u32",
MAX_LOCAL_REJECTS,
);
} else if var == MAX_GLOBAL_REJECTS {
parse_or_warn(
&value,
&mut result.max_global_rejects,
"u32",
MAX_GLOBAL_REJECTS,
);
} else if var == MAX_FLAT_MAP_REGENS {
parse_or_warn(
&value,
&mut result.max_flat_map_regens,
"u32",
MAX_FLAT_MAP_REGENS,
);
} else if var == MAX_SHRINK_TIME {
parse_or_warn(
&value,
&mut result.max_shrink_time,
"u32",
MAX_SHRINK_TIME,
);
} else if var == MAX_SHRINK_ITERS {
parse_or_warn(
&value,
&mut result.max_shrink_iters,
"u32",
MAX_SHRINK_ITERS,
);
} else if var == MAX_DEFAULT_SIZE_RANGE {
parse_or_warn(
&value,
&mut result.max_default_size_range,
"usize",
MAX_DEFAULT_SIZE_RANGE,
);
} else if var == VERBOSE {
parse_or_warn(&value, &mut result.verbose, "u32", VERBOSE);
} else if var == RNG_ALGORITHM {
parse_or_warn(
&value,
&mut result.rng_algorithm,
"RngAlgorithm",
RNG_ALGORITHM,
);
} else if var == DISABLE_FAILURE_PERSISTENCE {
result.failure_persistence = None;
} else if var.starts_with("PROPTEST_") {
eprintln!("proptest: Ignoring unknown env-var {}.", var);
}
}
result
}
/// Without the `std` feature this function returns config unchanged.
#[cfg(not(all(feature = "std", not(target_arch = "wasm32"))))]
pub fn contextualize_config(result: Config) -> Config {
result
}
fn default_default_config() -> Config {
Config {
cases: 256,
max_local_rejects: 65_536,
max_global_rejects: 1024,
max_flat_map_regens: 1_000_000,
failure_persistence: None,
source_file: None,
test_name: None,
#[cfg(feature = "fork")]
fork: false,
#[cfg(feature = "timeout")]
timeout: 0,
#[cfg(feature = "std")]
max_shrink_time: 0,
max_shrink_iters: u32::MAX,
max_default_size_range: 100,
result_cache: noop_result_cache,
#[cfg(feature = "std")]
verbose: 0,
rng_algorithm: RngAlgorithm::default(),
_non_exhaustive: (),
}
}
// The default config, computed by combining environment variables and
// defaults.
#[cfg(feature = "std")]
lazy_static! {
static ref DEFAULT_CONFIG: Config = {
let mut default_config = default_default_config();
default_config.failure_persistence = Some(Box::new(crate::test_runner::FileFailurePersistence::default()));
contextualize_config(default_config)
};
}
/// Configuration for how a proptest test should be run.
#[derive(Clone, Debug, PartialEq)]
pub struct Config {
/// The number of successful test cases that must execute for the test as a
/// whole to pass.
///
/// This does not include implicitly-replayed persisted failing cases.
///
/// The default is 256, which can be overridden by setting the
/// `PROPTEST_CASES` environment variable. (The variable is only considered
/// when the `std` feature is enabled, which it is by default.)
pub cases: u32,
/// The maximum number of individual inputs that may be rejected before the
/// test as a whole aborts.
///
/// The default is 65536, which can be overridden by setting the
/// `PROPTEST_MAX_LOCAL_REJECTS` environment variable. (The variable is only
/// considered when the `std` feature is enabled, which it is by default.)
pub max_local_rejects: u32,
/// The maximum number of combined inputs that may be rejected before the
/// test as a whole aborts.
///
/// The default is 1024, which can be overridden by setting the
/// `PROPTEST_MAX_GLOBAL_REJECTS` environment variable. (The variable is
/// only considered when the `std` feature is enabled, which it is by
/// default.)
pub max_global_rejects: u32,
/// The maximum number of times all `Flatten` combinators will attempt to
/// regenerate values. This puts a limit on the worst-case exponential
/// explosion that can happen with nested `Flatten`s.
///
/// The default is 1_000_000, which can be overridden by setting the
/// `PROPTEST_MAX_FLAT_MAP_REGENS` environment variable. (The variable is
/// only considered when the `std` feature is enabled, which it is by
/// default.)
pub max_flat_map_regens: u32,
/// Indicates whether and how to persist failed test results.
///
/// When compiling with "std" feature (i.e. the standard library is available), the default
/// is `Some(Box::new(FileFailurePersistence::SourceParallel("proptest-regressions")))`.
///
/// Without the standard library, the default is `None`, and no persistence occurs.
///
/// See the docs of [`FileFailurePersistence`](enum.FileFailurePersistence.html)
/// and [`MapFailurePersistence`](struct.MapFailurePersistence.html) for more information.
///
/// You can disable failure persistence with the `PROPTEST_DISABLE_FAILURE_PERSISTENCE`
/// environment variable but its not currently possible to set the persistence file
/// with an environment variable. (The variable is
/// only considered when the `std` feature is enabled, which it is by
/// default.)
pub failure_persistence: Option<Box<dyn FailurePersistence>>,
/// File location of the current test, relevant for persistence
/// and debugging.
///
/// Note the use of `&str` rather than `Path` to be compatible with
/// `#![no_std]` use cases where `Path` is unavailable.
///
/// See the docs of [`FileFailurePersistence`](enum.FileFailurePersistence.html)
/// for more information on how it may be used for persistence.
pub source_file: Option<&'static str>,
/// The fully-qualified name of the test being run, as would be passed to
/// the test executable to run just that test.
///
/// This must be set if `fork` is `true`. Otherwise, it is unused. It is
/// automatically set by `proptest!`.
///
/// This must include the crate name at the beginning, as produced by
/// `module_path!()`.
pub test_name: Option<&'static str>,
/// If true, tests are run in a subprocess.
///
/// Forking allows proptest to work with tests which may fail by aborting
/// the process, causing a segmentation fault, etc, but can be a lot slower
/// in certain environments or when running a very large number of tests.
///
/// For forking to work correctly, both the `Strategy` and the content of
/// the test case itself must be deterministic.
///
/// This requires the "fork" feature, enabled by default.
///
/// The default is `false`, which can be overridden by setting the
/// `PROPTEST_FORK` environment variable. (The variable is
/// only considered when the `std` feature is enabled, which it is by
/// default.)
#[cfg(feature = "fork")]
#[cfg_attr(docsrs, doc(cfg(feature = "fork")))]
pub fork: bool,
/// If non-zero, tests are run in a subprocess and each generated case
/// fails if it takes longer than this number of milliseconds.
///
/// This implicitly enables forking, even if the `fork` field is `false`.
///
/// The type here is plain `u32` (rather than
/// `Option<std::time::Duration>`) for the sake of ergonomics.
///
/// This requires the "timeout" feature, enabled by default.
///
/// Setting a timeout to less than the time it takes the process to start
/// up and initialise the first test case will cause the whole test to be
/// aborted.
///
/// The default is `0` (i.e., no timeout), which can be overridden by
/// setting the `PROPTEST_TIMEOUT` environment variable. (The variable is
/// only considered when the `std` feature is enabled, which it is by
/// default.)
#[cfg(feature = "timeout")]
#[cfg_attr(docsrs, doc(cfg(feature = "timeout")))]
pub timeout: u32,
/// If non-zero, give up the shrinking process after this many milliseconds
/// have elapsed since the start of the shrinking process.
///
/// This will not cause currently running test cases to be interrupted.
///
/// This configuration is only available when the `std` feature is enabled
/// (which it is by default).
///
/// The default is `0` (i.e., no limit), which can be overridden by setting
/// the `PROPTEST_MAX_SHRINK_TIME` environment variable. (The variable is
/// only considered when the `std` feature is enabled, which it is by
/// default.)
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub max_shrink_time: u32,
/// Give up on shrinking if more than this number of iterations of the test
/// code are run.
///
/// Setting this to `std::u32::MAX` causes the actual limit to be four
/// times the number of test cases.
///
/// Setting this value to `0` disables shrinking altogether.
///
/// Note that the type of this field will change in a future version of
/// proptest to better accommodate its special values.
///
/// The default is `std::u32::MAX`, which can be overridden by setting the
/// `PROPTEST_MAX_SHRINK_ITERS` environment variable. (The variable is only
/// considered when the `std` feature is enabled, which it is by default.)
pub max_shrink_iters: u32,
/// The default maximum size to `proptest::collection::SizeRange`. The default
/// strategy for collections (like `Vec`) use collections in the range of
/// `0..max_default_size_range`.
///
/// The default is `100` which can be overridden by setting the
/// `PROPTEST_MAX_DEFAULT_SIZE_RANGE` environment variable. (The variable
/// is only considered when the `std` feature is enabled, which it is by
/// default.)
pub max_default_size_range: usize,
/// A function to create new result caches.
///
/// The default is to do no caching. The easiest way to enable caching is
/// to set this field to `basic_result_cache` (though that is currently
/// only available with the `std` feature).
///
/// This is useful for strategies which have a tendency to produce
/// duplicate values, or for tests where shrinking can take a very long
/// time due to exploring the same output multiple times.
///
/// When caching is enabled, generated values themselves are not stored, so
/// this does not pose a risk of memory exhaustion for large test inputs
/// unless using extraordinarily large test case counts.
///
/// Caching incurs its own overhead, and may very well make your test run
/// more slowly.
pub result_cache: fn() -> Box<dyn ResultCache>,
/// Set to non-zero values to cause proptest to emit human-targeted
/// messages to stderr as it runs.
///
/// Greater values cause greater amounts of logs to be emitted. The exact
/// meaning of certain levels other than 0 is subject to change.
///
/// - 0: No extra output.
/// - 1: Log test failure messages. In state machine tests, this level is
/// used to print transitions.
/// - 2: Trace low-level details.
///
/// This is only available with the `std` feature (enabled by default)
/// since on nostd proptest has no way to produce output.
///
/// The default is `0`, which can be overridden by setting the
/// `PROPTEST_VERBOSE` environment variable. (The variable is only considered
/// when the `std` feature is enabled, which it is by default.)
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub verbose: u32,
/// The RNG algorithm to use when not using a user-provided RNG.
///
/// The default is `RngAlgorithm::default()`, which can be overridden by
/// setting the `PROPTEST_RNG_ALGORITHM` environment variable to one of the following:
///
/// - `xs` — `RngAlgorithm::XorShift`
/// - `cc` — `RngAlgorithm::ChaCha`
///
/// (The variable is only considered when the `std` feature is enabled,
/// which it is by default.)
pub rng_algorithm: RngAlgorithm,
// Needs to be public so FRU syntax can be used.
#[doc(hidden)]
pub _non_exhaustive: (),
}
impl Config {
/// Constructs a `Config` only differing from the `default()` in the
/// number of test cases required to pass the test successfully.
///
/// This is simply a more concise alternative to using field-record update
/// syntax:
///
/// ```
/// # use proptest::test_runner::Config;
/// assert_eq!(
/// Config::with_cases(42),
/// Config { cases: 42, .. Config::default() }
/// );
/// ```
pub fn with_cases(cases: u32) -> Self {
Self {
cases,
..Config::default()
}
}
/// Constructs a `Config` only differing from the `default()` in the
/// source_file of the present test.
///
/// This is simply a more concise alternative to using field-record update
/// syntax:
///
/// ```
/// # use proptest::test_runner::Config;
/// assert_eq!(
/// Config::with_source_file("computer/question"),
/// Config { source_file: Some("computer/question"), .. Config::default() }
/// );
/// ```
pub fn with_source_file(source_file: &'static str) -> Self {
Self {
source_file: Some(source_file),
..Config::default()
}
}
/// Constructs a `Config` only differing from the provided Config instance, `self`,
/// in the source_file of the present test.
///
/// This is simply a more concise alternative to using field-record update
/// syntax:
///
/// ```
/// # use proptest::test_runner::Config;
/// let a = Config::with_source_file("computer/question");
/// let b = a.clone_with_source_file("answer/42");
/// assert_eq!(
/// a,
/// Config { source_file: Some("computer/question"), .. Config::default() }
/// );
/// assert_eq!(
/// b,
/// Config { source_file: Some("answer/42"), .. Config::default() }
/// );
/// ```
pub fn clone_with_source_file(&self, source_file: &'static str) -> Self {
let mut result = self.clone();
result.source_file = Some(source_file);
result
}
/// Constructs a `Config` only differing from the `default()` in the
/// failure_persistence member.
///
/// This is simply a more concise alternative to using field-record update
/// syntax:
///
/// ```
/// # use proptest::test_runner::{Config, FileFailurePersistence};
/// assert_eq!(
/// Config::with_failure_persistence(FileFailurePersistence::WithSource("regressions")),
/// Config {
/// failure_persistence: Some(Box::new(FileFailurePersistence::WithSource("regressions"))),
/// .. Config::default()
/// }
/// );
/// ```
pub fn with_failure_persistence<T>(failure_persistence: T) -> Self
where
T: FailurePersistence + 'static,
{
Self {
failure_persistence: Some(Box::new(failure_persistence)),
..Default::default()
}
}
/// Return whether this configuration implies forking.
///
/// This method exists even if the "fork" feature is disabled, in which
/// case it simply returns false.
pub fn fork(&self) -> bool {
self._fork() || self.timeout() > 0
}
#[cfg(feature = "fork")]
fn _fork(&self) -> bool {
self.fork
}
#[cfg(not(feature = "fork"))]
fn _fork(&self) -> bool {
false
}
/// Returns the configured timeout.
///
/// This method exists even if the "timeout" feature is disabled, in which
/// case it simply returns 0.
#[cfg(feature = "timeout")]
pub fn timeout(&self) -> u32 {
self.timeout
}
/// Returns the configured timeout.
///
/// This method exists even if the "timeout" feature is disabled, in which
/// case it simply returns 0.
#[cfg(not(feature = "timeout"))]
pub fn timeout(&self) -> u32 {
0
}
/// Returns the configured limit on shrinking iterations.
///
/// This takes into account the special "automatic" behaviour.
pub fn max_shrink_iters(&self) -> u32 {
if u32::MAX == self.max_shrink_iters {
self.cases.saturating_mul(4)
} else {
self.max_shrink_iters
}
}
// Used by macros to force the config to be owned without depending on
// certain traits being `use`d.
#[allow(missing_docs)]
#[doc(hidden)]
pub fn __sugar_to_owned(&self) -> Self {
self.clone()
}
}
#[cfg(feature = "std")]
impl Default for Config {
fn default() -> Self {
DEFAULT_CONFIG.clone()
}
}
#[cfg(not(feature = "std"))]
impl Default for Config {
fn default() -> Self {
default_default_config()
}
}