Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Linting] Enforce 120 max lines, and import order with custom linting #319

Closed
wants to merge 65 commits into from
Closed
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
a9c5727
feat: enforce 150 max lines and import order via in golangci-lint yml
Jan 12, 2024
9b4e1c5
feat: add the Linter workflow file
Jan 12, 2024
c4b82d2
feat: exclude errors.go file for line length check
Jan 12, 2024
26f3598
chore: fix linter-settings duplication
Jan 12, 2024
cd50b47
chore: fix errors.go exclude linter
Jan 12, 2024
c209ac5
chore: increase linter workflow timeout
Jan 12, 2024
fb7d589
chore: remove linter workflow and move into test workflow
Jan 12, 2024
5830bf3
feat: only apply linting checks to new files
Jan 12, 2024
38dca85
chore: fix golangci_lint errors (part 1)
Jan 12, 2024
5ec364d
chore: run gci on all files and add script and makefile target
Jan 12, 2024
8fd7aaf
chore: remove cmd print in script
Jan 12, 2024
2ba6dfc
chore: remove custom linting section from gci
Jan 12, 2024
24c1195
chore: address more linter errors
Jan 13, 2024
4955f75
feat: add a step to the test workflow where we fix imports
Jan 13, 2024
27217d4
chore: add script, makefile target and gofumpt the repo
Jan 13, 2024
0fa5f81
feat: add formatting to the test workflow
Jan 13, 2024
b3ba99b
feat: add run customisations to config file and remove test files from
Jan 13, 2024
f070da6
chore: catch all linting errors from `make go_lint`
Jan 15, 2024
39734f0
feat: add new filter to gci to ignore files with import block scaffol…
Jan 15, 2024
019799d
chore: fix filter for scaffold imports
Jan 15, 2024
2eb47ca
feat: add Linter workflow separating it from testing, with appropriate
Jan 16, 2024
03ff717
feat: replace scaffold lines with correct gci import ordering
Jan 16, 2024
1866316
chore: enable custom ordering when running `make gci`
Jan 16, 2024
95cb78f
chore final lint changes
Jan 16, 2024
b0cebf5
feat: enable new flag in golangci-lint config
Jan 16, 2024
4e71478
chore: fix linter error
Jan 16, 2024
a6b790b
chore: address comments
Jan 16, 2024
34f9e8a
chore: remove mocked addressing comment
Jan 16, 2024
0574b25
chore: fix golangci-lint config syntax
Jan 16, 2024
38e4df2
Merge branch 'main' into feat/linting-updates
Jan 16, 2024
11c1885
chore: add --version checks to fix CI "hopefully"
Jan 16, 2024
5f5615b
feat: merge linting workflow back into tests workflow
Jan 16, 2024
eed0aed
feat: update workflow to not double lint files
Jan 16, 2024
3c74ca0
chore: Name -> name in workflow
Jan 16, 2024
520e07b
chore: address comments
Jan 16, 2024
1fb1820
chore: add nolint reason on same line
Jan 16, 2024
6a23c05
chore: address comments
Jan 16, 2024
f4f40a2
feat: only filter go files
Jan 16, 2024
321c64e
chore: remove the only new issues
Jan 16, 2024
879b388
feat: add custom golangci-lint script
Jan 17, 2024
a4838e6
feat: add new targets and update their names re: linting
Jan 17, 2024
577c3d7
feat: add golint tool to run golangci-lint selectively
Jan 17, 2024
3021487
chore: remove un-needed sorting
Jan 17, 2024
c77c217
chore: add default args to config and move linting to after tests
Jan 17, 2024
098a1d2
chore: remove un-used permissions
Jan 17, 2024
041f0eb
feat: fail when linter errors are found
Jan 17, 2024
522361d
Merge branch 'main' into feat/linting-updates
Jan 17, 2024
040cec9
feat: exclude certain directories and print error on fail
Jan 17, 2024
cb09498
chore: fix linter errors, add nolints where needed
Jan 17, 2024
51cb379
chore: go mod tidy & ignite chain build
Jan 17, 2024
1953b7e
feat: replace custom linter with golangci-lint action
Jan 17, 2024
0ae08d2
feat: remove custom linter script
Jan 17, 2024
7f32c69
feat: add extra linters
Jan 17, 2024
ecf6ef8
feat: replace golangci-lint install with binary installer, and make
Jan 17, 2024
160e857
chore: reorg steps in test job back to how they were before
Jan 17, 2024
087e6f7
feat: install golangci-lint with go install and correct go version
Jan 18, 2024
4bec743
feat: add mise.local.toml exclusion
Jan 18, 2024
00da2e6
feat: add godoc comments to the formatting tool scripts
Jan 18, 2024
a949347
bug: fix check golangci-lint make target
Jan 18, 2024
f9f0923
chore: update comments
Jan 18, 2024
91d2907
Merge branch 'main' into feat/linting-updates
Jan 18, 2024
943669e
bug: fix golangci-lint Cannot open: File exists error by skipping cache
Jan 18, 2024
ed4ebf1
feat: fix make go_lint
Jan 20, 2024
67a8a6b
Merge branch 'main' into feat/linting-updates
Jan 20, 2024
0ddd1c4
chore: final linting
Jan 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Run tests
Name: Run tests
Olshansk marked this conversation as resolved.
Show resolved Hide resolved

on:
push:
Expand All @@ -13,6 +13,11 @@ env:
GKE_CLUSTER: protocol-us-central1
GKE_ZONE: us-central1

permissions:
contents: read
pull-requests: read
checks: write

jobs:
go-test:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -44,8 +49,44 @@ jobs:
- name: Generate mocks
run: make go_mockgen

- name: Run golangci-lint
run: make go_lint
- name: Fix imports
run: make gci

- name: Format the repo
run: make gofumpt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this modify the code in CI (i.e. outside of version control)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes according to filters but yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It did previously with the make goimports step so I added these if this isnt intended then Ill remove them altogether

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't make sense to me for linting fixes to be applied in CI. The CI linter should fail if there are any issues, as opposed to fixing and then abandoning the changes.


# Fetch a list of all modified files in this PR.
- name: Get Modified Files
run: git diff --name-only ${{ github.event.before }} ${{ github.sha }} > modified_files.txt

# Filter the list of files modified in this PR to exclude those that have
# ignite scaffold comments in their import block, this is because gci
h5law marked this conversation as resolved.
Show resolved Hide resolved
# (golangci-lint's import formatter) doesn't support this kind of filtering
# and removes all comments from import blocks.
- name: Filter For Lintable Files
run: go run ./tools/scripts/scaffold_filter/main.go $(cat modified_files.txt) > files_to_lint.txt

# Run golangci-lint on the filtered files from this PR - allowing gci to
# catch any improperly formatted import blocks in this PR.
- name: Run golangci-lint (filtered for scaffold comments)
uses: golangci/golangci-lint-action@v3
with:
version: latest
args: --timeout=15m --config=.golangci.yml $(cat files_to_lint.txt)
only-new-issues: true
skip-cache: true

# Do a second lint pass without import checking (after filtered lint) to
# catcy any linter errors in files that were excluded in the first pass.
h5law marked this conversation as resolved.
Show resolved Hide resolved
# This step disables gci checks, to check for all other linter errors,
# on every file in the PR, without filtering.
- name: Run golangci-lint (without import checking)
uses: golangci/golangci-lint-action@v3
with:
version: latest
args: --timeout=15m --config=.golangci.yml --disable gci $(cat modified_files.txt)
only-new-issues: true
skip-cache: true

- name: Test
run: make go_test
68 changes: 57 additions & 11 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
linters-settings:
govet:
check-shadowing: true
run:
timeout: 15m
build-tags:
- e2e
- test
- integration

# TODO_TECHDEBT: Enable each linter listed, 1 by 1, fixing issues as they appear.
# Don't forget to delete the `disable-all: true` line as well.
linters:
disable-all: true
enable:
# - govet
# - revive
# - errcheck
# - unused
- goimports
# - govet
h5law marked this conversation as resolved.
Show resolved Hide resolved
# - revive
# - errcheck
# - unused
- gofumpt
- lll
- gci

linters-settings:
govet:
check-shadowing: true
lll:
line-length: 120
tab-width: 4
gci:
sections:
- standard # std lib
- default # external
- prefix(github.com/pokt-network/poktroll) # local imports
skip-generated: true
h5law marked this conversation as resolved.
Show resolved Hide resolved
custom-order: true
skip-files:
- "*.pb.go"
- "*/vendor/*"

issues:
exclude-use-default: true
Expand All @@ -27,30 +49,54 @@ issues:
# empty import block containing a comment used by ignite CLI.
- path: ^x/.+/genesis\.go$
linters:
- goimports
- gci
# Exclude cosmos-sdk module module.go files as they are generated with unused
# parameters and unchecked errors.
- path: ^x/.+/module\.go$
linters:
- lll
- gci
- revive
- errcheck
# Exclude cosmos-sdk module tx.go files as they are generated with unused
# constants.
- path: ^x/.+/cli/tx\.go$
linters:
- gci
- lll
- unused
# Exclude simulation code as it's generated with lots of unused parameters.
- path: .*/simulation/.*|_simulation\.go$
linters:
- gci
- lll
- revive
# Exclude cosmos-sdk module codec files as they are scaffolded with a unused
# paramerters and a comment used by ignite CLI.
# parameters and a comment used by ignite CLI.
- path: ^x/.+/codec.go$
linters:
- lll
- revive
- path: _test\.go$
linters:
- errcheck
# Exclude line length check for errors.go files as they are easier to read
# as as single line.
- path: errors\.go$
linters:
- lll
# Exclude app module code as it's generated with lots with long lines and
# improperly formatted imports.
- path: ^app
linters:
- lll
- gci
# Exclude cmd directory code as it's generated with lots with long lines and
# improperly formatted imports.
- path: ^cmd/poc
linters:
- lll
- gci
# TODO_IMPROVE: see https://golangci-lint.run/usage/configuration/#issues-configuration
#new: true,
new: true
#fix: true,
34 changes: 30 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ POCKET_ADDR_PREFIX = pokt
install_ci_deps: ## Installs `mockgen`
go install "github.com/golang/mock/[email protected]" && mockgen --version
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest && golangci-lint --version
go install golang.org/x/tools/cmd/goimports@latest
go install github.com/daixiang0/gci@latest && gci --version
go install mvdan.cc/gofumpt@latest && gofumpt --version

########################
### Makefile Helpers ###
Expand Down Expand Up @@ -71,6 +72,26 @@ check_godoc:
fi; \
}

.PHONY: check_gci
# Internal helper target - check if gci is installed
check_gci:
{ \
if ( ! ( command -v gci >/dev/null )); then \
echo "Seems like you don't have gci installed. Make sure you install it via 'go install github.com/daixiang0/gci@latest' before continuing"; \
exit 1; \
fi; \
}

.PHONY: check_gofumpt
# Internal helper target - check if gofumpt is installed
check_gofumpt:
{ \
if ( ! ( command -v gci >/dev/null )); then \
h5law marked this conversation as resolved.
Show resolved Hide resolved
echo "Seems like you don't have gofumpt installed. Make sure you install it via 'go install mvdan.cc/gofumpt@latest' before continuing"; \
exit 1; \
fi; \
}

.PHONY: check_npm
# Internal helper target - check if npm is installed
check_npm:
Expand Down Expand Up @@ -150,10 +171,15 @@ localnet_regenesis: ## Regenerate the localnet genesis file

.PHONY: go_lint
go_lint: ## Run all go linters
golangci-lint run --timeout 5m --build-tags test
golangci-lint run --timeout=15m --build-tags=e2e,test,integration

.PHONY: gci
h5law marked this conversation as resolved.
Show resolved Hide resolved
gci: check_gci ## Run gci (import ordering) on all applicable files, this writes changes in place
go run ./tools/scripts/gci

go_imports: check_go_version ## Run goimports on all go files
go run ./tools/scripts/goimports
.PHONY: gofumpt
gofumpt: check_gofumpt ## Run gofumpt (stricter gofmt) on all applicable files, this writes changes in place
go run ./tools/scripts/gofumpt

#############
### Tests ###
Expand Down
18 changes: 13 additions & 5 deletions app/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func fauxMerkleModeOpt(bapp *baseapp.BaseApp) {
// Running using starport command:
// `starport chain simulate -v --numBlocks 200 --blockSize 50`
// Running as go benchmark test:
// `go test -benchmem -run=^$ -bench ^BenchmarkSimulation ./app -NumBlocks=200 -BlockSize 50 -Commit=true -Verbose=true -Enabled=true`
//
// $ go test -benchmem -run=^$ -bench ^BenchmarkSimulation ./app -NumBlocks=200 \
// -BlockSize 50 -Commit=true -Verbose=true -Enabled=true
func BenchmarkSimulation(b *testing.B) {
simcli.FlagSeedValue = time.Now().Unix()
simcli.FlagVerboseValue = true
Expand Down Expand Up @@ -209,7 +211,8 @@ func TestAppStateDeterminism(t *testing.T) {
if j != 0 {
require.Equal(
t, string(appHashList[0]), string(appHashList[j]),
"non-determinism in seed %d: %d/%d, attempt: %d/%d\n", config.Seed, i+1, numSeeds, j+1, numTimesToRunPerSeed,
"non-determinism in seed %d: %d/%d, attempt: %d/%d\n",
config.Seed, i+1, numSeeds, j+1, numTimesToRunPerSeed,
)
}
}
Expand Down Expand Up @@ -344,7 +347,8 @@ func TestAppImportExport(t *testing.T) {
bApp.GetKey(stakingtypes.StoreKey), newApp.GetKey(stakingtypes.StoreKey),
[][]byte{
stakingtypes.UnbondingQueueKey, stakingtypes.RedelegationQueueKey, stakingtypes.ValidatorQueueKey,
stakingtypes.HistoricalInfoKey, stakingtypes.UnbondingIDKey, stakingtypes.UnbondingIndexKey, stakingtypes.UnbondingTypeKey, stakingtypes.ValidatorUpdatesKey,
stakingtypes.HistoricalInfoKey, stakingtypes.UnbondingIDKey, stakingtypes.UnbondingIndexKey,
stakingtypes.UnbondingTypeKey, stakingtypes.ValidatorUpdatesKey,
},
}, // ordering may change but it doesn't matter
{bApp.GetKey(slashingtypes.StoreKey), newApp.GetKey(slashingtypes.StoreKey), [][]byte{}},
Expand All @@ -355,7 +359,9 @@ func TestAppImportExport(t *testing.T) {
{bApp.GetKey(govtypes.StoreKey), newApp.GetKey(govtypes.StoreKey), [][]byte{}},
{bApp.GetKey(evidencetypes.StoreKey), newApp.GetKey(evidencetypes.StoreKey), [][]byte{}},
{bApp.GetKey(capabilitytypes.StoreKey), newApp.GetKey(capabilitytypes.StoreKey), [][]byte{}},
{bApp.GetKey(authzkeeper.StoreKey), newApp.GetKey(authzkeeper.StoreKey), [][]byte{authzkeeper.GrantKey, authzkeeper.GrantQueuePrefix}},
{bApp.GetKey(authzkeeper.StoreKey), newApp.GetKey(authzkeeper.StoreKey), [][]byte{
authzkeeper.GrantKey, authzkeeper.GrantQueuePrefix,
}},
}

for _, skp := range storeKeysPrefixes {
Expand All @@ -366,7 +372,9 @@ func TestAppImportExport(t *testing.T) {
require.Equal(t, len(failedKVAs), len(failedKVBs), "unequal sets of key-values to compare")

fmt.Printf("compared %d different key/value pairs between %s and %s\n", len(failedKVAs), skp.A, skp.B)
require.Equal(t, 0, len(failedKVAs), simtestutil.GetSimulationLog(skp.A.Name(), bApp.SimulationManager().StoreDecoders, failedKVAs, failedKVBs))
require.Equal(t, 0, len(failedKVAs), simtestutil.GetSimulationLog(
skp.A.Name(), bApp.SimulationManager().StoreDecoders, failedKVAs, failedKVBs),
)
}
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/pocketd/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (

// InitSDKConfig initializes the SDK's config with the appropriate parameters
// and prefixes so everything is named appropriately for Pocket Network.
// TODO_DISCUSS: Exporting publically for testing purposes only.
// Consider adding a helper per the discussion here: https://github.com/pokt-network/poktroll/pull/59#discussion_r1357816798
// TODO_DISCUSS: Exporting publicly for testing purposes only.
// Consider adding a helper per the discussion here:
// https://github.com/pokt-network/poktroll/pull/59#discussion_r1357816798
func InitSDKConfig() {
// Set prefixes
accountPubKeyPrefix := app.AccountAddressPrefix + "pub"
Expand Down
19 changes: 14 additions & 5 deletions cmd/pocketd/cmd/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,20 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
},
}

cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
cmd.Flags().String(flagVestingAmt, "", "amount of coins for vesting accounts")
cmd.Flags().Int64(flagVestingStart, 0, "schedule start time (unix epoch) for vesting accounts")
cmd.Flags().Int64(flagVestingEnd, 0, "schedule end time (unix epoch) for vesting accounts")
cmd.Flags().
String(
flags.FlagKeyringBackend,
flags.DefaultKeyringBackend,
"Select keyring's backend (os|file|kwallet|pass|test)",
)
cmd.Flags().
String(flags.FlagHome, defaultNodeHome, "The application home directory")
cmd.Flags().
String(flagVestingAmt, "", "amount of coins for vesting accounts")
cmd.Flags().
Int64(flagVestingStart, 0, "schedule start time (unix epoch) for vesting accounts")
cmd.Flags().
Int64(flagVestingEnd, 0, "schedule end time (unix epoch) for vesting accounts")
flags.AddQueryFlagsToCmd(cmd)

return cmd
Expand Down
2 changes: 0 additions & 2 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79107,8 +79107,6 @@ definitions:
description: params holds all the parameters of this module.
type: object
description: QueryParamsResponse is response type for the Query/Params RPC method.
poktroll.service.MsgAddServiceResponse:
type: object
poktroll.tokenomics.Params:
type: object
properties:
Expand Down
26 changes: 19 additions & 7 deletions e2e/tests/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import (

tmcli "github.com/cometbft/cometbft/libs/cli"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/regen-network/gocuke"
"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/app"
"github.com/pokt-network/poktroll/testutil/testclient"
apptypes "github.com/pokt-network/poktroll/x/application/types"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
suppliertypes "github.com/pokt-network/poktroll/x/supplier/types"
"github.com/regen-network/gocuke"
"github.com/stretchr/testify/require"
)

var (
Expand All @@ -37,14 +38,20 @@ var (

flagFeaturesPath string
keyRingFlag = "--keyring-backend=test"
appGateServerUrl = "http://localhost:42069" // Keeping localhost by default because that is how we run the tests on our machines locally
// Keeping localhost by default because that is how we run the tests on our machines locally
appGateServerUrl = "http://localhost:42069"
)

func init() {
addrRe = regexp.MustCompile(`address:\s+(\S+)\s+name:\s+(\S+)`)
amountRe = regexp.MustCompile(`amount:\s+"(.+?)"\s+denom:\s+upokt`)

flag.StringVar(&flagFeaturesPath, "features-path", "*.feature", "Specifies glob paths for the runner to look up .feature files")
flag.StringVar(
&flagFeaturesPath,
"features-path",
"*.feature",
"Specifies glob paths for the runner to look up .feature files",
)

// If "APPGATE_SERVER_URL" envar is present, use it for appGateServerUrl
if url := os.Getenv("APPGATE_SERVER_URL"); url != "" {
Expand Down Expand Up @@ -255,7 +262,7 @@ func (s *suite) TheSupplierIsStakedForService(supplierName string, serviceId str
s.Fatalf("supplier %s is not staked for service %s", supplierName, serviceId)
}

func (s *suite) TheSessionForApplicationAndServiceContainsTheSupplier(appName string, serviceId string, supplierName string) {
func (s *suite) TheSessionForApplicationAndServiceContainsTheSupplier(appName, serviceId, supplierName string) {
app, found := accNameToAppMap[appName]
if !found {
s.Fatalf("application %s not found", appName)
Expand Down Expand Up @@ -287,10 +294,15 @@ func (s *suite) TheSessionForApplicationAndServiceContainsTheSupplier(appName st
s.Fatalf("session for app %s and service %s does not contain supplier %s", appName, serviceId, supplierName)
}

func (s *suite) TheApplicationSendsTheSupplierARequestForServiceWithData(appName, supplierName, serviceId, requestData string) {
func (s *suite) TheApplicationSendsTheSupplierARequestForServiceWithData(
appName, supplierName, serviceId, requestData string,
) {
res, err := s.pocketd.RunCurl(appGateServerUrl, serviceId, requestData)
if err != nil {
s.Fatalf("error sending relay request from app %s to supplier %s for service %s: %v", appName, supplierName, serviceId, err)
s.Fatalf(
"error sending relay request from app %s to supplier %s for service %s: %v",
appName, supplierName, serviceId, err,
)
}

relayKey := relayReferenceKey(appName, supplierName)
Expand Down
Loading