Skip to content

Commit

Permalink
Merge branch 'development' into clear-prefix-limit
Browse files Browse the repository at this point in the history
  • Loading branch information
arijitAD authored Oct 27, 2021
2 parents 4628194 + b53627a commit c5c9c0a
Show file tree
Hide file tree
Showing 77 changed files with 524 additions and 844 deletions.
19 changes: 11 additions & 8 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,22 @@ One important distinction is that we are building the Polkadot Runtime Environme
go test <file_you_are_working_on>
```

Sometimes you may need to create mocks for interfaces, in that case just execute:
Sometimes you may need to create mocks for interfaces, in that case, add a go generate comment. For example, for interface `MyInterface`, the comment would be:

```sh
make mock path=$(path to the interface) interface=$(interface name)
```go
//go:generate mockery --name MyInterface --structname MyInterface --case underscore --keeptree
```

The command above will generate a file with prefix `mock_` inside the interface folder, if you want to generate the same mock but inside the `mocks` folder, you can execute:
This will generate a Go file `./mocks/my_interface.go` with the `MyInterface` Mockery mock.

```sh
make mock path=$(path to the interface) interface=$(interface name) INMOCKS=1
```
If you want to have your mock in the same package you are working on:
- Replace `--keeptree` with `-inpackage`
- Add `--filename mock_my_interface_test.go` so it's clear it's only used for package local tests
- Prefix the `--structname` value with `mock` to differentiate it with the original interface.

This will generate a Go file `./mock_my_interface_test.go` with mock `mockMyInterface`.

The command above will generate the mock inside the folder `$(path)/mocks`, and the mocks will be in the same package that your interface is.
Generate the mock code with `go generate -run "mockery" ./...` from your working directory. This will also update existing mocks. You can update all mocks by running `go generate -run "mockery" ./...` from the repository root.

8. **Lint your changes.**

Expand Down
19 changes: 0 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,3 @@ gossamer: clean
## install: install the gossamer binary in $GOPATH/bin
install:
GOBIN=$(GOPATH)/bin go run scripts/ci.go install

MOCKGEN := $(shell command -v $(GOPATH)/bin/mockery 2> /dev/null)
INMOCKS=0
mock:
ifndef MOCKGEN
@echo "> Installing mockery ..."
@go get github.com/vektra/mockery/v2/.../
endif
@echo "> Generating mocks at $(path)"

ifeq ($(INMOCKS),1)
cd $(path); $(GOPATH)/bin/mockery --name $(interface) --structname Mock$(interface) --case underscore --keeptree
else
$(GOPATH)/bin/mockery --srcpkg $(path) --name $(interface) --case underscore --inpackage
endif




2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ ignore:
- "lib/runtime/test_data"
- "**/errors.go"
- "**/*/errors.go"
- "**/*/mocks/*.go"
- "**/*/mock_*.go"
- "go.mod"
- "go.sum"
8 changes: 8 additions & 0 deletions dot/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"github.com/ChainSafe/gossamer/lib/transaction"
)

//go:generate mockery --name BlockState --structname BlockState --case underscore --keeptree

// BlockState interface for block state methods
type BlockState interface {
BestBlockHash() common.Hash
Expand Down Expand Up @@ -55,6 +57,8 @@ type BlockState interface {
StoreRuntime(common.Hash, runtime.Instance)
}

//go:generate mockery --name StorageState --structname StorageState --case underscore --keeptree

// StorageState interface for storage state methods
type StorageState interface {
LoadCode(root *common.Hash) ([]byte, error)
Expand All @@ -76,6 +80,8 @@ type TransactionState interface {
PendingInPool() []*transaction.ValidTransaction
}

//go:generate mockery --name Network --structname Network --case underscore --keeptree

// Network is the interface for the network service
type Network interface {
GossipMessage(network.NotificationsMessage)
Expand All @@ -95,6 +101,8 @@ type CodeSubstitutedState interface {
StoreCodeSubstitutedBlockHash(hash common.Hash) error
}

//go:generate mockery --name DigestHandler --structname DigestHandler --case underscore --keeptree

// DigestHandler is the interface for the consensus digest handler
type DigestHandler interface {
HandleDigests(header *types.Header)
Expand Down
8 changes: 2 additions & 6 deletions dot/core/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"testing"
"time"

. "github.com/ChainSafe/gossamer/dot/core/mocks" // nolint
"github.com/ChainSafe/gossamer/dot/core/mocks"
"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/sync"
Expand Down Expand Up @@ -80,7 +80,7 @@ func createExtrinsic(t *testing.T, rt runtime.Instance, genHash common.Hash, non
}

func TestService_HandleBlockProduced(t *testing.T) {
net := new(MockNetwork)
net := new(mocks.Network)
cfg := &Config{
Network: net,
Keystore: keystore.NewGlobalKeystore(),
Expand Down Expand Up @@ -132,10 +132,6 @@ func TestService_HandleTransactionMessage(t *testing.T) {
ks := keystore.NewGlobalKeystore()
ks.Acco.Insert(kp)

bp := new(MockBlockProducer) // nolint
blockC := make(chan types.Block)
bp.On("GetBlockChannel", nil).Return(blockC)

cfg := &Config{
Keystore: ks,
TransactionState: state.NewTransactionState(),
Expand Down
8 changes: 4 additions & 4 deletions dot/core/mocks/Network.go

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

34 changes: 0 additions & 34 deletions dot/core/mocks/block_producer.go

This file was deleted.

51 changes: 26 additions & 25 deletions dot/core/mocks/block_state.go

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

Loading

0 comments on commit c5c9c0a

Please sign in to comment.