forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockgen_private.sh
executable file
·49 lines (43 loc) · 1.35 KB
/
mockgen_private.sh
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
#!/bin/bash
DEST=$2
PACKAGE=$3
TMPFILE="mockgen_tmp.go"
# uppercase the name of the interface
ORIG_INTERFACE_NAME=$4
INTERFACE_NAME="$(tr '[:lower:]' '[:upper:]' <<< ${ORIG_INTERFACE_NAME:0:1})${ORIG_INTERFACE_NAME:1}"
# Gather all files that contain interface definitions.
# These interfaces might be used as embedded interfaces,
# so we need to pass them to mockgen as aux_files.
AUX=()
for f in *.go; do
if [[ -z ${f##*_test.go} ]]; then
# skip test files
continue;
fi
if $(egrep -qe "type (.*) interface" $f); then
AUX+=("github.com/lucas-clemente/quic-go=$f")
fi
done
# Find the file that defines the interface we're mocking.
for f in *.go; do
if [[ -z ${f##*_test.go} ]]; then
# skip test files
continue;
fi
INTERFACE=$(sed -n "/^type $ORIG_INTERFACE_NAME interface/,/^}/p" $f)
if [[ -n "$INTERFACE" ]]; then
SRC=$f
break
fi
done
if [[ -z "$INTERFACE" ]]; then
echo "Interface $ORIG_INTERFACE_NAME not found."
exit 1
fi
AUX_FILES=$(IFS=, ; echo "${AUX[*]}")
## create a public alias for the interface, so that mockgen can process it
echo -e "package $1\n" > $TMPFILE
echo "$INTERFACE" | sed "s/$ORIG_INTERFACE_NAME/$INTERFACE_NAME/" >> $TMPFILE
mockgen -package $1 -self_package $3 -destination $DEST -source=$TMPFILE -aux_files $AUX_FILES
sed "s/$TMPFILE/$SRC/" "$DEST" > "$DEST.new" && mv "$DEST.new" "$DEST"
rm "$TMPFILE"