diff options
-rw-r--r-- | tests/tests.rs | 138 |
1 files changed, 86 insertions, 52 deletions
diff --git a/tests/tests.rs b/tests/tests.rs index 5611baaf..cc64cfdd 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -115,6 +115,62 @@ The latest `rustfmt` is required to run the `bindgen` test suite. Install (bindings, stderr) } +fn should_overwrite_expected() -> bool { + if let Some(var) = env::var_os("BINDGEN_OVERWRITE_EXPECTED") { + if var == "1" { + return true; + } + if var != "0" && var != "" { + panic!("Invalid value of BINDGEN_OVERWRITE_EXPECTED"); + } + } + false +} + +fn error_diff_mismatch( + actual: &str, + expected: &str, + header: Option<&Path>, + filename: &Path, +) -> Result<(), Error> { + println!("diff expected generated"); + println!("--- expected: {:?}", filename); + if let Some(header) = header { + println!("+++ generated from: {:?}", header); + } + + for diff in diff::lines(&expected, &actual) { + match diff { + diff::Result::Left(l) => println!("-{}", l), + diff::Result::Both(l, _) => println!(" {}", l), + diff::Result::Right(r) => println!("+{}", r), + } + } + + if should_overwrite_expected() { + // Overwrite the expectation with actual output. + let mut expectation_file = fs::File::create(filename)?; + expectation_file.write_all(actual.as_bytes())?; + } + + if let Some(var) = env::var_os("BINDGEN_TESTS_DIFFTOOL") { + //usecase: var = "meld" -> You can hand check differences + let name = match filename.components().last() { + Some(std::path::Component::Normal(name)) => name, + _ => panic!("Why is the header variable so weird?"), + }; + let actual_result_path = + PathBuf::from(env::var("OUT_DIR").unwrap()).join(name); + let mut actual_result_file = fs::File::create(&actual_result_path)?; + actual_result_file.write_all(actual.as_bytes())?; + std::process::Command::new(var) + .args(&[filename, &actual_result_path]) + .output()?; + } + + return Err(Error::new(ErrorKind::Other, "Header and binding differ! Run with BINDGEN_OVERWRITE_EXPECTED=1 in the environment to automatically overwrite the expectation or with BINDGEN_TESTS_DIFFTOOL=meld to do this manually.")); +} + fn compare_generated_header( header: &Path, builder: BuilderState, @@ -211,46 +267,12 @@ fn compare_generated_header( if actual != expected { println!("{}", rustfmt_stderr); - - println!("diff expected generated"); - println!("--- expected: {:?}", looked_at.last().unwrap()); - println!("+++ generated from: {:?}", header); - - for diff in diff::lines(&expected, &actual) { - match diff { - diff::Result::Left(l) => println!("-{}", l), - diff::Result::Both(l, _) => println!(" {}", l), - diff::Result::Right(r) => println!("+{}", r), - } - } - - if let Some(var) = env::var_os("BINDGEN_OVERWRITE_EXPECTED") { - if var == "1" { - // Overwrite the expectation with actual output. - let mut expectation_file = - fs::File::create(looked_at.last().unwrap())?; - expectation_file.write_all(actual.as_bytes())?; - } else if var != "0" && var != "" { - panic!("Invalid value of BINDGEN_OVERWRITE_EXPECTED"); - } - } - - if let Some(var) = env::var_os("BINDGEN_TESTS_DIFFTOOL") { - //usecase: var = "meld" -> You can hand check differences - let filename = match header.components().last() { - Some(std::path::Component::Normal(name)) => name, - _ => panic!("Why is the header variable so weird?"), - }; - let actual_result_path = - PathBuf::from(env::var("OUT_DIR").unwrap()).join(filename); - let mut actual_result_file = fs::File::create(&actual_result_path)?; - actual_result_file.write_all(actual.as_bytes())?; - std::process::Command::new(var) - .args(&[looked_at.last().unwrap(), &actual_result_path]) - .output()?; - } - - return Err(Error::new(ErrorKind::Other, "Header and binding differ! Run with BINDGEN_OVERWRITE_EXPECTED=1 in the environment to automatically overwrite the expectation or with BINDGEN_TESTS_DIFFTOOL=meld to do this manually.")); + return error_diff_mismatch( + &actual, + &expected, + Some(header), + looked_at.last().unwrap(), + ); } if let Some(roundtrip_builder) = roundtrip_builder { @@ -485,6 +507,10 @@ fn test_multiple_header_calls_in_builder() { let (actual, stderr) = rustfmt(actual); println!("{}", stderr); + let expected_filename = concat!( + env!("CARGO_MANIFEST_DIR"), + "/tests/expectations/tests/test_multiple_header_calls_in_builder.rs" + ); let expected = include_str!(concat!( env!("CARGO_MANIFEST_DIR"), "/tests/expectations/tests/test_multiple_header_calls_in_builder.rs" @@ -493,16 +519,13 @@ fn test_multiple_header_calls_in_builder() { if actual != expected { println!("Generated bindings differ from expected!"); - - for diff in diff::lines(&actual, &expected) { - match diff { - diff::Result::Left(l) => println!("-{}", l), - diff::Result::Both(l, _) => println!(" {}", l), - diff::Result::Right(r) => println!("+{}", r), - } - } - - panic!(); + error_diff_mismatch( + &actual, + &expected, + None, + Path::new(expected_filename), + ) + .unwrap(); } } @@ -551,13 +574,24 @@ fn test_mixed_header_and_header_contents() { let (actual, stderr) = rustfmt(actual); println!("{}", stderr); + let expected_filename = concat!( + env!("CARGO_MANIFEST_DIR"), + "/tests/expectations/tests/test_mixed_header_and_header_contents.rs" + ); let expected = include_str!(concat!( env!("CARGO_MANIFEST_DIR"), "/tests/expectations/tests/test_mixed_header_and_header_contents.rs" )); let (expected, _) = rustfmt(expected.to_string()); - - assert_eq!(expected, actual); + if expected != actual { + error_diff_mismatch( + &actual, + &expected, + None, + Path::new(expected_filename), + ) + .unwrap(); + } } #[test] |