Skip to content

Commit 59babdd

Browse files
committed
tests: Make some other tests auto-update with BINDGEN_OVERWRITE_EXPECTED=1
1 parent ae1b386 commit 59babdd

File tree

1 file changed

+86
-52
lines changed

1 file changed

+86
-52
lines changed

tests/tests.rs

Lines changed: 86 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,62 @@ The latest `rustfmt` is required to run the `bindgen` test suite. Install
115115
(bindings, stderr)
116116
}
117117

118+
fn should_overwrite_expected() -> bool {
119+
if let Some(var) = env::var_os("BINDGEN_OVERWRITE_EXPECTED") {
120+
if var == "1" {
121+
return true;
122+
}
123+
if var != "0" && var != "" {
124+
panic!("Invalid value of BINDGEN_OVERWRITE_EXPECTED");
125+
}
126+
}
127+
false
128+
}
129+
130+
fn error_diff_mismatch(
131+
actual: &str,
132+
expected: &str,
133+
header: Option<&Path>,
134+
filename: &Path,
135+
) -> Result<(), Error> {
136+
println!("diff expected generated");
137+
println!("--- expected: {:?}", filename);
138+
if let Some(header) = header {
139+
println!("+++ generated from: {:?}", header);
140+
}
141+
142+
for diff in diff::lines(&expected, &actual) {
143+
match diff {
144+
diff::Result::Left(l) => println!("-{}", l),
145+
diff::Result::Both(l, _) => println!(" {}", l),
146+
diff::Result::Right(r) => println!("+{}", r),
147+
}
148+
}
149+
150+
if should_overwrite_expected() {
151+
// Overwrite the expectation with actual output.
152+
let mut expectation_file = fs::File::create(filename)?;
153+
expectation_file.write_all(actual.as_bytes())?;
154+
}
155+
156+
if let Some(var) = env::var_os("BINDGEN_TESTS_DIFFTOOL") {
157+
//usecase: var = "meld" -> You can hand check differences
158+
let name = match filename.components().last() {
159+
Some(std::path::Component::Normal(name)) => name,
160+
_ => panic!("Why is the header variable so weird?"),
161+
};
162+
let actual_result_path =
163+
PathBuf::from(env::var("OUT_DIR").unwrap()).join(name);
164+
let mut actual_result_file = fs::File::create(&actual_result_path)?;
165+
actual_result_file.write_all(actual.as_bytes())?;
166+
std::process::Command::new(var)
167+
.args(&[filename, &actual_result_path])
168+
.output()?;
169+
}
170+
171+
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."));
172+
}
173+
118174
fn compare_generated_header(
119175
header: &Path,
120176
builder: BuilderState,
@@ -211,46 +267,12 @@ fn compare_generated_header(
211267

212268
if actual != expected {
213269
println!("{}", rustfmt_stderr);
214-
215-
println!("diff expected generated");
216-
println!("--- expected: {:?}", looked_at.last().unwrap());
217-
println!("+++ generated from: {:?}", header);
218-
219-
for diff in diff::lines(&expected, &actual) {
220-
match diff {
221-
diff::Result::Left(l) => println!("-{}", l),
222-
diff::Result::Both(l, _) => println!(" {}", l),
223-
diff::Result::Right(r) => println!("+{}", r),
224-
}
225-
}
226-
227-
if let Some(var) = env::var_os("BINDGEN_OVERWRITE_EXPECTED") {
228-
if var == "1" {
229-
// Overwrite the expectation with actual output.
230-
let mut expectation_file =
231-
fs::File::create(looked_at.last().unwrap())?;
232-
expectation_file.write_all(actual.as_bytes())?;
233-
} else if var != "0" && var != "" {
234-
panic!("Invalid value of BINDGEN_OVERWRITE_EXPECTED");
235-
}
236-
}
237-
238-
if let Some(var) = env::var_os("BINDGEN_TESTS_DIFFTOOL") {
239-
//usecase: var = "meld" -> You can hand check differences
240-
let filename = match header.components().last() {
241-
Some(std::path::Component::Normal(name)) => name,
242-
_ => panic!("Why is the header variable so weird?"),
243-
};
244-
let actual_result_path =
245-
PathBuf::from(env::var("OUT_DIR").unwrap()).join(filename);
246-
let mut actual_result_file = fs::File::create(&actual_result_path)?;
247-
actual_result_file.write_all(actual.as_bytes())?;
248-
std::process::Command::new(var)
249-
.args(&[looked_at.last().unwrap(), &actual_result_path])
250-
.output()?;
251-
}
252-
253-
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."));
270+
return error_diff_mismatch(
271+
&actual,
272+
&expected,
273+
Some(header),
274+
looked_at.last().unwrap(),
275+
);
254276
}
255277

256278
if let Some(roundtrip_builder) = roundtrip_builder {
@@ -485,6 +507,10 @@ fn test_multiple_header_calls_in_builder() {
485507
let (actual, stderr) = rustfmt(actual);
486508
println!("{}", stderr);
487509

510+
let expected_filename = concat!(
511+
env!("CARGO_MANIFEST_DIR"),
512+
"/tests/expectations/tests/test_multiple_header_calls_in_builder.rs"
513+
);
488514
let expected = include_str!(concat!(
489515
env!("CARGO_MANIFEST_DIR"),
490516
"/tests/expectations/tests/test_multiple_header_calls_in_builder.rs"
@@ -493,16 +519,13 @@ fn test_multiple_header_calls_in_builder() {
493519

494520
if actual != expected {
495521
println!("Generated bindings differ from expected!");
496-
497-
for diff in diff::lines(&actual, &expected) {
498-
match diff {
499-
diff::Result::Left(l) => println!("-{}", l),
500-
diff::Result::Both(l, _) => println!(" {}", l),
501-
diff::Result::Right(r) => println!("+{}", r),
502-
}
503-
}
504-
505-
panic!();
522+
error_diff_mismatch(
523+
&actual,
524+
&expected,
525+
None,
526+
Path::new(expected_filename),
527+
)
528+
.unwrap();
506529
}
507530
}
508531

@@ -551,13 +574,24 @@ fn test_mixed_header_and_header_contents() {
551574
let (actual, stderr) = rustfmt(actual);
552575
println!("{}", stderr);
553576

577+
let expected_filename = concat!(
578+
env!("CARGO_MANIFEST_DIR"),
579+
"/tests/expectations/tests/test_mixed_header_and_header_contents.rs"
580+
);
554581
let expected = include_str!(concat!(
555582
env!("CARGO_MANIFEST_DIR"),
556583
"/tests/expectations/tests/test_mixed_header_and_header_contents.rs"
557584
));
558585
let (expected, _) = rustfmt(expected.to_string());
559-
560-
assert_eq!(expected, actual);
586+
if expected != actual {
587+
error_diff_mismatch(
588+
&actual,
589+
&expected,
590+
None,
591+
Path::new(expected_filename),
592+
)
593+
.unwrap();
594+
}
561595
}
562596

563597
#[test]

0 commit comments

Comments
 (0)