mockgen_internal.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/bin/bash
  2. # Mockgen refuses to generate mocks for internal packages.
  3. # This script copies the internal directory and renames it to internalpackage.
  4. # It also creages a public alias for private types.
  5. # It then creates a mock for this public (alias) type.
  6. # Afterwards, it corrects the import paths (replaces internalpackage back to internal).
  7. TEMP_DIR=$(mktemp -d)
  8. mkdir -p $TEMP_DIR/src/github.com/lucas-clemente/quic-go/internalpackage
  9. # uppercase the name of the interface (only has an effect for private interfaces)
  10. INTERFACE_NAME="$(tr '[:lower:]' '[:upper:]' <<< ${4:0:1})${4:1}"
  11. PACKAGE_NAME=`echo $3 | sed 's/.*\///'`
  12. cp -r $GOPATH/src/github.com/lucas-clemente/quic-go/internal/* $TEMP_DIR/src/github.com/lucas-clemente/quic-go/internalpackage
  13. find $TEMP_DIR -type f -name "*.go" -exec sed -i '' 's/internal/internalpackage/g' {} \;
  14. export GOPATH="$TEMP_DIR:$GOPATH"
  15. PACKAGE_PATH=${3/internal/internalpackage}
  16. # if we're mocking a private interface, we need to add a public alias
  17. if [ "$INTERFACE_NAME" != "$4" ]; then
  18. # create a public alias for the interface, so that mockgen can process it
  19. echo -e "package $PACKAGE_NAME\n" > $TEMP_DIR/src/$PACKAGE_PATH/mockgen_interface.go
  20. echo "type $INTERFACE_NAME = $4" >> $TEMP_DIR/src/$PACKAGE_PATH/mockgen_interface.go
  21. fi
  22. mockgen -package $1 -self_package $1 -destination $2 $PACKAGE_PATH $INTERFACE_NAME
  23. sed -i '' 's/internalpackage/internal/g' $2
  24. # mockgen imports the package we're generating a mock for
  25. sed -i '' "s/$1\.//g" $2
  26. goimports -w $2
  27. rm -r "$TEMP_DIR"