Skip to content

Commit 8bd17a5

Browse files
committed
Put rule ID suffix on new line when rule message is multi-line
Readability of rule message output may be improved by inserting strategic line breaks. In this case, the current behavior of appending the rule ID suffix to the non-verbose rule violation output on the same line will not always be appropriate. So when the rule message contains explicit line breaks, the rule ID should be appended on a new line, with the previous behavior preserved in the case of single line rule messages (which will be wrapped according to the column size).
1 parent 53180ab commit 8bd17a5

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

Diff for: internal/result/result.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ func (results *Type) Record(lintedProject project.Type, ruleConfiguration ruleco
144144
}
145145
} else {
146146
if ruleResult == ruleresult.Fail {
147-
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+
}
148152
}
149153
}
150154

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)