github.com/alexey-mercari/reviewdog@v0.10.1-0.20200514053941-928943b10766/filter_test.go (about) 1 package reviewdog 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 9 "github.com/reviewdog/reviewdog/diff" 10 "github.com/reviewdog/reviewdog/difffilter" 11 ) 12 13 const diffContent = `--- sample.old.txt 2016-10-13 05:09:35.820791185 +0900 14 +++ sample.new.txt 2016-10-13 05:15:26.839245048 +0900 15 @@ -1,3 +1,4 @@ 16 unchanged, contextual line 17 -deleted line 18 +added line 19 +added line 20 unchanged, contextual line 21 --- nonewline.old.txt 2016-10-13 15:34:14.931778318 +0900 22 +++ nonewline.new.txt 2016-10-13 15:34:14.868444672 +0900 23 @@ -1,4 +1,4 @@ 24 " vim: nofixeol noendofline 25 No newline at end of both the old and new file 26 -a 27 -a 28 \ No newline at end of file 29 +b 30 +b 31 \ No newline at end of file 32 ` 33 34 const diffContentAddedStrip = `diff --git a/test_added.go b/test_added.go 35 new file mode 100644 36 index 0000000..264c67e 37 --- /dev/null 38 +++ b/test_added.go 39 @@ -0,0 +1,3 @@ 40 +package reviewdog 41 + 42 +var TestAdded = 14 43 ` 44 45 func TestFilterCheckByAddedLines(t *testing.T) { 46 results := []*CheckResult{ 47 { 48 Path: "sample.new.txt", 49 Lnum: 1, 50 }, 51 { 52 Path: "sample.new.txt", 53 Lnum: 2, 54 }, 55 { 56 Path: "nonewline.new.txt", 57 Lnum: 1, 58 }, 59 { 60 Path: "nonewline.new.txt", 61 Lnum: 3, 62 }, 63 } 64 want := []*FilteredCheck{ 65 { 66 CheckResult: &CheckResult{ 67 Path: "sample.new.txt", 68 Lnum: 1, 69 }, 70 ShouldReport: false, 71 InDiffFile: true, 72 OldPath: "sample.old.txt", 73 OldLine: 1, 74 }, 75 { 76 CheckResult: &CheckResult{ 77 Path: "sample.new.txt", 78 Lnum: 2, 79 }, 80 ShouldReport: true, 81 InDiffFile: true, 82 LnumDiff: 3, 83 OldPath: "sample.old.txt", 84 OldLine: 0, 85 }, 86 { 87 CheckResult: &CheckResult{ 88 Path: "nonewline.new.txt", 89 Lnum: 1, 90 }, 91 ShouldReport: false, 92 InDiffFile: true, 93 OldPath: "nonewline.old.txt", 94 OldLine: 1, 95 }, 96 { 97 CheckResult: &CheckResult{ 98 Path: "nonewline.new.txt", 99 Lnum: 3, 100 }, 101 ShouldReport: true, 102 InDiffFile: true, 103 LnumDiff: 5, 104 OldPath: "nonewline.old.txt", 105 OldLine: 0, 106 }, 107 } 108 filediffs, _ := diff.ParseMultiFile(strings.NewReader(diffContent)) 109 got := FilterCheck(results, filediffs, 0, "", difffilter.ModeAdded) 110 if diff := cmp.Diff(got, want); diff != "" { 111 t.Error(diff) 112 } 113 } 114 115 // All lines that are in diff are taken into account 116 func TestFilterCheckByDiffContext(t *testing.T) { 117 results := []*CheckResult{ 118 { 119 Path: "sample.new.txt", 120 Lnum: 1, 121 }, 122 { 123 Path: "sample.new.txt", 124 Lnum: 2, 125 }, 126 { 127 Path: "sample.new.txt", 128 Lnum: 3, 129 }, 130 } 131 want := []*FilteredCheck{ 132 { 133 CheckResult: &CheckResult{ 134 Path: "sample.new.txt", 135 Lnum: 1, 136 }, 137 ShouldReport: true, 138 InDiffFile: true, 139 LnumDiff: 1, 140 OldPath: "sample.old.txt", 141 OldLine: 1, 142 }, 143 { 144 CheckResult: &CheckResult{ 145 Path: "sample.new.txt", 146 Lnum: 2, 147 }, 148 ShouldReport: true, 149 InDiffFile: true, 150 LnumDiff: 3, 151 OldPath: "sample.old.txt", 152 OldLine: 0, 153 }, 154 { 155 CheckResult: &CheckResult{ 156 Path: "sample.new.txt", 157 Lnum: 3, 158 }, 159 ShouldReport: true, 160 InDiffFile: true, 161 LnumDiff: 4, 162 OldPath: "sample.old.txt", 163 OldLine: 0, 164 }, 165 } 166 filediffs, _ := diff.ParseMultiFile(strings.NewReader(diffContent)) 167 got := FilterCheck(results, filediffs, 0, "", difffilter.ModeDiffContext) 168 if diff := cmp.Diff(got, want); diff != "" { 169 t.Error(diff) 170 } 171 } 172 173 func findFileDiff(filediffs []*diff.FileDiff, path string, strip int) *diff.FileDiff { 174 for _, file := range filediffs { 175 if difffilter.NormalizeDiffPath(file.PathNew, strip) == path { 176 return file 177 } 178 } 179 return nil 180 } 181 182 func TestGetOldPosition(t *testing.T) { 183 const strip = 0 184 filediffs, _ := diff.ParseMultiFile(strings.NewReader(diffContent)) 185 tests := []struct { 186 newPath string 187 newLine int 188 wantOldPath string 189 wantOldLine int 190 }{ 191 { 192 newPath: "sample.new.txt", 193 newLine: 1, 194 wantOldPath: "sample.old.txt", 195 wantOldLine: 1, 196 }, 197 { 198 newPath: "sample.new.txt", 199 newLine: 2, 200 wantOldPath: "sample.old.txt", 201 wantOldLine: 0, 202 }, 203 { 204 newPath: "sample.new.txt", 205 newLine: 3, 206 wantOldPath: "sample.old.txt", 207 wantOldLine: 0, 208 }, 209 { 210 newPath: "sample.new.txt", 211 newLine: 14, 212 wantOldPath: "sample.old.txt", 213 wantOldLine: 13, 214 }, 215 { 216 newPath: "not_found", 217 newLine: 14, 218 wantOldPath: "", 219 wantOldLine: 0, 220 }, 221 } 222 for _, tt := range tests { 223 fdiff := findFileDiff(filediffs, tt.newPath, strip) 224 gotPath, gotLine := getOldPosition(fdiff, strip, tt.newPath, tt.newLine) 225 if !(gotPath == tt.wantOldPath && gotLine == tt.wantOldLine) { 226 t.Errorf("getOldPosition(..., %s, %d) = (%s, %d), want (%s, %d)", 227 tt.newPath, tt.newLine, gotPath, gotLine, tt.wantOldPath, tt.wantOldLine) 228 } 229 } 230 } 231 232 func TestGetOldPosition_added(t *testing.T) { 233 const strip = 1 234 filediffs, _ := diff.ParseMultiFile(strings.NewReader(diffContentAddedStrip)) 235 path := "test_added.go" 236 fdiff := findFileDiff(filediffs, path, strip) 237 gotPath, _ := getOldPosition(fdiff, strip, path, 1) 238 if gotPath != "" { 239 t.Errorf("got %q as old path for addedd diff file, want empty", gotPath) 240 } 241 }