Skip to content

Commit

Permalink
[FAB-3966] Add go linting checks
Browse files Browse the repository at this point in the history
This change introduces lint checking to the build process. Minor
lint fixes are also included in this patch.

Change-Id: I1615909db05a20c07e1188b82e40830a162ece58
Signed-off-by: Troy Ronda <troy@troyronda.com>
  • Loading branch information
troyronda committed May 17, 2017
1 parent 4521257 commit 93f5da5
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 8 deletions.
8 changes: 5 additions & 3 deletions Makefile
Expand Up @@ -33,15 +33,17 @@
all: unit-test integration-test

depend:
go get -u github.com/axw/gocov/... && go get -u github.com/AlekSi/gocov-xml
go get -u github.com/client9/misspell/cmd/misspell
@test/scripts/dependencies.sh

checks: depend license spelling
checks: depend license lint spelling

.PHONY: license
license:
@test/scripts/check_license.sh

lint:
@test/scripts/check_lint.sh

spelling:
@test/scripts/check_spelling.sh

Expand Down
3 changes: 1 addition & 2 deletions fabric-client/events/eventhub.go
Expand Up @@ -124,12 +124,11 @@ func (ccf *consumerClientFactory) newEventsClient(peerAddress string, certificat
// NewEventHub ...
func NewEventHub() EventHub {
chaincodeRegistrants := make(map[string][]*ChainCodeCBE)
blockRegistrants := make([]func(*common.Block), 0)
txRegistrants := make(map[string]func(string, pb.TxValidationCode, error))

eventHub := &eventHub{
chaincodeRegistrants: chaincodeRegistrants,
blockRegistrants: blockRegistrants,
blockRegistrants: nil,
txRegistrants: txRegistrants,
interestedEvents: nil,
eventsClientFactory: &consumerClientFactory{},
Expand Down
2 changes: 1 addition & 1 deletion fabric-client/packager/golang.go
Expand Up @@ -139,7 +139,7 @@ func generateTarGz(descriptors []*Descriptor) ([]byte, error) {
err := packEntry(tw, gw, v)
if err != nil {
closeStream(tw, gw)
return nil, fmt.Errorf("error from packEntry for %s error %s:", v.fqp, err.Error())
return nil, fmt.Errorf("error from packEntry for %s error %s", v.fqp, err.Error())
}
}
closeStream(tw, gw)
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/src/github.com/events_cc/events_cc.go
Expand Up @@ -39,13 +39,13 @@ func (t *EventSender) Init(stub shim.ChaincodeStubInterface) pb.Response {

// Invoke function
func (t *EventSender) invoke(stub shim.ChaincodeStubInterface) pb.Response {
_ , args := stub.GetFunctionAndParameters()
_, args := stub.GetFunctionAndParameters()
if len(args) != 2 {
return shim.Error("Incorrect number of arguments. Expecting 2")
}
b, err := stub.GetState("noevents")
if err != nil {
return shim.Error("Failed to get state")
return shim.Error("Failed to get state")
}
noevts, _ := strconv.Atoi(string(b))

Expand Down Expand Up @@ -82,6 +82,8 @@ func (t *EventSender) query(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Success(b)
}

// Invoke (CC shim method)
// Implements events testing
func (t *EventSender) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()

Expand Down
46 changes: 46 additions & 0 deletions test/scripts/check_lint.sh
@@ -0,0 +1,46 @@
#!/bin/bash
# This script runs Go linting and vetting tools
# Original script from Hyperledger Fabric and Fabric CA projects

set -e

declare -a arr=(
"./config"
"./fabric-ca-client"
"./fabric-client"
"./test"
)

echo "Running linters..."
for i in "${arr[@]}"
do
echo "Checking $i"
OUTPUT="$(golint $i/...)"
if [[ $OUTPUT ]]; then
echo "You should check the following golint suggestions:"
printf "$OUTPUT\n"
echo "end golint suggestions"
fi

OUTPUT="$(go vet $i/...)"
if [[ $OUTPUT ]]; then
echo "You should check the following govet suggestions:"
printf "$OUTPUT\n"
echo "end govet suggestions"
fi

found=`gofmt -l \`find $i -name "*.go" |grep -v "./vendor"\` 2>&1`
if [ $? -ne 0 ]; then
echo "The following files need reformatting with 'gofmt -w <file>':"
printf "$badformat\n"
exit 1
fi

OUTPUT="$(goimports -srcdir $GOPATH/src/github.com/hyperledger/fabric-sdk-go -l $i)"
if [[ $OUTPUT ]]; then
echo "YOU MUST FIX THE FOLLOWING GOIMPORTS ERRORS:"
printf "$OUTPUT\n"
echo "END GOIMPORTS ERRORS"
exit 1
fi
done
9 changes: 9 additions & 0 deletions test/scripts/dependencies.sh
@@ -0,0 +1,9 @@
#!/bin/bash
# This script installs dependencies for testing tools

echo "Installing dependencies..."
go get -u github.com/axw/gocov/...
go get -u github.com/AlekSi/gocov-xml
go get -u github.com/client9/misspell/cmd/misspell
go get -u github.com/golang/lint/golint
go get -u golang.org/x/tools/cmd/goimports

0 comments on commit 93f5da5

Please sign in to comment.