Skip to content

Commit

Permalink
rename methods from Settings, change tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prog-supdex committed Mar 8, 2024
1 parent 4e6d053 commit 4a0bc8f
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 175 deletions.
12 changes: 6 additions & 6 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error {
logSettings.ApplySettings(outputSkipTags, cfg.SkipOutput)
}

if !logSettings.SkipMeta() {
if logSettings.LogMeta() {
log.Box(
log.Cyan("🥊 lefthook ")+log.Gray(fmt.Sprintf("v%s", version.Version(false))),
log.Gray("hook: ")+log.Bold(hookName),
Expand Down Expand Up @@ -187,7 +187,7 @@ Run 'lefthook install' manually.`,
return errors.New("Interrupted")
}

if !logSettings.SkipSummary() {
if logSettings.LogSummary() {
printSummary(time.Since(startTime), results, logSettings)
}

Expand All @@ -207,12 +207,12 @@ func printSummary(
) {
summaryPrint := log.Separate

if logSettings.SkipExecution() || (logSettings.SkipExecutionInfo() && logSettings.SkipExecutionOutput()) {
if !logSettings.LogExecution() || !(logSettings.LogExecutionInfo() && logSettings.LogExecutionOutput()) {
summaryPrint = func(s string) { log.Info(s) }
}

if len(results) == 0 {
if !logSettings.SkipEmptySummary() {
if logSettings.LogEmptySummary() {
summaryPrint(
fmt.Sprintf(
"%s %s %s",
Expand All @@ -229,7 +229,7 @@ func printSummary(
log.Cyan("summary: ") + log.Gray(fmt.Sprintf("(done in %.2f seconds)", duration.Seconds())),
)

if !logSettings.SkipSuccess() {
if logSettings.LogSuccess() {
for _, result := range results {
if result.Status != run.StatusOk {
continue
Expand All @@ -239,7 +239,7 @@ func printSummary(
}
}

if !logSettings.SkipFailure() {
if logSettings.LogFailure() {
for _, result := range results {
if result.Status != run.StatusErr {
continue
Expand Down
16 changes: 8 additions & 8 deletions internal/lefthook/run/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,14 @@ func (r *Runner) run(ctx context.Context, opts exec.Options, follow bool) bool {
log.SetName(opts.Name)
defer log.UnsetName(opts.Name)

if (follow || opts.Interactive) && !r.SkipSettings.SkipExecution() {
if (follow || opts.Interactive) && r.SkipSettings.LogExecution() {
r.logExecute(opts.Name, nil, nil)

var out io.Writer
if r.SkipSettings.SkipExecutionOutput() {
out = io.Discard
} else {
if r.SkipSettings.LogExecutionOutput() {
out = os.Stdout
} else {
out = io.Discard
}

err := r.executor.Execute(ctx, opts, out)
Expand Down Expand Up @@ -478,7 +478,7 @@ func intersect(a, b []string) bool {
}

func (r *Runner) logSkip(name, reason string) {
if r.SkipSettings.SkipSkips() {
if !r.SkipSettings.LogSkips() {
return
}

Expand All @@ -493,14 +493,14 @@ func (r *Runner) logSkip(name, reason string) {
}

func (r *Runner) logExecute(name string, err error, out io.Reader) {
if err == nil && r.SkipSettings.SkipExecution() {
if err == nil && !r.SkipSettings.LogExecution() {
return
}

var execLog string
var color lipgloss.TerminalColor
switch {
case r.SkipSettings.SkipExecutionInfo():
case !r.SkipSettings.LogExecutionInfo():
execLog = ""
case err != nil:
execLog = log.Red(fmt.Sprintf("%s ❯ ", name))
Expand All @@ -518,7 +518,7 @@ func (r *Runner) logExecute(name string, err error, out io.Reader) {
log.Info()
}

if err == nil && r.SkipSettings.SkipExecutionOutput() {
if err == nil && !r.SkipSettings.LogExecutionOutput() {
return
}

Expand Down
61 changes: 32 additions & 29 deletions internal/log/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ const (

type Settings interface {
ApplySettings(tags string, skipOutput interface{})
SkipSuccess() bool
SkipFailure() bool
SkipSummary() bool
SkipMeta() bool
SkipExecution() bool
SkipExecutionOutput() bool
SkipExecutionInfo() bool
SkipSkips() bool
SkipEmptySummary() bool
LogSuccess() bool
LogFailure() bool
LogSummary() bool
LogMeta() bool
LogExecution() bool
LogExecutionOutput() bool
LogExecutionInfo() bool
LogSkips() bool
LogEmptySummary() bool
}

type OutputSettings int16
Expand All @@ -49,6 +49,10 @@ func (s *OutputSettings) ApplySettings(tags string, output interface{}) {
}

if options, ok := output.([]interface{}); ok {
if len(options) == 0 {
s.enableAll(true)
return
}
for _, option := range options {
if optStr, ok := option.(string); ok {
s.applySetting(optStr)
Expand Down Expand Up @@ -90,7 +94,7 @@ func (s *OutputSettings) enableAll(val bool) {
if val {
*s = enableAll // Enable all params
} else {
*s |= skipFailure // Disable all params
*s |= failure // Disable all params
}
}

Expand All @@ -99,39 +103,38 @@ func (s OutputSettings) isEnable(option int16) bool {
return int16(s)&option != 0
}

// Using `SkipX` to maintain backward compatibility.
func (s OutputSettings) SkipSuccess() bool {
return !s.isEnable(success)
func (s OutputSettings) LogSuccess() bool {
return s.isEnable(success)
}

func (s OutputSettings) SkipFailure() bool {
return !s.isEnable(failure)
func (s OutputSettings) LogFailure() bool {
return s.isEnable(failure)
}

func (s OutputSettings) SkipSummary() bool {
return !s.isEnable(summary)
func (s OutputSettings) LogSummary() bool {
return s.isEnable(summary)
}

func (s OutputSettings) SkipMeta() bool {
return !s.isEnable(meta)
func (s OutputSettings) LogMeta() bool {
return s.isEnable(meta)
}

func (s OutputSettings) SkipExecution() bool {
return !s.isEnable(execution)
func (s OutputSettings) LogExecution() bool {
return s.isEnable(execution)
}

func (s OutputSettings) SkipExecutionOutput() bool {
return !s.isEnable(executionOutput)
func (s OutputSettings) LogExecutionOutput() bool {
return s.isEnable(executionOutput)
}

func (s OutputSettings) SkipExecutionInfo() bool {
return !s.isEnable(executionInfo)
func (s OutputSettings) LogExecutionInfo() bool {
return s.isEnable(executionInfo)
}

func (s OutputSettings) SkipSkips() bool {
return !s.isEnable(skips)
func (s OutputSettings) LogSkips() bool {
return s.isEnable(skips)
}

func (s OutputSettings) SkipEmptySummary() bool {
return !s.isEnable(emptySummary)
func (s OutputSettings) LogEmptySummary() bool {
return s.isEnable(emptySummary)
}
107 changes: 44 additions & 63 deletions internal/log/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,15 @@ func TestSetting(t *testing.T) {
tags: "",
settings: false,
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,
"failure": 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,
"failure": true,
"execution": true,
},
},
{
Expand All @@ -70,45 +55,41 @@ func TestSetting(t *testing.T) {
"empty_summary",
},
results: map[string]bool{
"meta": false,
"summary": false,
"success": false,
"failure": false,
"skips": false,
"execution": false,
"execution_out": false,
"execution_info": false,
"empty_summary": false,
"meta": true,
"summary": true,
"success": true,
"failure": true,
"skips": true,
"execution": true,
"execution_out": true,
"execution_info": true,
"empty_summary": true,
},
},
{
tags: "",
settings: true,
results: map[string]bool{
"meta": false,
"summary": false,
"success": false,
"failure": false,
"skips": false,
"execution": false,
"execution_out": false,
"execution_info": false,
"empty_summary": false,
"meta": true,
"summary": true,
"success": true,
"failure": true,
"skips": true,
"execution": true,
"execution_out": true,
"execution_info": true,
"empty_summary": true,
},
},
{
tags: "meta,summary,success,skips,empty_summary",
settings: nil,
results: map[string]bool{
"meta": false,
"summary": false,
"success": false,
"failure": true,
"skips": false,
"execution": true,
"execution_out": true,
"execution_info": true,
"empty_summary": false,
"meta": true,
"summary": true,
"success": true,
"skips": true,
"empty_summary": true,
},
},
} { //nolint:dupl // In next versions the `skip_settings_test` will be removed
Expand All @@ -117,40 +98,40 @@ func TestSetting(t *testing.T) {

(&settings).ApplySettings(tt.tags, tt.settings)

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

if settings.SkipSuccess() != tt.results["success"] {
t.Errorf("expected SkipSuccess to be %v", tt.results["success"])
if settings.LogSuccess() != tt.results["success"] {
t.Errorf("expected LogSuccess to be %v", tt.results["success"])
}

if settings.SkipFailure() != tt.results["failure"] {
t.Errorf("expected SkipFailure to be %v", tt.results["failure"])
if settings.LogFailure() != tt.results["failure"] {
t.Errorf("expected LogFailure to be %v", tt.results["failure"])
}

if settings.SkipSummary() != tt.results["summary"] {
t.Errorf("expected SkipSummary to be %v", tt.results["summary"])
if settings.LogSummary() != tt.results["summary"] {
t.Errorf("expected LogSummary to be %v", tt.results["summary"])
}

if settings.SkipExecution() != tt.results["execution"] {
t.Errorf("expected SkipExecution to be %v", tt.results["execution"])
if settings.LogExecution() != tt.results["execution"] {
t.Errorf("expected LogExecution to be %v", tt.results["execution"])
}

if settings.SkipExecutionOutput() != tt.results["execution_out"] {
t.Errorf("expected SkipExecutionOutput to be %v", tt.results["execution_out"])
if settings.LogExecutionOutput() != tt.results["execution_out"] {
t.Errorf("expected LogExecutionOutput to be %v", tt.results["execution_out"])
}

if settings.SkipExecutionInfo() != tt.results["execution_info"] {
t.Errorf("expected SkipExecutionInfo to be %v", tt.results["execution_info"])
if settings.LogExecutionInfo() != tt.results["execution_info"] {
t.Errorf("expected LogExecutionInfo to be %v", tt.results["execution_info"])
}

if settings.SkipEmptySummary() != tt.results["empty_summary"] {
t.Errorf("expected SkipEmptySummary to be %v", tt.results["empty_summary"])
if settings.LogEmptySummary() != tt.results["empty_summary"] {
t.Errorf("expected LogEmptySummary to be %v", tt.results["empty_summary"])
}

if settings.SkipSkips() != tt.results["skips"] {
t.Errorf("expected SkipSkips to be %v", tt.results["skip"])
if settings.LogSkips() != tt.results["skips"] {
t.Errorf("expected LogSkips to be %v", tt.results["skip"])
}
})
}
Expand Down
Loading

0 comments on commit 4a0bc8f

Please sign in to comment.