diff --git a/p2p/host/basic/mock_nat_test.go b/p2p/host/basic/mock_nat_test.go index b6d7e9c526..7714b25853 100644 --- a/p2p/host/basic/mock_nat_test.go +++ b/p2p/host/basic/mock_nat_test.go @@ -1,5 +1,5 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: natmgr.go +// Source: github.com/libp2p/go-libp2p/p2p/host/basic (interfaces: NAT) // Package basichost is a generated GoMock package. package basichost @@ -11,45 +11,45 @@ import ( gomock "github.com/golang/mock/gomock" ) -// MockNat is a mock of Nat interface. -type MockNat struct { +// MockNAT is a mock of NAT interface. +type MockNAT struct { ctrl *gomock.Controller - recorder *MockNatMockRecorder + recorder *MockNATMockRecorder } -// MockNatMockRecorder is the mock recorder for MockNat. -type MockNatMockRecorder struct { - mock *MockNat +// MockNATMockRecorder is the mock recorder for MockNAT. +type MockNATMockRecorder struct { + mock *MockNAT } -// NewMockNat creates a new mock instance. -func NewMockNat(ctrl *gomock.Controller) *MockNat { - mock := &MockNat{ctrl: ctrl} - mock.recorder = &MockNatMockRecorder{mock} +// NewMockNAT creates a new mock instance. +func NewMockNAT(ctrl *gomock.Controller) *MockNAT { + mock := &MockNAT{ctrl: ctrl} + mock.recorder = &MockNATMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockNat) EXPECT() *MockNatMockRecorder { +func (m *MockNAT) EXPECT() *MockNATMockRecorder { return m.recorder } // AddMapping mocks base method. -func (m *MockNat) AddMapping(protocol string, port int) error { +func (m *MockNAT) AddMapping(arg0 string, arg1 int) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "AddMapping", protocol, port) + ret := m.ctrl.Call(m, "AddMapping", arg0, arg1) ret0, _ := ret[0].(error) return ret0 } // AddMapping indicates an expected call of AddMapping. -func (mr *MockNatMockRecorder) AddMapping(protocol, port interface{}) *gomock.Call { +func (mr *MockNATMockRecorder) AddMapping(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddMapping", reflect.TypeOf((*MockNat)(nil).AddMapping), protocol, port) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddMapping", reflect.TypeOf((*MockNAT)(nil).AddMapping), arg0, arg1) } // Close mocks base method. -func (m *MockNat) Close() error { +func (m *MockNAT) Close() error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Close") ret0, _ := ret[0].(error) @@ -57,36 +57,36 @@ func (m *MockNat) Close() error { } // Close indicates an expected call of Close. -func (mr *MockNatMockRecorder) Close() *gomock.Call { +func (mr *MockNATMockRecorder) Close() *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockNat)(nil).Close)) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockNAT)(nil).Close)) } // GetMapping mocks base method. -func (m *MockNat) GetMapping(protocol string, port int) (netip.AddrPort, bool) { +func (m *MockNAT) GetMapping(arg0 string, arg1 int) (netip.AddrPort, bool) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetMapping", protocol, port) + ret := m.ctrl.Call(m, "GetMapping", arg0, arg1) ret0, _ := ret[0].(netip.AddrPort) ret1, _ := ret[1].(bool) return ret0, ret1 } // GetMapping indicates an expected call of GetMapping. -func (mr *MockNatMockRecorder) GetMapping(protocol, port interface{}) *gomock.Call { +func (mr *MockNATMockRecorder) GetMapping(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetMapping", reflect.TypeOf((*MockNat)(nil).GetMapping), protocol, port) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetMapping", reflect.TypeOf((*MockNAT)(nil).GetMapping), arg0, arg1) } // RemoveMapping mocks base method. -func (m *MockNat) RemoveMapping(protocol string, port int) error { +func (m *MockNAT) RemoveMapping(arg0 string, arg1 int) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "RemoveMapping", protocol, port) + ret := m.ctrl.Call(m, "RemoveMapping", arg0, arg1) ret0, _ := ret[0].(error) return ret0 } // RemoveMapping indicates an expected call of RemoveMapping. -func (mr *MockNatMockRecorder) RemoveMapping(protocol, port interface{}) *gomock.Call { +func (mr *MockNATMockRecorder) RemoveMapping(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoveMapping", reflect.TypeOf((*MockNat)(nil).RemoveMapping), protocol, port) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoveMapping", reflect.TypeOf((*MockNAT)(nil).RemoveMapping), arg0, arg1) } diff --git a/p2p/host/basic/mockgen_private.sh b/p2p/host/basic/mockgen_private.sh deleted file mode 100755 index 79f63eee3e..0000000000 --- a/p2p/host/basic/mockgen_private.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/bash - -DEST=$2 -PACKAGE=$3 -TMPFILE="mockgen_tmp.go" -# uppercase the name of the interface -ORIG_INTERFACE_NAME=$4 -INTERFACE_NAME="$(tr '[:lower:]' '[:upper:]' <<< ${ORIG_INTERFACE_NAME:0:1})${ORIG_INTERFACE_NAME:1}" - -# Gather all files that contain interface definitions. -# These interfaces might be used as embedded interfaces, -# so we need to pass them to mockgen as aux_files. -AUX=() -for f in *.go; do - if [[ -z ${f##*_test.go} ]]; then - # skip test files - continue; - fi - if $(egrep -qe "type (.*) interface" $f); then - AUX+=("github.com/quic-go/quic-go=$f") - fi -done - -# Find the file that defines the interface we're mocking. -for f in *.go; do - if [[ -z ${f##*_test.go} ]]; then - # skip test files - continue; - fi - INTERFACE=$(sed -n "/^type $ORIG_INTERFACE_NAME interface/,/^}/p" $f) - if [[ -n "$INTERFACE" ]]; then - SRC=$f - break - fi -done - -if [[ -z "$INTERFACE" ]]; then - echo "Interface $ORIG_INTERFACE_NAME not found." - exit 1 -fi - -AUX_FILES=$(IFS=, ; echo "${AUX[*]}") - -## create a public alias for the interface, so that mockgen can process it -echo -e "package $1\n" > $TMPFILE -echo "$INTERFACE" | sed "s/$ORIG_INTERFACE_NAME/$INTERFACE_NAME/" >> $TMPFILE -go run github.com/golang/mock/mockgen -package $1 -self_package $3 -destination $DEST -source=$TMPFILE -aux_files $AUX_FILES -sed "s/$TMPFILE/$SRC/" "$DEST" > "$DEST.new" && mv "$DEST.new" "$DEST" -rm "$TMPFILE" diff --git a/p2p/host/basic/mocks.go b/p2p/host/basic/mocks.go new file mode 100644 index 0000000000..3ad4d4e90b --- /dev/null +++ b/p2p/host/basic/mocks.go @@ -0,0 +1,6 @@ +//go:build gomock || generate + +package basichost + +//go:generate sh -c "go run github.com/golang/mock/mockgen -build_flags=\"-tags=gomock\" -package basichost -destination mock_nat_test.go github.com/libp2p/go-libp2p/p2p/host/basic NAT" +type NAT nat diff --git a/p2p/host/basic/natmgr_test.go b/p2p/host/basic/natmgr_test.go index 8ee772e62a..e507b45c82 100644 --- a/p2p/host/basic/natmgr_test.go +++ b/p2p/host/basic/natmgr_test.go @@ -15,12 +15,10 @@ import ( "github.com/golang/mock/gomock" ) -//go:generate sh -c "./mockgen_private.sh basichost mock_nat_test.go github.com/libp2p/go-libp2p/p2p/host/basic nat" - -func setupMockNAT(t *testing.T) (mockNAT *MockNat, reset func()) { +func setupMockNAT(t *testing.T) (mockNAT *MockNAT, reset func()) { t.Helper() ctrl := gomock.NewController(t) - mockNAT = NewMockNat(ctrl) + mockNAT = NewMockNAT(ctrl) origDiscoverNAT := discoverNAT discoverNAT = func(ctx context.Context) (nat, error) { return mockNAT, nil } return mockNAT, func() {