Skip to content

Commit

Permalink
chore: add custom linter (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Oct 19, 2024
1 parent e7675af commit 3af3704
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 13 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/golang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ concurrency:
cancel-in-progress: true

jobs:
custom-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: |
curl -sLO https://github.com/BurntSushi/ripgrep/releases/download/14.1.0/ripgrep_14.1.0-1_amd64.deb
sudo dpkg -i ripgrep_14.1.0-1_amd64.deb
- run: ./scripts/linter.sh

lint:
runs-on: ubuntu-latest
steps:
Expand All @@ -40,7 +49,6 @@ jobs:
with:
go-version: '1.23'
- run: |
make check-no-lint
version=$(grep golangci_version= ./Makefile | awk -F '=' '{print $2}')
echo "GOLANGCI_VERSION=$version" >> $GITHUB_ENV
- uses: golangci/golangci-lint-action@v6
Expand Down
10 changes: 3 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,10 @@ lint-install:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version); \
fi

check-no-lint:
@if [ $$(find . -name '*.go' -type f | xargs grep 'nolint\|#nosec' | wc -l) -ne 26 ]; then \
echo "\033[91m--> increase or decrease nolint, please recheck them\033[0m"; \
echo "\033[91m--> list nolint: \`find . -name '*.go' -type f | xargs grep 'nolint\|#nosec'\`\033[0m"; \
exit 1;\
fi
custom-lint:
@./scripts/linter.sh

lint: check-no-lint lint-install
lint: custom-lint lint-install
@echo "--> Running linter"
@golangci-lint run --build-tags=$(GO_BUILD) --out-format=tab

Expand Down
55 changes: 55 additions & 0 deletions scripts/linter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash

set -eo pipefail

patternLimits=(
"nolint:21"
"#nosec:5"
"CrossChain:4"
"cross chain:0"
)

if ! command -v rg &>/dev/null; then
echo "rg command not found, please install rg first: https://github.com/BurntSushi/ripgrep?tab=readme-ov-file#installation" && exit 1
fi

check_pattern_count() {
local pattern=$1
local allowed_count=$2
local file_type=$3

# Default values if not provided
file_type=${file_type:-go}

rg_args=(--type go --glob '!*.pb.go' --glob '!*.pulsar.go' --glob '!*.sol.go' --glob '!legacy.go')

if [[ "$allowed_count" -eq 0 ]]; then
if rg "${rg_args[@]}" "$pattern" ./ >/dev/null; then
echo "Warning: Matches found for '$pattern'."
exit 1
fi
return
fi

# Count the actual number of 'pattern' in specified file types
actual_count=$(rg "${rg_args[@]}" "$pattern" ./ | wc -l)
echo "Actual count of '$pattern': $actual_count"

# Compare with the allowed count
if [ "$actual_count" -eq "$allowed_count" ]; then
echo "The count matches the allowed number of suppressions."
elif [ "$actual_count" -lt "$allowed_count" ]; then
echo "The actual count is less than $allowed_count. Consider further reducing the allowed count."
exit 1
else
echo "Warning: The actual count is higher than $allowed_count. Please review and update suppressions or adjust the allowed count."
rg "${rg_args[@]}" "$pattern" ./
exit 1
fi
}

for pattern_limit in "${patternLimits[@]}"; do
pattern=$(echo "$pattern_limit" | cut -d: -f1)
allowed_count=$(echo "$pattern_limit" | cut -d: -f2)
check_pattern_count "$pattern" "$allowed_count"
done
2 changes: 1 addition & 1 deletion tests/crosschain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
crosschaintypes "github.com/functionx/fx-core/v8/x/crosschain/types"
)

func (suite *IntegrationTest) CrossChainTest() {
func (suite *IntegrationTest) CrosschainTest() {
}

func (suite *IntegrationTest) UpdateParamsTest() {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestIntegrationTest(t *testing.T) {
}

func (suite *IntegrationTest) TestRun() {
suite.CrossChainTest()
suite.CrosschainTest()

suite.StakingTest()
suite.StakingContractTest()
Expand All @@ -89,7 +89,7 @@ func (suite *IntegrationTest) TestRun() {
suite.ByPassFeeTest()
}

func (suite *IntegrationTest) GetCrossChainByName(chainName string) CrosschainTestSuite {
func (suite *IntegrationTest) GetByName(chainName string) CrosschainTestSuite {
for _, c := range suite.crosschain {
if c.chainName == chainName {
return c
Expand Down
4 changes: 2 additions & 2 deletions tests/precompile_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (suite *PrecompileTestSuite) HexAddress() common.Address {
return common.BytesToAddress(suite.privKey.PubKey().Address())
}

func (suite *PrecompileTestSuite) TransferCrossChain(token common.Address, recipient string, amount, fee *big.Int, target string) *ethtypes.Transaction {
func (suite *PrecompileTestSuite) TransferCrosschain(token common.Address, recipient string, amount, fee *big.Int, target string) *ethtypes.Transaction {
privateKey := suite.privKey
beforeBalanceOf := suite.BalanceOf(token, common.BytesToAddress(privateKey.PubKey().Address().Bytes()))
pack, err := contract.GetFIP20().ABI.Pack("transferCrossChain", recipient, amount, fee, fxtypes.MustStrToByte32(target))
Expand All @@ -48,7 +48,7 @@ func (suite *PrecompileTestSuite) TransferCrossChain(token common.Address, recip
return ethTx
}

func (suite *PrecompileTestSuite) CrossChainAndResponse(token common.Address, recipient string, amount, fee *big.Int, target string) *ethtypes.Transaction {
func (suite *PrecompileTestSuite) CrosschainAndResponse(token common.Address, recipient string, amount, fee *big.Int, target string) *ethtypes.Transaction {
privateKey := suite.privKey
crosschainContract := crosschaintypes.GetAddress()
suite.ApproveERC20(privateKey, token, crosschainContract, big.NewInt(0).Add(amount, fee))
Expand Down

0 comments on commit 3af3704

Please sign in to comment.