Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into feature-extensionle…
Browse files Browse the repository at this point in the history
…ss-configs
  • Loading branch information
vogJust committed Aug 9, 2024
2 parents 612e960 + e601c40 commit c04c409
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 29 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
## Supported config files formats:
* Apple PList XML
* CSV
* EDITORCONFIG
* ENV
* HCL
* HOCON
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ require (
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/editorconfig/editorconfig-core-go/v2 v2.6.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/go-envparse v0.1.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/zclconf/go-cty v1.13.0 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.11.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmms
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/editorconfig/editorconfig-core-go/v2 v2.6.2 h1:dKG8sc7n321deIVRcQtwlMNoBEra7j0qQ8RwxO8RN0w=
github.com/editorconfig/editorconfig-core-go/v2 v2.6.2/go.mod h1:7dvD3GCm7eBw53xZ/lsiq72LqobdMg3ITbMBxnmJmqY=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gurkankaymak/hocon v1.2.18 h1:/COj3okWh58himiYO0R7PrPX+iE7PbuzTn2cEv7fPsw=
github.com/gurkankaymak/hocon v1.2.18/go.mod h1:dQCfhnuDKlLqAZRGhFTd81HkAfMx7STHv0w2JkJ6iq4=
github.com/hashicorp/go-envparse v0.1.0 h1:bE++6bhIsNCPLvgDZkYqo3nA+/PFI51pkrHdmPSDFPY=
Expand Down Expand Up @@ -48,6 +51,8 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/zclconf/go-cty v1.13.0 h1:It5dfKTTZHe9aeppbNOda3mN7Ag7sg6QkBNm6TkyFa0=
github.com/zclconf/go-cty v1.13.0/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
1 change: 1 addition & 0 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
## Supported config files formats:
* Apple PList XML
* CSV
* EDITORCONFIG
* ENV
* HCL
* HOCON
Expand Down
9 changes: 9 additions & 0 deletions pkg/filetype/file_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ var EnvFileType = FileType{
validator.EnvValidator{},
}

// Instance of the FileType object to
// represent an EDITORCONFIG file
var EditorConfigFileType = FileType{
"editorconfig",
misc.ArrToMap("editorconfig"),
validator.EditorConfigValidator{},
}

// An array of files types that are supported
// by the validator
var FileTypes = []FileType{
Expand All @@ -117,4 +125,5 @@ var FileTypes = []FileType{
CsvFileType,
HoconFileType,
EnvFileType,
EditorConfigFileType,
}
26 changes: 24 additions & 2 deletions pkg/finder/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ func Test_FileSystemFinderMultipleFinder(t *testing.T) {
func Test_FileSystemFinderDuplicateFiles(t *testing.T) {
fsFinder := FileSystemFinderInit(
WithPathRoots(
"../../test/fixtures/subdir/good.json",
"../../test/fixtures/subdir/",
"../../test/fixtures/subdir/../subdir/good.json",
),
)

Expand Down Expand Up @@ -251,6 +249,30 @@ func Test_FileFinderBadPath(t *testing.T) {
}
}

func Benchmark_Finder(b *testing.B) {

Check failure on line 252 in pkg/finder/finder_test.go

View workflow job for this annotation

GitHub Actions / lint (1.21, macos-latest)

other declaration of Benchmark_Finder
fsFinder := FileSystemFinderInit(
WithPathRoots("../../test/fixtures/"),
)

b.ResetTimer()

for i := 0; i < b.N; i++ {
_, _ = fsFinder.Find()
}
}

func Benchmark_Finder(b *testing.B) {

Check failure on line 264 in pkg/finder/finder_test.go

View workflow job for this annotation

GitHub Actions / lint (1.21, macos-latest)

Benchmark_Finder redeclared in this block
fsFinder := FileSystemFinderInit(
WithPathRoots("../../test/fixtures/"),
)

b.ResetTimer()

for i := 0; i < b.N; i++ {
_, _ = fsFinder.Find()
}
}

func Test_fsFinderBadConfig(t *testing.T) {
// Missing config file
fsFinder := FileSystemFinderInit(
Expand Down
55 changes: 29 additions & 26 deletions pkg/finder/fsfinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,14 @@ func ReadConfig(file string, fsf FileSystemFinder) (map[string]string, error) {

func FileSystemFinderInit(opts ...FSFinderOptions) *FileSystemFinder {
defaultExcludeDirs := make(map[string]struct{})
defaultExcludeFileTypes := make(map[string]struct{})
defaultPathRoots := []string{"."}

fsfinder := &FileSystemFinder{
PathRoots: defaultPathRoots,
FileTypes: filetype.FileTypes,
ExcludeDirs: defaultExcludeDirs,
PathRoots: defaultPathRoots,
FileTypes: filetype.FileTypes,
ExcludeDirs: defaultExcludeDirs,
ExcludeFileTypes: defaultExcludeFileTypes,
}

for _, opt := range opts {
Expand All @@ -140,29 +142,19 @@ func (fsf FileSystemFinder) Find() ([]FileMetadata, error) {
seen := make(map[string]struct{}, 0)
uniqueMatches := make([]FileMetadata, 0)
for _, pathRoot := range fsf.PathRoots {
matches, err := fsf.findOne(pathRoot)
matches, err := fsf.findOne(pathRoot, seen)
if err != nil {
return nil, err
}
for _, match := range matches {
absPath, err := filepath.Abs(match.Path)
if err != nil {
return nil, err
}
if _, ok := seen[absPath]; ok {
continue
}
uniqueMatches = append(uniqueMatches, match)
seen[absPath] = struct{}{}
}
uniqueMatches = append(uniqueMatches, matches...)
}
return uniqueMatches, nil
}

// findOne recursively walks through all subdirectories (excluding the excluded subdirectories)
// and identifying if the file matches a type defined in the fileTypes array for a
// single path and returns the file metadata.
func (fsf FileSystemFinder) findOne(pathRoot string) ([]FileMetadata, error) {
func (fsf FileSystemFinder) findOne(pathRoot string, seenMap map[string]struct{}) ([]FileMetadata, error) {
var matchingFiles []FileMetadata

// check that the path exists before walking it or the error returned
Expand Down Expand Up @@ -196,15 +188,18 @@ func (fsf FileSystemFinder) findOne(pathRoot string) ([]FileMetadata, error) {
}

// determine if directory is in the excludeDirs list or if the depth is greater than the maxDepth
_, isExcluded := fsf.ExcludeDirs[dirEntry.Name()]
if dirEntry.IsDir() && ((fsf.Depth != nil && strings.Count(path, string(os.PathSeparator)) > maxDepth) || isExcluded) {
return filepath.SkipDir
if dirEntry.IsDir() {
_, isExcluded := fsf.ExcludeDirs[dirEntry.Name()]
if isExcluded || (fsf.Depth != nil && strings.Count(path, string(os.PathSeparator)) > maxDepth) {
return filepath.SkipDir
}
}

if !dirEntry.IsDir() {
// filepath.Ext() returns the extension name with a dot so it
// needs to be removed.
walkFileExtension := strings.TrimPrefix(filepath.Ext(path), ".")
extensionLowerCase := strings.ToLower(walkFileExtension)

// If a file is extensionless, check if its stored as an additional file and update the extension. Otherwise ignore
if len(walkFileExtension) == 0 {
Expand All @@ -217,19 +212,27 @@ func (fsf FileSystemFinder) findOne(pathRoot string) ([]FileMetadata, error) {
}

// Checking for case sensitive exclusion
if _, ok := fsf.ExcludeFileTypes[walkFileExtension]; ok {
if _, isExcluded := fsf.ExcludeFileTypes[extensionLowerCase]; isExcluded {
return nil
}
extensionLowerCase := strings.ToLower(walkFileExtension)

Check failure on line 218 in pkg/finder/fsfinder.go

View workflow job for this annotation

GitHub Actions / lint (1.21, macos-latest)

no new variables on left side of :=) (typecheck)

Check failure on line 218 in pkg/finder/fsfinder.go

View workflow job for this annotation

GitHub Actions / lint (1.21, macos-latest)

no new variables on left side of :=) (typecheck)

Check failure on line 218 in pkg/finder/fsfinder.go

View workflow job for this annotation

GitHub Actions / lint (1.21, macos-latest)

no new variables on left side of := (typecheck)

Check failure on line 218 in pkg/finder/fsfinder.go

View workflow job for this annotation

GitHub Actions / Update coverage badge

no new variables on left side of :=

Check failure on line 218 in pkg/finder/fsfinder.go

View workflow job for this annotation

GitHub Actions / lint

no new variables on left side of :=

Check failure on line 218 in pkg/finder/fsfinder.go

View workflow job for this annotation

GitHub Actions / build

no new variables on left side of :=

// Check each fileType, ignore non-matching extensions
for _, fileType := range fsf.FileTypes {
if _, ok := fileType.Extensions[extensionLowerCase]; ok {
fileMetadata := FileMetadata{dirEntry.Name(), path, fileType}
matchingFiles = append(matchingFiles, fileMetadata)
break
if _, isMatched := fileType.Extensions[extensionLowerCase]; isMatched {
absPath, err := filepath.Abs(path)
if err != nil {
return err
}

if _, seen := seenMap[absPath]; !seen {
fileMetadata := FileMetadata{dirEntry.Name(), absPath, fileType}
matchingFiles = append(matchingFiles, fileMetadata)
seenMap[absPath] = struct{}{}
}

return nil
}
}
fsf.ExcludeFileTypes[extensionLowerCase] = struct{}{}
}

return nil
Expand Down
18 changes: 18 additions & 0 deletions pkg/validator/editorconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package validator

import (
"bytes"

"github.com/editorconfig/editorconfig-core-go/v2"
)

type EditorConfigValidator struct{}

// Validate implements the Validator interface by attempting to
// parse a byte array of an editorconfig file using editorconfig-core-go package
func (EditorConfigValidator) Validate(b []byte) (bool, error) {
if _, err := editorconfig.Parse(bytes.NewReader(b)); err != nil {
return false, err
}
return true, nil
}
2 changes: 2 additions & 0 deletions pkg/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ var testData = []struct {
{"invalidHocon", []byte(`test = [1, 2,, 3]`), false, HoconValidator{}},
{"validEnv", []byte("KEY=VALUE"), true, EnvValidator{}},
{"invalidEnv", []byte("=TEST"), false, EnvValidator{}},
{"validEditorConfig", []byte("working = true"), true, EditorConfigValidator{}},
{"invalidEditorConfig", []byte("[*.md\nworking=false"), false, EditorConfigValidator{}},
}

func Test_ValidationInput(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/good.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
working = true
2 changes: 2 additions & 0 deletions test/fixtures/subdir2/bad.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*.md
working = false

0 comments on commit c04c409

Please sign in to comment.