From 4cf36f5eac550a5d1691e9a12218d9d36b8f3496 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Thu, 14 Nov 2019 14:39:02 +0100 Subject: [PATCH 1/2] Improved the Makefile --- Makefile | 36 ++++++++++------ Makefile.ledger | 2 - contrib/devtools/Makefile | 52 +++++++++++++++++++++++ contrib/devtools/install-golangci-lint.sh | 33 ++++++++++++++ 4 files changed, 108 insertions(+), 15 deletions(-) create mode 100644 contrib/devtools/Makefile create mode 100644 contrib/devtools/install-golangci-lint.sh diff --git a/Makefile b/Makefile index 5ac3fb93a9..31a90a34bc 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,35 @@ +#!/usr/bin/make -f + PACKAGES_NOSIMULATION=$(shell go list ./... | grep -v '/simulation') VERSION := $(shell echo $(shell git describe --tags) | sed 's/^v//') COMMIT := $(shell git log -1 --format='%H') +LEDGER_ENABLED ?= true +BINDIR ?= $(GOPATH)/bin export GO111MODULE = on include Makefile.ledger +include contrib/devtools/Makefile + +######################################## +### Build flags + +ifneq ($(GOSUM),) + ldflags += -X github.com/cosmos/cosmos-sdk/version.VendorDirHash=$(shell $(GOSUM) go.sum) +endif ifeq ($(WITH_CLEVELDB),yes) build_tags += gcc + ldflags += -X github.com/cosmos/cosmos-sdk/types.DBBackend=cleveldb endif + build_tags += $(BUILD_TAGS) build_tags := $(strip $(build_tags)) -# process linker flags +ldflags += $(LDFLAGS) +ldflags := $(strip $(ldflags)) +# Process linker flags ldflags = -X "github.com/cosmos/cosmos-sdk/version.Name=Desmos" \ -X "github.com/cosmos/cosmos-sdk/version.ServerName=desmosd" \ -X "github.com/cosmos/cosmos-sdk/version.ClientName=desmoscli" \ @@ -21,18 +37,11 @@ ldflags = -X "github.com/cosmos/cosmos-sdk/version.Name=Desmos" \ -X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT) \ -X "github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags)" -ifneq ($(GOSUM),) -ldflags += -X github.com/cosmos/cosmos-sdk/version.VendorDirHash=$(shell $(GOSUM) go.sum) -endif - -ifeq ($(WITH_CLEVELDB),yes) - ldflags += -X github.com/cosmos/cosmos-sdk/types.DBBackend=cleveldb -endif -ldflags += $(LDFLAGS) -ldflags := $(strip $(ldflags)) - BUILD_FLAGS := -tags "$(build_tags)" -ldflags '$(ldflags)' +######################################## +### All + all: lint install ######################################## @@ -68,10 +77,11 @@ go.sum: go.mod @go mod verify @go mod tidy -lint: - golangci-lint run +lint: golangci-lint + $(BINDIR)/golangci-lint run find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" | xargs gofmt -d -s go mod verify +.PHONY: lint ######################################## ### Testing diff --git a/Makefile.ledger b/Makefile.ledger index 040a9e4a8f..ecf5d043e9 100644 --- a/Makefile.ledger +++ b/Makefile.ledger @@ -1,5 +1,3 @@ -LEDGER_ENABLED ?= true - build_tags = netgo ifeq ($(LEDGER_ENABLED),true) ifeq ($(OS),Windows_NT) diff --git a/contrib/devtools/Makefile b/contrib/devtools/Makefile new file mode 100644 index 0000000000..730367d27c --- /dev/null +++ b/contrib/devtools/Makefile @@ -0,0 +1,52 @@ +### +# Find OS and Go environment +# GO contains the Go binary +# FS contains the OS file separator +### +ifeq ($(OS),Windows_NT) + GO := $(shell where go.exe 2> NUL) + FS := "\\" +else + GO := $(shell command -v go 2> /dev/null) + FS := "/" +endif + +ifeq ($(GO),) + $(error could not find go. Is it in PATH? $(GO)) +endif + +GOPATH ?= $(shell $(GO) env GOPATH) +GITHUBDIR := $(GOPATH)$(FS)src$(FS)github.com +GOLANGCI_LINT_HASHSUM := 8d21cc95da8d3daf8321ac40091456fc26123c964d7c2281d339d431f2f4c840 + +### +# Functions +### + +mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) +mkfile_dir := $(shell cd $(shell dirname $(mkfile_path)); pwd) + +### +# tools +### + +TOOLS_DESTDIR ?= $(GOPATH)/bin + +GOLANGCI_LINT = $(TOOLS_DESTDIR)/golangci-lint + +all: tools + +tools: golangci-lint + +golangci-lint: $(GOLANGCI_LINT) +$(GOLANGCI_LINT): $(mkfile_dir)/install-golangci-lint.sh + @echo "Installing golangci-lint..." + @bash $(mkfile_dir)/install-golangci-lint.sh $(TOOLS_DESTDIR) $(GOLANGCI_LINT_HASHSUM) + + + +tools-clean: + rm -f $(GOLANGCI_LINT) + rm -f tools-stamp + +.PHONY: all tools tools-clean diff --git a/contrib/devtools/install-golangci-lint.sh b/contrib/devtools/install-golangci-lint.sh new file mode 100644 index 0000000000..3b0842e59c --- /dev/null +++ b/contrib/devtools/install-golangci-lint.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +set -euo pipefail + +installer="$(mktemp)" +trap "rm -f ${installer}" EXIT + +GOBIN="${1}" +CURL="$(which curl)" +HASHSUM="${2}" + +f_sha256() { + local l_file + l_file=$1 + python -sBc "import hashlib;print(hashlib.sha256(open('$l_file','rb').read()).hexdigest())" +} + +get_latest_release() { + "${CURL}" --silent "https://api.github.com/repos/$1/releases/latest" | \ + grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' +} + +VERSION="$(get_latest_release golangci/golangci-lint)" + +echo "Downloading golangci-lint ${VERSION} installer ..." >&2 +"${CURL}" -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/${VERSION}/install.sh" > "${installer}" + +echo "Checking hashsum ..." >&2 +[ "${HASHSUM}" = "$(f_sha256 ${installer})" ] +chmod +x "${installer}" + +echo "Launching installer ..." >&2 +exec "${installer}" -d -b "${GOBIN}" "${VERSION}" From eb4859b43619ca683009a41a9ec8b8b020d9fadd Mon Sep 17 00:00:00 2001 From: RiccardoM Date: Wed, 20 Nov 2019 08:48:06 +0100 Subject: [PATCH 2/2] Run lint Signed-off-by: RiccardoM --- x/magpie/internal/keeper/keeper_test.go | 6 +++--- x/magpie/internal/types/msgs.go | 3 ++- x/magpie/internal/types/session_test.go | 3 ++- x/posts/internal/keeper/keeper.go | 1 + x/posts/internal/keeper/keeper_test.go | 12 ++++++------ x/posts/internal/types/post.go | 4 ++-- 6 files changed, 16 insertions(+), 13 deletions(-) diff --git a/x/magpie/internal/keeper/keeper_test.go b/x/magpie/internal/keeper/keeper_test.go index 1d44b20fa5..d8c32ffb68 100644 --- a/x/magpie/internal/keeper/keeper_test.go +++ b/x/magpie/internal/keeper/keeper_test.go @@ -79,9 +79,9 @@ func TestKeeper_SaveSession(t *testing.T) { k.Cdc.MustUnmarshalBinaryBare(store.Get([]byte(types.SessionStorePrefix+session.SessionID.String())), &stored) assert.Equal(t, session, stored) - var storedLastId types.SessionID - k.Cdc.MustUnmarshalBinaryBare(store.Get([]byte(types.LastSessionIDStoreKey)), &storedLastId) - assert.Equal(t, session.SessionID, storedLastId) + var storedLastID types.SessionID + k.Cdc.MustUnmarshalBinaryBare(store.Get([]byte(types.LastSessionIDStoreKey)), &storedLastID) + assert.Equal(t, session.SessionID, storedLastID) } func TestKeeper_GetSession(t *testing.T) { diff --git a/x/magpie/internal/types/msgs.go b/x/magpie/internal/types/msgs.go index 45f64f146a..6948712f1c 100644 --- a/x/magpie/internal/types/msgs.go +++ b/x/magpie/internal/types/msgs.go @@ -2,8 +2,9 @@ package types import ( "fmt" - sdk "github.com/cosmos/cosmos-sdk/types" "strings" + + sdk "github.com/cosmos/cosmos-sdk/types" ) // MsgCreateSession defines the MsgCreateSession message diff --git a/x/magpie/internal/types/session_test.go b/x/magpie/internal/types/session_test.go index 1463244f99..245f5e4b0d 100644 --- a/x/magpie/internal/types/session_test.go +++ b/x/magpie/internal/types/session_test.go @@ -2,10 +2,11 @@ package types_test import ( "fmt" + "testing" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/desmos-labs/desmos/x/magpie/internal/types" "github.com/stretchr/testify/assert" - "testing" ) // ------------------ diff --git a/x/posts/internal/keeper/keeper.go b/x/posts/internal/keeper/keeper.go index 20b41edfbd..b263d01c53 100644 --- a/x/posts/internal/keeper/keeper.go +++ b/x/posts/internal/keeper/keeper.go @@ -91,6 +91,7 @@ func (k Keeper) GetPosts(ctx sdk.Context) []types.Post { // SaveLike allows to save the given like inside the store. // It assumes that the given like is valid. // If another like from the same owner and for the same post exists, returns an error. +// nolint: interfacer func (k Keeper) SaveLike(ctx sdk.Context, postID types.PostID, like types.Like) sdk.Error { store := ctx.KVStore(k.StoreKey) key := []byte(types.LikesStorePrefix + postID.String()) diff --git a/x/posts/internal/keeper/keeper_test.go b/x/posts/internal/keeper/keeper_test.go index 427ed093d2..18752be958 100644 --- a/x/posts/internal/keeper/keeper_test.go +++ b/x/posts/internal/keeper/keeper_test.go @@ -15,7 +15,7 @@ import ( func TestKeeper_GetLastPostId(t *testing.T) { tests := []struct { name string - existingId types.PostID + existingID types.PostID expected types.PostID }{ { @@ -24,7 +24,7 @@ func TestKeeper_GetLastPostId(t *testing.T) { }, { name: "Existing ID returns correct value", - existingId: types.PostID(3), + existingID: types.PostID(3), expected: types.PostID(3), }, } @@ -34,9 +34,9 @@ func TestKeeper_GetLastPostId(t *testing.T) { t.Run(test.name, func(t *testing.T) { ctx, k := SetupTestInput() - if test.existingId.Valid() { + if test.existingID.Valid() { store := ctx.KVStore(k.StoreKey) - store.Set([]byte(types.LastPostIDStoreKey), k.Cdc.MustMarshalBinaryBare(test.existingId)) + store.Set([]byte(types.LastPostIDStoreKey), k.Cdc.MustMarshalBinaryBare(test.existingID)) } actual := k.GetLastPostID(ctx) @@ -163,7 +163,7 @@ func TestKeeper_GetPosts(t *testing.T) { store := ctx.KVStore(k.StoreKey) for _, p := range test.posts { - store.Set([]byte(types.PostStorePrefix+p.PostID.String()), k.Cdc.MustMarshalBinaryBare(&p)) + store.Set([]byte(types.PostStorePrefix+p.PostID.String()), k.Cdc.MustMarshalBinaryBare(p)) } posts := k.GetPosts(ctx) @@ -269,7 +269,7 @@ func TestKeeper_GetLikes(t *testing.T) { ctx, k := SetupTestInput() store := ctx.KVStore(k.StoreKey) for postID, likes := range test.likes { - store.Set([]byte(types.LikesStorePrefix+postID.String()), k.Cdc.MustMarshalBinaryBare(&likes)) + store.Set([]byte(types.LikesStorePrefix+postID.String()), k.Cdc.MustMarshalBinaryBare(likes)) } likesData := k.GetLikes(ctx) diff --git a/x/posts/internal/types/post.go b/x/posts/internal/types/post.go index 5dcfce73e3..62341cac76 100644 --- a/x/posts/internal/types/post.go +++ b/x/posts/internal/types/post.go @@ -50,9 +50,9 @@ type Post struct { Owner sdk.AccAddress `json:"owner"` } -func NewPost(ID, parentID PostID, message string, created int64, owner sdk.AccAddress) Post { +func NewPost(id, parentID PostID, message string, created int64, owner sdk.AccAddress) Post { return Post{ - PostID: ID, + PostID: id, ParentID: parentID, Message: message, Created: created,