1#!/usr/bin/env bash 2 3# Usage: 4# 5# ./tests/test-one.sh <fuzzy-name> 6# 7# Generate bindings for the first match of `./tests/headers/*<fuzzy-name>*`, use 8# `rustc` to compile the bindings with unit tests enabled, and run the generated 9# layout tests. 10 11set -eu 12 13if [ $# -ne 1 ]; then 14 echo "Usage: $0 <fuzzy-name>" 15 exit 1 16fi 17 18cd "$(dirname "$0")" 19cd .. 20 21export RUST_BACKTRACE=1 22 23unique_fuzzy_file() { 24 local pattern="$1" 25 local results="$(find ./tests/headers -type f | egrep -i "$pattern")" 26 local num_results=$(echo "$results" | wc -l) 27 28 if [[ -z "$results" ]]; then 29 >&2 echo "ERROR: no files found with pattern \"$pattern\"" 30 exit 1 31 elif [[ "$num_results" -ne 1 ]]; then 32 >&2 echo "ERROR: Expected exactly 1 result, got $num_results:" 33 >&2 echo "$results" 34 exit 1 35 fi 36 37 echo "$results" 38} 39 40TEST="$(unique_fuzzy_file "$1")" 41 42BINDINGS=$(mktemp -t bindings.rs.XXXXXX) 43TEST_BINDINGS_BINARY=$(mktemp -t bindings.XXXXXX) 44 45FLAGS="$(grep "// bindgen-flags: " "$TEST" || echo)" 46FLAGS="${FLAGS/\/\/ bindgen\-flags:/}" 47# Prepend the default flags added in test.rs's `create_bindgen_builder`. 48FLAGS="--with-derive-default --raw-line '' --raw-line '#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]' --raw-line '' $FLAGS" 49 50 51eval ../target/debug/bindgen \ 52 "\"$TEST\"" \ 53 --emit-ir \ 54 --emit-ir-graphviz ir.dot \ 55 --emit-clang-ast \ 56 --formatter prettyplease \ 57 -o "\"$BINDINGS\"" \ 58 $FLAGS 59 60dot -Tpng ir.dot -o ir.png 61 62echo 63echo "=== Input header ========================================================" 64echo 65 66cat "$TEST" 67 68echo 69echo "=== Generated bindings ==================================================" 70echo 71 72cat "$BINDINGS" 73 74echo 75echo "=== Diff w/ expected bindings ===========================================" 76echo 77 78EXPECTED=${TEST/headers/expectations\/tests} 79EXPECTED=${EXPECTED/.hpp/.rs} 80EXPECTED=${EXPECTED/.h/.rs} 81 82# Don't exit early if there is a diff. 83diff -U8 "$EXPECTED" "$BINDINGS" || true 84 85echo 86echo "=== Building bindings ===================================================" 87echo 88 89rustc --test -o "$TEST_BINDINGS_BINARY" "$BINDINGS" --crate-name bindgen_test_one 90 91echo 92echo "=== Testing bindings ====================================================" 93echo 94 95"$TEST_BINDINGS_BINARY" 96