mockgen_private.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/bin/bash
  2. DEST=$2
  3. PACKAGE=$3
  4. TMPFILE="mockgen_tmp.go"
  5. # uppercase the name of the interface
  6. ORIG_INTERFACE_NAME=$4
  7. INTERFACE_NAME="$(tr '[:lower:]' '[:upper:]' <<< ${ORIG_INTERFACE_NAME:0:1})${ORIG_INTERFACE_NAME:1}"
  8. # Gather all files that contain interface definitions.
  9. # These interfaces might be used as embedded interfaces,
  10. # so we need to pass them to mockgen as aux_files.
  11. AUX=()
  12. for f in *.go; do
  13. if [[ -z ${f##*_test.go} ]]; then
  14. # skip test files
  15. continue;
  16. fi
  17. if $(egrep -qe "type (.*) interface" $f); then
  18. AUX+=("github.com/Psiphon-Labs/quic-go=$f")
  19. fi
  20. done
  21. # Find the file that defines the interface we're mocking.
  22. for f in *.go; do
  23. if [[ -z ${f##*_test.go} ]]; then
  24. # skip test files
  25. continue;
  26. fi
  27. INTERFACE=$(sed -n "/^type $ORIG_INTERFACE_NAME interface/,/^}/p" $f)
  28. if [[ -n "$INTERFACE" ]]; then
  29. SRC=$f
  30. break
  31. fi
  32. done
  33. if [[ -z "$INTERFACE" ]]; then
  34. echo "Interface $ORIG_INTERFACE_NAME not found."
  35. exit 1
  36. fi
  37. AUX_FILES=$(IFS=, ; echo "${AUX[*]}")
  38. ## create a public alias for the interface, so that mockgen can process it
  39. echo -e "package $1\n" > $TMPFILE
  40. echo "$INTERFACE" | sed "s/$ORIG_INTERFACE_NAME/$INTERFACE_NAME/" >> $TMPFILE
  41. mockgen -package $1 -self_package $3 -destination $DEST -source=$TMPFILE -aux_files $AUX_FILES
  42. sed "s/$TMPFILE/$SRC/" "$DEST" > "$DEST.new" && mv "$DEST.new" "$DEST"
  43. rm "$TMPFILE"