Move sliceContains to new package pkg/sliceutil (#1333)

* Move sliceContains to new package pkg/sliceutil

* Test sliceutil
This commit is contained in:
SystemGlitch 2020-08-24 13:56:54 +02:00 committed by GitHub
parent 13c2a34028
commit 029278c70a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 10 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/sliceutil"
)
type FileReader struct {
@ -183,7 +184,7 @@ func (r *FileReader) setupConfigFileSearch() {
// find home directory for global config
if home, err := homedir.Dir(); err != nil {
r.log.Warnf("Can't get user's home directory: %s", err.Error())
} else if !sliceContains(configSearchPaths, home) {
} else if !sliceutil.Contains(configSearchPaths, home) {
configSearchPaths = append(configSearchPaths, home)
}
@ -194,15 +195,6 @@ func (r *FileReader) setupConfigFileSearch() {
}
}
func sliceContains(slice []string, value string) bool {
for _, v := range slice {
if v == value {
return true
}
}
return false
}
var errConfigDisabled = errors.New("config is disabled by --no-config")
func (r *FileReader) parseConfigOption() (string, error) {

View File

@ -0,0 +1,17 @@
package sliceutil
// IndexOf get the index of the given value in the given string slice,
// or -1 if not found.
func IndexOf(slice []string, value string) int {
for i, v := range slice {
if v == value {
return i
}
}
return -1
}
// Contains check if a string slice contains a value.
func Contains(slice []string, value string) bool {
return IndexOf(slice, value) != -1
}

View File

@ -0,0 +1,17 @@
package sliceutil
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestContains(t *testing.T) {
assert.True(t, Contains([]string{"val1", "val2", "val3"}, "val2"))
assert.False(t, Contains([]string{"val1", "val2", "val3"}, "val4"))
}
func TestIndexOf(t *testing.T) {
assert.Equal(t, 1, IndexOf([]string{"val1", "val2", "val3"}, "val2"))
assert.Equal(t, -1, IndexOf([]string{"val1", "val2", "val3"}, "val4"))
}