Skip to content

Commit

Permalink
precommit: check log and error capitalisation (#693)
Browse files Browse the repository at this point in the history
Adds pre-commit hooks that checks:
 - log messages are capitalised
 - error messages are not capitalised

category: misc
ticket: none
  • Loading branch information
corverroos committed Jun 10, 2022
1 parent 937835e commit f48f1c4
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ linters-settings:
default-signifies-exhaustive: true
forbidigo:
forbid:
- 'fmt\.Print.*' # Prevent debug logging
- 'fmt\.Print.*(# Avoid debug logging)?'
gci:
local-prefixes: github.com/obolnetwork/charon
gocritic:
Expand Down
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ repos:
language: script
entry: .pre-commit/run_buf.sh
types: [ file, proto ]
- id: run-regexp
name: run-regexp
language: script
entry: .pre-commit/run_regexp.sh
types: [ file, go ]
exclude: "_test.go"
12 changes: 12 additions & 0 deletions .pre-commit/run_regexp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

FILES=$@

function check() {
grep -HnE "$2" $FILES && printf "\n❌ Regexp check failed: %s\n\n" "$1"
}

check 'Log messages must be capitalised' 'log\.(Error|Warn|Info|Debug)\(ctx, "[[:lower:]]' && exit 1
check 'Error messages must not be capitalised' 'errors\.(New|Wrap)\((err, )?"[[:upper:]]' && exit 1

true
2 changes: 1 addition & 1 deletion cmd/enr.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func newEnrCmd(runFunc func(io.Writer, p2p.Config, string) error) *cobra.Command
func runNewENR(w io.Writer, config p2p.Config, dataDir string) error {
key, err := p2p.LoadPrivKey(dataDir)
if errors.Is(err, fs.ErrNotExist) {
return errors.New("ENR private key not found. If this is your first time running this client, create one with `charon create enr`.", z.Str("enr_path", p2p.KeyPath(dataDir))) //nolint:revive
return errors.New("private key not found. If this is your first time running this client, create one with `charon create enr`.", z.Str("enr_path", p2p.KeyPath(dataDir))) //nolint:revive
} else if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/enr_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ func TestRunNewEnr(t *testing.T) {
require.NoError(t, err)

got := runNewENR(io.Discard, p2p.Config{}, temp)
expected := errors.New("ENR private key not found. If this is your first time running this client, create one with `charon create enr`.", z.Str("enr_path", p2p.KeyPath(temp)))
expected := errors.New("private key not found. If this is your first time running this client, create one with `charon create enr`.", z.Str("enr_path", p2p.KeyPath(temp)))
require.Equal(t, expected.Error(), got.Error())
}
6 changes: 3 additions & 3 deletions core/parsigex/parsigex.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ func (m *ParSigEx) handle(s network.Stream) {

b, err := io.ReadAll(s)
if err != nil {
log.Error(ctx, "read proto bytes", err)
log.Error(ctx, "Read proto bytes", err)
return
}

var pb pbv1.ParSigExMsg
if err := proto.Unmarshal(b, &pb); err != nil {
log.Error(ctx, "unmarshal parsigex proto", err)
log.Error(ctx, "Unmarshal parsigex proto", err)
return
}

Expand All @@ -87,7 +87,7 @@ func (m *ParSigEx) handle(s network.Stream) {
for _, sub := range m.subs {
err := sub(ctx, duty, set)
if err != nil {
log.Error(ctx, "subscribe error", err)
log.Error(ctx, "Subscribe error", err)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func waitChainStart(ctx context.Context, eth2Cl eth2Provider, clock clockwork.Cl
for {
genesis, err := eth2Cl.GenesisTime(ctx)
if err != nil {
log.Error(ctx, "failure getting genesis time", err)
log.Error(ctx, "Failure getting genesis time", err)
clock.Sleep(time.Second * 5) // TODO(corver): Improve backoff

continue
Expand All @@ -544,7 +544,7 @@ func waitBeaconSync(ctx context.Context, eth2Cl eth2Provider, clock clockwork.Cl
for {
state, err := eth2Cl.NodeSyncing(ctx)
if err != nil {
log.Error(ctx, "failure getting sync state", err)
log.Error(ctx, "Failure getting sync state", err)
clock.Sleep(time.Second * 5) // TODO(corver): Improve backoff

continue
Expand Down
4 changes: 2 additions & 2 deletions testutil/beaconmock/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func WithValidatorSet(set ValidatorSet) Option {
if ok {
resp[val.Index] = cloneValidator(val)
} else {
log.Debug(ctx, "pubkey not found")
log.Debug(ctx, "Pubkey not found")
}
}

Expand All @@ -141,7 +141,7 @@ func WithValidatorSet(set ValidatorSet) Option {
if ok {
resp[index] = cloneValidator(val)
} else {
log.Debug(ctx, "index not found")
log.Debug(ctx, "Index not found")
}
}

Expand Down
18 changes: 1 addition & 17 deletions testutil/compose/compose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func newAutoCmd(tmplCallback func(data *compose.TmplData)) *cobra.Command {
fail = true
}
if fail {
return errors.New("Alerts detected")
return errors.New("alerts detected")
}

return nil
Expand All @@ -236,22 +236,6 @@ func printDockerCompose(ctx context.Context, dir string) error {
return errors.Wrap(err, "exec cat docker-compose.yml")
}

return ls(ctx, dir)
}

// printDockerCompose prints the docker-compose.yml file to stdout.
func ls(ctx context.Context, dir string) error {
log.Info(ctx, "ls dir")
cmd := exec.CommandContext(ctx, "ls", "-la")
cmd.Dir = dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

err := cmd.Run()
if err != nil {
return errors.Wrap(err, "exec ls")
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion testutil/validatormock/validatormock.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func getBlock(endpoint string, base string) (io.Reader, error) {

statusFamily := res.StatusCode / 100
if statusFamily != 2 {
return nil, errors.New("GET failed", z.Int("status", res.StatusCode), z.Str("data", string(data)))
return nil, errors.New("get failed", z.Int("status", res.StatusCode), z.Str("data", string(data)))
}

return bytes.NewReader(data), nil
Expand Down

0 comments on commit f48f1c4

Please sign in to comment.