github.com/chenfeining/golangci-lint@v1.0.2-0.20230730162517-14c6c67868df/test/enabled_linters_test.go (about)

     1  package test
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  	"testing"
     8  
     9  	"golang.org/x/exp/slices"
    10  
    11  	"github.com/chenfeining/golangci-lint/pkg/lint/lintersdb"
    12  	"github.com/chenfeining/golangci-lint/test/testshared"
    13  )
    14  
    15  func TestEnabledLinters(t *testing.T) {
    16  	// require to display the message "Active x linters: [x,y]"
    17  	t.Setenv(lintersdb.EnvTestRun, "1")
    18  
    19  	cases := []struct {
    20  		name           string
    21  		cfg            string
    22  		enabledLinters []string
    23  		args           []string
    24  		noImplicitFast bool
    25  	}{
    26  		{
    27  			name: "disable govet in config",
    28  			cfg: `
    29  			linters:
    30  				disable:
    31  					- govet
    32  			`,
    33  			enabledLinters: getEnabledByDefaultFastLintersExcept("govet"),
    34  		},
    35  		{
    36  			name: "enable revive in config",
    37  			cfg: `
    38  			linters:
    39  				enable:
    40  					- revive
    41  			`,
    42  			enabledLinters: getEnabledByDefaultFastLintersWith("revive"),
    43  		},
    44  		{
    45  			name:           "disable govet in cmd",
    46  			args:           []string{"-Dgovet"},
    47  			enabledLinters: getEnabledByDefaultFastLintersExcept("govet"),
    48  		},
    49  		{
    50  			name: "enable gofmt in cmd and enable revive in config",
    51  			args: []string{"-Egofmt"},
    52  			cfg: `
    53  			linters:
    54  				enable:
    55  					- revive
    56  			`,
    57  			enabledLinters: getEnabledByDefaultFastLintersWith("revive", "gofmt"),
    58  		},
    59  		{
    60  			name: "fast option in config",
    61  			cfg: `
    62  			linters:
    63  				fast: true
    64  			`,
    65  			enabledLinters: getEnabledByDefaultFastLintersWith(),
    66  			noImplicitFast: true,
    67  		},
    68  		{
    69  			name: "explicitly unset fast option in config",
    70  			cfg: `
    71  			linters:
    72  				fast: false
    73  			`,
    74  			enabledLinters: getEnabledByDefaultLinters(),
    75  			noImplicitFast: true,
    76  		},
    77  		{
    78  			name:           "set fast option in command-line",
    79  			args:           []string{"--fast"},
    80  			enabledLinters: getEnabledByDefaultFastLintersWith(),
    81  			noImplicitFast: true,
    82  		},
    83  		{
    84  			name: "fast option in command-line has higher priority to enable",
    85  			cfg: `
    86  			linters:
    87  				fast: false
    88  			`,
    89  			args:           []string{"--fast"},
    90  			enabledLinters: getEnabledByDefaultFastLintersWith(),
    91  			noImplicitFast: true,
    92  		},
    93  		{
    94  			name: "fast option in command-line has higher priority to disable",
    95  			cfg: `
    96  			linters:
    97  				fast: true
    98  			`,
    99  			args:           []string{"--fast=false"},
   100  			enabledLinters: getEnabledByDefaultLinters(),
   101  			noImplicitFast: true,
   102  		},
   103  		{
   104  			name:           "fast option combined with enable and enable-all",
   105  			args:           []string{"--enable-all", "--fast", "--enable=unused"},
   106  			enabledLinters: getAllFastLintersWith("unused"),
   107  			noImplicitFast: true,
   108  		},
   109  	}
   110  
   111  	testshared.InstallGolangciLint(t)
   112  
   113  	for _, c := range cases {
   114  		c := c
   115  		t.Run(c.name, func(t *testing.T) {
   116  			t.Parallel()
   117  
   118  			args := []string{"--verbose"}
   119  			if !c.noImplicitFast {
   120  				args = append(args, "--fast")
   121  			}
   122  
   123  			r := testshared.NewRunnerBuilder(t).
   124  				WithCommand("linters").
   125  				WithArgs(args...).
   126  				WithArgs(c.args...).
   127  				WithConfig(c.cfg).
   128  				Runner().
   129  				Run()
   130  
   131  			sort.StringSlice(c.enabledLinters).Sort()
   132  
   133  			r.ExpectOutputContains(fmt.Sprintf("Active %d linters: [%s]",
   134  				len(c.enabledLinters), strings.Join(c.enabledLinters, " ")))
   135  		})
   136  	}
   137  }
   138  
   139  func getEnabledByDefaultFastLintersExcept(except ...string) []string {
   140  	m := lintersdb.NewManager(nil, nil)
   141  	ebdl := m.GetAllEnabledByDefaultLinters()
   142  	var ret []string
   143  	for _, lc := range ebdl {
   144  		if lc.IsSlowLinter() {
   145  			continue
   146  		}
   147  
   148  		if !slices.Contains(except, lc.Name()) {
   149  			ret = append(ret, lc.Name())
   150  		}
   151  	}
   152  
   153  	return ret
   154  }
   155  
   156  func getAllFastLintersWith(with ...string) []string {
   157  	linters := lintersdb.NewManager(nil, nil).GetAllSupportedLinterConfigs()
   158  	ret := append([]string{}, with...)
   159  	for _, lc := range linters {
   160  		if lc.IsSlowLinter() {
   161  			continue
   162  		}
   163  		ret = append(ret, lc.Name())
   164  	}
   165  
   166  	return ret
   167  }
   168  
   169  func getEnabledByDefaultLinters() []string {
   170  	ebdl := lintersdb.NewManager(nil, nil).GetAllEnabledByDefaultLinters()
   171  	var ret []string
   172  	for _, lc := range ebdl {
   173  		if lc.Internal {
   174  			continue
   175  		}
   176  
   177  		ret = append(ret, lc.Name())
   178  	}
   179  
   180  	return ret
   181  }
   182  
   183  func getEnabledByDefaultFastLintersWith(with ...string) []string {
   184  	ebdl := lintersdb.NewManager(nil, nil).GetAllEnabledByDefaultLinters()
   185  	ret := append([]string{}, with...)
   186  	for _, lc := range ebdl {
   187  		if lc.IsSlowLinter() {
   188  			continue
   189  		}
   190  
   191  		ret = append(ret, lc.Name())
   192  	}
   193  
   194  	return ret
   195  }