Skip to content

Commit db3d170

Browse files
Volker-Weissmannemilio
authored andcommitted
tests: Added option to hand check test differences.
1 parent 2c72903 commit db3d170

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

CONTRIBUTING.md

+6
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ the expected bindings with `bindgen`'s current output:
125125
$ BINDGEN_OVERWRITE_EXPECTED=1 cargo test
126126
```
127127

128+
If you set the BINDGEN_TESTS_DIFFTOOL environment variable, `cargo test` will
129+
execute $BINDGEN_TESTS_DIFFTOOL /path/of/expected/output /path/of/actual/output
130+
when the expected output differs from the actual output. You can use this to
131+
hand check differences by setting it to e.g. "meld" (assuming you have meld
132+
installed).
133+
128134
If you're not changing command line arguments, you may want to set
129135
`BINDGEN_DISABLE_ROUNDTRIP_TEST` to avoid a lot of tests for round-tripping of
130136
those.

tests/tests.rs

+26-7
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ fn compare_generated_header(
174174
expectation.push(file_name);
175175
expectation.set_extension("rs");
176176
expectation_file = fs::File::open(&expectation).ok();
177-
looked_at.push(expectation);
177+
looked_at.push(expectation.clone());
178178
}
179179

180180
let mut expected = String::new();
@@ -233,14 +233,33 @@ fn compare_generated_header(
233233
}
234234
}
235235

236-
// Overwrite the expectation with actual output.
237-
if env::var_os("BINDGEN_OVERWRITE_EXPECTED").is_some() {
238-
let mut expectation_file =
239-
fs::File::create(looked_at.last().unwrap())?;
240-
expectation_file.write_all(actual.as_bytes())?;
236+
if let Some(var) = env::var_os("BINDGEN_OVERWRITE_EXPECTED") {
237+
if var == "1" {
238+
// Overwrite the expectation with actual output.
239+
let mut expectation_file =
240+
fs::File::create(looked_at.last().unwrap())?;
241+
expectation_file.write_all(actual.as_bytes())?;
242+
} else if var != "0" && var != "" {
243+
panic!("Invalid value of BINDGEN_OVERWRITE_EXPECTED");
244+
}
245+
}
246+
247+
if let Some(var) = env::var_os("BINDGEN_TESTS_DIFFTOOL") {
248+
//usecase: var = "meld" -> You can hand check differences
249+
let filename = match header.components().last() {
250+
Some(std::path::Component::Normal(name)) => name,
251+
_ => panic!("Why is the header variable so weird?"),
252+
};
253+
let actual_result_path =
254+
PathBuf::from(env::var("OUT_DIR").unwrap()).join(filename);
255+
let mut actual_result_file = fs::File::create(&actual_result_path)?;
256+
actual_result_file.write_all(actual.as_bytes())?;
257+
std::process::Command::new(var)
258+
.args(&[looked_at.last().unwrap(), &actual_result_path])
259+
.output()?;
241260
}
242261

243-
return Err(Error::new(ErrorKind::Other, "Header and binding differ! Run with BINDGEN_OVERWRITE_EXPECTED=1 in the environment to automatically overwrite the expectation."));
262+
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."));
244263
}
245264

246265
if check_roundtrip {

0 commit comments

Comments
 (0)