Skip to content

Commit c22e500

Browse files
authored
Merge pull request #6 from natefinch/natefinch/wholefiles
add flag to report changes anywhere in changed files
2 parents cd28932 + 7b16880 commit c22e500

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

revgrep.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ type Checker struct {
3131
// RevisionFrom check revision starting at, leave blank for auto detection
3232
// ignored if patch is set.
3333
RevisionFrom string
34+
// WholeFiles indicates that the user wishes to see all issues that comes up
35+
// anywhere in any file that has been changed in this revision or patch.
36+
WholeFiles bool
3437
// RevisionTo checks revision finishing at, leave blank for auto detection
3538
// ignored if patch is set.
3639
RevisionTo string
@@ -113,6 +116,10 @@ func (c Checker) IsNewIssue(i InputIssue) (hunkPos int, isNew bool) {
113116
return 0, false
114117
}
115118

119+
if c.WholeFiles {
120+
return i.Line(), true
121+
}
122+
116123
var (
117124
fpos pos
118125
changed bool

revgrep_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,46 @@ func TestCheckerRegexp(t *testing.T) {
7878
}
7979
}
8080

81+
// TestWholeFile tests Checker.WholeFiles will report any issues in files that have changes, even if
82+
// they are outside the diff.
83+
func TestWholeFiles(t *testing.T) {
84+
tests := []struct {
85+
name string
86+
line string
87+
matches bool
88+
}{
89+
{"inside diff", "file.go:1:issue", true},
90+
{"outside diff", "file.go:10:5:issue", true},
91+
{"different file", "file2.go:1:issue", false},
92+
}
93+
94+
diff := []byte(`--- a/file.go
95+
+++ b/file.go
96+
@@ -1,1 +1,1 @@
97+
-func Line() {}
98+
+func NewLine() {}`)
99+
100+
for _, test := range tests {
101+
t.Run(test.name, func(t *testing.T) {
102+
checker := Checker{
103+
Patch: bytes.NewReader(diff),
104+
WholeFiles: true,
105+
}
106+
107+
issues, err := checker.Check(bytes.NewReader([]byte(test.line)), ioutil.Discard)
108+
if err != nil {
109+
t.Fatalf("unexpected error: %v", err)
110+
}
111+
if test.matches && len(issues) != 1 {
112+
t.Fatalf("expected one issue to be returned, but got %#v", issues)
113+
}
114+
if !test.matches && len(issues) != 0 {
115+
t.Fatalf("expected no issues to be returned, but got %#v", issues)
116+
}
117+
})
118+
}
119+
}
120+
81121
// TestChangesReturn tests the writer in the argument to the Changes function
82122
// and generally tests the entire programs functionality.
83123
func TestChangesWriter(t *testing.T) {

0 commit comments

Comments
 (0)