Skip to content

Commit

Permalink
test: (wip) adapt unit tests to use tracing output, pt. 2
Browse files Browse the repository at this point in the history
  • Loading branch information
rami3l committed Jun 1, 2024
1 parent 9b4ca52 commit 2b5d4dd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
34 changes: 34 additions & 0 deletions src/currentprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ use cwdsource::*;
use filesource::*;
use varsource::*;

use crate::currentprocess;

/// An abstraction for the current process.
///
/// This acts as a clonable proxy to the global state provided by some key OS
Expand Down Expand Up @@ -136,13 +138,16 @@ pub fn with<F, R>(process: Process, f: F) -> R
where
F: FnOnce() -> R,
{
use tracing_subscriber::util::SubscriberInitExt;

ensure_hook();

PROCESS.with(|p| {
if let Some(old_p) = &*p.borrow() {
panic!("current process already set {old_p:?}");
}
*p.borrow_mut() = Some(process);
let _guard = console_logger().set_default();
let result = f();
*p.borrow_mut() = None;
result
Expand All @@ -159,6 +164,35 @@ fn ensure_hook() {
});
}

fn console_logger() -> impl tracing::Subscriber {
use tracing_subscriber::{
filter::{EnvFilter, LevelFilter},
layer::SubscriberExt,
Layer, Registry,
};

let curr_process = currentprocess::process();
let maybe_directives = curr_process.var_os("RUST_LOG").clone();
let logger = tracing_subscriber::fmt::layer()
.with_writer(move || curr_process.stderr())
.with_ansi(false);
let console_logger = if let Some(directives) = maybe_directives {
let env_filter = EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.parse_lossy(directives.to_string_lossy());
logger.compact().with_filter(env_filter).boxed()
} else {
// Receive log lines from Rustup only.
let env_filter = EnvFilter::new("rustup=DEBUG");
logger
.event_format(crate::cli::log::EventFormatter)
.with_filter(env_filter)
.boxed()
};
// TODO: What about the original otel logger?
Registry::default().with(console_logger)
}

/// Run a function in the context of a process definition and a tokio runtime.
///
/// The process state is injected into a thread-local in every work thread of
Expand Down
31 changes: 5 additions & 26 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,7 @@ type Tracer = ();

/// A tracer for the tests.
static TRACER: Lazy<Tracer> = Lazy::new(|| {
use crate::currentprocess::{filesource::StderrSource, varsource::VarSource};

use tokio::runtime::Handle;
use tracing_subscriber::{fmt, layer::SubscriberExt, EnvFilter, Layer, Registry};

// Use the current runtime, or the sync test runtime otherwise.
let handle = match Handle::try_current() {
Expand All @@ -251,9 +248,6 @@ static TRACER: Lazy<Tracer> = Lazy::new(|| {
};

let _guard = handle.enter();
// TODO: However this will not be set before running the actual test. What now?
let curr_process = currentprocess::process();
let has_ansi = curr_process.stderr().is_a_tty();

#[cfg(feature = "otel")]
let tracer = {
Expand Down Expand Up @@ -284,6 +278,7 @@ static TRACER: Lazy<Tracer> = Lazy::new(|| {
let telemetry = {
use opentelemetry::global;
use opentelemetry_sdk::propagation::TraceContextPropagator;
use tracing_subscriber::{EnvFilter, Layer};

global::set_text_map_propagator(TraceContextPropagator::new());

Expand All @@ -292,32 +287,16 @@ static TRACER: Lazy<Tracer> = Lazy::new(|| {
.with_tracer(tracer.clone())
.with_filter(env_filter)
};
let console_logger = {
let is_verbose = curr_process.var_os("RUST_LOG").is_some();
let logger = fmt::layer()
.with_writer(move || curr_process.stderr())
.with_ansi(has_ansi);
if is_verbose {
let env_filter =
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("INFO"));
logger.compact().with_filter(env_filter).boxed()
} else {
// Receive log lines from Rustup only.
let env_filter = EnvFilter::new("rustup=DEBUG");
logger
.event_format(crate::cli::log::EventFormatter)
.with_filter(env_filter)
.boxed()
}
};
let subscriber = {
use tracing_subscriber::Registry;
#[cfg(feature = "otel")]
{
Registry::default().with(console_logger).with(telemetry)
use tracing_subscriber::layer::SubscriberExt;
Registry::default().with(telemetry)
}
#[cfg(not(feature = "otel"))]
{
Registry::default().with(console_logger)
Registry::default()
}
};
tracing::subscriber::set_global_default(subscriber).unwrap();
Expand Down

0 comments on commit 2b5d4dd

Please sign in to comment.