Skip to content

Commit

Permalink
Init Logger for unit tests (rust-lang#3829)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
the10thWiz authored and topecongiro committed Oct 4, 2019
1 parent dbd8936 commit e28ae8b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/test/configuration_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ impl ConfigCodeBlock {

#[test]
fn configuration_snippet_tests() {
super::init_log();
let blocks = get_code_blocks();
let failures = blocks
.iter()
Expand Down
22 changes: 22 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ const SKIP_FILE_WHITE_LIST: &[&str] = &[
"cfg_mod/wasm32.rs",
];

fn init_log() {
let _ = env_logger::builder().is_test(true).try_init();
}

struct TestSetting {
/// The size of the stack of the thread that run tests.
stack_size: usize,
Expand Down Expand Up @@ -137,6 +141,7 @@ fn verify_config_used(path: &Path, config_name: &str) {

#[test]
fn verify_config_test_names() {
init_log();
for path in &[
Path::new("tests/source/configs"),
Path::new("tests/target/configs"),
Expand Down Expand Up @@ -169,6 +174,7 @@ fn write_message(msg: &str) {
// exactly.
#[test]
fn system_tests() {
init_log();
run_test_with(&TestSetting::default(), || {
// Get all files in the tests/source directory.
let files = get_test_files(Path::new("tests/source"), true);
Expand All @@ -189,6 +195,7 @@ fn system_tests() {
// The only difference is the coverage mode.
#[test]
fn coverage_tests() {
init_log();
let files = get_test_files(Path::new("tests/coverage/source"), true);
let (_reports, count, fails) = check_files(files, &None);

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

#[test]
fn checkstyle_test() {
init_log();
let filename = "tests/writemode/source/fn-single-line.rs";
let expected_filename = "tests/writemode/target/checkstyle.xml";
assert_output(Path::new(filename), Path::new(expected_filename));
}

#[test]
fn json_test() {
init_log();
let filename = "tests/writemode/source/json.rs";
let expected_filename = "tests/writemode/target/output.json";
assert_output(Path::new(filename), Path::new(expected_filename));
}

#[test]
fn modified_test() {
init_log();
use std::io::BufRead;

// Test "modified" output
Expand Down Expand Up @@ -297,6 +307,7 @@ fn assert_output(source: &Path, expected_filename: &Path) {
// rustfmt.
#[test]
fn idempotence_tests() {
init_log();
run_test_with(&TestSetting::default(), || {
// these tests require nightly
if !is_nightly_channel!() {
Expand All @@ -321,6 +332,7 @@ fn idempotence_tests() {
// no warnings are emitted.
#[test]
fn self_tests() {
init_log();
// Issue-3443: these tests require nightly
if !is_nightly_channel!() {
return;
Expand Down Expand Up @@ -359,6 +371,7 @@ fn self_tests() {

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

#[test]
fn stdin_parser_panic_caught() {
init_log();
// See issue #3239.
for text in ["{", "}"].iter().cloned().map(String::from) {
let mut buf = vec![];
Expand All @@ -391,6 +405,7 @@ fn stdin_parser_panic_caught() {
/// when embedding Rustfmt (e.g. inside RLS).
#[test]
fn stdin_works_with_modified_lines() {
init_log();
let input = "\nfn\n some( )\n{\n}\nfn main () {}\n";
let output = "1 6 2\nfn some() {}\nfn main() {}\n";

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

#[test]
fn stdin_disable_all_formatting_test() {
init_log();
match option_env!("CFG_RELEASE_CHANNEL") {
None | Some("nightly") => {}
// These tests require nightly.
Expand Down Expand Up @@ -441,6 +457,7 @@ fn stdin_disable_all_formatting_test() {

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

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

#[test]
fn rustfmt_diff_make_diff_tests() {
init_log();
let diff = make_diff("a\nb\nc\nd", "a\ne\nc\nd", 3);
assert_eq!(
diff,
Expand All @@ -738,6 +757,7 @@ fn rustfmt_diff_make_diff_tests() {

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

#[test]
fn string_eq_ignore_newline_repr_test() {
init_log();
assert!(string_eq_ignore_newline_repr("", ""));
assert!(!string_eq_ignore_newline_repr("", "abc"));
assert!(!string_eq_ignore_newline_repr("abc", ""));
Expand Down Expand Up @@ -833,6 +854,7 @@ fn rustfmt() -> PathBuf {

#[test]
fn verify_check_works() {
init_log();
let temp_file = make_temp_file("temp_check.rs");

Command::new(rustfmt().to_str().unwrap())
Expand Down

0 comments on commit e28ae8b

Please sign in to comment.