Skip to content

Commit

Permalink
fix: fix printing when using output log setting (#672)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox authored Mar 12, 2024
1 parent ce6c29b commit eafe8a3
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 45 deletions.
46 changes: 23 additions & 23 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Run 'lefthook install' manually.`,
HookName: hookName,
GitArgs: gitArgs,
ResultChan: resultChan,
SkipSettings: logSettings,
LogSettings: logSettings,
DisableTTY: cfg.NoTTY || args.NoTTY,
Files: args.Files,
Force: args.Force,
Expand Down Expand Up @@ -187,9 +187,7 @@ Run 'lefthook install' manually.`,
return errors.New("Interrupted")
}

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

for _, result := range results {
if result.Status == run.StatusErr {
Expand All @@ -205,29 +203,31 @@ func printSummary(
results []run.Result,
logSettings log.Settings,
) {
summaryPrint := log.Separate
if logSettings.LogSummary() {
summaryPrint := log.Separate

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

if len(results) == 0 {
if logSettings.LogEmptySummary() {
summaryPrint(
fmt.Sprintf(
"%s %s %s",
log.Cyan("summary:"),
log.Gray("(skip)"),
log.Yellow("empty"),
),
)
if len(results) == 0 {
if logSettings.LogEmptySummary() {
summaryPrint(
fmt.Sprintf(
"%s %s %s",
log.Cyan("summary:"),
log.Gray("(skip)"),
log.Yellow("empty"),
),
)
}
return
}
return
}

summaryPrint(
log.Cyan("summary: ") + log.Gray(fmt.Sprintf("(done in %.2f seconds)", duration.Seconds())),
)
summaryPrint(
log.Cyan("summary: ") + log.Gray(fmt.Sprintf("(done in %.2f seconds)", duration.Seconds())),
)
}

if logSettings.LogSuccess() {
for _, result := range results {
Expand Down
14 changes: 7 additions & 7 deletions internal/lefthook/run/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Options struct {
HookName string
GitArgs []string
ResultChan chan Result
SkipSettings log.Settings
LogSettings log.Settings
DisableTTY bool
Force bool
Files []string
Expand Down Expand Up @@ -426,11 +426,11 @@ 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.LogExecution() {
if (follow || opts.Interactive) && r.LogSettings.LogExecution() {
r.logExecute(opts.Name, nil, nil)

var out io.Writer
if r.SkipSettings.LogExecutionOutput() {
if r.LogSettings.LogExecutionOutput() {
out = os.Stdout
} else {
out = io.Discard
Expand Down Expand Up @@ -478,7 +478,7 @@ func intersect(a, b []string) bool {
}

func (r *Runner) logSkip(name, reason string) {
if !r.SkipSettings.LogSkips() {
if !r.LogSettings.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.LogExecution() {
if err == nil && !r.LogSettings.LogExecution() {
return
}

var execLog string
var color lipgloss.TerminalColor
switch {
case !r.SkipSettings.LogExecutionInfo():
case !r.LogSettings.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.LogExecutionOutput() {
if err == nil && !r.LogSettings.LogExecutionOutput() {
return
}

Expand Down
14 changes: 7 additions & 7 deletions internal/lefthook/run/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,13 +750,13 @@ func TestRunAll(t *testing.T) {
executor := TestExecutor{}
runner := &Runner{
Options: Options{
Repo: repo,
Hook: tt.hook,
HookName: tt.hookName,
SkipSettings: log.NewSettings(),
GitArgs: tt.args,
ResultChan: resultChan,
Force: tt.force,
Repo: repo,
Hook: tt.hook,
HookName: tt.hookName,
LogSettings: log.NewSettings(),
GitArgs: tt.args,
ResultChan: resultChan,
Force: tt.force,
},
executor: executor,
}
Expand Down
10 changes: 5 additions & 5 deletions internal/log/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ func (s OutputSettings) isEnable(option int16) bool {
}

func (s OutputSettings) LogSuccess() bool {
return s.isEnable(success)
return s.isEnable(success) || s.isEnable(summary)
}

func (s OutputSettings) LogFailure() bool {
return s.isEnable(failure)
return s.isEnable(failure) || s.isEnable(summary)
}

func (s OutputSettings) LogSummary() bool {
Expand All @@ -120,15 +120,15 @@ func (s OutputSettings) LogMeta() bool {
}

func (s OutputSettings) LogExecution() bool {
return s.isEnable(execution)
return s.isEnable(execution) || s.isEnable(executionOutput) || s.isEnable(executionInfo)
}

func (s OutputSettings) LogExecutionOutput() bool {
return s.isEnable(executionOutput)
return s.isEnable(execution) || s.isEnable(executionOutput)
}

func (s OutputSettings) LogExecutionInfo() bool {
return s.isEnable(executionInfo)
return s.isEnable(execution) || s.isEnable(executionInfo)
}

func (s OutputSettings) LogSkips() bool {
Expand Down
43 changes: 40 additions & 3 deletions internal/log/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,48 @@ func TestSetting(t *testing.T) {
"failure": true,
},
},
{
tags: "",
settings: []interface{}{"success"},
results: map[string]bool{
"success": true,
},
},
{
tags: "",
settings: []interface{}{"summary"},
results: map[string]bool{
"summary": true,
"success": true,
"failure": true,
},
},
{
tags: "",
settings: []interface{}{"failure", "execution"},
results: map[string]bool{
"failure": true,
"execution": true,
"failure": true,
"execution": true,
"execution_info": true,
"execution_out": true,
},
},
{
tags: "",
settings: []interface{}{"failure", "execution_out"},
results: map[string]bool{
"failure": true,
"execution": true,
"execution_out": true,
},
},
{
tags: "",
settings: []interface{}{"failure", "execution_info"},
results: map[string]bool{
"failure": true,
"execution": true,
"execution_info": true,
},
},
{
Expand Down Expand Up @@ -82,12 +118,13 @@ func TestSetting(t *testing.T) {
},
},
{
tags: "meta,summary,success,skips,empty_summary",
tags: "meta,summary,skips,empty_summary",
settings: nil,
results: map[string]bool{
"meta": true,
"summary": true,
"success": true,
"failure": true,
"skips": true,
"empty_summary": true,
},
Expand Down

0 comments on commit eafe8a3

Please sign in to comment.