github.com/nozzle/golangci-lint@v1.49.0-nz3/test/enabled_linters_test.go (about)

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