Skip to content

Commit 6edad11

Browse files
authored
Merge pull request #211 from per1234/explicit-output-breaks
Use strategic newlines to improve rule output readability
2 parents 3fb6daf + c306439 commit 6edad11

File tree

7 files changed

+314
-291
lines changed

7 files changed

+314
-291
lines changed

Diff for: internal/result/result.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"io"
24+
"regexp"
2425
"strings"
2526
"text/template"
2627

@@ -96,6 +97,8 @@ func (results *Type) Initialize() {
9697
}
9798
}
9899

100+
var blankLineRegexp = regexp.MustCompile("\n[[:space:]]*\n")
101+
99102
// Record records the result of a rule and returns a text summary for it.
100103
func (results *Type) Record(lintedProject project.Type, ruleConfiguration ruleconfiguration.Type, ruleResult ruleresult.Type, ruleOutput string) string {
101104
ruleLevel, err := rulelevel.RuleLevel(ruleConfiguration, ruleResult, lintedProject)
@@ -124,10 +127,13 @@ func (results *Type) Record(lintedProject project.Type, ruleConfiguration ruleco
124127
table.SetColumnSeparator("")
125128
table.SetNoWhiteSpace(true)
126129
table.SetColWidth(width - len(prefix))
130+
table.SetReflowDuringAutoWrap(false) // Reflow removes explicit line breaks.
127131
table.Append([]string{prefix, message})
128132
table.Render()
133+
// Remove blank lines on explicit line breaks caused by tablewriter bug.
134+
cleanedOutput := blankLineRegexp.ReplaceAllLiteralString(formattedOutput.String(), "\n")
129135

130-
return formattedOutput.String()
136+
return cleanedOutput
131137
}
132138

133139
if configuration.Verbose() {
@@ -138,7 +144,11 @@ func (results *Type) Record(lintedProject project.Type, ruleConfiguration ruleco
138144
}
139145
} else {
140146
if ruleResult == ruleresult.Fail {
141-
summaryText = formatRuleText(ruleLevel, fmt.Sprintf("%s (Rule %s)", ruleMessage, ruleConfiguration.ID))
147+
if strings.Contains(ruleMessage, "\n") {
148+
summaryText = formatRuleText(ruleLevel, fmt.Sprintf("%s\n(Rule %s)", ruleMessage, ruleConfiguration.ID))
149+
} else {
150+
summaryText = formatRuleText(ruleLevel, fmt.Sprintf("%s (Rule %s)", ruleMessage, ruleConfiguration.ID))
151+
}
142152
}
143153
}
144154

Diff for: internal/result/result_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,15 @@ func TestRecord(t *testing.T) {
8585
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")
8686
flags.Set("verbose", "false")
8787
require.Nil(t, configuration.Initialize(flags, projectPaths))
88-
summaryText = results.Record(lintedProject, ruleConfiguration, ruleresult.Fail, ruleOutput)
89-
outputAssertion = "ERROR: Path does not contain a valid Arduino library. See: \n https://arduino.github.io/arduino-cli/latest/library-specification (Rule LS001) \n"
90-
assert.Equal(t, outputAssertion, summaryText)
88+
ruleConfigurationCopy := ruleConfiguration
89+
ruleConfigurationCopy.MessageTemplate = "bar"
90+
summaryText = results.Record(lintedProject, ruleConfigurationCopy, ruleresult.Fail, ruleOutput)
91+
outputAssertion = "ERROR: bar (Rule LS001)\n"
92+
assert.Equal(t, outputAssertion, summaryText, "Rule ID is appended to non-verbose fail message on same line when rule message is single line")
93+
ruleConfigurationCopy.MessageTemplate = "bar\nbaz"
94+
summaryText = results.Record(lintedProject, ruleConfigurationCopy, ruleresult.Fail, ruleOutput)
95+
outputAssertion = "ERROR: bar \n baz \n (Rule LS001)\n"
96+
assert.Equal(t, outputAssertion, summaryText, "Rule ID is appended to non-verbose fail message on same line when rule message is multiple lines")
9197
summaryText = results.Record(lintedProject, ruleConfiguration, ruleresult.NotRun, ruleOutput)
9298
assert.Equal(t, "", summaryText, "Non-fail result should not result in output in non-verbose mode")
9399
summaryText = results.Record(lintedProject, ruleConfiguration, ruleresult.Pass, "")

0 commit comments

Comments
 (0)