github.com/be-b10g/golangci-lint@v1.17.2/test/enabled_linters_test.go (about)

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