Skip to content

Commit

Permalink
tests: Added option to hand check test differences.
Browse files Browse the repository at this point in the history
  • Loading branch information
Volker-Weissmann authored and emilio committed Nov 25, 2020
1 parent 2c72903 commit db3d170
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Expand Up @@ -125,6 +125,12 @@ the expected bindings with `bindgen`'s current output:
$ BINDGEN_OVERWRITE_EXPECTED=1 cargo test
```

If you set the BINDGEN_TESTS_DIFFTOOL environment variable, `cargo test` will
execute $BINDGEN_TESTS_DIFFTOOL /path/of/expected/output /path/of/actual/output
when the expected output differs from the actual output. You can use this to
hand check differences by setting it to e.g. "meld" (assuming you have meld
installed).

If you're not changing command line arguments, you may want to set
`BINDGEN_DISABLE_ROUNDTRIP_TEST` to avoid a lot of tests for round-tripping of
those.
Expand Down
33 changes: 26 additions & 7 deletions tests/tests.rs
Expand Up @@ -174,7 +174,7 @@ fn compare_generated_header(
expectation.push(file_name);
expectation.set_extension("rs");
expectation_file = fs::File::open(&expectation).ok();
looked_at.push(expectation);
looked_at.push(expectation.clone());
}

let mut expected = String::new();
Expand Down Expand Up @@ -233,14 +233,33 @@ fn compare_generated_header(
}
}

// Overwrite the expectation with actual output.
if env::var_os("BINDGEN_OVERWRITE_EXPECTED").is_some() {
let mut expectation_file =
fs::File::create(looked_at.last().unwrap())?;
expectation_file.write_all(actual.as_bytes())?;
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."));
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."));
}

if check_roundtrip {
Expand Down

0 comments on commit db3d170

Please sign in to comment.