summaryrefslogtreecommitdiff
path: root/tests/test-one.sh
blob: 91da55b1a5e597bf2d8757de75094dd90f781fef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env bash

# Usage:
#
#     ./tests/test-one.sh <fuzzy-name>
#
# Generate bindings for the first match of `./tests/headers/*<fuzzy-name>*`, use
# `rustc` to compile the bindings with unit tests enabled, and run the generated
# layout tests.

set -eu

if [ $# -ne 1 ]; then
    echo "Usage: $0 <fuzzy-name>"
    exit 1
fi

cd "$(dirname "$0")"
cd ..

export RUST_BACKTRACE=1

unique_fuzzy_file() {
    local pattern="$1"
    local results="$(find ./tests/headers -type f | egrep -i "$pattern")"
    local num_results=$(echo "$results" | wc -l)

    if [[ -z "$results" ]]; then
        >&2 echo "ERROR: no files found with pattern \"$pattern\""
        exit 1
    elif [[ "$num_results" -ne 1 ]]; then
        >&2 echo "ERROR: Expected exactly 1 result, got $num_results:"
        >&2 echo "$results"
        exit 1
    fi

    echo "$results"
}

TEST="$(unique_fuzzy_file "$1")"

BINDINGS=$(mktemp -t bindings.rs.XXXXXX)
TEST_BINDINGS_BINARY=$(mktemp -t bindings.XXXXXX)

FLAGS="$(grep "// bindgen-flags: " "$TEST" || echo)"
FLAGS="${FLAGS/\/\/ bindgen\-flags:/}"
# Prepend the default flags added in test.rs's `create_bindgen_builder`.
FLAGS="--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"


eval ./target/debug/bindgen \
    "\"$TEST\"" \
    --emit-ir \
    --emit-ir-graphviz ir.dot \
    --emit-clang-ast \
    -o "\"$BINDINGS\"" \
    $FLAGS

rustup run nightly rustfmt "$BINDINGS" || true

dot -Tpng ir.dot -o ir.png

echo
echo "=== Input header ========================================================"
echo

cat "$TEST"

echo
echo "=== Generated bindings =================================================="
echo

cat "$BINDINGS"

echo
echo "=== Diff w/ expected bindings ==========================================="
echo

EXPECTED=${TEST/headers/expectations\/tests}
EXPECTED=${EXPECTED/.hpp/.rs}
EXPECTED=${EXPECTED/.h/.rs}

rustup run nightly rustfmt "$EXPECTED" || true

# Don't exit early if there is a diff.
diff -U8 "$EXPECTED" "$BINDINGS" || true

echo
echo "=== Building bindings ==================================================="
echo

rustc --test -o "$TEST_BINDINGS_BINARY" "$BINDINGS" --crate-name bindgen_test_one

echo
echo "=== Testing bindings ===================================================="
echo

"$TEST_BINDINGS_BINARY"