Skip to content

Commit ac6f9a3

Browse files
committed
Add reference URL field to rule configuration
Many of the rule messages contain a URL that can be visited to find the detailed information about the problem and how to fix it. Moving this information to a dedicated field in the struct allows for improved formatting of the rule output as well as enhanced capabilities for the use of this data in other applications.
1 parent 8096c62 commit ac6f9a3

File tree

3 files changed

+438
-128
lines changed

3 files changed

+438
-128
lines changed

Diff for: internal/result/result.go

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ func (results *Type) Record(lintedProject project.Type, ruleConfiguration ruleco
109109
ruleMessage := ""
110110
if ruleResult == ruleresult.Fail {
111111
ruleMessage = message(ruleConfiguration.MessageTemplate, ruleOutput)
112+
if ruleConfiguration.Reference != "" {
113+
ruleMessage = fmt.Sprintf("%s\nSee: %s", ruleMessage, ruleConfiguration.Reference)
114+
}
112115
} else {
113116
// Rules may provide an explanation for their non-fail result.
114117
// The message template should not be used in this case, since it is written for a failure result.

Diff for: internal/result/result_test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,14 @@ func TestRecord(t *testing.T) {
7676
ruleOutput := "foo"
7777
flags.Set("verbose", "true")
7878
require.Nil(t, configuration.Initialize(flags, projectPaths))
79+
ruleConfiguration.Reference = ""
7980
summaryText := results.Record(lintedProject, ruleConfiguration, ruleresult.Fail, ruleOutput)
80-
outputAssertion := "Rule LS001 result: fail\nERROR: Path does not contain a valid Arduino library. See: \n https://arduino.github.io/arduino-cli/latest/library-specification \n"
81-
assert.Equal(t, outputAssertion, summaryText)
81+
outputAssertion := "Rule LS001 result: fail\nERROR: Path does not contain a valid Arduino library.\n"
82+
assert.Equal(t, outputAssertion, summaryText, "No reference URL")
83+
ruleConfiguration.Reference = "https://arduino.github.io/arduino-cli/latest/library-specification"
84+
summaryText = results.Record(lintedProject, ruleConfiguration, ruleresult.Fail, ruleOutput)
85+
outputAssertion = "Rule LS001 result: fail\nERROR: Path does not contain a valid Arduino library. \n See: https://arduino.github.io/arduino-cli/latest/library-specification\n"
86+
assert.Equal(t, outputAssertion, summaryText, "Reference URL is appended if one is defined")
8287
summaryText = results.Record(lintedProject, ruleConfiguration, ruleresult.NotRun, ruleOutput)
8388
assert.Equal(t, fmt.Sprintf("Rule %s result: %s\n%s: %s\n", ruleConfiguration.ID, ruleresult.NotRun, rulelevel.Notice, ruleOutput), summaryText, "Non-fail result should not use message")
8489
summaryText = results.Record(lintedProject, ruleConfiguration, ruleresult.Pass, "")
@@ -87,6 +92,7 @@ func TestRecord(t *testing.T) {
8792
require.Nil(t, configuration.Initialize(flags, projectPaths))
8893
ruleConfigurationCopy := ruleConfiguration
8994
ruleConfigurationCopy.MessageTemplate = "bar"
95+
ruleConfigurationCopy.Reference = ""
9096
summaryText = results.Record(lintedProject, ruleConfigurationCopy, ruleresult.Fail, ruleOutput)
9197
outputAssertion = "ERROR: bar (Rule LS001)\n"
9298
assert.Equal(t, outputAssertion, summaryText, "Rule ID is appended to non-verbose fail message on same line when rule message is single line")

0 commit comments

Comments
 (0)