github.com/chenfeining/golangci-lint@v1.0.2-0.20230730162517-14c6c67868df/pkg/lint/lintersdb/enabled_set_test.go (about)

     1  package lintersdb
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/chenfeining/golangci-lint/pkg/config"
     9  	"github.com/chenfeining/golangci-lint/pkg/lint/linter"
    10  )
    11  
    12  func TestGetEnabledLintersSet(t *testing.T) {
    13  	type cs struct {
    14  		cfg  config.Linters
    15  		name string   // test case name
    16  		def  []string // enabled by default linters
    17  		exp  []string // alphabetically ordered enabled linter names
    18  	}
    19  
    20  	allMegacheckLinterNames := []string{"gosimple", "staticcheck", "unused"}
    21  
    22  	cases := []cs{
    23  		{
    24  			cfg: config.Linters{
    25  				Disable: []string{"megacheck"},
    26  			},
    27  			name: "disable all linters from megacheck",
    28  			def:  allMegacheckLinterNames,
    29  			exp:  []string{"typecheck"}, // all disabled
    30  		},
    31  		{
    32  			cfg: config.Linters{
    33  				Disable: []string{"staticcheck"},
    34  			},
    35  			name: "disable only staticcheck",
    36  			def:  allMegacheckLinterNames,
    37  			exp:  []string{"gosimple", "typecheck", "unused"},
    38  		},
    39  		{
    40  			name: "don't merge into megacheck",
    41  			def:  allMegacheckLinterNames,
    42  			exp:  []string{"gosimple", "staticcheck", "typecheck", "unused"},
    43  		},
    44  		{
    45  			name: "expand megacheck",
    46  			cfg: config.Linters{
    47  				Enable: []string{"megacheck"},
    48  			},
    49  			def: nil,
    50  			exp: []string{"gosimple", "staticcheck", "typecheck", "unused"},
    51  		},
    52  		{
    53  			name: "don't disable anything",
    54  			def:  []string{"gofmt", "govet", "typecheck"},
    55  			exp:  []string{"gofmt", "govet", "typecheck"},
    56  		},
    57  		{
    58  			name: "enable gosec by gas alias",
    59  			cfg: config.Linters{
    60  				Enable: []string{"gas"},
    61  			},
    62  			exp: []string{"gosec", "typecheck"},
    63  		},
    64  		{
    65  			name: "enable gosec by primary name",
    66  			cfg: config.Linters{
    67  				Enable: []string{"gosec"},
    68  			},
    69  			exp: []string{"gosec", "typecheck"},
    70  		},
    71  		{
    72  			name: "enable gosec by both names",
    73  			cfg: config.Linters{
    74  				Enable: []string{"gosec", "gas"},
    75  			},
    76  			exp: []string{"gosec", "typecheck"},
    77  		},
    78  		{
    79  			name: "disable gosec by gas alias",
    80  			cfg: config.Linters{
    81  				Disable: []string{"gas"},
    82  			},
    83  			def: []string{"gosec"},
    84  			exp: []string{"typecheck"},
    85  		},
    86  		{
    87  			name: "disable gosec by primary name",
    88  			cfg: config.Linters{
    89  				Disable: []string{"gosec"},
    90  			},
    91  			def: []string{"gosec"},
    92  			exp: []string{"typecheck"},
    93  		},
    94  	}
    95  
    96  	m := NewManager(nil, nil)
    97  	es := NewEnabledSet(m, NewValidator(m), nil, nil)
    98  
    99  	for _, c := range cases {
   100  		c := c
   101  		t.Run(c.name, func(t *testing.T) {
   102  			var defaultLinters []*linter.Config
   103  			for _, ln := range c.def {
   104  				lcs := m.GetLinterConfigs(ln)
   105  				assert.NotNil(t, lcs, ln)
   106  				defaultLinters = append(defaultLinters, lcs...)
   107  			}
   108  
   109  			els := es.build(&c.cfg, defaultLinters)
   110  			var enabledLinters []string
   111  			for ln, lc := range els {
   112  				assert.Equal(t, ln, lc.Name())
   113  				enabledLinters = append(enabledLinters, ln)
   114  			}
   115  
   116  			assert.ElementsMatch(t, c.exp, enabledLinters)
   117  		})
   118  	}
   119  }