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};
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 snapbox::{self, assert_data_eq, file, str};
43
44/// Initializes tracing for tests.
45pub fn init_tracing() {
46    use std::sync::Once;
47    static ONCE: Once = Once::new();
48    ONCE.call_once(|| {
49        if std::env::var_os("RUST_BACKTRACE").is_none() {
50            unsafe { std::env::set_var("RUST_BACKTRACE", "1") };
51        }
52        let _ = tracing_subscriber::FmtSubscriber::builder()
53            .with_env_filter(env_filter())
54            .with_test_writer()
55            .try_init();
56        let _ = ui_test::color_eyre::install();
57    });
58}
59
60fn env_filter() -> tracing_subscriber::EnvFilter {
61    const DEFAULT_DIRECTIVES: &[&str] = &include!("../../cli/src/utils/default_directives.txt");
62    let mut filter = tracing_subscriber::EnvFilter::builder()
63        .with_default_directive("foundry_test_utils=debug".parse().unwrap())
64        .from_env_lossy();
65    for &directive in DEFAULT_DIRECTIVES {
66        filter = filter.add_directive(directive.parse().unwrap());
67    }
68    filter
69}
70
71pub fn test_debug(args: std::fmt::Arguments<'_>) {
72    init_tracing();
73    debug!("{args}");
74}
75
76pub fn test_trace(args: std::fmt::Arguments<'_>) {
77    init_tracing();
78    trace!("{args}");
79}