2018-07-24 00:06:04 +02:00
|
|
|
#!/usr/bin/env bash
|
2017-08-31 04:06:58 +02:00
|
|
|
# Script to rewrite the autogenerated mocks in a unit test between
|
|
|
|
# /* AUTOGENERATED MOCKS START */ and /* AUTOGENERATED MOCKS END */
|
|
|
|
# based on link failures.
|
|
|
|
|
|
|
|
set -e
|
|
|
|
FILE="$1"
|
|
|
|
|
2018-04-03 22:23:57 +02:00
|
|
|
BASE=/tmp/mocktmp.$$.$(echo "$@" | tr / _)
|
|
|
|
trap 'mv $BASE.old $FILE; rm -f $BASE.*' EXIT
|
2017-08-31 04:06:58 +02:00
|
|
|
|
2018-04-05 07:11:19 +02:00
|
|
|
START=$(grep -F -n '/* AUTOGENERATED MOCKS START */' "$FILE" | cut -d: -f1)
|
|
|
|
END=$(grep -F -n '/* AUTOGENERATED MOCKS END */' "$FILE" | cut -d: -f1)
|
2017-08-31 04:06:58 +02:00
|
|
|
|
2019-12-13 21:26:15 +01:00
|
|
|
function make_binary() {
|
2023-10-22 06:07:21 +02:00
|
|
|
# Make sure we don't optimize out options, and we use debug build
|
|
|
|
$MAKE SUPPRESS_GENERATION=1 "${FILE/%.c/}" DEBUGBUILD=1 COPTFLAGS="" 2> "${BASE}.err" >/dev/null
|
2019-12-13 21:26:15 +01:00
|
|
|
}
|
|
|
|
|
2017-08-31 04:06:58 +02:00
|
|
|
if [ -n "$START" ]; then
|
2018-04-03 22:23:57 +02:00
|
|
|
mv "$FILE" "${BASE}.old"
|
2020-05-29 23:15:26 +02:00
|
|
|
echo "mocking out ${FILE}:"
|
2018-04-03 22:23:57 +02:00
|
|
|
head -n "$START" "${BASE}.old" > "$FILE"
|
|
|
|
tail -n +"$END" "${BASE}.old" >> "$FILE"
|
2017-08-31 04:06:58 +02:00
|
|
|
# Try to make binary.
|
2019-12-13 21:26:15 +01:00
|
|
|
if ! make_binary; then
|
|
|
|
# Some linkers (e.g. LLVM's one) don't print all errors. If this is the
|
|
|
|
# case, then re-run, asking them to do so. Search for something like
|
|
|
|
# this in the output:
|
|
|
|
# ld: error: too many errors emitted, stopping now (use -error-limit=0 to see all errors)
|
|
|
|
if grep -q 'too many errors emitted.*-error-limit=0' "${BASE}.err"; then
|
|
|
|
LDFLAGS=-Wl,-error-limit=0 make_binary || :
|
|
|
|
fi
|
2021-05-21 07:19:05 +02:00
|
|
|
tools/mockup.sh "$FILE" < "${BASE}.err" >> "${BASE}.stubs"
|
2017-08-31 04:06:58 +02:00
|
|
|
# If there are no link errors, maybe compile fail for other reason?
|
2018-04-05 07:11:19 +02:00
|
|
|
if ! grep -F -q 'Generated stub for' "${BASE}.stubs"; then
|
2018-04-03 22:23:57 +02:00
|
|
|
cat "${BASE}.err"
|
2017-08-31 04:06:58 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
2019-12-13 21:26:15 +01:00
|
|
|
sed -n 's,.*Generated stub for \(.*\) .*, \1,p' < "${BASE}.stubs"
|
2018-04-03 22:23:57 +02:00
|
|
|
head -n "$START" "${BASE}.old" > "$FILE"
|
|
|
|
cat "${BASE}.stubs" >> "$FILE"
|
|
|
|
tail -n +"$END" "${BASE}.old" >> "$FILE"
|
2017-08-31 04:06:58 +02:00
|
|
|
else
|
|
|
|
echo "...build succeeded without stubs"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# All good.
|
2018-04-03 22:23:57 +02:00
|
|
|
rm -f "$BASE".*
|
2017-08-31 04:06:58 +02:00
|
|
|
trap "" EXIT
|