Skip to content

Commit e28ae8b

Browse files
the10thWiztopecongiro
authored andcommitted
Init Logger for unit tests (#3829)
Add `init_log()` function which attempts to init logger, and ignores failure. The function is called at the beginning of every test, and will fail if the logger is already initialized. The logger must be initialized in every test, becuase cargo runs the tests in parallel, with no garentees about the order and time each starts.
1 parent dbd8936 commit e28ae8b

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/test/configuration_snippet.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ impl ConfigCodeBlock {
247247

248248
#[test]
249249
fn configuration_snippet_tests() {
250+
super::init_log();
250251
let blocks = get_code_blocks();
251252
let failures = blocks
252253
.iter()

src/test/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ const SKIP_FILE_WHITE_LIST: &[&str] = &[
4040
"cfg_mod/wasm32.rs",
4141
];
4242

43+
fn init_log() {
44+
let _ = env_logger::builder().is_test(true).try_init();
45+
}
46+
4347
struct TestSetting {
4448
/// The size of the stack of the thread that run tests.
4549
stack_size: usize,
@@ -137,6 +141,7 @@ fn verify_config_used(path: &Path, config_name: &str) {
137141

138142
#[test]
139143
fn verify_config_test_names() {
144+
init_log();
140145
for path in &[
141146
Path::new("tests/source/configs"),
142147
Path::new("tests/target/configs"),
@@ -169,6 +174,7 @@ fn write_message(msg: &str) {
169174
// exactly.
170175
#[test]
171176
fn system_tests() {
177+
init_log();
172178
run_test_with(&TestSetting::default(), || {
173179
// Get all files in the tests/source directory.
174180
let files = get_test_files(Path::new("tests/source"), true);
@@ -189,6 +195,7 @@ fn system_tests() {
189195
// The only difference is the coverage mode.
190196
#[test]
191197
fn coverage_tests() {
198+
init_log();
192199
let files = get_test_files(Path::new("tests/coverage/source"), true);
193200
let (_reports, count, fails) = check_files(files, &None);
194201

@@ -198,20 +205,23 @@ fn coverage_tests() {
198205

199206
#[test]
200207
fn checkstyle_test() {
208+
init_log();
201209
let filename = "tests/writemode/source/fn-single-line.rs";
202210
let expected_filename = "tests/writemode/target/checkstyle.xml";
203211
assert_output(Path::new(filename), Path::new(expected_filename));
204212
}
205213

206214
#[test]
207215
fn json_test() {
216+
init_log();
208217
let filename = "tests/writemode/source/json.rs";
209218
let expected_filename = "tests/writemode/target/output.json";
210219
assert_output(Path::new(filename), Path::new(expected_filename));
211220
}
212221

213222
#[test]
214223
fn modified_test() {
224+
init_log();
215225
use std::io::BufRead;
216226

217227
// Test "modified" output
@@ -297,6 +307,7 @@ fn assert_output(source: &Path, expected_filename: &Path) {
297307
// rustfmt.
298308
#[test]
299309
fn idempotence_tests() {
310+
init_log();
300311
run_test_with(&TestSetting::default(), || {
301312
// these tests require nightly
302313
if !is_nightly_channel!() {
@@ -321,6 +332,7 @@ fn idempotence_tests() {
321332
// no warnings are emitted.
322333
#[test]
323334
fn self_tests() {
335+
init_log();
324336
// Issue-3443: these tests require nightly
325337
if !is_nightly_channel!() {
326338
return;
@@ -359,6 +371,7 @@ fn self_tests() {
359371

360372
#[test]
361373
fn stdin_formatting_smoke_test() {
374+
init_log();
362375
let input = Input::Text("fn main () {}".to_owned());
363376
let mut config = Config::default();
364377
config.set().emit_mode(EmitMode::Stdout);
@@ -377,6 +390,7 @@ fn stdin_formatting_smoke_test() {
377390

378391
#[test]
379392
fn stdin_parser_panic_caught() {
393+
init_log();
380394
// See issue #3239.
381395
for text in ["{", "}"].iter().cloned().map(String::from) {
382396
let mut buf = vec![];
@@ -391,6 +405,7 @@ fn stdin_parser_panic_caught() {
391405
/// when embedding Rustfmt (e.g. inside RLS).
392406
#[test]
393407
fn stdin_works_with_modified_lines() {
408+
init_log();
394409
let input = "\nfn\n some( )\n{\n}\nfn main () {}\n";
395410
let output = "1 6 2\nfn some() {}\nfn main() {}\n";
396411

@@ -413,6 +428,7 @@ fn stdin_works_with_modified_lines() {
413428

414429
#[test]
415430
fn stdin_disable_all_formatting_test() {
431+
init_log();
416432
match option_env!("CFG_RELEASE_CHANNEL") {
417433
None | Some("nightly") => {}
418434
// These tests require nightly.
@@ -441,6 +457,7 @@ fn stdin_disable_all_formatting_test() {
441457

442458
#[test]
443459
fn format_lines_errors_are_reported() {
460+
init_log();
444461
let long_identifier = String::from_utf8(vec![b'a'; 239]).unwrap();
445462
let input = Input::Text(format!("fn {}() {{}}", long_identifier));
446463
let mut config = Config::default();
@@ -452,6 +469,7 @@ fn format_lines_errors_are_reported() {
452469

453470
#[test]
454471
fn format_lines_errors_are_reported_with_tabs() {
472+
init_log();
455473
let long_identifier = String::from_utf8(vec![b'a'; 97]).unwrap();
456474
let input = Input::Text(format!("fn a() {{\n\t{}\n}}", long_identifier));
457475
let mut config = Config::default();
@@ -719,6 +737,7 @@ fn get_target(file_name: &Path, target: Option<&str>) -> PathBuf {
719737

720738
#[test]
721739
fn rustfmt_diff_make_diff_tests() {
740+
init_log();
722741
let diff = make_diff("a\nb\nc\nd", "a\ne\nc\nd", 3);
723742
assert_eq!(
724743
diff,
@@ -738,6 +757,7 @@ fn rustfmt_diff_make_diff_tests() {
738757

739758
#[test]
740759
fn rustfmt_diff_no_diff_test() {
760+
init_log();
741761
let diff = make_diff("a\nb\nc\nd", "a\nb\nc\nd", 3);
742762
assert_eq!(diff, vec![]);
743763
}
@@ -772,6 +792,7 @@ impl<'a> Iterator for CharsIgnoreNewlineRepr<'a> {
772792

773793
#[test]
774794
fn string_eq_ignore_newline_repr_test() {
795+
init_log();
775796
assert!(string_eq_ignore_newline_repr("", ""));
776797
assert!(!string_eq_ignore_newline_repr("", "abc"));
777798
assert!(!string_eq_ignore_newline_repr("abc", ""));
@@ -833,6 +854,7 @@ fn rustfmt() -> PathBuf {
833854

834855
#[test]
835856
fn verify_check_works() {
857+
init_log();
836858
let temp_file = make_temp_file("temp_check.rs");
837859

838860
Command::new(rustfmt().to_str().unwrap())

0 commit comments

Comments
 (0)