• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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="--rustfmt-bindings --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    -o "\"$BINDINGS\"" \
57    $FLAGS
58
59rustup run nightly rustfmt "$BINDINGS" || true
60
61dot -Tpng ir.dot -o ir.png
62
63echo
64echo "=== Input header ========================================================"
65echo
66
67cat "$TEST"
68
69echo
70echo "=== Generated bindings =================================================="
71echo
72
73cat "$BINDINGS"
74
75echo
76echo "=== Diff w/ expected bindings ==========================================="
77echo
78
79EXPECTED=${TEST/headers/expectations\/tests}
80EXPECTED=${EXPECTED/.hpp/.rs}
81EXPECTED=${EXPECTED/.h/.rs}
82
83rustup run nightly rustfmt "$EXPECTED" || true
84
85# Don't exit early if there is a diff.
86diff -U8 "$EXPECTED" "$BINDINGS" || true
87
88echo
89echo "=== Building bindings ==================================================="
90echo
91
92rustc --test -o "$TEST_BINDINGS_BINARY" "$BINDINGS" --crate-name bindgen_test_one
93
94echo
95echo "=== Testing bindings ===================================================="
96echo
97
98"$TEST_BINDINGS_BINARY"
99