Refactor and add tests for checkstyle comments on changed lines This is in prevision to reporting checkstyle warnings and errors around changed lines. Change-Id: I0c15a4e6e15886967162d19e9619a72c22304078
diff --git a/lint/lint.go b/lint/lint.go index 5ad9a7a..218c378 100644 --- a/lint/lint.go +++ b/lint/lint.go
@@ -139,15 +139,19 @@ if err != nil { return err } - for _, err := range file.Errors { - if blameable[err.Line] { - l.comment(file.Name, err.Line, err.Severity, err.Message) - } - } + l.addCheckStyleComments(file, blameable) } return nil } +func (l *linter) addCheckStyleComments(file checkstyle.File, blameable map[int]bool) { + for _, err := range file.Errors { + if blameable[err.Line] { + l.comment(file.Name, err.Line, err.Severity, err.Message) + } + } +} + var fixesIssueRegexp = regexp.MustCompile("(?i)^fixes issue [0-9]{3,}[.]?$") var issueRegexp = regexp.MustCompile("(?i)issue [0-9]{3,}") var footerRegexp = regexp.MustCompile("^[A-Za-z0-9-]+:")
diff --git a/lint/lint_test.go b/lint/lint_test.go index 76f0dce..ce25a69 100644 --- a/lint/lint_test.go +++ b/lint/lint_test.go
@@ -21,6 +21,7 @@ "strings" "testing" + "gwt.googlesource.com/buildglue.git/checkstyle" "gwt.googlesource.com/buildglue.git/gerrit" ) @@ -30,6 +31,53 @@ } } +func TestAddCheckStyleComments(t *testing.T) { + type testcase struct { + errors []checkstyle.Error + blameable []int + comments []gerrit.Comment + } + tests := []testcase{ + { + errors: []checkstyle.Error{ + checkstyle.Error{ + Line: 2, + Severity: "warning", + Message: "Warning on line 2", + }, + }, + blameable: []int{1, 2, 3}, + comments: []gerrit.Comment{ + gerrit.Comment{ + Line: 2, + Message: "[warning] Warning on line 2", + }, + }, + }, + { + errors: []checkstyle.Error{ + checkstyle.Error{ + Line: 10, + Severity: "error", + Message: "Error far from changed lines", + }, + }, + blameable: []int{1, 2, 3}, + }, + } + + for _, test := range tests { + blameableLines := make(map[int]bool) + for _, l := range test.blameable { + blameableLines[l] = true + } + + linter := newLinter("fakeref") + linter.addCheckStyleComments(checkstyle.File{Name: "fakefile", Errors: test.errors}, blameableLines) + checkEqual(t, test.comments, linter.comments["fakefile"]) + } +} + func TestCheckMessage(t *testing.T) { type testcase struct { message []string