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

Added bool option for skip noise #601

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Config struct {
SourceDir string `mapstructure:"source_dir"`
SourceDirLocal string `mapstructure:"source_dir_local"`
Rc string `mapstructure:"rc,omitempty"`
SkipOutput []string `mapstructure:"skip_output,omitempty"`
SkipOutput interface{} `mapstructure:"skip_output,omitempty"`
Extends []string `mapstructure:"extends,omitempty"`
NoTTY bool `mapstructure:"no_tty,omitempty"`
AssertLefthookInstalled bool `mapstructure:"assert_lefthook_installed,omitempty"`
Expand Down
10 changes: 3 additions & 7 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os/signal"
"path/filepath"
"slices"
"strings"
"time"

"github.com/evilmartians/lefthook/internal/config"
Expand Down Expand Up @@ -61,6 +60,7 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error {

return err
}

if err = cfg.Validate(); err != nil {
return err
}
Expand All @@ -73,14 +73,10 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error {
log.SetLevel(log.WarnLevel)
}

if tags := os.Getenv(envSkipOutput); tags != "" {
cfg.SkipOutput = append(cfg.SkipOutput, strings.Split(tags, ",")...)
}
tags := os.Getenv(envSkipOutput)

var logSettings log.SkipSettings
for _, skipOption := range cfg.SkipOutput {
(&logSettings).ApplySetting(skipOption)
}
(&logSettings).ApplySettings(tags, cfg.SkipOutput)

if !logSettings.SkipMeta() {
log.Box(
Expand Down
27 changes: 26 additions & 1 deletion internal/log/skip_settings.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package log

import "strings"

const (
skipMeta = 1 << iota
skipSuccess
Expand All @@ -10,11 +12,26 @@ const (
skipExecutionOutput
skipExecutionInfo
skipEmptySummary
skipAll = (1 << iota) - 1
)

type SkipSettings int16

func (s *SkipSettings) ApplySetting(setting string) {
func (s *SkipSettings) ApplySettings(tags string, skipOutput interface{}) {
switch typedSkipOutput := skipOutput.(type) {
case bool:
s.SkipAll(typedSkipOutput)
case []string:
if tags != "" {
typedSkipOutput = append(typedSkipOutput, strings.Split(tags, ",")...)
}
for _, skipOption := range typedSkipOutput {
s.applySetting(skipOption)
}
}
}

func (s *SkipSettings) applySetting(setting string) {
switch setting {
case "meta":
*s |= skipMeta
Expand All @@ -37,6 +54,14 @@ func (s *SkipSettings) ApplySetting(setting string) {
}
}

func (s *SkipSettings) SkipAll(val bool) {
if val {
*s = skipAll &^ skipFailure
} else {
*s = 0
}
}

func (s SkipSettings) SkipSuccess() bool {
return s.doSkip(skipSuccess)
}
Expand Down
24 changes: 20 additions & 4 deletions internal/log/skip_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (

func TestSkipSetting(t *testing.T) {
for i, tt := range [...]struct {
settings []string
settings interface{}
results map[string]bool
}{
{
settings: []string{},
results: map[string]bool{},
},
{
settings: false,
results: map[string]bool{},
},
{
settings: []string{"failure", "execution"},
results: map[string]bool{
Expand Down Expand Up @@ -45,13 +49,25 @@ func TestSkipSetting(t *testing.T) {
"empty_summary": true,
},
},
{
settings: true,
results: map[string]bool{
"meta": true,
"summary": true,
"success": true,
"failure": false,
"skips": true,
"execution": true,
"execution_out": true,
"execution_info": true,
"empty_summary": true,
},
},
} {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
var settings SkipSettings

for _, option := range tt.settings {
(&settings).ApplySetting(option)
}
(&settings).ApplySettings("", tt.settings)

if settings.SkipMeta() != tt.results["meta"] {
t.Errorf("expected SkipMeta to be %v", tt.results["meta"])
Expand Down
Loading