]> git.proxmox.com Git - rustc.git/blob - vendor/env_logger-0.6.2/tests/regexp_filter.rs
New upstream version 1.40.0+dfsg1
[rustc.git] / vendor / env_logger-0.6.2 / tests / regexp_filter.rs
1 #[macro_use] extern crate log;
2 extern crate env_logger;
3
4 use std::process;
5 use std::env;
6 use std::str;
7
8 fn main() {
9 if env::var("LOG_REGEXP_TEST").ok() == Some(String::from("1")) {
10 child_main();
11 } else {
12 parent_main()
13 }
14 }
15
16 fn child_main() {
17 env_logger::init();
18 info!("XYZ Message");
19 }
20
21 fn run_child(rust_log: String) -> bool {
22 let exe = env::current_exe().unwrap();
23 let out = process::Command::new(exe)
24 .env("LOG_REGEXP_TEST", "1")
25 .env("RUST_LOG", rust_log)
26 .output()
27 .unwrap_or_else(|e| panic!("Unable to start child process: {}", e));
28 str::from_utf8(out.stderr.as_ref()).unwrap().contains("XYZ Message")
29 }
30
31 fn assert_message_printed(rust_log: &str) {
32 if !run_child(rust_log.to_string()) {
33 panic!("RUST_LOG={} should allow the test log message", rust_log)
34 }
35 }
36
37 fn assert_message_not_printed(rust_log: &str) {
38 if run_child(rust_log.to_string()) {
39 panic!("RUST_LOG={} should not allow the test log message", rust_log)
40 }
41 }
42
43 fn parent_main() {
44 // test normal log severity levels
45 assert_message_printed("info");
46 assert_message_not_printed("warn");
47
48 // test of regular expression filters
49 assert_message_printed("info/XYZ");
50 assert_message_not_printed("info/XXX");
51 }