github.com/alecthomas/golangci-lint@v1.4.2-0.20180609094924-581a3564ff68/pkg/result/processors/nolint_test.go (about)

     1  package processors
     2  
     3  import (
     4  	"go/token"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/golangci/golangci-lint/pkg/result"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func newNolintFileIssue(line int, fromLinter string) result.Issue {
    13  	return result.Issue{
    14  		Pos: token.Position{
    15  			Filename: filepath.Join("testdata", "nolint.go"),
    16  			Line:     line,
    17  		},
    18  		FromLinter: fromLinter,
    19  	}
    20  }
    21  
    22  func TestNolint(t *testing.T) {
    23  	p := NewNolint(token.NewFileSet())
    24  
    25  	// test inline comments
    26  	processAssertEmpty(t, p, newNolintFileIssue(3, "gofmt"))
    27  	processAssertEmpty(t, p, newNolintFileIssue(3, "gofmt")) // check cached is ok
    28  	processAssertSame(t, p, newNolintFileIssue(3, "gofmtA")) // check different name
    29  
    30  	processAssertEmpty(t, p, newNolintFileIssue(4, "gofmt"))
    31  	processAssertSame(t, p, newNolintFileIssue(4, "gofmtA")) // check different name
    32  
    33  	processAssertEmpty(t, p, newNolintFileIssue(5, "gofmt"))
    34  	processAssertEmpty(t, p, newNolintFileIssue(5, "govet"))
    35  	processAssertSame(t, p, newNolintFileIssue(5, "gofmtA")) // check different name
    36  
    37  	processAssertEmpty(t, p, newNolintFileIssue(6, "any"))
    38  	processAssertEmpty(t, p, newNolintFileIssue(7, "any"))
    39  
    40  	processAssertSame(t, p, newNolintFileIssue(1, "golint")) // no directive
    41  
    42  	// test preceding comments
    43  	processAssertEmpty(t, p, newNolintFileIssue(10, "any")) // preceding comment for var
    44  	processAssertEmpty(t, p, newNolintFileIssue(9, "any"))  // preceding comment for var itself
    45  
    46  	processAssertSame(t, p, newNolintFileIssue(14, "any"))  // preceding comment with extra \n
    47  	processAssertEmpty(t, p, newNolintFileIssue(12, "any")) // preceding comment with extra \n itself
    48  
    49  	processAssertSame(t, p, newNolintFileIssue(17, "any"))  // preceding comment on different column
    50  	processAssertEmpty(t, p, newNolintFileIssue(16, "any")) // preceding comment on different column itself
    51  
    52  	// preceding comment for func name and comment itself
    53  	for i := 19; i <= 23; i++ {
    54  		processAssertEmpty(t, p, newNolintFileIssue(i, "any"))
    55  	}
    56  
    57  	processAssertSame(t, p, newNolintFileIssue(24, "any")) // right after func
    58  
    59  	// preceding multiline comment: last line
    60  	for i := 25; i <= 30; i++ {
    61  		processAssertEmpty(t, p, newNolintFileIssue(i, "any"))
    62  	}
    63  
    64  	processAssertSame(t, p, newNolintFileIssue(31, "any")) // between funcs
    65  
    66  	// preceding multiline comment: first line
    67  	for i := 32; i <= 37; i++ {
    68  		processAssertEmpty(t, p, newNolintFileIssue(i, "any"))
    69  	}
    70  
    71  	processAssertSame(t, p, newNolintFileIssue(38, "any")) // between funcs
    72  
    73  	// preceding multiline comment: medium line
    74  	for i := 39; i <= 45; i++ {
    75  		processAssertEmpty(t, p, newNolintFileIssue(i, "any"))
    76  	}
    77  }
    78  
    79  func TestNoIssuesInAutogeneratedFiles(t *testing.T) {
    80  	files := []string{
    81  		"nolint_autogenerated.go",
    82  		"nolint_autogenerated_alt_hdr.go",
    83  		"nolint_autogenerated_alt_hdr2.go",
    84  	}
    85  	for _, file := range files {
    86  		t.Run(file, func(t *testing.T) {
    87  			i := result.Issue{
    88  				Pos: token.Position{
    89  					Filename: filepath.Join("testdata", file),
    90  					Line:     4,
    91  				},
    92  			}
    93  			p := NewNolint(token.NewFileSet())
    94  			processAssertEmpty(t, p, i)
    95  		})
    96  	}
    97  }
    98  
    99  func TestIgnoredRangeMatches(t *testing.T) {
   100  	var testcases = []struct {
   101  		doc      string
   102  		issue    result.Issue
   103  		linters  []string
   104  		expected bool
   105  	}{
   106  		{
   107  			doc: "unmatched line",
   108  			issue: result.Issue{
   109  				Pos: token.Position{
   110  					Line: 100,
   111  				},
   112  			},
   113  		},
   114  		{
   115  			doc: "matched line, all linters",
   116  			issue: result.Issue{
   117  				Pos: token.Position{
   118  					Line: 5,
   119  				},
   120  			},
   121  			expected: true,
   122  		},
   123  		{
   124  			doc: "matched line, unmatched linter",
   125  			issue: result.Issue{
   126  				Pos: token.Position{
   127  					Line: 5,
   128  				},
   129  			},
   130  			linters: []string{"vet"},
   131  		},
   132  		{
   133  			doc: "matched line and linters",
   134  			issue: result.Issue{
   135  				Pos: token.Position{
   136  					Line: 20,
   137  				},
   138  				FromLinter: "vet",
   139  			},
   140  			linters:  []string{"vet"},
   141  			expected: true,
   142  		},
   143  	}
   144  
   145  	for _, testcase := range testcases {
   146  		ir := ignoredRange{
   147  			col: 20,
   148  			Range: result.Range{
   149  				From: 5,
   150  				To:   20,
   151  			},
   152  			linters: testcase.linters,
   153  		}
   154  		assert.Equal(t, testcase.expected, ir.doesMatch(&testcase.issue), testcase.doc)
   155  	}
   156  }