github.com/chenfeining/golangci-lint@v1.0.2-0.20230730162517-14c6c67868df/pkg/result/processors/skip_files_test.go (about)

     1  package processors
     2  
     3  import (
     4  	"go/token"
     5  	"path/filepath"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/chenfeining/golangci-lint/pkg/result"
    12  )
    13  
    14  func newFileIssue(file string) result.Issue {
    15  	return result.Issue{
    16  		Pos: token.Position{
    17  			Filename: file,
    18  		},
    19  	}
    20  }
    21  
    22  func newTestSkipFiles(t *testing.T, patterns ...string) *SkipFiles {
    23  	p, err := NewSkipFiles(patterns, "")
    24  	assert.NoError(t, err)
    25  	return p
    26  }
    27  
    28  func TestSkipFiles(t *testing.T) {
    29  	processAssertSame(t, newTestSkipFiles(t), newFileIssue("any.go"))
    30  
    31  	processAssertEmpty(t, newTestSkipFiles(t, "file"),
    32  		newFileIssue("file.go"),
    33  		newFileIssue("file"),
    34  		newFileIssue("nofile.go"))
    35  
    36  	processAssertEmpty(t, newTestSkipFiles(t, ".*"), newFileIssue("any.go"))
    37  
    38  	cleanPath := strings.ReplaceAll(filepath.FromSlash("a/b/c.go"), `\`, `\\`)
    39  	processAssertEmpty(t, newTestSkipFiles(t, cleanPath), newFileIssue(filepath.FromSlash("a/b/c.go")))
    40  	processAssertSame(t, newTestSkipFiles(t, cleanPath), newFileIssue(filepath.FromSlash("a/b/d.go")))
    41  
    42  	processAssertEmpty(t, newTestSkipFiles(t, ".*\\.pb\\.go"), newFileIssue(filepath.FromSlash("a/b.pb.go")))
    43  	processAssertSame(t, newTestSkipFiles(t, ".*\\.pb\\.go"), newFileIssue(filepath.FromSlash("a/b.go")))
    44  
    45  	processAssertEmpty(t, newTestSkipFiles(t, ".*\\.pb\\.go$"), newFileIssue(filepath.FromSlash("a/b.pb.go")))
    46  	processAssertSame(t, newTestSkipFiles(t, ".*\\.pb\\.go$"), newFileIssue(filepath.FromSlash("a/b.go")))
    47  }
    48  
    49  func TestSkipFilesInvalidPattern(t *testing.T) {
    50  	p, err := NewSkipFiles([]string{"\\o"}, "")
    51  	assert.Error(t, err)
    52  	assert.Nil(t, p)
    53  }