From f32e497a9813b5495ba33f7bf6d9c6acc259d79a Mon Sep 17 00:00:00 2001 From: Nikita Sklyarov <12935413+nsklyarov@users.noreply.github.com> Date: Wed, 10 Jan 2024 17:49:21 +0400 Subject: [PATCH] feat: allow setting a bool value for skip_output (#601) * added skip_output: true option that silences everything * chore: add tests --------- Co-authored-by: Valentin Kiselev --- internal/config/config.go | 2 +- internal/lefthook/run.go | 10 +++------- internal/log/skip_settings.go | 27 ++++++++++++++++++++++++++- internal/log/skip_settings_test.go | 24 ++++++++++++++++++++---- 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 7aea9cf7..ea8ffd24 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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"` diff --git a/internal/lefthook/run.go b/internal/lefthook/run.go index 0fec2287..92a1db1f 100644 --- a/internal/lefthook/run.go +++ b/internal/lefthook/run.go @@ -8,7 +8,6 @@ import ( "os/signal" "path/filepath" "slices" - "strings" "time" "github.com/evilmartians/lefthook/internal/config" @@ -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 } @@ -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( diff --git a/internal/log/skip_settings.go b/internal/log/skip_settings.go index cf3527ea..6fbe6f27 100644 --- a/internal/log/skip_settings.go +++ b/internal/log/skip_settings.go @@ -1,5 +1,7 @@ package log +import "strings" + const ( skipMeta = 1 << iota skipSuccess @@ -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 @@ -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) } diff --git a/internal/log/skip_settings_test.go b/internal/log/skip_settings_test.go index 8d063a16..9eb12b53 100644 --- a/internal/log/skip_settings_test.go +++ b/internal/log/skip_settings_test.go @@ -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{ @@ -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"])