Skip to main content

foundry_test_utils/
lib.rs

1//! # foundry-test-utils
2//!
3//! Internal Foundry testing utilities.
4
5#![cfg_attr(not(test), warn(unused_crate_dependencies))]
6#![cfg_attr(docsrs, feature(doc_cfg))]
7// Shouldn't use sh_* macros here, as they don't get captured by the test runner.
8#![allow(clippy::disallowed_macros)]
9
10#[macro_use]
11extern crate tracing;
12
13// Macros useful for testing.
14#[macro_use]
15mod macros;
16
17pub mod etherscan;
18pub mod rpc;
19
20pub mod fd_lock;
21
22mod filter;
23pub use filter::Filter;
24
25mod ext;
26pub use ext::ExtTester;
27
28mod prj;
29pub use prj::{TestCommand, TestProject, cargo_profile_dir};
30
31// Utilities for making it easier to handle tests.
32pub mod util;
33
34mod script;
35pub use script::{ScriptOutcome, ScriptTester};
36
37pub mod ui_runner;
38
39// re-exports for convenience
40pub use foundry_compilers;
41
42pub use foundry_test_macros::{casttest, forgetest, forgetest_init};
43pub use snapbox::{self, assert_data_eq, file, str};
44
45/// Initializes tracing for tests.
46pub fn init_tracing() {
47    use std::sync::Once;
48    static ONCE: Once = Once::new();
49    ONCE.call_once(|| {
50        if std::env::var_os("RUST_BACKTRACE").is_none() {
51            unsafe { std::env::set_var("RUST_BACKTRACE", "1") };
52        }
53        let _ = tracing_subscriber::FmtSubscriber::builder()
54            .with_env_filter(env_filter())
55            .with_test_writer()
56            .try_init();
57        let _ = ui_test::color_eyre::install();
58    });
59}
60
61fn env_filter() -> tracing_subscriber::EnvFilter {
62    const DEFAULT_DIRECTIVES: &[&str] = &include!("../../cli/src/utils/default_directives.txt");
63    let mut filter = tracing_subscriber::EnvFilter::builder()
64        .with_default_directive("foundry_test_utils=debug".parse().unwrap())
65        .from_env_lossy();
66    for &directive in DEFAULT_DIRECTIVES {
67        filter = filter.add_directive(directive.parse().unwrap());
68    }
69    filter
70}
71
72pub fn test_debug(args: std::fmt::Arguments<'_>) {
73    init_tracing();
74    debug!("{args}");
75}
76
77pub fn test_trace(args: std::fmt::Arguments<'_>) {
78    init_tracing();
79    trace!("{args}");
80}