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