Skip to content

Commit 2b5d4dd

Browse files
committed
test: (wip) adapt unit tests to use tracing output, pt. 2
1 parent 9b4ca52 commit 2b5d4dd

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

src/currentprocess.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ use cwdsource::*;
3232
use filesource::*;
3333
use varsource::*;
3434

35+
use crate::currentprocess;
36+
3537
/// An abstraction for the current process.
3638
///
3739
/// This acts as a clonable proxy to the global state provided by some key OS
@@ -136,13 +138,16 @@ pub fn with<F, R>(process: Process, f: F) -> R
136138
where
137139
F: FnOnce() -> R,
138140
{
141+
use tracing_subscriber::util::SubscriberInitExt;
142+
139143
ensure_hook();
140144

141145
PROCESS.with(|p| {
142146
if let Some(old_p) = &*p.borrow() {
143147
panic!("current process already set {old_p:?}");
144148
}
145149
*p.borrow_mut() = Some(process);
150+
let _guard = console_logger().set_default();
146151
let result = f();
147152
*p.borrow_mut() = None;
148153
result
@@ -159,6 +164,35 @@ fn ensure_hook() {
159164
});
160165
}
161166

167+
fn console_logger() -> impl tracing::Subscriber {
168+
use tracing_subscriber::{
169+
filter::{EnvFilter, LevelFilter},
170+
layer::SubscriberExt,
171+
Layer, Registry,
172+
};
173+
174+
let curr_process = currentprocess::process();
175+
let maybe_directives = curr_process.var_os("RUST_LOG").clone();
176+
let logger = tracing_subscriber::fmt::layer()
177+
.with_writer(move || curr_process.stderr())
178+
.with_ansi(false);
179+
let console_logger = if let Some(directives) = maybe_directives {
180+
let env_filter = EnvFilter::builder()
181+
.with_default_directive(LevelFilter::INFO.into())
182+
.parse_lossy(directives.to_string_lossy());
183+
logger.compact().with_filter(env_filter).boxed()
184+
} else {
185+
// Receive log lines from Rustup only.
186+
let env_filter = EnvFilter::new("rustup=DEBUG");
187+
logger
188+
.event_format(crate::cli::log::EventFormatter)
189+
.with_filter(env_filter)
190+
.boxed()
191+
};
192+
// TODO: What about the original otel logger?
193+
Registry::default().with(console_logger)
194+
}
195+
162196
/// Run a function in the context of a process definition and a tokio runtime.
163197
///
164198
/// The process state is injected into a thread-local in every work thread of

src/test.rs

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,7 @@ type Tracer = ();
239239

240240
/// A tracer for the tests.
241241
static TRACER: Lazy<Tracer> = Lazy::new(|| {
242-
use crate::currentprocess::{filesource::StderrSource, varsource::VarSource};
243-
244242
use tokio::runtime::Handle;
245-
use tracing_subscriber::{fmt, layer::SubscriberExt, EnvFilter, Layer, Registry};
246243

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

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

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

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

@@ -292,32 +287,16 @@ static TRACER: Lazy<Tracer> = Lazy::new(|| {
292287
.with_tracer(tracer.clone())
293288
.with_filter(env_filter)
294289
};
295-
let console_logger = {
296-
let is_verbose = curr_process.var_os("RUST_LOG").is_some();
297-
let logger = fmt::layer()
298-
.with_writer(move || curr_process.stderr())
299-
.with_ansi(has_ansi);
300-
if is_verbose {
301-
let env_filter =
302-
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("INFO"));
303-
logger.compact().with_filter(env_filter).boxed()
304-
} else {
305-
// Receive log lines from Rustup only.
306-
let env_filter = EnvFilter::new("rustup=DEBUG");
307-
logger
308-
.event_format(crate::cli::log::EventFormatter)
309-
.with_filter(env_filter)
310-
.boxed()
311-
}
312-
};
313290
let subscriber = {
291+
use tracing_subscriber::Registry;
314292
#[cfg(feature = "otel")]
315293
{
316-
Registry::default().with(console_logger).with(telemetry)
294+
use tracing_subscriber::layer::SubscriberExt;
295+
Registry::default().with(telemetry)
317296
}
318297
#[cfg(not(feature = "otel"))]
319298
{
320-
Registry::default().with(console_logger)
299+
Registry::default()
321300
}
322301
};
323302
tracing::subscriber::set_global_default(subscriber).unwrap();

0 commit comments

Comments
 (0)