github.com/alexey-mercari/reviewdog@v0.10.1-0.20200514053941-928943b10766/difffilter/filter_test.go (about) 1 package difffilter 2 3 import ( 4 "os" 5 "path/filepath" 6 "strings" 7 "testing" 8 9 "github.com/reviewdog/reviewdog/diff" 10 ) 11 12 func TestMode_Set(t *testing.T) { 13 tests := []struct { 14 value string 15 want Mode 16 wantErr bool 17 }{ 18 {value: "", want: ModeDefault}, 19 {value: "default", want: ModeDefault}, 20 {value: "added", want: ModeAdded}, 21 {value: "diff_context", want: ModeDiffContext}, 22 {value: "file", want: ModeFile}, 23 {value: "nofilter", want: ModeNoFilter}, 24 {value: "unknown", wantErr: true}, 25 } 26 for _, tt := range tests { 27 var mode Mode 28 err := (&mode).Set(tt.value) 29 if err != nil && !tt.wantErr { 30 t.Errorf("got error for %q: %v", tt.value, err) 31 } else if err == nil && tt.wantErr { 32 t.Errorf("want error, but got nil for %q", tt.value) 33 } 34 if mode != tt.want { 35 t.Errorf("[value=%s] got %q, want %q", tt.value, mode.String(), tt.want.String()) 36 } 37 } 38 } 39 40 const sampleDiffRoot = `--- a/sample.old.txt 2016-10-13 05:09:35.820791185 +0900 41 +++ b/sample.new.txt 2016-10-13 05:15:26.839245048 +0900 42 @@ -1,3 +1,4 @@ 43 unchanged, contextual line 44 -deleted line 45 +added line 46 +added line 47 unchanged, contextual line 48 --- a/subdir/nonewline.old.txt 2016-10-13 15:34:14.931778318 +0900 49 +++ b/subdir/nonewline.new.txt 2016-10-13 15:34:14.868444672 +0900 50 @@ -1,4 +1,4 @@ 51 " vim: nofixeol noendofline 52 No newline at end of both the old and new file 53 -a 54 -a 55 \ No newline at end of file 56 +b 57 +b 58 \ No newline at end of file 59 ` 60 61 const sampleDiffSubDir = `--- a/difffilter/sample.old.txt 2016-10-13 05:09:35.820791185 +0900 62 +++ b/difffilter/sample.new.txt 2016-10-13 05:15:26.839245048 +0900 63 @@ -1,3 +1,4 @@ 64 unchanged, contextual line 65 -deleted line 66 +added line 67 +added line 68 unchanged, contextual line 69 --- a/sample.old.txt 2016-10-13 15:34:14.931778318 +0900 70 +++ b/sample.new.txt 2016-10-13 15:34:14.868444672 +0900 71 @@ -1,4 +1,5 @@ 72 " vim: nofixeol noendofline 73 No newline at end of both the old and new file 74 -a 75 -a 76 \ No newline at end of file 77 +b 78 +b 79 +b 80 \ No newline at end of file 81 ` 82 83 func getCwd() string { 84 cwd, _ := os.Getwd() 85 return cwd 86 } 87 88 func cd(path string) (cleanup func()) { 89 cwd := getCwd() 90 os.Chdir(path) 91 return func() { 92 os.Chdir(cwd) 93 } 94 } 95 96 func getDiff(t *testing.T, difftext string) []*diff.FileDiff { 97 t.Helper() 98 files, err := diff.ParseMultiFile(strings.NewReader(difftext)) 99 if err != nil { 100 t.Fatal(err) 101 } 102 return files 103 } 104 105 func TestDiffFilter_root(t *testing.T) { 106 defer cd("..")() 107 files := getDiff(t, sampleDiffRoot) 108 tests := []struct { 109 path string 110 lnum int 111 mode Mode 112 want bool 113 wantFileDiff bool 114 wantLineDiff bool 115 }{ 116 { 117 path: "sample.new.txt", 118 lnum: 2, 119 mode: ModeAdded, 120 want: true, 121 wantFileDiff: true, 122 wantLineDiff: true, 123 }, 124 { 125 path: filepath.Join(getCwd(), "sample.new.txt"), 126 lnum: 2, 127 mode: ModeAdded, 128 want: true, 129 wantFileDiff: true, 130 wantLineDiff: true, 131 }, 132 { 133 path: "sample.new.txt", 134 lnum: 1, 135 mode: ModeAdded, 136 want: false, 137 wantFileDiff: true, 138 wantLineDiff: false, 139 }, 140 { 141 path: "sample.new.txt", 142 lnum: 1, 143 mode: ModeDiffContext, 144 want: true, 145 wantFileDiff: true, 146 wantLineDiff: true, 147 }, 148 { 149 path: "subdir/nonewline.new.txt", 150 lnum: 3, 151 mode: ModeAdded, 152 want: true, 153 wantFileDiff: true, 154 wantLineDiff: true, 155 }, 156 { 157 path: "sample.new.txt", 158 lnum: 14, 159 mode: ModeFile, 160 want: true, 161 wantFileDiff: true, 162 wantLineDiff: false, 163 }, 164 { 165 path: "sample.new.txt", 166 lnum: 0, // Only file path. 167 mode: ModeFile, 168 want: true, 169 wantFileDiff: true, 170 wantLineDiff: false, 171 }, 172 { 173 path: "sample.new.txt", 174 lnum: 0, // Only file path. 175 mode: ModeAdded, 176 want: false, 177 wantFileDiff: true, 178 wantLineDiff: false, 179 }, 180 { 181 path: "sample.new.txt", 182 lnum: 2, 183 mode: ModeNoFilter, 184 want: true, 185 wantFileDiff: true, 186 wantLineDiff: true, 187 }, 188 { 189 path: "sample.new.txt", 190 lnum: 2, 191 mode: ModeNoFilter, 192 want: true, 193 wantFileDiff: true, 194 wantLineDiff: true, // ModeNoFilter returns linediff if possible. 195 }, 196 { 197 path: "any_path_with_any_line.txt", 198 lnum: 141414, 199 mode: ModeNoFilter, 200 want: true, 201 wantFileDiff: false, 202 wantLineDiff: false, 203 }, 204 { 205 path: "any_path_only.txt", 206 mode: ModeNoFilter, 207 want: true, 208 wantFileDiff: false, 209 wantLineDiff: false, 210 }, 211 } 212 for _, tt := range tests { 213 df := New(files, 1, getCwd(), tt.mode) 214 if got, gotFile, gotLine := df.ShouldReport(tt.path, tt.lnum); got != tt.want || 215 (gotFile != nil) != tt.wantFileDiff || 216 (gotLine != nil) != tt.wantLineDiff { 217 t.Errorf("[%s] ShouldReport(%q, %d) = (%v, %t, %t), want (%v, %t, %t)", 218 tt.mode.String(), tt.path, tt.lnum, got, gotFile != nil, gotLine != nil, tt.want, tt.wantFileDiff, tt.wantLineDiff) 219 } 220 } 221 } 222 223 func TestDiffFilter_subdir(t *testing.T) { 224 // git diff (including diff from GitHub) returns path relative to a project 225 // root directory (See sampleDiffSubDir), but given path from linters can be 226 // relative path to current working directory. 227 files := getDiff(t, sampleDiffSubDir) 228 tests := []struct { 229 path string 230 lnum int 231 mode Mode 232 want bool 233 wantLineDiff bool 234 wantFileDiff bool 235 }{ 236 { 237 path: "sample.new.txt", 238 lnum: 2, 239 mode: ModeAdded, 240 want: true, 241 wantFileDiff: true, 242 wantLineDiff: true, 243 }, 244 { 245 path: "sample.new.txt", 246 lnum: 2, 247 mode: ModeDefault, 248 want: true, 249 wantFileDiff: true, 250 wantLineDiff: true, 251 }, 252 { 253 path: filepath.Join(getCwd(), "sample.new.txt"), 254 lnum: 2, 255 mode: ModeAdded, 256 want: true, 257 wantFileDiff: true, 258 wantLineDiff: true, 259 }, 260 { 261 path: "sample.new.txt", 262 lnum: 5, 263 mode: ModeAdded, 264 want: false, 265 wantFileDiff: true, 266 wantLineDiff: false, 267 }, 268 } 269 for _, tt := range tests { 270 df := New(files, 1, getCwd(), tt.mode) 271 if got, gotFile, gotLine := df.ShouldReport(tt.path, tt.lnum); got != tt.want || 272 (gotFile != nil) != tt.wantFileDiff || 273 (gotLine != nil) != tt.wantLineDiff { 274 t.Errorf("ShouldReport(%q, %d) = (%v, %t, %t), want (%v, %t, %t)", 275 tt.path, tt.lnum, got, gotFile != nil, gotLine != nil, tt.want, tt.wantFileDiff, tt.wantLineDiff) 276 } 277 } 278 }