Skip to content

Commit

Permalink
Add reference URL field to rule configuration
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
per1234 committed Aug 25, 2021
1 parent 8096c62 commit ac6f9a3
Show file tree
Hide file tree
Showing 3 changed files with 438 additions and 128 deletions.
3 changes: 3 additions & 0 deletions internal/result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ func (results *Type) Record(lintedProject project.Type, ruleConfiguration ruleco
ruleMessage := ""
if ruleResult == ruleresult.Fail {
ruleMessage = message(ruleConfiguration.MessageTemplate, ruleOutput)
if ruleConfiguration.Reference != "" {
ruleMessage = fmt.Sprintf("%s\nSee: %s", ruleMessage, ruleConfiguration.Reference)
}
} else {
// Rules may provide an explanation for their non-fail result.
// The message template should not be used in this case, since it is written for a failure result.
Expand Down
10 changes: 8 additions & 2 deletions internal/result/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,14 @@ func TestRecord(t *testing.T) {
ruleOutput := "foo"
flags.Set("verbose", "true")
require.Nil(t, configuration.Initialize(flags, projectPaths))
ruleConfiguration.Reference = ""
summaryText := results.Record(lintedProject, ruleConfiguration, ruleresult.Fail, ruleOutput)
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"
assert.Equal(t, outputAssertion, summaryText)
outputAssertion := "Rule LS001 result: fail\nERROR: Path does not contain a valid Arduino library.\n"
assert.Equal(t, outputAssertion, summaryText, "No reference URL")
ruleConfiguration.Reference = "https://arduino.github.io/arduino-cli/latest/library-specification"
summaryText = results.Record(lintedProject, ruleConfiguration, ruleresult.Fail, ruleOutput)
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"
assert.Equal(t, outputAssertion, summaryText, "Reference URL is appended if one is defined")
summaryText = results.Record(lintedProject, ruleConfiguration, ruleresult.NotRun, ruleOutput)
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")
summaryText = results.Record(lintedProject, ruleConfiguration, ruleresult.Pass, "")
Expand All @@ -87,6 +92,7 @@ func TestRecord(t *testing.T) {
require.Nil(t, configuration.Initialize(flags, projectPaths))
ruleConfigurationCopy := ruleConfiguration
ruleConfigurationCopy.MessageTemplate = "bar"
ruleConfigurationCopy.Reference = ""
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")
Expand Down

0 comments on commit ac6f9a3

Please sign in to comment.