Skip to content

Use strategic newlines to improve rule output readability #211

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

Merged
merged 4 commits into from
Jul 26, 2021
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
14 changes: 12 additions & 2 deletions internal/result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"io"
"regexp"
"strings"
"text/template"

Expand Down Expand Up @@ -96,6 +97,8 @@ func (results *Type) Initialize() {
}
}

var blankLineRegexp = regexp.MustCompile("\n[[:space:]]*\n")

// Record records the result of a rule and returns a text summary for it.
func (results *Type) Record(lintedProject project.Type, ruleConfiguration ruleconfiguration.Type, ruleResult ruleresult.Type, ruleOutput string) string {
ruleLevel, err := rulelevel.RuleLevel(ruleConfiguration, ruleResult, lintedProject)
Expand Down Expand Up @@ -124,10 +127,13 @@ func (results *Type) Record(lintedProject project.Type, ruleConfiguration ruleco
table.SetColumnSeparator("")
table.SetNoWhiteSpace(true)
table.SetColWidth(width - len(prefix))
table.SetReflowDuringAutoWrap(false) // Reflow removes explicit line breaks.
table.Append([]string{prefix, message})
table.Render()
// Remove blank lines on explicit line breaks caused by tablewriter bug.
cleanedOutput := blankLineRegexp.ReplaceAllLiteralString(formattedOutput.String(), "\n")

return formattedOutput.String()
return cleanedOutput
}

if configuration.Verbose() {
Expand All @@ -138,7 +144,11 @@ func (results *Type) Record(lintedProject project.Type, ruleConfiguration ruleco
}
} else {
if ruleResult == ruleresult.Fail {
summaryText = formatRuleText(ruleLevel, fmt.Sprintf("%s (Rule %s)", ruleMessage, ruleConfiguration.ID))
if strings.Contains(ruleMessage, "\n") {
summaryText = formatRuleText(ruleLevel, fmt.Sprintf("%s\n(Rule %s)", ruleMessage, ruleConfiguration.ID))
} else {
summaryText = formatRuleText(ruleLevel, fmt.Sprintf("%s (Rule %s)", ruleMessage, ruleConfiguration.ID))
}
}
}

Expand Down
12 changes: 9 additions & 3 deletions internal/result/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,15 @@ func TestRecord(t *testing.T) {
assert.Equal(t, fmt.Sprintf("Rule %s result: %s\n", ruleConfiguration.ID, ruleresult.Pass), summaryText, "Non-failure result with no rule function output should only use preface")
flags.Set("verbose", "false")
require.Nil(t, configuration.Initialize(flags, projectPaths))
summaryText = results.Record(lintedProject, ruleConfiguration, ruleresult.Fail, ruleOutput)
outputAssertion = "ERROR: Path does not contain a valid Arduino library. See: \n https://arduino.github.io/arduino-cli/latest/library-specification (Rule LS001) \n"
assert.Equal(t, outputAssertion, summaryText)
ruleConfigurationCopy := ruleConfiguration
ruleConfigurationCopy.MessageTemplate = "bar"
summaryText = results.Record(lintedProject, ruleConfigurationCopy, ruleresult.Fail, ruleOutput)
outputAssertion = "ERROR: bar (Rule LS001)\n"
assert.Equal(t, outputAssertion, summaryText, "Rule ID is appended to non-verbose fail message on same line when rule message is single line")
ruleConfigurationCopy.MessageTemplate = "bar\nbaz"
summaryText = results.Record(lintedProject, ruleConfigurationCopy, ruleresult.Fail, ruleOutput)
outputAssertion = "ERROR: bar \n baz \n (Rule LS001)\n"
assert.Equal(t, outputAssertion, summaryText, "Rule ID is appended to non-verbose fail message on same line when rule message is multiple lines")
summaryText = results.Record(lintedProject, ruleConfiguration, ruleresult.NotRun, ruleOutput)
assert.Equal(t, "", summaryText, "Non-fail result should not result in output in non-verbose mode")
summaryText = results.Record(lintedProject, ruleConfiguration, ruleresult.Pass, "")
Expand Down
Loading