From 2367017e92a65cf4dd2cebeee6d6db3b4d6a1447 Mon Sep 17 00:00:00 2001 From: Piotr Korkus Date: Fri, 14 Nov 2025 12:53:00 +0100 Subject: [PATCH] Refactor tracing subscriber initialization --- MODULE.bazel | 27 ++++++++++----------------- pyproject.toml | 2 +- test_scenarios_rust/BUILD | 11 ++++++++--- test_scenarios_rust/Cargo.lock | 2 +- test_scenarios_rust/Cargo.toml | 2 +- test_scenarios_rust/src/cli.rs | 23 ++++++++--------------- 6 files changed, 29 insertions(+), 38 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index 1a2e3c8..bd26548 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -16,19 +16,19 @@ module( name = "score_test_scenarios", - version = "0.3.0", + version = "0.3.1", compatibility_level = 0, ) # Starlark language server -bazel_dep(name = "score_starpls_lsp", version = "0.1.0") +bazel_dep(name = "score_starpls_lsp", version = "0.1.0", dev_dependency = True) # Python rules. -bazel_dep(name = "rules_python", version = "1.0.0") +bazel_dep(name = "rules_python", version = "1.0.0", dev_dependency = True) PYTHON_VERSION = "3.12" -python = use_extension("@rules_python//python/extensions:python.bzl", "python") +python = use_extension("@rules_python//python/extensions:python.bzl", "python", dev_dependency = True) python.toolchain( is_default = True, python_version = PYTHON_VERSION, @@ -36,13 +36,13 @@ python.toolchain( use_repo(python) # C++ GoogleTest dependencies. -bazel_dep(name = "googletest", version = "1.14.0") +bazel_dep(name = "googletest", version = "1.14.0", dev_dependency = True) # Rust rules. bazel_dep(name = "rules_rust", version = "0.56.0") # C/C++ rules. -bazel_dep(name = "rules_cc", version = "0.1.1") +bazel_dep(name = "rules_cc", version = "0.1.1", dev_dependency = True) # Rust module dependencies. rust = use_extension("@rules_rust//rust:extensions.bzl", "rust") @@ -51,15 +51,8 @@ rust.toolchain( versions = ["1.85.0"], ) -crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate") -crate.from_cargo( - name = "test_scenarios_rust_crates", - cargo_lockfile = "//test_scenarios_rust:Cargo.lock", - manifests = [ - "//test_scenarios_rust:Cargo.toml", - ], -) -use_repo(crate, "test_scenarios_rust_crates") - # C++ base libs. -bazel_dep(name = "score_baselibs", version = "0.1.2") +bazel_dep(name = "score_baselibs", version = "0.1.2", dev_dependency = True) + +# Score Rust crates. +bazel_dep(name = "score_crates", version = "0.0.4") diff --git a/pyproject.toml b/pyproject.toml index 9d4faf0..6b284ce 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "testing-utils" -version = "0.3.0" +version = "0.3.1" dependencies = ["pytest>=8.3.5", "pytest-html>=4.1.1", "pytest-repeat>=0.9.4"] requires-python = ">=3.12" authors = [ diff --git a/test_scenarios_rust/BUILD b/test_scenarios_rust/BUILD index 5e0aa1f..03e6f16 100644 --- a/test_scenarios_rust/BUILD +++ b/test_scenarios_rust/BUILD @@ -11,18 +11,23 @@ # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") -load("@test_scenarios_rust_crates//:defs.bzl", "all_crate_deps") rust_library( name = "test_scenarios_rust", srcs = glob(["src/**/*.rs"]), visibility = ["//visibility:public"], - deps = all_crate_deps(normal = True), + deps = [ + "@score_crates//:tracing", + "@score_crates//:tracing_subscriber", + ], ) rust_test( name = "tests", crate = ":test_scenarios_rust", visibility = ["//visibility:private"], - deps = all_crate_deps(normal = True), + deps = [ + "@score_crates//:tracing", + "@score_crates//:tracing_subscriber", + ], ) diff --git a/test_scenarios_rust/Cargo.lock b/test_scenarios_rust/Cargo.lock index 397e1ee..aa92dbb 100644 --- a/test_scenarios_rust/Cargo.lock +++ b/test_scenarios_rust/Cargo.lock @@ -144,7 +144,7 @@ dependencies = [ [[package]] name = "test_scenarios_rust" -version = "0.3.0" +version = "0.3.1" dependencies = [ "tracing", "tracing-subscriber", diff --git a/test_scenarios_rust/Cargo.toml b/test_scenarios_rust/Cargo.toml index 366e2ca..c3100d3 100644 --- a/test_scenarios_rust/Cargo.toml +++ b/test_scenarios_rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "test_scenarios_rust" -version = "0.3.0" +version = "0.3.1" edition = "2021" [dependencies] diff --git a/test_scenarios_rust/src/cli.rs b/test_scenarios_rust/src/cli.rs index 17c1ca2..c127c96 100644 --- a/test_scenarios_rust/src/cli.rs +++ b/test_scenarios_rust/src/cli.rs @@ -12,23 +12,21 @@ // ******************************************************************************* use crate::monotonic_clock::MonotonicClock; use crate::test_context::TestContext; -use std::sync::Once; use tracing::Level; +use tracing_subscriber::fmt::format::{Format, JsonFields}; use tracing_subscriber::FmtSubscriber; -/// Tracing subscriber should be initialized only once. -static TRACING_SUBSCRIBER_INIT: Once = Once::new(); - -fn init_tracing_subscriber() { - let subscriber = FmtSubscriber::builder() +/// Create a tracing subscriber that outputs logs in JSON format with monotonic timestamps. +/// # Returns +/// configured `FmtSubscriber`. +pub fn create_tracing_subscriber( +) -> FmtSubscriber> { + FmtSubscriber::builder() .with_max_level(Level::TRACE) .with_thread_ids(true) .with_timer(MonotonicClock::new()) .json() - .finish(); - - tracing::subscriber::set_global_default(subscriber) - .expect("Setting default subscriber failed!"); + .finish() } /// Test scenario arguments. @@ -154,11 +152,6 @@ pub fn run_cli_app(raw_arguments: &[String], test_context: &TestContext) -> Resu None => return Err("Test scenario input must be provided".to_string()), }; - // Initialize tracing subscriber. - TRACING_SUBSCRIBER_INIT.call_once(|| { - init_tracing_subscriber(); - }); - test_context.run(&scenario_name, &scenario_input) }