Skip to content

Commit 6480533

Browse files
committed
tests: forward logs to cargo's test output
Allows separating output per test according to `cargo test` options.
1 parent 3e1636c commit 6480533

17 files changed

+132
-150
lines changed

stun-proto/src/agent.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -677,12 +677,9 @@ impl From<StunWriteError> for StunError {
677677
pub(crate) mod tests {
678678
use super::*;
679679

680-
fn init() {
681-
crate::tests::test_init_log();
682-
}
683-
684680
#[test]
685681
fn agent_getters_setters() {
682+
let _log = crate::tests::test_init_log();
686683
let local_addr = "10.0.0.1:12345".parse().unwrap();
687684
let remote_addr = "10.0.0.2:3478".parse().unwrap();
688685
let mut agent = StunAgent::builder(TransportType::Udp, local_addr)
@@ -708,7 +705,7 @@ pub(crate) mod tests {
708705

709706
#[test]
710707
fn request() {
711-
init();
708+
let _log = crate::tests::test_init_log();
712709
let local_addr = "127.0.0.1:2000".parse().unwrap();
713710
let remote_addr = "127.0.0.1:1000".parse().unwrap();
714711
let mut agent = StunAgent::builder(TransportType::Udp, local_addr)
@@ -736,7 +733,7 @@ pub(crate) mod tests {
736733

737734
#[test]
738735
fn indication_with_invalid_response() {
739-
init();
736+
let _log = crate::tests::test_init_log();
740737
let local_addr = "127.0.0.1:2000".parse().unwrap();
741738
let remote_addr = "127.0.0.1:1000".parse().unwrap();
742739
let mut agent = StunAgent::builder(TransportType::Udp, local_addr)
@@ -768,7 +765,7 @@ pub(crate) mod tests {
768765

769766
#[test]
770767
fn request_with_credentials() {
771-
init();
768+
let _log = crate::tests::test_init_log();
772769
let local_addr = "10.0.0.1:12345".parse().unwrap();
773770
let remote_addr = "10.0.0.2:3478".parse().unwrap();
774771

@@ -822,7 +819,7 @@ pub(crate) mod tests {
822819

823820
#[test]
824821
fn request_unanswered() {
825-
init();
822+
let _log = crate::tests::test_init_log();
826823
let local_addr = "127.0.0.1:2000".parse().unwrap();
827824
let remote_addr = "127.0.0.1:1000".parse().unwrap();
828825
let mut agent = StunAgent::builder(TransportType::Udp, local_addr)
@@ -851,7 +848,7 @@ pub(crate) mod tests {
851848

852849
#[test]
853850
fn request_without_credentials() {
854-
init();
851+
let _log = crate::tests::test_init_log();
855852
let local_addr = "10.0.0.1:12345".parse().unwrap();
856853
let remote_addr = "10.0.0.2:3478".parse().unwrap();
857854

@@ -888,7 +885,7 @@ pub(crate) mod tests {
888885

889886
#[test]
890887
fn response_without_credentials() {
891-
init();
888+
let _log = crate::tests::test_init_log();
892889
let local_addr = "10.0.0.1:12345".parse().unwrap();
893890
let remote_addr = "10.0.0.2:3478".parse().unwrap();
894891

@@ -929,7 +926,7 @@ pub(crate) mod tests {
929926

930927
#[test]
931928
fn agent_response_without_credentials() {
932-
init();
929+
let _log = crate::tests::test_init_log();
933930
let local_addr = "10.0.0.1:12345".parse().unwrap();
934931
let remote_addr = "10.0.0.2:3478".parse().unwrap();
935932

@@ -968,7 +965,7 @@ pub(crate) mod tests {
968965

969966
#[test]
970967
fn response_with_incorrect_credentials() {
971-
init();
968+
let _log = crate::tests::test_init_log();
972969
let local_addr = "10.0.0.1:12345".parse().unwrap();
973970
let remote_addr = "10.0.0.2:3478".parse().unwrap();
974971

@@ -1010,7 +1007,7 @@ pub(crate) mod tests {
10101007

10111008
#[test]
10121009
fn duplicate_response_ignored() {
1013-
init();
1010+
let _log = crate::tests::test_init_log();
10141011
let local_addr = "10.0.0.1:12345".parse().unwrap();
10151012
let remote_addr = "10.0.0.2:3478".parse().unwrap();
10161013

@@ -1043,6 +1040,7 @@ pub(crate) mod tests {
10431040

10441041
#[test]
10451042
fn request_cancel() {
1043+
let _log = crate::tests::test_init_log();
10461044
let local_addr = "10.0.0.1:12345".parse().unwrap();
10471045
let remote_addr = "10.0.0.2:3478".parse().unwrap();
10481046

@@ -1070,6 +1068,7 @@ pub(crate) mod tests {
10701068

10711069
#[test]
10721070
fn request_cancel_send() {
1071+
let _log = crate::tests::test_init_log();
10731072
let local_addr = "10.0.0.1:12345".parse().unwrap();
10741073
let remote_addr = "10.0.0.2:3478".parse().unwrap();
10751074

@@ -1102,6 +1101,7 @@ pub(crate) mod tests {
11021101

11031102
#[test]
11041103
fn request_duplicate() {
1104+
let _log = crate::tests::test_init_log();
11051105
let local_addr = "10.0.0.1:12345".parse().unwrap();
11061106
let remote_addr = "10.0.0.2:3478".parse().unwrap();
11071107

@@ -1140,6 +1140,7 @@ pub(crate) mod tests {
11401140

11411141
#[test]
11421142
fn incoming_request() {
1143+
let _log = crate::tests::test_init_log();
11431144
let local_addr = "10.0.0.1:12345".parse().unwrap();
11441145
let remote_addr = "10.0.0.2:3478".parse().unwrap();
11451146

@@ -1158,7 +1159,7 @@ pub(crate) mod tests {
11581159

11591160
#[test]
11601161
fn tcp_request() {
1161-
init();
1162+
let _log = crate::tests::test_init_log();
11621163
let local_addr = "127.0.0.1:2000".parse().unwrap();
11631164
let remote_addr = "127.0.0.1:1000".parse().unwrap();
11641165
let mut agent = StunAgent::builder(TransportType::Tcp, local_addr)
@@ -1178,7 +1179,7 @@ pub(crate) mod tests {
11781179

11791180
#[test]
11801181
fn tcp_buffer_split_recv() {
1181-
init();
1182+
let _log = crate::tests::test_init_log();
11821183

11831184
let mut tcp_buffer = TcpBuffer::default();
11841185

stun-proto/src/lib.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,27 @@ impl<T> DebugWrapper<T> {
9898

9999
#[cfg(test)]
100100
pub(crate) mod tests {
101-
use std::sync::Once;
102-
use tracing_subscriber::EnvFilter;
101+
use tracing::subscriber::DefaultGuard;
102+
use tracing_subscriber::layer::SubscriberExt;
103+
use tracing_subscriber::Layer;
103104

104-
static TRACING: Once = Once::new();
105-
106-
pub fn test_init_log() {
107-
TRACING.call_once(|| {
108-
if let Ok(filter) = EnvFilter::try_from_default_env() {
109-
tracing_subscriber::fmt().with_env_filter(filter).init();
110-
}
111-
});
105+
pub fn test_init_log() -> DefaultGuard {
106+
let level_filter = std::env::var("STUN_LOG")
107+
.or(std::env::var("RUST_LOG"))
108+
.ok()
109+
.and_then(|var| var.parse::<tracing_subscriber::filter::Targets>().ok())
110+
.unwrap_or(
111+
tracing_subscriber::filter::Targets::new().with_default(tracing::Level::TRACE),
112+
);
113+
let registry = tracing_subscriber::registry().with(
114+
tracing_subscriber::fmt::layer()
115+
.with_file(true)
116+
.with_line_number(true)
117+
.with_level(true)
118+
.with_target(false)
119+
.with_test_writer()
120+
.with_filter(level_filter),
121+
);
122+
tracing::subscriber::set_default(registry)
112123
}
113124
}

stun-types/src/attribute/alternate.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,9 @@ mod tests {
156156
use super::*;
157157
use byteorder::{BigEndian, ByteOrder};
158158

159-
fn init() {
160-
crate::tests::test_init_log();
161-
}
162-
163159
#[test]
164160
fn alternate_server() {
165-
init();
161+
let _log = crate::tests::test_init_log();
166162
let addrs = &[
167163
"192.168.0.1:40000".parse().unwrap(),
168164
"[fd12:3456:789a:1::1]:41000".parse().unwrap(),
@@ -203,7 +199,7 @@ mod tests {
203199

204200
#[test]
205201
fn alternative_domain() {
206-
init();
202+
let _log = crate::tests::test_init_log();
207203
let dns = "example.com";
208204
let attr = AlternateDomain::new(dns);
209205
assert_eq!(attr.domain(), dns);

stun-types/src/attribute/error.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,9 @@ mod tests {
332332
use super::*;
333333
use crate::attribute::{AlternateServer, Nonce, Realm};
334334

335-
fn init() {
336-
crate::tests::test_init_log();
337-
}
338-
339335
#[test]
340336
fn error_code() {
341-
init();
337+
let _log = crate::tests::test_init_log();
342338
let codes = [300, 401, 699];
343339
for code in codes.iter().copied() {
344340
let reason = ErrorCode::default_reason_for_code(code);
@@ -360,6 +356,7 @@ mod tests {
360356

361357
#[test]
362358
fn error_code_parse_short() {
359+
let _log = crate::tests::test_init_log();
363360
let err = error_code_new(420);
364361
let raw = RawAttribute::from(&err);
365362
// no data
@@ -377,6 +374,7 @@ mod tests {
377374

378375
#[test]
379376
fn error_code_parse_wrong_implementation() {
377+
let _log = crate::tests::test_init_log();
380378
let err = error_code_new(420);
381379
let raw = RawAttribute::from(&err);
382380
// provide incorrectly typed data
@@ -390,6 +388,7 @@ mod tests {
390388

391389
#[test]
392390
fn error_code_parse_out_of_range_code() {
391+
let _log = crate::tests::test_init_log();
393392
let err = error_code_new(420);
394393
let raw = RawAttribute::from(&err);
395394
let mut data: Vec<_> = raw.into();
@@ -404,6 +403,7 @@ mod tests {
404403

405404
#[test]
406405
fn error_code_parse_invalid_reason() {
406+
let _log = crate::tests::test_init_log();
407407
let err = error_code_new(420);
408408
let raw = RawAttribute::from(&err);
409409
let mut data: Vec<_> = raw.into();
@@ -418,13 +418,15 @@ mod tests {
418418

419419
#[test]
420420
fn error_code_build_default_reason() {
421+
let _log = crate::tests::test_init_log();
421422
let err = ErrorCode::builder(420).build().unwrap();
422423
assert_eq!(err.code(), 420);
423424
assert!(err.reason().len() > 0);
424425
}
425426

426427
#[test]
427428
fn error_code_build_out_of_range() {
429+
let _log = crate::tests::test_init_log();
428430
assert!(matches!(
429431
ErrorCode::builder(700).build(),
430432
Err(StunWriteError::OutOfRange {
@@ -437,6 +439,7 @@ mod tests {
437439

438440
#[test]
439441
fn error_code_new_out_of_range() {
442+
let _log = crate::tests::test_init_log();
440443
assert!(matches!(
441444
ErrorCode::new(700, "some-reason"),
442445
Err(StunWriteError::OutOfRange {
@@ -449,7 +452,7 @@ mod tests {
449452

450453
#[test]
451454
fn unknown_attributes() {
452-
init();
455+
let _log = crate::tests::test_init_log();
453456
let mut unknown = UnknownAttributes::new(&[Realm::TYPE]);
454457
unknown.add_attribute(AlternateServer::TYPE);
455458
// duplicates ignored

stun-types/src/attribute/fingerprint.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,9 @@ mod tests {
104104
use super::*;
105105
use byteorder::{BigEndian, ByteOrder};
106106

107-
fn init() {
108-
crate::tests::test_init_log();
109-
}
110-
111107
#[test]
112108
fn fingerprint() {
113-
init();
109+
let _log = crate::tests::test_init_log();
114110
let val = [1; 4];
115111
let attr = Fingerprint::new(val);
116112
assert_eq!(attr.fingerprint(), &val);

stun-types/src/attribute/ice.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,9 @@ impl std::fmt::Display for IceControlling {
266266
mod tests {
267267
use super::*;
268268

269-
fn init() {
270-
crate::tests::test_init_log();
271-
}
272-
273269
#[test]
274270
fn priority() {
275-
init();
271+
let _log = crate::tests::test_init_log();
276272
let val = 100;
277273
let priority = Priority::new(val);
278274
assert_eq!(priority.priority(), val);
@@ -303,7 +299,7 @@ mod tests {
303299

304300
#[test]
305301
fn use_candidate() {
306-
init();
302+
let _log = crate::tests::test_init_log();
307303
let use_candidate = UseCandidate::default();
308304
assert_eq!(use_candidate.length(), 0);
309305
let raw = RawAttribute::from(&use_candidate);
@@ -320,7 +316,7 @@ mod tests {
320316

321317
#[test]
322318
fn ice_controlling() {
323-
init();
319+
let _log = crate::tests::test_init_log();
324320
let tb = 100;
325321
let attr = IceControlling::new(tb);
326322
assert_eq!(attr.tie_breaker(), tb);
@@ -351,7 +347,7 @@ mod tests {
351347

352348
#[test]
353349
fn ice_controlled() {
354-
init();
350+
let _log = crate::tests::test_init_log();
355351
let tb = 100;
356352
let attr = IceControlled::new(tb);
357353
assert_eq!(attr.tie_breaker(), tb);

stun-types/src/attribute/integrity.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,9 @@ mod tests {
292292
use super::*;
293293
use byteorder::{BigEndian, ByteOrder};
294294

295-
fn init() {
296-
crate::tests::test_init_log();
297-
}
298-
299295
#[test]
300296
fn message_integrity() {
301-
init();
297+
let _log = crate::tests::test_init_log();
302298
let val = [1; 20];
303299
let attr = MessageIntegrity::new(val);
304300
assert_eq!(attr.hmac(), &val);
@@ -331,7 +327,7 @@ mod tests {
331327

332328
#[test]
333329
fn message_integrity_sha256() {
334-
init();
330+
let _log = crate::tests::test_init_log();
335331
let val = [1; 32];
336332
let attr = MessageIntegritySha256::new(&val).unwrap();
337333
assert_eq!(attr.hmac(), &val);
@@ -361,7 +357,7 @@ mod tests {
361357

362358
#[test]
363359
fn message_integrity_sha256_new_too_large() {
364-
init();
360+
let _log = crate::tests::test_init_log();
365361
let val = [1; 33];
366362
assert!(matches!(
367363
MessageIntegritySha256::new(&val),
@@ -374,7 +370,7 @@ mod tests {
374370

375371
#[test]
376372
fn message_integrity_sha256_new_too_small() {
377-
init();
373+
let _log = crate::tests::test_init_log();
378374
let val = [1; 15];
379375
assert!(matches!(
380376
MessageIntegritySha256::new(&val),
@@ -387,7 +383,7 @@ mod tests {
387383

388384
#[test]
389385
fn message_integrity_sha256_new_not_multiple_of_4() {
390-
init();
386+
let _log = crate::tests::test_init_log();
391387
let val = [1; 19];
392388
assert!(matches!(
393389
MessageIntegritySha256::new(&val),

0 commit comments

Comments
 (0)