Skip to content

Commit 099e2ca

Browse files
authored
Merge pull request #1486 from LegNeato/test_record
Add `BINDGEN_OVERWRITE_EXPECTED` for recording tests
2 parents ab538ee + ebc1874 commit 099e2ca

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

CONTRIBUTING.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ $ cargo test
118118
As long as you aren't making any changes to `bindgen`'s output, running this
119119
should be sufficient to test your local modifications.
120120

121+
You may set the `BINDGEN_OVERWRITE_EXPECTED` environment variable to overwrite
122+
the expected bindings with `bindgen`'s current output:
123+
124+
```
125+
$BINDGEN_OVERWRITE_EXPECTED=1 cargo test
126+
```
127+
121128
### Testing Generated Bindings
122129

123130
If your local changes are introducing expected modifications in the

build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ mod testgen {
5656
dst,
5757
"test_header!(header_{}, {:?});",
5858
func,
59-
entry.path()
59+
entry.path(),
6060
).unwrap();
6161
}
6262
_ => {}

tests/expectations/tests/enum.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

510
#[repr(u32)]
611
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]

tests/tests.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extern crate bindgen;
44
extern crate shlex;
55

66
use bindgen::{Builder, builder, clang_version};
7+
use std::env;
78
use std::fs;
89
use std::io::{self, BufRead, BufReader, Error, ErrorKind, Read, Write};
910
use std::path::PathBuf;
@@ -14,6 +15,9 @@ use std::sync::{Once, ONCE_INIT};
1415
mod options;
1516
use options::builder_from_flags;
1617

18+
/// The environment variable that determines if test expectations are overwritten.
19+
static OVERWRITE_ENV_VAR: &str = "BINDGEN_OVERWRITE_EXPECTED";
20+
1721
// Run `rustfmt` on the given source string and return a tuple of the formatted
1822
// bindings, and rustfmt's stderr.
1923
fn rustfmt(source: String) -> (String, String) {
@@ -197,8 +201,8 @@ fn compare_generated_header(
197201
}
198202
}
199203

200-
// Override the diff.
201-
{
204+
// Overwrite the expectation with actual output.
205+
if env::var_os(OVERWRITE_ENV_VAR).is_some() {
202206
let mut expectation_file = fs::File::create(&expectation)?;
203207
expectation_file.write_all(actual.as_bytes())?;
204208
}

0 commit comments

Comments
 (0)