Skip to content

feat(test): add a test utils crate to make log initialization possible everywhere #5521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ matrix-sdk-qrcode = { path = "crates/matrix-sdk-qrcode", version = "0.13.0" }
matrix-sdk-sqlite = { path = "crates/matrix-sdk-sqlite", version = "0.13.0", default-features = false }
matrix-sdk-store-encryption = { path = "crates/matrix-sdk-store-encryption", version = "0.13.0" }
matrix-sdk-test = { path = "testing/matrix-sdk-test", version = "0.13.0" }
matrix-sdk-test-utils = { path = "testing/matrix-sdk-test-utils", version = "0.13.0" }
matrix-sdk-ui = { path = "crates/matrix-sdk-ui", version = "0.13.0", default-features = false }
matrix-sdk-search = { path = "crates/matrix-sdk-search", version = "0.13.0" }

Expand Down
1 change: 1 addition & 0 deletions crates/matrix-sdk-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ assign = "1.1.1"
futures-executor.workspace = true
http.workspace = true
matrix-sdk-test.workspace = true
matrix-sdk-test-utils.workspace = true
similar-asserts.workspace = true
stream_assert.workspace = true

Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk-base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub use utils::{
};

#[cfg(test)]
matrix_sdk_test::init_tracing_for_tests!();
matrix_sdk_test_utils::init_tracing_for_tests!();

/// The Matrix user session info.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions crates/matrix-sdk-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ assert_matches.workspace = true
assert_matches2.workspace = true
insta.workspace = true
matrix-sdk-test-macros = { path = "../../testing/matrix-sdk-test-macros" }
matrix-sdk-test-utils.workspace = true
proptest.workspace = true
wasm-bindgen-test.workspace = true

Expand Down
3 changes: 3 additions & 0 deletions crates/matrix-sdk-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

use std::pin::Pin;

#[cfg(test)]
matrix_sdk_test_utils::init_tracing_for_tests!();

use futures_core::Future;
#[doc(no_inline)]
pub use ruma;
Expand Down
1 change: 1 addition & 0 deletions crates/matrix-sdk-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ http.workspace = true
indoc = "2.0.5"
insta.workspace = true
matrix-sdk-test.workspace = true
matrix-sdk-test-utils.workspace = true
proptest.workspace = true
rmp-serde.workspace = true
similar-asserts.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub use vodozemac;
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg(test)]
matrix_sdk_test::init_tracing_for_tests!();
matrix_sdk_test_utils::init_tracing_for_tests!();

#[cfg(feature = "uniffi")]
uniffi::setup_scaffolding!();
Expand Down
1 change: 1 addition & 0 deletions crates/matrix-sdk-sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ matrix-sdk-base = { workspace = true, features = ["testing"] }
matrix-sdk-common.workspace = true
matrix-sdk-crypto = { workspace = true, features = ["testing"] }
matrix-sdk-test.workspace = true
matrix-sdk-test-utils.workspace = true
once_cell.workspace = true
similar-asserts.workspace = true
tempfile.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk-sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub use self::event_cache_store::SqliteEventCacheStore;
pub use self::state_store::{SqliteStateStore, DATABASE_NAME as STATE_STORE_DATABASE_NAME};

#[cfg(test)]
matrix_sdk_test::init_tracing_for_tests!();
matrix_sdk_test_utils::init_tracing_for_tests!();

/// A configuration structure used for opening a store.
#[derive(Clone)]
Expand Down
1 change: 1 addition & 0 deletions crates/matrix-sdk-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ assert_matches2.workspace = true
eyeball-im-util.workspace = true
matrix-sdk = { workspace = true, features = ["testing", "sqlite"] }
matrix-sdk-test.workspace = true
matrix-sdk-test-utils.workspace = true
stream_assert.workspace = true
tempfile.workspace = true
url.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub use self::{room_list_service::RoomListService, timeline::Timeline};
const DEFAULT_SANITIZER_MODE: HtmlSanitizerMode = HtmlSanitizerMode::Compat;

#[cfg(test)]
matrix_sdk_test::init_tracing_for_tests!();
matrix_sdk_test_utils::init_tracing_for_tests!();

#[cfg(feature = "uniffi")]
uniffi::setup_scaffolding!();
2 changes: 1 addition & 1 deletion crates/matrix-sdk-ui/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod sliding_sync;
mod sync_service;
mod timeline;

matrix_sdk_test::init_tracing_for_tests!();
matrix_sdk_test_utils::init_tracing_for_tests!();

/// Mount a Mock on the given server to handle the `GET /sync` endpoint with
/// an optional `since` param that returns a 200 status code with the given
Expand Down
1 change: 1 addition & 0 deletions crates/matrix-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ futures-executor.workspace = true
insta.workspace = true
matrix-sdk-base = { workspace = true, features = ["testing"] }
matrix-sdk-test.workspace = true
matrix-sdk-test-utils.workspace = true
serde_urlencoded = "0.7.1"
similar-asserts.workspace = true
stream_assert.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,4 @@ pub mod live_location_share;
pub mod test_utils;

#[cfg(test)]
matrix_sdk_test::init_tracing_for_tests!();
matrix_sdk_test_utils::init_tracing_for_tests!();
2 changes: 1 addition & 1 deletion crates/matrix-sdk/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod send_queue;
#[cfg(feature = "experimental-widgets")]
mod widget;

matrix_sdk_test::init_tracing_for_tests!();
matrix_sdk_test_utils::init_tracing_for_tests!();

/// Mount a Mock on the given server to handle the `GET /sync` endpoint with
/// an optional `since` param that returns a 200 status code with the given
Expand Down
1 change: 1 addition & 0 deletions testing/matrix-sdk-integration-testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ matrix-sdk = { workspace = true, default-features = true, features = ["testing",
matrix-sdk-base = { workspace = true, default-features = true, features = ["testing", "qrcode"] }
matrix-sdk-common.workspace = true
matrix-sdk-test.workspace = true
matrix-sdk-test-utils.workspace = true
matrix-sdk-ui = { workspace = true, default-features = true }
once_cell.workspace = true
rand.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion testing/matrix-sdk-integration-testing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg(test)]

matrix_sdk_test::init_tracing_for_tests!();
matrix_sdk_test_utils::init_tracing_for_tests!();

mod helpers;
mod tests;
18 changes: 18 additions & 0 deletions testing/matrix-sdk-test-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
homepage = "https://github.com/matrix-org/matrix-rust-sdk"
keywords = ["matrix", "chat", "messaging", "ruma"]
license = "Apache-2.0"
name = "matrix-sdk-test-utils"
version = "0.13.0"
edition = "2024"
rust-version.workspace = true

[package.metadata.release]
release = true

[target.'cfg(not(target_family = "wasm"))'.dependencies]
ctor = "0.2.9"
tracing-subscriber = { workspace = true, features = ["env-filter"] }

[lints]
workspace = true
1 change: 1 addition & 0 deletions testing/matrix-sdk-test-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Utils function for the test helpers
41 changes: 41 additions & 0 deletions testing/matrix-sdk-test-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// Initialize a tracing subscriber if the target architecture is not WASM.
///
/// Uses a sensible default filter that can be overridden through the `RUST_LOG`
/// environment variable and runs once before all tests by using the [`ctor`]
/// crate.
///
/// Invoke this macro once per compilation unit (`lib.rs`, `tests/*.rs`,
/// `tests/*/main.rs`).
#[macro_export]
macro_rules! init_tracing_for_tests {
() => {
#[cfg(not(target_family = "wasm"))]
#[$crate::__macro_support::ctor]
fn init_logging() {
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use $crate::__macro_support::tracing_subscriber;

tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
// Output is only printed for failing tests, but still we shouldn't overload
// the output with unnecessary info. When debugging a specific test, it's easy
// to override this default by setting the `RUST_LOG` environment variable.
//
// Since tracing_subscriber does prefix matching, the `matrix_sdk=` directive
// takes effect for all the main crates (`matrix_sdk_base`, `matrix_sdk_crypto`
// and so on).
"info,matrix_sdk=debug".into()
}))
.with(tracing_subscriber::fmt::layer().with_test_writer())
.init();
}
};
}

#[doc(hidden)]
pub mod __macro_support {
#[cfg(not(target_family = "wasm"))]
pub use ctor::ctor;
#[cfg(not(target_family = "wasm"))]
pub use tracing_subscriber;
}
42 changes: 0 additions & 42 deletions testing/matrix-sdk-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,48 +72,6 @@ macro_rules! stripped_state_event {
}
}

/// Initialize a tracing subscriber if the target architecture is not WASM.
///
/// Uses a sensible default filter that can be overridden through the `RUST_LOG`
/// environment variable and runs once before all tests by using the [`ctor`]
/// crate.
///
/// Invoke this macro once per compilation unit (`lib.rs`, `tests/*.rs`,
/// `tests/*/main.rs`).
#[macro_export]
macro_rules! init_tracing_for_tests {
() => {
#[cfg(not(target_family = "wasm"))]
#[$crate::__macro_support::ctor]
fn init_logging() {
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use $crate::__macro_support::tracing_subscriber;

tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
// Output is only printed for failing tests, but still we shouldn't overload
// the output with unnecessary info. When debugging a specific test, it's easy
// to override this default by setting the `RUST_LOG` environment variable.
//
// Since tracing_subscriber does prefix matching, the `matrix_sdk=` directive
// takes effect for all the main crates (`matrix_sdk_base`, `matrix_sdk_crypto`
// and so on).
"info,matrix_sdk=debug".into()
}))
.with(tracing_subscriber::fmt::layer().with_test_writer())
.init();
}
};
}

#[doc(hidden)]
pub mod __macro_support {
#[cfg(not(target_family = "wasm"))]
pub use ctor::ctor;
#[cfg(not(target_family = "wasm"))]
pub use tracing_subscriber;
}

#[cfg(not(target_family = "wasm"))]
pub mod mocks;

Expand Down
Loading