Skip to content

Commit

Permalink
precommit: lint importing of testutil
Browse files Browse the repository at this point in the history
  • Loading branch information
corverroos committed Apr 28, 2023
1 parent 19b3263 commit bbe4c00
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ repos:
entry: .pre-commit/run_regexp.sh
types: [ file, go ]
exclude: "_test.go"
- id: run-testutil
name: run-testutil
language: script
entry: .pre-commit/run_testutil.sh
types: [ file, go ]
27 changes: 27 additions & 0 deletions .pre-commit/run_testutil.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

FILES=$@

# check runs a regexp check on the given files
function check() {
grep -HnE "$2" $FILES && printf "\n❌ Regexp check failed: %s\n\n" "$1"
}

# exclude_names excludes file names matching the given regex from the list of files
function exclude_names() {
FILES=$(echo $FILES | tr ' ' '\n' | grep -vE "$1" | tr '\n' ' ')
}

# exclude_names excludes files with content matching the given regex from the list of files
function exclude_content() {
FILES=$(echo $FILES | tr ' ' '\n' | xargs grep -LZE "$1" | tr '\n' ' ')
}

# Exclude all file names with 'test' in the path.
exclude_names 'test'
exclude_content '// Allow testutil'

# These checks apply to all non-test files
check 'Testutil package may only be imported by tests' 'github.com/obolnetwork/charon/testutil' && exit 1

true
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import (
"github.com/obolnetwork/charon/p2p"
"github.com/obolnetwork/charon/tbls"
"github.com/obolnetwork/charon/tbls/tblsconv"
"github.com/obolnetwork/charon/testutil/beaconmock"
"github.com/obolnetwork/charon/testutil/beaconmock" // Allow testutil
)

const eth2ClientTimeout = time.Second * 2
Expand Down
2 changes: 1 addition & 1 deletion app/vmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/obolnetwork/charon/core"
"github.com/obolnetwork/charon/eth2util"
"github.com/obolnetwork/charon/eth2util/keystore"
"github.com/obolnetwork/charon/testutil/validatormock"
"github.com/obolnetwork/charon/testutil/validatormock" // Allow testutil
)

// wireValidatorMock wires the validator mock if enabled. It connects via http validatorapi.Router.
Expand Down
2 changes: 2 additions & 0 deletions tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package main
// This file contains build time developer tools used in the charon repo.
// To install all the tools run: go generate tools.go

// Allow testutil

import (
_ "github.com/bufbuild/buf/cmd/buf"
_ "golang.org/x/tools/cmd/stringer"
Expand Down

0 comments on commit bbe4c00

Please sign in to comment.