mockgen_private.sh 1.1 KB

12345678910111213141516171819202122232425262728
  1. #!/bin/bash
  2. # Mockgen refuses to generate mocks private types.
  3. # This script copies the quic package to a temporary directory, and adds an public alias for the private type.
  4. # It then creates a mock for this public (alias) type.
  5. TEMP_DIR=$(mktemp -d)
  6. mkdir -p $TEMP_DIR/src/github.com/lucas-clemente/quic-go/
  7. # uppercase the name of the interface
  8. INTERFACE_NAME="$(tr '[:lower:]' '[:upper:]' <<< ${4:0:1})${4:1}"
  9. # copy all .go files to a temporary directory
  10. rsync -r --exclude 'vendor' --include='*.go' --include '*/' --exclude '*' $GOPATH/src/github.com/lucas-clemente/quic-go/ $TEMP_DIR/src/github.com/lucas-clemente/quic-go/
  11. # create a public alias for the interface, so that mockgen can process it
  12. echo -e "package $1\n" > $TEMP_DIR/src/github.com/lucas-clemente/quic-go/mockgen_interface.go
  13. echo "type $INTERFACE_NAME = $4" >> $TEMP_DIR/src/github.com/lucas-clemente/quic-go/mockgen_interface.go
  14. export GOPATH="$TEMP_DIR:$GOPATH"
  15. mockgen -package $1 -self_package $1 -destination $2 $3 $INTERFACE_NAME
  16. # mockgen imports quic-go as 'import quic_go github.com/lucas_clemente/quic-go'
  17. sed -i '' 's/quic_go.//g' $2
  18. goimports -w $2
  19. rm -r "$TEMP_DIR"