Skip to content

Commit

Permalink
test: disable formatting on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdrz committed Dec 4, 2024
1 parent 3880196 commit 879cbcb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bindgen-tests/tests/headers/derive-and-attribute-order.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// bindgen-flags: --no-layout-tests
// bindgen-parse-callbacks: derive-uppercase-serialize=color
// bindgen-skip-formatting
typedef struct {
int red;
int green;
Expand Down
26 changes: 19 additions & 7 deletions bindgen-tests/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,23 @@ fn compare_generated_header(
looked_at,
),
};

let do_formatting = builder.do_formatting;
let (builder, roundtrip_builder) = builder.into_builder(check_roundtrip)?;

// We skip the generate() error here so we get a full diff below
let actual = match builder.generate() {
Ok(bindings) => format_code(bindings.to_string()).map_err(|err| {
Error::new(
ErrorKind::Other,
format!("Cannot parse the generated bindings: {err}"),
)
})?,
Ok(bindings) => {
if do_formatting {
format_code(bindings.to_string()).map_err(|err| {
Error::new(
ErrorKind::Other,
format!("Cannot parse the generated bindings: {err}"),
)
})?
} else {
bindings.to_string()
}
}
Err(_) => "/* error generating bindings */\n".into(),
};

Expand Down Expand Up @@ -237,6 +243,7 @@ fn builder() -> Builder {
struct BuilderState {
builder: Builder,
parse_callbacks: Option<String>,
do_formatting: bool,
}

impl BuilderState {
Expand All @@ -255,6 +262,7 @@ impl BuilderState {
Some(BuilderState {
builder,
parse_callbacks: self.parse_callbacks,
do_formatting: self.do_formatting,
})
} else {
None
Expand All @@ -273,6 +281,7 @@ fn create_bindgen_builder(header: &Path) -> Result<BuilderState, Error> {
// Scoop up bindgen-flags from test header
let mut flags = Vec::with_capacity(2);
let mut parse_callbacks = None;
let mut do_formatting = true;

for line in reader.lines() {
let line = line?;
Expand All @@ -298,6 +307,8 @@ fn create_bindgen_builder(header: &Path) -> Result<BuilderState, Error> {
let parse_cb =
line.split("bindgen-parse-callbacks: ").last().unwrap();
parse_callbacks = Some(parse_cb.to_owned());
} else if line.contains("bindgen-skip-formatting") {
do_formatting = false;
}
}

Expand Down Expand Up @@ -345,6 +356,7 @@ fn create_bindgen_builder(header: &Path) -> Result<BuilderState, Error> {
Ok(BuilderState {
builder,
parse_callbacks,
do_formatting,
})
}

Expand Down

0 comments on commit 879cbcb

Please sign in to comment.