diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index ab9d4d6b..2562efa1 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -18,4 +18,4 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v3 with: - version: v1.54.1 + version: v1.56.2 diff --git a/.golangci.yml b/.golangci.yml index 5e7f3bc4..c4588167 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,4 +1,9 @@ linters-settings: + gocritic: + disabled-checks: + - hugeParam + enabled-tags: + - performance govet: check-shadowing: true misspell: @@ -10,6 +15,12 @@ linters-settings: - name: unused-parameter disabled: true +issues: + exclude: + # https://github.com/catenacyber/perfsprint/issues/21 + # https://github.com/catenacyber/perfsprint/issues/23 + - "^fmt\\.Sprintf can be replaced with string (addi|concatena)tion$" + linters: disable-all: true enable: @@ -38,7 +49,6 @@ linters: - gocyclo - godot - godox - - gofmt - gofumpt - goheader - goimports @@ -47,14 +57,22 @@ linters: - gosimple - govet - ineffassign + - makezero + - mirror - misspell - nestif - noctx - nolintlint + - perfsprint + - predeclared + - reassign - revive - staticcheck + - tagalign + - tenv - typecheck - unconvert - unparam - unused + - usestdlibvars - whitespace diff --git a/.typos.toml b/.typos.toml new file mode 100644 index 00000000..0ca883a5 --- /dev/null +++ b/.typos.toml @@ -0,0 +1,3 @@ +[default.extend-words] +# .golangci.yml +decorder = "decorder" diff --git a/Makefile b/Makefile index d11919e9..179c3fe1 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ bench: bin/golangci-lint: @test -x $$(go env GOPATH)/bin/golangci-lint || \ - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.54.1 + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.56.2 lint: bin/golangci-lint $$(go env GOPATH)/bin/golangci-lint run --fix diff --git a/internal/config/command.go b/internal/config/command.go index 1d47ac9a..7fce4c1e 100644 --- a/internal/config/command.go +++ b/internal/config/command.go @@ -12,23 +12,23 @@ import ( var errFilesIncompatible = errors.New("One of your runners contains incompatible file types") type Command struct { - Run string `mapstructure:"run" yaml:"run" json:"run" toml:"run"` - - Skip interface{} `mapstructure:"skip" yaml:",omitempty" json:"skip,omitempty" toml:"skip,omitempty,inline"` - Only interface{} `mapstructure:"only" yaml:",omitempty" json:"only,omitempty" toml:"only,omitempty,inline"` - Tags []string `mapstructure:"tags" yaml:",omitempty" json:"tags,omitempty" toml:"tags,omitempty"` - Glob string `mapstructure:"glob" yaml:",omitempty" json:"glob,omitempty" toml:"glob,omitempty"` - Files string `mapstructure:"files" yaml:",omitempty" json:"files,omitempty" toml:"files,omitempty"` - Env map[string]string `mapstructure:"env" yaml:",omitempty" json:"env,omitempty" toml:"env,omitempty"` - - Root string `mapstructure:"root" yaml:",omitempty" json:"root,omitempty" toml:"root,omitempty"` - Exclude string `mapstructure:"exclude" yaml:",omitempty" json:"exclude,omitempty" toml:"exclude,omitempty"` - Priority int `mapstructure:"priority" yaml:",omitempty" json:"priority,omitempty" toml:"priority,omitempty"` - - FailText string `mapstructure:"fail_text" yaml:"fail_text,omitempty" json:"fail_text,omitempty" toml:"fail_text,omitempty"` - Interactive bool `mapstructure:"interactive" yaml:",omitempty" json:"interactive,omitempty" toml:"interactive,omitempty"` - UseStdin bool `mapstructure:"use_stdin" yaml:",omitempty" json:"use_stdin,omitempty" toml:"use_stdin,omitempty"` - StageFixed bool `mapstructure:"stage_fixed" yaml:"stage_fixed,omitempty" json:"stage_fixed,omitempty" toml:"stage_fixed,omitempty"` + Run string `json:"run" mapstructure:"run" toml:"run" yaml:"run"` + + Skip interface{} `json:"skip,omitempty" mapstructure:"skip" toml:"skip,omitempty,inline" yaml:",omitempty"` + Only interface{} `json:"only,omitempty" mapstructure:"only" toml:"only,omitempty,inline" yaml:",omitempty"` + Tags []string `json:"tags,omitempty" mapstructure:"tags" toml:"tags,omitempty" yaml:",omitempty"` + Glob string `json:"glob,omitempty" mapstructure:"glob" toml:"glob,omitempty" yaml:",omitempty"` + Files string `json:"files,omitempty" mapstructure:"files" toml:"files,omitempty" yaml:",omitempty"` + Env map[string]string `json:"env,omitempty" mapstructure:"env" toml:"env,omitempty" yaml:",omitempty"` + + Root string `json:"root,omitempty" mapstructure:"root" toml:"root,omitempty" yaml:",omitempty"` + Exclude string `json:"exclude,omitempty" mapstructure:"exclude" toml:"exclude,omitempty" yaml:",omitempty"` + Priority int `json:"priority,omitempty" mapstructure:"priority" toml:"priority,omitempty" yaml:",omitempty"` + + FailText string `json:"fail_text,omitempty" mapstructure:"fail_text" toml:"fail_text,omitempty" yaml:"fail_text,omitempty"` + Interactive bool `json:"interactive,omitempty" mapstructure:"interactive" toml:"interactive,omitempty" yaml:",omitempty"` + UseStdin bool `json:"use_stdin,omitempty" mapstructure:"use_stdin" toml:"use_stdin,omitempty" yaml:",omitempty"` + StageFixed bool `json:"stage_fixed,omitempty" mapstructure:"stage_fixed" toml:"stage_fixed,omitempty" yaml:"stage_fixed,omitempty"` } func (c Command) Validate() error { diff --git a/internal/config/hook.go b/internal/config/hook.go index 3a958c72..8103add4 100644 --- a/internal/config/hook.go +++ b/internal/config/hook.go @@ -18,20 +18,20 @@ type Hook struct { // Should be unmarshalled with `mapstructure:"commands"` // But replacing '{cmd}' is still an issue // Unmarshalling it manually, so omit auto unmarshalling - Commands map[string]*Command `mapstructure:"-" yaml:",omitempty" json:"commands,omitempty" toml:"commands,omitempty"` + Commands map[string]*Command `json:"commands,omitempty" mapstructure:"-" toml:"commands,omitempty" yaml:",omitempty"` // Should be unmarshalled with `mapstructure:"scripts"` // But parsing keys with dots in it is still an issue: https://github.com/spf13/viper/issues/324 // Unmarshalling it manually, so omit auto unmarshalling - Scripts map[string]*Script `mapstructure:"-" yaml:",omitempty" json:"scripts,omitempty" toml:"scripts,omitempty"` - - Files string `mapstructure:"files" yaml:",omitempty" json:"files,omitempty" toml:"files,omitempty"` - Parallel bool `mapstructure:"parallel" yaml:",omitempty" json:"parallel,omitempty" toml:"parallel,omitempty"` - Piped bool `mapstructure:"piped" yaml:",omitempty" json:"piped,omitempty" toml:"piped,omitempty"` - Follow bool `mapstructure:"follow" yaml:",omitempty" json:"follow,omitempty" toml:"follow,omitempty"` - ExcludeTags []string `mapstructure:"exclude_tags" yaml:"exclude_tags,omitempty" json:"exclude_tags,omitempty" toml:"exclude_tags,omitempty"` - Skip interface{} `mapstructure:"skip" yaml:",omitempty" json:"skip,omitempty" toml:"skip,omitempty,inline"` - Only interface{} `mapstructure:"only" yaml:",omitempty" json:"only,omitempty" toml:"only,omitempty,inline"` + Scripts map[string]*Script `json:"scripts,omitempty" mapstructure:"-" toml:"scripts,omitempty" yaml:",omitempty"` + + Files string `json:"files,omitempty" mapstructure:"files" toml:"files,omitempty" yaml:",omitempty"` + Parallel bool `json:"parallel,omitempty" mapstructure:"parallel" toml:"parallel,omitempty" yaml:",omitempty"` + Piped bool `json:"piped,omitempty" mapstructure:"piped" toml:"piped,omitempty" yaml:",omitempty"` + Follow bool `json:"follow,omitempty" mapstructure:"follow" toml:"follow,omitempty" yaml:",omitempty"` + ExcludeTags []string `json:"exclude_tags,omitempty" mapstructure:"exclude_tags" toml:"exclude_tags,omitempty" yaml:"exclude_tags,omitempty"` + Skip interface{} `json:"skip,omitempty" mapstructure:"skip" toml:"skip,omitempty,inline" yaml:",omitempty"` + Only interface{} `json:"only,omitempty" mapstructure:"only" toml:"only,omitempty,inline" yaml:",omitempty"` } func (h *Hook) Validate() error { diff --git a/internal/config/remote.go b/internal/config/remote.go index ebdfca57..64687e75 100644 --- a/internal/config/remote.go +++ b/internal/config/remote.go @@ -1,11 +1,11 @@ package config type Remote struct { - GitURL string `mapstructure:"git_url" yaml:"git_url" json:"git_url,omitempty" toml:"git_url"` - Ref string `mapstructure:"ref,omitempty" yaml:",omitempty" json:"ref,omitempty" toml:"ref,omitempty"` + GitURL string `json:"git_url,omitempty" mapstructure:"git_url" toml:"git_url" yaml:"git_url"` + Ref string `json:"ref,omitempty" mapstructure:"ref,omitempty" toml:"ref,omitempty" yaml:",omitempty"` // Deprecated - Config string `mapstructure:"config,omitempty" yaml:",omitempty" json:"config,omitempty" toml:"config,omitempty"` - Configs []string `mapstructure:"configs,omitempty" yaml:",omitempty" json:"configs,omitempty" toml:"configs,omitempty"` + Config string `json:"config,omitempty" mapstructure:"config,omitempty" toml:"config,omitempty" yaml:",omitempty"` + Configs []string `json:"configs,omitempty" mapstructure:"configs,omitempty" toml:"configs,omitempty" yaml:",omitempty"` } func (r *Remote) Configured() bool { diff --git a/internal/config/script.go b/internal/config/script.go index 085b65f7..f84faad4 100644 --- a/internal/config/script.go +++ b/internal/config/script.go @@ -10,17 +10,17 @@ import ( ) type Script struct { - Runner string `mapstructure:"runner" yaml:"runner" json:"runner" toml:"runner"` + Runner string `json:"runner" mapstructure:"runner" toml:"runner" yaml:"runner"` - Skip interface{} `mapstructure:"skip" yaml:",omitempty" json:"skip,omitempty" toml:"skip,omitempty,inline"` - Only interface{} `mapstructure:"only" yaml:",omitempty" json:"only,omitempty" toml:"only,omitempty,inline"` - Tags []string `mapstructure:"tags" yaml:",omitempty" json:"tags,omitempty" toml:"tags,omitempty"` - Env map[string]string `mapstructure:"env" yaml:",omitempty" json:"env,omitempty" toml:"env,omitempty"` + Skip interface{} `json:"skip,omitempty" mapstructure:"skip" toml:"skip,omitempty,inline" yaml:",omitempty"` + Only interface{} `json:"only,omitempty" mapstructure:"only" toml:"only,omitempty,inline" yaml:",omitempty"` + Tags []string `json:"tags,omitempty" mapstructure:"tags" toml:"tags,omitempty" yaml:",omitempty"` + Env map[string]string `json:"env,omitempty" mapstructure:"env" toml:"env,omitempty" yaml:",omitempty"` - FailText string `mapstructure:"fail_text" yaml:"fail_text,omitempty" json:"fail_text,omitempty" toml:"fail_text,omitempty"` - Interactive bool `mapstructure:"interactive" yaml:",omitempty" json:"interactive,omitempty" toml:"interactive,omitempty"` - UseStdin bool `mapstructure:"use_stdin" yaml:",omitempty" json:"use_stdin,omitempty" toml:"use_stdin,omitempty"` - StageFixed bool `mapstructure:"stage_fixed" yaml:"stage_fixed,omitempty" json:"stage_fixed,omitempty" toml:"stage_fixed,omitempty"` + FailText string `json:"fail_text,omitempty" mapstructure:"fail_text" toml:"fail_text,omitempty" yaml:"fail_text,omitempty"` + Interactive bool `json:"interactive,omitempty" mapstructure:"interactive" toml:"interactive,omitempty" yaml:",omitempty"` + UseStdin bool `json:"use_stdin,omitempty" mapstructure:"use_stdin" toml:"use_stdin,omitempty" yaml:",omitempty"` + StageFixed bool `json:"stage_fixed,omitempty" mapstructure:"stage_fixed" toml:"stage_fixed,omitempty" yaml:"stage_fixed,omitempty"` } func (s Script) DoSkip(gitState git.State) bool { diff --git a/internal/lefthook/install.go b/internal/lefthook/install.go index 549f207d..a90b552b 100644 --- a/internal/lefthook/install.go +++ b/internal/lefthook/install.go @@ -4,7 +4,7 @@ import ( "bufio" "crypto/md5" "encoding/hex" - "fmt" + "errors" "io" "os" "path/filepath" @@ -31,7 +31,7 @@ const ( var ( lefthookChecksumRegexp = regexp.MustCompile(`(\w+)\s+(\d+)`) configGlob = glob.MustCompile("{.,}lefthook.{yml,yaml,json,toml}") - errNoConfig = fmt.Errorf("no lefthook config found") + errNoConfig = errors.New("no lefthook config found") ) // Install installs the hooks from config file to the .git/hooks. diff --git a/internal/lefthook/run.go b/internal/lefthook/run.go index 431c082f..002968eb 100644 --- a/internal/lefthook/run.go +++ b/internal/lefthook/run.go @@ -76,21 +76,16 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error { log.SetLevel(log.WarnLevel) } - outputLogTags := os.Getenv(envOutput) - outputSkipTags := os.Getenv(envSkipOutput) + enableLogTags := os.Getenv(envOutput) + disableLogTags := os.Getenv(envSkipOutput) - var logSettings log.Settings + logSettings := log.NewSettings() + logSettings.Apply(enableLogTags, disableLogTags, cfg.Output, cfg.SkipOutput) - if outputSkipTags == "" && cfg.SkipOutput == nil { - logSettings = log.NewSettings() - logSettings.ApplySettings(outputLogTags, cfg.Output) - } else { - // Deprecate skip_output in the future. Leaving as is to reduce noise in output. - // log.Warn("skip_output is deprecated, please use output option") - - logSettings = log.NewSkipSettings() //nolint:staticcheck //SA1019: for temporary backward compatibility - logSettings.ApplySettings(outputSkipTags, cfg.SkipOutput) - } + // Deprecate skip_output in the future. Leaving as is to reduce noise in output. + // if outputSkipTags != "" || cfg.SkipOutput != nil { + // log.Warn("skip_output is deprecated, please use output option") + // } if logSettings.LogMeta() { log.Box( diff --git a/internal/lefthook/run/exec/execute_unix.go b/internal/lefthook/run/exec/execute_unix.go index d716bab3..9958e8b7 100644 --- a/internal/lefthook/run/exec/execute_unix.go +++ b/internal/lefthook/run/exec/execute_unix.go @@ -44,7 +44,7 @@ func (e CommandExecutor) Execute(ctx context.Context, opts Options, out io.Write } root, _ := filepath.Abs(opts.Root) - envs := make([]string, len(opts.Env)) + envs := make([]string, 0, len(opts.Env)) for name, value := range opts.Env { envs = append( envs, diff --git a/internal/lefthook/run/runner.go b/internal/lefthook/run/runner.go index 9f2146e4..a49d4449 100644 --- a/internal/lefthook/run/runner.go +++ b/internal/lefthook/run/runner.go @@ -83,7 +83,7 @@ func (r *Runner) RunAll(ctx context.Context, sourceDirs []string) { defer log.StopSpinner() } - scriptDirs := make([]string, len(sourceDirs)) + scriptDirs := make([]string, 0, len(sourceDirs)) for _, sourceDir := range sourceDirs { scriptDirs = append(scriptDirs, filepath.Join( sourceDir, r.HookName, diff --git a/internal/log/settings.go b/internal/log/settings.go index 8cc3b59d..0df81511 100644 --- a/internal/log/settings.go +++ b/internal/log/settings.go @@ -14,11 +14,23 @@ const ( executionOutput executionInfo emptySummary - enableAll = ^0 // Set all bits as 1 +) + +const ( + disableAll = 0 + skipMeta = (1 << iota) + skipSuccess + skipFailure + skipSummary + skipSkips + skipExecution + skipExecutionOutput + skipExecutionInfo + skipEmptySummary ) type Settings interface { - ApplySettings(tags string, skipOutput interface{}) + Apply(enableTags, disableTags string, enable, disable interface{}) LogSuccess() bool LogFailure() bool LogSummary() bool @@ -30,111 +42,163 @@ type Settings interface { LogEmptySummary() bool } -type OutputSettings int16 +type LogSettings struct { + bitmap int16 +} func NewSettings() Settings { - var s OutputSettings + s := LogSettings{^disableAll} return &s } -func (s *OutputSettings) ApplySettings(tags string, output interface{}) { - if tags == "" && (output == nil || output == "") { - s.enableAll(true) +func (s *LogSettings) Apply(enableTags, disableTags string, enable, disable interface{}) { + if enableTags == "" && disableTags == "" && (enable == nil || enable == "") && (disable == nil || disable == "") { + s.enableAll() return } - if val, ok := output.(bool); ok { - s.enableAll(val) + if enableOutput, ok := enable.(bool); ok && enableTags == "" && disableTags == "" { + if enableOutput { + s.enableAll() + } else { + s.disableAll() + } return } - if options, ok := output.([]interface{}); ok { - if len(options) == 0 { - s.enableAll(true) + if disableOutput, ok := disable.(bool); ok && enableTags == "" && disableTags == "" { + if disableOutput { + s.disableAll() return } - for _, option := range options { - if optStr, ok := option.(string); ok { - s.applySetting(optStr) + } + + if enableOptions, ok := enable.([]interface{}); ok { + if len(enableOptions) != 0 { + s.bitmap = disableAll + } + + for _, option := range enableOptions { + if value, ok := option.(string); ok { + s.enable(value) + } + } + } + + if disableOptions, ok := disable.([]interface{}); ok { + for _, option := range disableOptions { + if value, ok := option.(string); ok { + s.disable(value) } } } - if tags != "" { - for _, tag := range strings.Split(tags, ",") { - s.applySetting(tag) + if enableTags != "" { + s.bitmap = disableAll + + for _, tag := range strings.Split(enableTags, ",") { + s.enable(tag) + } + } + + if disableTags != "" { + for _, tag := range strings.Split(disableTags, ",") { + s.disable(tag) } } } -func (s *OutputSettings) applySetting(setting string) { +func (s *LogSettings) enable(setting string) { switch setting { case "meta": - *s |= meta + s.bitmap |= meta case "success": - *s |= success + s.bitmap |= success case "failure": - *s |= failure + s.bitmap |= failure case "summary": - *s |= summary + s.bitmap |= summary | success | failure case "skips": - *s |= skips + s.bitmap |= skips case "execution": - *s |= execution + s.bitmap |= execution | executionOutput | executionInfo case "execution_out": - *s |= executionOutput + s.bitmap |= executionOutput | execution case "execution_info": - *s |= executionInfo + s.bitmap |= executionInfo | execution case "empty_summary": - *s |= emptySummary + s.bitmap |= emptySummary } } -func (s *OutputSettings) enableAll(val bool) { - if val { - *s = enableAll // Enable all params - } else { - *s |= failure // Disable all params +func (s *LogSettings) disable(setting string) { + switch setting { + case "meta": + s.bitmap &= ^meta + case "success": + s.bitmap &= ^success + case "failure": + s.bitmap &= ^failure + case "summary": + s.bitmap &= ^summary & ^success & ^failure + case "skips": + s.bitmap &= ^skips + case "execution": + s.bitmap &= ^execution & ^executionOutput & ^executionInfo + case "execution_out": + s.bitmap &= ^executionOutput + case "execution_info": + s.bitmap &= ^executionInfo + case "empty_summary": + s.bitmap &= ^emptySummary } } +func (s *LogSettings) enableAll() { + s.bitmap = ^disableAll +} + +func (s *LogSettings) disableAll() { + s.bitmap = failure +} + // Checks the state of params. -func (s OutputSettings) isEnable(option int16) bool { - return int16(s)&option != 0 +func (s LogSettings) isEnable(option int16) bool { + return s.bitmap&option != 0 } -func (s OutputSettings) LogSuccess() bool { - return s.isEnable(success) || s.isEnable(summary) +func (s LogSettings) LogSuccess() bool { + return s.isEnable(success) } -func (s OutputSettings) LogFailure() bool { - return s.isEnable(failure) || s.isEnable(summary) +func (s LogSettings) LogFailure() bool { + return s.isEnable(failure) } -func (s OutputSettings) LogSummary() bool { +func (s LogSettings) LogSummary() bool { return s.isEnable(summary) } -func (s OutputSettings) LogMeta() bool { +func (s LogSettings) LogMeta() bool { return s.isEnable(meta) } -func (s OutputSettings) LogExecution() bool { - return s.isEnable(execution) || s.isEnable(executionOutput) || s.isEnable(executionInfo) +func (s LogSettings) LogExecution() bool { + return s.isEnable(execution) } -func (s OutputSettings) LogExecutionOutput() bool { - return s.isEnable(execution) || s.isEnable(executionOutput) +func (s LogSettings) LogExecutionOutput() bool { + return s.isEnable(executionOutput) } -func (s OutputSettings) LogExecutionInfo() bool { - return s.isEnable(execution) || s.isEnable(executionInfo) +func (s LogSettings) LogExecutionInfo() bool { + return s.isEnable(executionInfo) } -func (s OutputSettings) LogSkips() bool { +func (s LogSettings) LogSkips() bool { return s.isEnable(skips) } -func (s OutputSettings) LogEmptySummary() bool { +func (s LogSettings) LogEmptySummary() bool { return s.isEnable(emptySummary) } diff --git a/internal/log/settings_test.go b/internal/log/settings_test.go index 76ce728c..994b091f 100644 --- a/internal/log/settings_test.go +++ b/internal/log/settings_test.go @@ -1,19 +1,19 @@ package log import ( - "fmt" + "strconv" "testing" ) func TestSetting(t *testing.T) { for i, tt := range [...]struct { - tags string - settings interface{} - results map[string]bool + enableTags, disableTags string + enableSettings, disableSettings interface{} + results map[string]bool }{ { - tags: "", - settings: []interface{}{}, + enableTags: "", + enableSettings: []interface{}{}, results: map[string]bool{ "meta": true, "summary": true, @@ -27,22 +27,22 @@ func TestSetting(t *testing.T) { }, }, { - tags: "", - settings: false, + enableTags: "", + enableSettings: false, results: map[string]bool{ "failure": true, }, }, { - tags: "", - settings: []interface{}{"success"}, + enableTags: "", + enableSettings: []interface{}{"success"}, results: map[string]bool{ "success": true, }, }, { - tags: "", - settings: []interface{}{"summary"}, + enableTags: "", + enableSettings: []interface{}{"summary"}, results: map[string]bool{ "summary": true, "success": true, @@ -50,8 +50,8 @@ func TestSetting(t *testing.T) { }, }, { - tags: "", - settings: []interface{}{"failure", "execution"}, + enableTags: "", + enableSettings: []interface{}{"failure", "execution"}, results: map[string]bool{ "failure": true, "execution": true, @@ -60,8 +60,8 @@ func TestSetting(t *testing.T) { }, }, { - tags: "", - settings: []interface{}{"failure", "execution_out"}, + enableTags: "", + enableSettings: []interface{}{"failure", "execution_out"}, results: map[string]bool{ "failure": true, "execution": true, @@ -69,8 +69,8 @@ func TestSetting(t *testing.T) { }, }, { - tags: "", - settings: []interface{}{"failure", "execution_info"}, + enableTags: "", + enableSettings: []interface{}{"failure", "execution_info"}, results: map[string]bool{ "failure": true, "execution": true, @@ -78,8 +78,8 @@ func TestSetting(t *testing.T) { }, }, { - tags: "", - settings: []interface{}{ + enableTags: "", + enableSettings: []interface{}{ "meta", "summary", "success", @@ -103,8 +103,8 @@ func TestSetting(t *testing.T) { }, }, { - tags: "", - settings: true, + enableTags: "", + enableSettings: true, results: map[string]bool{ "meta": true, "summary": true, @@ -118,8 +118,8 @@ func TestSetting(t *testing.T) { }, }, { - tags: "meta,summary,skips,empty_summary", - settings: nil, + enableTags: "meta,summary,skips,empty_summary", + enableSettings: nil, results: map[string]bool{ "meta": true, "summary": true, @@ -129,11 +129,95 @@ func TestSetting(t *testing.T) { "empty_summary": true, }, }, - } { //nolint:dupl // In next versions the `skip_settings_test` will be removed - t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { - var settings OutputSettings - - (&settings).ApplySettings(tt.tags, tt.settings) + { + disableTags: "", + disableSettings: []interface{}{}, + results: map[string]bool{ + "meta": true, + "summary": true, + "success": true, + "failure": true, + "skips": true, + "execution": true, + "execution_out": true, + "execution_info": true, + "empty_summary": true, + }, + }, + { + disableTags: "", + disableSettings: false, + results: map[string]bool{ + "meta": true, + "summary": true, + "success": true, + "failure": true, + "skips": true, + "execution": true, + "execution_out": true, + "execution_info": true, + "empty_summary": true, + }, + }, + { + disableTags: "", + disableSettings: []interface{}{"failure", "execution"}, + results: map[string]bool{ + "meta": true, + "summary": true, + "success": true, + "failure": false, + "skips": true, + "execution": false, + "execution_out": false, + "execution_info": false, + "empty_summary": true, + }, + }, + { + disableTags: "", + disableSettings: []interface{}{ + "meta", + "summary", + "skips", + "execution", + "execution_out", + "execution_info", + "empty_summary", + }, + results: map[string]bool{}, + }, + { + disableTags: "", + disableSettings: true, + results: map[string]bool{ + "failure": true, + }, + }, + { + disableTags: "meta,summary,success,skips,empty_summary", + disableSettings: nil, + results: map[string]bool{ + "execution": true, + "execution_out": true, + "execution_info": true, + }, + }, + { + disableTags: "meta,success,skips,empty_summary", + disableSettings: nil, + results: map[string]bool{ + "summary": true, + "failure": true, + "execution": true, + "execution_out": true, + "execution_info": true, + }, + }, + } { + t.Run(strconv.Itoa(i), func(t *testing.T) { + settings := NewSettings() + settings.Apply(tt.enableTags, tt.disableTags, tt.enableSettings, tt.disableSettings) if settings.LogMeta() != tt.results["meta"] { t.Errorf("expected LogMeta to be %v", tt.results["meta"]) diff --git a/internal/log/skip_settings.go b/internal/log/skip_settings.go deleted file mode 100644 index 04841a10..00000000 --- a/internal/log/skip_settings.go +++ /dev/null @@ -1,115 +0,0 @@ -package log - -import ( - "strings" -) - -const ( - skipMeta = 1 << iota - skipSuccess - skipFailure - skipSummary - skipSkips - skipExecution - skipExecutionOutput - skipExecutionInfo - skipEmptySummary - skipAll = (1 << iota) - 1 -) - -// Deprecated: Use Settings instead. -type SkipSettings int16 - -// Deprecated: Use NewSettings instead. -func NewSkipSettings() Settings { - var s SkipSettings - return &s -} - -func (s *SkipSettings) ApplySettings(tags string, skipOutput interface{}) { - switch typedSkipOutput := skipOutput.(type) { - case bool: - s.skipAll(typedSkipOutput) - case []interface{}: - for _, skipOption := range typedSkipOutput { - s.applySetting(skipOption.(string)) - } - } - - if tags != "" { - for _, skipOption := range strings.Split(tags, ",") { - s.applySetting(skipOption) - } - } -} - -func (s *SkipSettings) applySetting(setting string) { - switch setting { - case "meta": - *s |= skipMeta - case "success": - *s |= skipSuccess - case "failure": - *s |= skipFailure - case "summary": - *s |= skipSummary - case "skips": - *s |= skipSkips - case "execution": - *s |= skipExecution - case "execution_out": - *s |= skipExecutionOutput - case "execution_info": - *s |= skipExecutionInfo - case "empty_summary": - *s |= skipEmptySummary - } -} - -func (s *SkipSettings) skipAll(val bool) { - if val { - *s = skipAll &^ skipFailure &^ skipSummary - } else { - *s = 0 - } -} - -func (s SkipSettings) LogSuccess() bool { - return !s.doSkip(skipSuccess) && !s.doSkip(skipSummary) -} - -func (s SkipSettings) LogFailure() bool { - return !s.doSkip(skipFailure) && !s.doSkip(skipSummary) -} - -func (s SkipSettings) LogSummary() bool { - return !s.doSkip(skipSummary) -} - -func (s SkipSettings) LogMeta() bool { - return !s.doSkip(skipMeta) -} - -func (s SkipSettings) LogExecution() bool { - return !s.doSkip(skipExecution) -} - -func (s SkipSettings) LogExecutionOutput() bool { - return !s.doSkip(skipExecutionOutput) -} - -func (s SkipSettings) LogExecutionInfo() bool { - return !s.doSkip(skipExecutionInfo) -} - -func (s SkipSettings) LogSkips() bool { - return !s.doSkip(skipSkips) -} - -func (s SkipSettings) LogEmptySummary() bool { - return !s.doSkip(skipEmptySummary) -} - -func (s SkipSettings) doSkip(option int16) bool { - return int16(s)&option != 0 -} diff --git a/internal/log/skip_settings_test.go b/internal/log/skip_settings_test.go deleted file mode 100644 index 30f1b21c..00000000 --- a/internal/log/skip_settings_test.go +++ /dev/null @@ -1,143 +0,0 @@ -package log - -import ( - "fmt" - "testing" -) - -func TestSkipSetting(t *testing.T) { - for i, tt := range [...]struct { - tags string - settings interface{} - results map[string]bool - }{ - { - tags: "", - settings: []interface{}{}, - results: map[string]bool{ - "meta": true, - "summary": true, - "success": true, - "failure": true, - "skips": true, - "execution": true, - "execution_out": true, - "execution_info": true, - "empty_summary": true, - }, - }, - { - tags: "", - settings: false, - results: map[string]bool{ - "meta": true, - "summary": true, - "success": true, - "failure": true, - "skips": true, - "execution": true, - "execution_out": true, - "execution_info": true, - "empty_summary": true, - }, - }, - { - tags: "", - settings: []interface{}{"failure", "execution"}, - results: map[string]bool{ - "meta": true, - "summary": true, - "success": true, - "failure": false, - "skips": true, - "execution": false, - "execution_out": true, - "execution_info": true, - "empty_summary": true, - }, - }, - { - tags: "", - settings: []interface{}{ - "meta", - "summary", - "skips", - "execution", - "execution_out", - "execution_info", - "empty_summary", - }, - results: map[string]bool{}, - }, - { - tags: "", - settings: true, - results: map[string]bool{ - "summary": true, - "failure": true, - }, - }, - { - tags: "meta,summary,success,skips,empty_summary", - settings: nil, - results: map[string]bool{ - "execution": true, - "execution_out": true, - "execution_info": true, - }, - }, - { - tags: "meta,success,skips,empty_summary", - settings: nil, - results: map[string]bool{ - "summary": true, - "failure": true, - "execution": true, - "execution_out": true, - "execution_info": true, - }, - }, - } { //nolint:dupl // In next versions the `skip_settings_test` will be removed - t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { - var settings SkipSettings - - (&settings).ApplySettings(tt.tags, tt.settings) - - if settings.LogMeta() != tt.results["meta"] { - t.Errorf("expected LogMeta to be %v", tt.results["meta"]) - } - - if settings.LogSuccess() != tt.results["success"] { - t.Errorf("expected LogSuccess to be %v", tt.results["success"]) - } - - if settings.LogFailure() != tt.results["failure"] { - t.Errorf("expected LogFailure to be %v", tt.results["failure"]) - } - - if settings.LogSummary() != tt.results["summary"] { - t.Errorf("expected LogSummary to be %v", tt.results["summary"]) - } - - if settings.LogExecution() != tt.results["execution"] { - t.Errorf("expected LogExecution to be %v", tt.results["execution"]) - } - - if settings.LogExecutionOutput() != tt.results["execution_out"] { - t.Errorf("expected LogExecutionOutput to be %v", tt.results["execution_out"]) - } - - if settings.LogExecutionInfo() != tt.results["execution_info"] { - t.Errorf("expected LogExecutionInfo to be %v", tt.results["execution_info"]) - } - - if settings.LogEmptySummary() != tt.results["empty_summary"] { - t.Errorf("expected LogEmptySummary to be %v", tt.results["empty_summary"]) - } - - if settings.LogSkips() != tt.results["skips"] { - t.Errorf("expected LogSkips to be %v", tt.results["skip"]) - } - }) - } -}