Test host-only code with cross-compilation.
Since Crubit itself only is expected to run on x86 (right now?), cross-compiling to arm, it makes sense to do the same in the unit tests. This makes the unit tests more like production, and avoids needing to actively work to get the rest of the internals working on arm. It's possible, but nah.
This was constructed by parameterizing the tests using an environment variable. This was easier than parameterizing the tests using loops, rstest, etc., since those would require adding each parameter to each function, when really we want to do ~all parameters for these (presumably).
This testing strategy only really works because both arm-linux and x86-linux use the Itanium ABI, which is effectively identical. If/when we add non-Itanium support, we'd need to add separate files, and restrict the tests to e.g. only run on Itanium platforms.
There is one major integration-y test that doesn't cross-compile for Arm, generate_bindings_and_metadata_test, but I think it's of relatively low value given the remaining "unit" tests and the integration tests. I'm fairly comfortable saying we test Arm -- we may even test it _too_ much, given how similar the two platforms are. We can mark the bug as fixed: Arm is tested and works.
PiperOrigin-RevId: 527324587
diff --git a/rs_bindings_from_cc/ir_matchers.rs b/rs_bindings_from_cc/ir_matchers.rs
index 423ddf2..f0cd616 100644
--- a/rs_bindings_from_cc/ir_matchers.rs
+++ b/rs_bindings_from_cc/ir_matchers.rs
@@ -2,13 +2,14 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-/// Like `token_stream_matchers::assert_cc_matches!`, but expects `IR` instance as input. The macro
-/// converts the instance to its corresponding struct expression and matches the pattern against
-/// that. See the documentation of `token_stream_matchers` for more information.
+/// Like `token_stream_matchers::assert_cc_matches!`, but expects `IR` instance
+/// as input. The macro converts the instance to its corresponding struct
+/// expression and matches the pattern against that. See the documentation of
+/// `token_stream_matchers` for more information.
///
/// Example:
/// ```rust
-/// let ir = ir_from_cc("struct SomeStruct {};').unwrap();
+/// let ir = ir_from_cc(..., "struct SomeStruct {};').unwrap();
/// assert_ir_matches!(
/// ir,
/// quote! {
@@ -45,12 +46,12 @@
macro_rules! assert_items_match {
($items:expr, $patterns:expr $(,)*) => {
assert_eq!($items.len(), $patterns.len());
- for (idx, (item, pattern)) in $items.into_iter().zip($patterns).enumerate() {
- $crate::internal::match_item(&item, &pattern).expect(&format!(
- "input at position {} unexpectedly didn't match the pattern",
- &idx
- ));
- }
+ for (idx, (item, pattern)) in $items.into_iter().zip($patterns).enumerate() {
+ $crate::internal::match_item(&item, &pattern).expect(&format!(
+ "input at position {} unexpectedly didn't match the pattern",
+ &idx
+ ));
+ }
};
}
@@ -129,9 +130,13 @@
#[cfg(test)]
mod tests {
use super::*;
- use ir_testing::ir_from_cc;
use quote::quote;
+ /// We aren't testing platform-specific details, just the matchers.
+ fn ir_from_cc(header: &str) -> arc_anyhow::Result<ir::IR> {
+ ir_testing::ir_from_cc(multiplatform_testing::Platform::X86Linux, header)
+ }
+
#[test]
fn test_optional_trailing_comma() {
assert_ir_matches!(ir_from_cc("").unwrap(), quote! { FlatIR { ... }});
@@ -143,10 +148,7 @@
#[test]
fn test_assert_ir_matches_assumes_trailing_commas_in_groups() {
- assert_ir_matches!(
- ir_from_cc("").unwrap(),
- quote! {{... , }}
- );
+ assert_ir_matches!(ir_from_cc("").unwrap(), quote! {{... , }});
}
#[test]