forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gosec: convert global settings as map with proper key type (golangci#…
…3779) Co-authored-by: Fernandez Ludovic <[email protected]>
- Loading branch information
Showing
4 changed files
with
122 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package golinters | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/securego/gosec/v2" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
) | ||
|
||
func Test_toGosecConfig(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
settings *config.GoSecSettings | ||
expected gosec.Config | ||
}{ | ||
{ | ||
desc: "empty config map", | ||
settings: &config.GoSecSettings{}, | ||
expected: gosec.Config{ | ||
"global": map[gosec.GlobalOption]string{}, | ||
}, | ||
}, | ||
{ | ||
desc: "with global settings", | ||
settings: &config.GoSecSettings{ | ||
Config: map[string]any{ | ||
gosec.Globals: map[string]any{ | ||
string(gosec.Nosec): true, | ||
string(gosec.Audit): "true", | ||
}, | ||
}, | ||
}, | ||
expected: gosec.Config{ | ||
"global": map[gosec.GlobalOption]string{ | ||
"audit": "true", | ||
"nosec": "true", | ||
}, | ||
}, | ||
}, | ||
{ | ||
desc: "rule specified setting", | ||
settings: &config.GoSecSettings{ | ||
Config: map[string]any{ | ||
"g101": map[string]any{ | ||
"pattern": "(?i)example", | ||
}, | ||
"G301": "0750", | ||
}, | ||
}, | ||
expected: gosec.Config{ | ||
"G101": map[string]any{"pattern": "(?i)example"}, | ||
"G301": "0750", | ||
"global": map[gosec.GlobalOption]string{}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
test := test | ||
t.Run(test.desc, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
conf := toGosecConfig(test.settings) | ||
|
||
assert.Equal(t, test.expected, conf) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
linters-settings: | ||
gosec: | ||
config: | ||
global: | ||
nosec: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//golangcitest:args -Egosec | ||
//golangcitest:config_path testdata/configs/gosec_global_option.yml | ||
package testdata | ||
|
||
import ( | ||
"crypto/md5" // want "G501: Blocklisted import crypto/md5: weak cryptographic primitive" | ||
"log" | ||
) | ||
|
||
func Gosec() { | ||
// #nosec G401 | ||
h := md5.New() // want "G401: Use of weak cryptographic primitive" | ||
log.Print(h) | ||
} |