@@ -1229,8 +1229,25 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
1229
1229
}
1230
1230
}
1231
1231
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
+
1232
1248
#[ cfg( test) ]
1233
1249
mod tests {
1250
+ use crate :: cli:: test_utils:: init_tracing_for_tests;
1234
1251
use super :: * ;
1235
1252
1236
1253
#[ test]
@@ -1295,6 +1312,7 @@ mod tests {
1295
1312
1296
1313
#[ test]
1297
1314
fn test_parse_opts ( ) {
1315
+ init_tracing_for_tests ( ) ;
1298
1316
assert ! ( matches!(
1299
1317
Cli :: parse_including_static( [ "bootc" , "status" ] ) . opt,
1300
1318
Opt :: Status ( StatusOpts {
@@ -1326,6 +1344,7 @@ mod tests {
1326
1344
1327
1345
#[ test]
1328
1346
fn test_parse_ostree_ext ( ) {
1347
+ init_tracing_for_tests ( ) ;
1329
1348
assert ! ( matches!(
1330
1349
Cli :: parse_including_static( [ "bootc" , "internals" , "ostree-container" ] ) . opt,
1331
1350
Opt :: Internals ( InternalsOpts :: OstreeContainer { .. } )
@@ -1376,19 +1395,11 @@ mod tracing_tests {
1376
1395
use std:: io:: { self , Read } ;
1377
1396
use std:: os:: unix:: io:: { AsRawFd , FromRawFd } ;
1378
1397
use std:: sync:: { Mutex , Once } ;
1398
+ use crate :: cli:: test_utils:: init_tracing_for_tests;
1379
1399
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
1382
1401
static TEST_MUTEX : Mutex < ( ) > = Mutex :: new ( ( ) ) ;
1383
1402
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
-
1392
1403
/// Captures `stderr` output using a pipe
1393
1404
fn capture_stderr < F : FnOnce ( ) > ( test_fn : F ) -> String {
1394
1405
let ( read_fd, write_fd) = pipe ( ) . expect ( "Failed to create pipe" ) ;
@@ -1462,30 +1473,4 @@ mod tracing_tests {
1462
1473
"Expected TRACE message not found"
1463
1474
) ;
1464
1475
}
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
- }
1491
1476
}
0 commit comments