Skip to content

Commit 7ce081c

Browse files
committed
Create a test util for the cli tests for helping initializing tracing
Also removed the test case for RUST_LOG as we will cover it via a highler level test Signed-off-by: mabulgu <[email protected]>
1 parent 03dfd04 commit 7ce081c

File tree

1 file changed

+21
-36
lines changed

1 file changed

+21
-36
lines changed

lib/src/cli.rs

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,8 +1229,25 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
12291229
}
12301230
}
12311231

1232+
#[cfg(test)]
1233+
mod test_utils {
1234+
use std::sync::Once;
1235+
use bootc_utils::initialize_tracing;
1236+
1237+
// Ensure logging is initialized once to prevent conflicts across tests
1238+
static INIT: Once = Once::new();
1239+
1240+
/// Helper function to initialize tracing for tests
1241+
pub fn init_tracing_for_tests() {
1242+
INIT.call_once(|| {
1243+
initialize_tracing();
1244+
});
1245+
}
1246+
}
1247+
12321248
#[cfg(test)]
12331249
mod tests {
1250+
use crate::cli::test_utils::init_tracing_for_tests;
12341251
use super::*;
12351252

12361253
#[test]
@@ -1295,6 +1312,7 @@ mod tests {
12951312

12961313
#[test]
12971314
fn test_parse_opts() {
1315+
init_tracing_for_tests();
12981316
assert!(matches!(
12991317
Cli::parse_including_static(["bootc", "status"]).opt,
13001318
Opt::Status(StatusOpts {
@@ -1326,6 +1344,7 @@ mod tests {
13261344

13271345
#[test]
13281346
fn test_parse_ostree_ext() {
1347+
init_tracing_for_tests();
13291348
assert!(matches!(
13301349
Cli::parse_including_static(["bootc", "internals", "ostree-container"]).opt,
13311350
Opt::Internals(InternalsOpts::OstreeContainer { .. })
@@ -1376,19 +1395,11 @@ mod tracing_tests {
13761395
use std::io::{self, Read};
13771396
use std::os::unix::io::{AsRawFd, FromRawFd};
13781397
use std::sync::{Mutex, Once};
1398+
use crate::cli::test_utils::init_tracing_for_tests;
13791399

1380-
// Ensure logging is initialized once to prevent conflicts across tests
1381-
static INIT: Once = Once::new();
1400+
// Used for ensuring ordered testing of the tracing tests
13821401
static TEST_MUTEX: Mutex<()> = Mutex::new(());
13831402

1384-
/// Helper function to initialize tracing for tests
1385-
fn init_tracing_for_tests() {
1386-
INIT.call_once(|| {
1387-
std::env::remove_var("RUST_LOG");
1388-
initialize_tracing();
1389-
});
1390-
}
1391-
13921403
/// Captures `stderr` output using a pipe
13931404
fn capture_stderr<F: FnOnce()>(test_fn: F) -> String {
13941405
let (read_fd, write_fd) = pipe().expect("Failed to create pipe");
@@ -1462,30 +1473,4 @@ mod tracing_tests {
14621473
"Expected TRACE message not found"
14631474
);
14641475
}
1465-
1466-
#[test]
1467-
fn test_update_tracing_respects_rust_log() {
1468-
let _lock = TEST_MUTEX.lock().unwrap(); // Ensure sequential execution
1469-
1470-
init_tracing_for_tests();
1471-
// Set RUST_LOG before initializing(not possible in this test) or after updating tracing
1472-
std::env::set_var("RUST_LOG", "info");
1473-
update_tracing_log_level(tracing::Level::DEBUG);
1474-
1475-
let output = capture_stderr(|| {
1476-
tracing::info!("Info message to stderr");
1477-
tracing::debug!("Debug message to stderr");
1478-
});
1479-
1480-
assert!(
1481-
output.contains("Info message to stderr"),
1482-
"Expected INFO message not found"
1483-
);
1484-
assert!(
1485-
!output.contains("Debug message to stderr"),
1486-
"Expected DEBUG message found"
1487-
);
1488-
1489-
std::env::remove_var("RUST_LOG");
1490-
}
14911476
}

0 commit comments

Comments
 (0)