Skip to content

Commit

Permalink
feat(gnodev): add node tests and lint (gnolang#1668)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfanton committed Apr 29, 2024
1 parent f660be5 commit 7bf662a
Show file tree
Hide file tree
Showing 14 changed files with 678 additions and 152 deletions.
40 changes: 39 additions & 1 deletion .github/workflows/contribs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,43 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.goversion }}
- run: make install ${{ matrix.program }}
- run: make install.${{ matrix.program }}
working-directory: contribs

test:
strategy:
fail-fast: false
matrix:
goversion: # two latest versions
- "1.21.x"
- "1.22.x"
program:
- "gnodev"
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: ${{ matrix.goversion }}
- run: make test.${{ matrix.program }}
working-directory: contribs

lint:
strategy:
fail-fast: false
matrix:
goversion:
- "1.22.x"
program:
- "gnodev"
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: ${{ matrix.goversion }}
- run: make lint.${{ matrix.program }}
working-directory: contribs

12 changes: 10 additions & 2 deletions contribs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,13 @@ tidy:
########################################
# Test suite
.PHONY: test
test:
@echo "nothing to do."
test: test.gnodev
test.gnodev:
$(MAKE) -C ./gnodev test

########################################
# Lint
.PHONY: test
lint: lint.gnodev
lint.gnodev:
$(MAKE) -C ./gnodev test
5 changes: 5 additions & 0 deletions contribs/gnodev/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
GNOROOT_DIR ?= $(abspath $(lastword $(MAKEFILE_LIST))/../../../)
GOBUILD_FLAGS ?= -ldflags "-X github.com/gnolang/gno/gnovm/pkg/gnoenv._GNOROOT=$(GNOROOT_DIR)"
GOTEST_FLAGS ?= $(GOBUILD_FLAGS) -v -p 1 -timeout=5m

rundep := go run -modfile ../../misc/devdeps/go.mod
golangci_lint := $(rundep) github.com/golangci/golangci-lint/cmd/golangci-lint
Expand All @@ -12,3 +13,7 @@ build:

lint:
$(golangci_lint) --config ../../.github/golangci.yml run ./...

test:
go test $(GOTEST_FLAGS) -v ./...

4 changes: 2 additions & 2 deletions contribs/gnodev/cmd/gnodev/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (c *devCfg) RegisterFlags(fs *flag.FlagSet) {

fs.BoolVar(
&c.verbose,
"verbose",
"v",
defaultDevOptions.verbose,
"enable verbose output for development",
)
Expand Down Expand Up @@ -251,7 +251,7 @@ func execDev(cfg *devCfg, args []string, io commands.IO) (err error) {
server := http.Server{
Handler: mux,
Addr: cfg.webListenerAddr,
ReadHeaderTimeout: time.Minute,
ReadHeaderTimeout: time.Second * 60,
}
defer server.Close()

Expand Down
35 changes: 35 additions & 0 deletions contribs/gnodev/internal/mock/server_emitter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package emitter

import (
"sync"

"github.com/gnolang/gno/contribs/gnodev/pkg/emitter"
"github.com/gnolang/gno/contribs/gnodev/pkg/events"
)

// ServerEmitter is an `emitter.Emitter`
var _ emitter.Emitter = (*ServerEmitter)(nil)

type ServerEmitter struct {
events []events.Event
muEvents sync.Mutex
}

func (m *ServerEmitter) Emit(evt events.Event) {
m.muEvents.Lock()
defer m.muEvents.Unlock()

m.events = append(m.events, evt)
}

func (m *ServerEmitter) NextEvent() (evt events.Event) {
m.muEvents.Lock()
defer m.muEvents.Unlock()

if len(m.events) > 0 {
// pull next event from the list
evt, m.events = m.events[0], m.events[1:]
}

return evt
}
8 changes: 0 additions & 8 deletions contribs/gnodev/pkg/address/book_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,19 @@ func TestAddEmptyName(t *testing.T) {
}

func TestAdd(t *testing.T) {
t.Parallel()

bk := NewBook()

// Add address
bk.Add(testAddr, "testname")

t.Run("get by address", func(t *testing.T) {
t.Parallel()

names, ok := bk.GetByAddress(testAddr)
require.True(t, ok)
require.Equal(t, 1, len(names))
assert.Equal(t, "testname", names[0])
})

t.Run("get by name", func(t *testing.T) {
t.Parallel()

addrFromName, ok := bk.GetByName("testname")
assert.True(t, ok)
assert.True(t, addrFromName.Compare(testAddr) == 0)
Expand All @@ -60,8 +54,6 @@ func TestAdd(t *testing.T) {
bk.Add(testAddr, "testname2")

t.Run("get two names with same address", func(t *testing.T) {
t.Parallel()

// Get by name
addr1, ok := bk.GetByName("testname")
require.True(t, ok)
Expand Down
Loading

0 comments on commit 7bf662a

Please sign in to comment.