github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/pkg/golinters/gosec_test.go (about)

     1  package golinters
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/securego/gosec/v2"
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/vanstinator/golangci-lint/pkg/config"
    10  )
    11  
    12  func Test_toGosecConfig(t *testing.T) {
    13  	testCases := []struct {
    14  		desc     string
    15  		settings *config.GoSecSettings
    16  		expected gosec.Config
    17  	}{
    18  		{
    19  			desc:     "empty config map",
    20  			settings: &config.GoSecSettings{},
    21  			expected: gosec.Config{
    22  				"global": map[gosec.GlobalOption]string{},
    23  			},
    24  		},
    25  		{
    26  			desc: "with global settings",
    27  			settings: &config.GoSecSettings{
    28  				Config: map[string]any{
    29  					gosec.Globals: map[string]any{
    30  						string(gosec.Nosec): true,
    31  						string(gosec.Audit): "true",
    32  					},
    33  				},
    34  			},
    35  			expected: gosec.Config{
    36  				"global": map[gosec.GlobalOption]string{
    37  					"audit": "true",
    38  					"nosec": "true",
    39  				},
    40  			},
    41  		},
    42  		{
    43  			desc: "rule specified setting",
    44  			settings: &config.GoSecSettings{
    45  				Config: map[string]any{
    46  					"g101": map[string]any{
    47  						"pattern": "(?i)example",
    48  					},
    49  					"G301": "0750",
    50  				},
    51  			},
    52  			expected: gosec.Config{
    53  				"G101":   map[string]any{"pattern": "(?i)example"},
    54  				"G301":   "0750",
    55  				"global": map[gosec.GlobalOption]string{},
    56  			},
    57  		},
    58  	}
    59  
    60  	for _, test := range testCases {
    61  		test := test
    62  		t.Run(test.desc, func(t *testing.T) {
    63  			t.Parallel()
    64  
    65  			conf := toGosecConfig(test.settings)
    66  
    67  			assert.Equal(t, test.expected, conf)
    68  		})
    69  	}
    70  }