Skip to content

Commit

Permalink
[FAB-15268] Check for go:generate parent dir references
Browse files Browse the repository at this point in the history
Added a check to makefile basic-checks to verify go:generate
directives create their mocks in the directory where they are
being utilized, and not in their parent directories

Change-Id: I1113826d07a63a8c5b985981ace64c24e4bb764f
Signed-off-by: Brett Logan <[email protected]>
  • Loading branch information
Brett Logan authored and Brett Logan committed May 10, 2019
1 parent cfe491c commit 3b511f4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ all: native docker checks

checks: basic-checks unit-test integration-test

basic-checks: license spelling trailing-spaces linter check-metrics-doc
basic-checks: license spelling references trailing-spaces linter check-metrics-doc

desk-check: checks verify

Expand All @@ -133,6 +133,10 @@ docker-thirdparty:
spelling:
@scripts/check_spelling.sh

.PHONY: references
references:
@scripts/check_references.sh

.PHONY: license
license:
@scripts/check_license.sh
Expand Down
2 changes: 1 addition & 1 deletion gossip/api/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hyperledger/fabric/gossip/common"
)

//go:generate mockery -dir . -name SecurityAdvisor -case underscore -output ../mocks/
//go:generate mockery -dir . -name SecurityAdvisor -case underscore -output mocks/

// SecurityAdvisor defines an external auxiliary object
// that provides security and identity related capabilities
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gossip/comm/comm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (
"github.com/hyperledger/fabric/common/metrics/disabled"
"github.com/hyperledger/fabric/core/comm"
"github.com/hyperledger/fabric/gossip/api"
"github.com/hyperledger/fabric/gossip/api/mocks"
"github.com/hyperledger/fabric/gossip/common"
"github.com/hyperledger/fabric/gossip/identity"
"github.com/hyperledger/fabric/gossip/metrics"
"github.com/hyperledger/fabric/gossip/mocks"
"github.com/hyperledger/fabric/gossip/protoext"
"github.com/hyperledger/fabric/gossip/util"
proto "github.com/hyperledger/fabric/protos/gossip"
Expand Down
22 changes: 22 additions & 0 deletions scripts/check_references.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

#
# Copyright IBM Corp All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

# Checks whether any files using go:generate directives make
# references to their parent directories, instead of generating
# the mock into the directory where it is being utilized
echo "Checking for go:generate parent path references"
OUTPUT="$(git ls-files "*.go" | grep -Ev 'vendor' | xargs grep 'go:generate.*\.\.')"
if [[ -n "$OUTPUT" ]]; then
echo "The following files contain references to parent directories in their go:generate directives."
echo "Creating mocks in directories in which they are not used, can create errors that are not"
echo "easily discoverable for code refactors that move the referenced code. It also implies that"
echo "a mock for a remote interface is being defined in some place other that where it is being used"
echo "It is best practice to generate the mock in the directory in which it will be used."
echo "$OUTPUT"
exit 1
fi

0 comments on commit 3b511f4

Please sign in to comment.