From 3b511f45e35fd7b83ceaf8a53ee2e9a899eec6c1 Mon Sep 17 00:00:00 2001 From: Brett Logan Date: Tue, 7 May 2019 15:02:14 -0400 Subject: [PATCH] [FAB-15268] Check for go:generate parent dir references 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 --- Makefile | 6 +++++- gossip/api/channel.go | 2 +- gossip/{ => api}/mocks/security_advisor.go | 1 + gossip/comm/comm_test.go | 2 +- scripts/check_references.sh | 22 ++++++++++++++++++++++ 5 files changed, 30 insertions(+), 3 deletions(-) rename gossip/{ => api}/mocks/security_advisor.go (99%) create mode 100755 scripts/check_references.sh diff --git a/Makefile b/Makefile index 6ad6ab18d30..be30bebde3b 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 diff --git a/gossip/api/channel.go b/gossip/api/channel.go index 2e17fef2c15..bc2ef7d8642 100644 --- a/gossip/api/channel.go +++ b/gossip/api/channel.go @@ -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 diff --git a/gossip/mocks/security_advisor.go b/gossip/api/mocks/security_advisor.go similarity index 99% rename from gossip/mocks/security_advisor.go rename to gossip/api/mocks/security_advisor.go index bbe40164e3b..0ddb3327977 100644 --- a/gossip/mocks/security_advisor.go +++ b/gossip/api/mocks/security_advisor.go @@ -1,4 +1,5 @@ // Code generated by mockery v1.0.0. DO NOT EDIT. + package mocks import api "github.com/hyperledger/fabric/gossip/api" diff --git a/gossip/comm/comm_test.go b/gossip/comm/comm_test.go index 9a94087ce61..e4a227b5c6d 100644 --- a/gossip/comm/comm_test.go +++ b/gossip/comm/comm_test.go @@ -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" diff --git a/scripts/check_references.sh b/scripts/check_references.sh new file mode 100755 index 00000000000..f6dc99ac4ad --- /dev/null +++ b/scripts/check_references.sh @@ -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