github.com/Mistwind/reviewdog@v0.0.0-20230317041057-48e69b6d9e86/filter/filter_test.go (about)

     1  package filter
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"google.golang.org/protobuf/testing/protocmp"
     9  
    10  	"github.com/reviewdog/reviewdog/diff"
    11  	"github.com/reviewdog/reviewdog/proto/rdf"
    12  )
    13  
    14  const diffContent = `--- sample.old.txt	2016-10-13 05:09:35.820791185 +0900
    15  +++ sample.new.txt	2016-10-13 05:15:26.839245048 +0900
    16  @@ -1,3 +1,4 @@
    17   unchanged, contextual line
    18  -deleted line
    19  +added line
    20  +added line
    21   unchanged, contextual line
    22  --- nonewline.old.txt	2016-10-13 15:34:14.931778318 +0900
    23  +++ nonewline.new.txt	2016-10-13 15:34:14.868444672 +0900
    24  @@ -1,4 +1,4 @@
    25   " vim: nofixeol noendofline
    26   No newline at end of both the old and new file
    27  -a
    28  -a
    29  \ No newline at end of file
    30  +b
    31  +b
    32  \ No newline at end of file
    33  `
    34  
    35  const diffContentAddedStrip = `diff --git a/test_added.go b/test_added.go
    36  new file mode 100644
    37  index 0000000..264c67e
    38  --- /dev/null
    39  +++ b/test_added.go
    40  @@ -0,0 +1,3 @@
    41  +package reviewdog
    42  +
    43  +var TestAdded = 14
    44  `
    45  
    46  func TestFilterCheckByAddedLines(t *testing.T) {
    47  	results := []*rdf.Diagnostic{
    48  		{
    49  			Location: &rdf.Location{
    50  				Path:  "sample.new.txt",
    51  				Range: &rdf.Range{Start: &rdf.Position{Line: 1}},
    52  			},
    53  		},
    54  		{
    55  			Location: &rdf.Location{
    56  				Path:  "sample.new.txt",
    57  				Range: &rdf.Range{Start: &rdf.Position{Line: 2}},
    58  			},
    59  		},
    60  		{
    61  			Location: &rdf.Location{
    62  				Path:  "nonewline.new.txt",
    63  				Range: &rdf.Range{Start: &rdf.Position{Line: 1}},
    64  			},
    65  		},
    66  		{
    67  			Location: &rdf.Location{
    68  				Path:  "nonewline.new.txt",
    69  				Range: &rdf.Range{Start: &rdf.Position{Line: 3}},
    70  			},
    71  		},
    72  		{
    73  			Message: "outside range (start)",
    74  			Location: &rdf.Location{
    75  				Path: "sample.new.txt",
    76  				Range: &rdf.Range{
    77  					Start: &rdf.Position{Line: 1},
    78  					End:   &rdf.Position{Line: 2},
    79  				},
    80  			},
    81  		},
    82  	}
    83  	want := []*FilteredDiagnostic{
    84  		{
    85  			Diagnostic: &rdf.Diagnostic{
    86  				Location: &rdf.Location{
    87  					Path:  "sample.new.txt",
    88  					Range: &rdf.Range{Start: &rdf.Position{Line: 1}},
    89  				},
    90  			},
    91  			ShouldReport:  false,
    92  			InDiffFile:    true,
    93  			InDiffContext: true,
    94  			SourceLines:   map[int]string{1: "unchanged, contextual line"},
    95  			OldPath:       "sample.old.txt",
    96  			OldLine:       1,
    97  		},
    98  		{
    99  			Diagnostic: &rdf.Diagnostic{
   100  				Location: &rdf.Location{
   101  					Path:  "sample.new.txt",
   102  					Range: &rdf.Range{Start: &rdf.Position{Line: 2}},
   103  				},
   104  			},
   105  			ShouldReport:  true,
   106  			InDiffFile:    true,
   107  			InDiffContext: true,
   108  			SourceLines:   map[int]string{2: "added line"},
   109  			OldPath:       "sample.old.txt",
   110  			OldLine:       0,
   111  		},
   112  		{
   113  			Diagnostic: &rdf.Diagnostic{
   114  				Location: &rdf.Location{
   115  					Path:  "nonewline.new.txt",
   116  					Range: &rdf.Range{Start: &rdf.Position{Line: 1}},
   117  				},
   118  			},
   119  			ShouldReport:  false,
   120  			InDiffFile:    true,
   121  			InDiffContext: true,
   122  			SourceLines:   map[int]string{1: `" vim: nofixeol noendofline`},
   123  			OldPath:       "nonewline.old.txt",
   124  			OldLine:       1,
   125  		},
   126  		{
   127  			Diagnostic: &rdf.Diagnostic{
   128  				Location: &rdf.Location{
   129  					Path:  "nonewline.new.txt",
   130  					Range: &rdf.Range{Start: &rdf.Position{Line: 3}},
   131  				},
   132  			},
   133  			ShouldReport:  true,
   134  			InDiffFile:    true,
   135  			InDiffContext: true,
   136  			SourceLines:   map[int]string{3: "b"},
   137  			OldPath:       "nonewline.old.txt",
   138  			OldLine:       0,
   139  		},
   140  		{
   141  			Diagnostic: &rdf.Diagnostic{
   142  				Message: "outside range (start)",
   143  				Location: &rdf.Location{
   144  					Path: "sample.new.txt",
   145  					Range: &rdf.Range{
   146  						Start: &rdf.Position{Line: 1},
   147  						End:   &rdf.Position{Line: 2},
   148  					},
   149  				},
   150  			},
   151  			ShouldReport:  true,
   152  			InDiffFile:    true,
   153  			InDiffContext: true,
   154  			SourceLines:   map[int]string{1: "unchanged, contextual line", 2: "added line"},
   155  			OldPath:       "sample.old.txt",
   156  			OldLine:       1,
   157  		},
   158  	}
   159  	filediffs, _ := diff.ParseMultiFile(strings.NewReader(diffContent))
   160  	got := FilterCheck(results, filediffs, 0, "", ModeAdded)
   161  	if value := cmp.Diff(got, want, protocmp.Transform()); value != "" {
   162  		t.Error(value)
   163  	}
   164  }
   165  
   166  // All lines that are in diff are taken into account
   167  func TestFilterCheckByDiffContext(t *testing.T) {
   168  	results := []*rdf.Diagnostic{
   169  		{
   170  			Location: &rdf.Location{
   171  				Path:  "sample.new.txt",
   172  				Range: &rdf.Range{Start: &rdf.Position{Line: 1}},
   173  			},
   174  		},
   175  		{
   176  			Location: &rdf.Location{
   177  				Path:  "sample.new.txt",
   178  				Range: &rdf.Range{Start: &rdf.Position{Line: 2}},
   179  			},
   180  		},
   181  		{
   182  			Location: &rdf.Location{
   183  				Path:  "sample.new.txt",
   184  				Range: &rdf.Range{Start: &rdf.Position{Line: 3}},
   185  			},
   186  		},
   187  		{
   188  			Location: &rdf.Location{
   189  				Path:  "sample.new.txt",
   190  				Range: &rdf.Range{Start: &rdf.Position{Line: 3}},
   191  			},
   192  			Suggestions: []*rdf.Suggestion{
   193  				{
   194  					Range: &rdf.Range{
   195  						Start: &rdf.Position{Line: 2},
   196  						End:   &rdf.Position{Line: 4},
   197  					},
   198  				},
   199  			},
   200  		},
   201  	}
   202  	want := []*FilteredDiagnostic{
   203  		{
   204  			Diagnostic: &rdf.Diagnostic{
   205  				Location: &rdf.Location{
   206  					Path:  "sample.new.txt",
   207  					Range: &rdf.Range{Start: &rdf.Position{Line: 1}},
   208  				},
   209  			},
   210  			ShouldReport:  true,
   211  			InDiffFile:    true,
   212  			InDiffContext: true,
   213  			SourceLines:   map[int]string{1: "unchanged, contextual line"},
   214  			OldPath:       "sample.old.txt",
   215  			OldLine:       1,
   216  		},
   217  		{
   218  			Diagnostic: &rdf.Diagnostic{
   219  				Location: &rdf.Location{
   220  					Path:  "sample.new.txt",
   221  					Range: &rdf.Range{Start: &rdf.Position{Line: 2}},
   222  				},
   223  			},
   224  			ShouldReport:  true,
   225  			InDiffFile:    true,
   226  			InDiffContext: true,
   227  			SourceLines:   map[int]string{2: "added line"},
   228  			OldPath:       "sample.old.txt",
   229  			OldLine:       0,
   230  		},
   231  		{
   232  			Diagnostic: &rdf.Diagnostic{
   233  				Location: &rdf.Location{
   234  					Path:  "sample.new.txt",
   235  					Range: &rdf.Range{Start: &rdf.Position{Line: 3}},
   236  				},
   237  			},
   238  			ShouldReport:  true,
   239  			InDiffFile:    true,
   240  			InDiffContext: true,
   241  			SourceLines:   map[int]string{3: "added line"},
   242  			OldPath:       "sample.old.txt",
   243  			OldLine:       0,
   244  		},
   245  		{
   246  			Diagnostic: &rdf.Diagnostic{
   247  				Location: &rdf.Location{
   248  					Path:  "sample.new.txt",
   249  					Range: &rdf.Range{Start: &rdf.Position{Line: 3}},
   250  				},
   251  				Suggestions: []*rdf.Suggestion{
   252  					{
   253  						Range: &rdf.Range{
   254  							Start: &rdf.Position{Line: 2},
   255  							End:   &rdf.Position{Line: 4},
   256  						},
   257  					},
   258  				},
   259  			},
   260  			ShouldReport:                 true,
   261  			InDiffFile:                   true,
   262  			InDiffContext:                true,
   263  			FirstSuggestionInDiffContext: true,
   264  			SourceLines: map[int]string{
   265  				2: "added line",
   266  				3: "added line",
   267  				4: "unchanged, contextual line",
   268  			},
   269  			OldPath: "sample.old.txt",
   270  			OldLine: 0,
   271  		},
   272  	}
   273  	filediffs, _ := diff.ParseMultiFile(strings.NewReader(diffContent))
   274  	got := FilterCheck(results, filediffs, 0, "", ModeDiffContext)
   275  	if value := cmp.Diff(got, want, protocmp.Transform()); value != "" {
   276  		t.Error(value)
   277  	}
   278  }
   279  
   280  func findFileDiff(filediffs []*diff.FileDiff, path string, strip int) *diff.FileDiff {
   281  	for _, file := range filediffs {
   282  		if NormalizeDiffPath(file.PathNew, strip) == path {
   283  			return file
   284  		}
   285  	}
   286  	return nil
   287  }
   288  
   289  func TestGetOldPosition(t *testing.T) {
   290  	const strip = 0
   291  	filediffs, _ := diff.ParseMultiFile(strings.NewReader(diffContent))
   292  	tests := []struct {
   293  		newPath     string
   294  		newLine     int
   295  		wantOldPath string
   296  		wantOldLine int
   297  	}{
   298  		{
   299  			newPath:     "sample.new.txt",
   300  			newLine:     1,
   301  			wantOldPath: "sample.old.txt",
   302  			wantOldLine: 1,
   303  		},
   304  		{
   305  			newPath:     "sample.new.txt",
   306  			newLine:     2,
   307  			wantOldPath: "sample.old.txt",
   308  			wantOldLine: 0,
   309  		},
   310  		{
   311  			newPath:     "sample.new.txt",
   312  			newLine:     3,
   313  			wantOldPath: "sample.old.txt",
   314  			wantOldLine: 0,
   315  		},
   316  		{
   317  			newPath:     "sample.new.txt",
   318  			newLine:     14,
   319  			wantOldPath: "sample.old.txt",
   320  			wantOldLine: 13,
   321  		},
   322  		{
   323  			newPath:     "not_found",
   324  			newLine:     14,
   325  			wantOldPath: "",
   326  			wantOldLine: 0,
   327  		},
   328  	}
   329  	for _, tt := range tests {
   330  		fdiff := findFileDiff(filediffs, tt.newPath, strip)
   331  		gotPath, gotLine := getOldPosition(fdiff, strip, tt.newPath, tt.newLine)
   332  		if !(gotPath == tt.wantOldPath && gotLine == tt.wantOldLine) {
   333  			t.Errorf("getOldPosition(..., %s, %d) = (%s, %d), want (%s, %d)",
   334  				tt.newPath, tt.newLine, gotPath, gotLine, tt.wantOldPath, tt.wantOldLine)
   335  		}
   336  	}
   337  }
   338  
   339  func TestGetOldPosition_added(t *testing.T) {
   340  	const strip = 1
   341  	filediffs, _ := diff.ParseMultiFile(strings.NewReader(diffContentAddedStrip))
   342  	path := "test_added.go"
   343  	fdiff := findFileDiff(filediffs, path, strip)
   344  	gotPath, _ := getOldPosition(fdiff, strip, path, 1)
   345  	if gotPath != "" {
   346  		t.Errorf("got %q as old path for added diff file, want empty", gotPath)
   347  	}
   348  }