Skip to content

Commit

Permalink
Feature: Exclude file types (#35)
Browse files Browse the repository at this point in the history
* add exclude file types argument

* add gitignore

* add exclude logic

* add tests

* Update pkg/finder/fsfinder.go

Co-authored-by: Jamie Davidson <[email protected]>

* fix tab to spaces indentation

---------

Co-authored-by: Jamie Davidson <[email protected]>
  • Loading branch information
ravsii and jd4235 authored Sep 25, 2023
1 parent ffd9ec1 commit ed46ce0
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ cmd/validator/validator

# Dependency directories (remove the comment below to include it)
# vendor/
.vscode
19 changes: 13 additions & 6 deletions cmd/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ Usage:
The flags are:
-exclude-dirs string
Subdirectories to exclude when searching for configuration files
-reporter string
Format of printed report. Currently supports standard, JSON.
Subdirectories to exclude when searching for configuration files.
-reporter string
Format of printed report. Currently supports standard, JSON.
-exclude-file-types string
A comma separated list of file types to ignore.
*/

package main
Expand All @@ -31,9 +33,10 @@ import (
)

type validatorConfig struct {
searchPath string
excludeDirs *string
reportType *string
searchPath string
excludeDirs *string
excludeFileTypes *string
reportType *string
}

// Custom Usage function to cover
Expand All @@ -56,6 +59,7 @@ func getFlags() (validatorConfig, error) {
flag.Usage = validatorUsage
excludeDirsPtr := flag.String("exclude-dirs", "", "Subdirectories to exclude when searching for configuration files")
reportTypePtr := flag.String("reporter", "standard", "Format of the printed report. Options are standard and json")
excludeFileTypesPtr := flag.String("exclude-file-types", "", "A comma separated list of file types to ignore")
flag.Parse()

var searchPath string
Expand All @@ -79,6 +83,7 @@ func getFlags() (validatorConfig, error) {
config := validatorConfig{
searchPath,
excludeDirsPtr,
excludeFileTypesPtr,
reportTypePtr,
}

Expand Down Expand Up @@ -107,11 +112,13 @@ func mainInit() int {
// it needs to be split into a slice of strings
excludeDirs := strings.Split(*validatorConfig.excludeDirs, ",")
reporter := getReporter(validatorConfig.reportType)
excludeFileTypes := strings.Split(*validatorConfig.excludeFileTypes, ",")

// Initialize a file system finder
fileSystemFinder := finder.FileSystemFinderInit(
finder.WithPathRoot(searchPath),
finder.WithExcludeDirs(excludeDirs),
finder.WithExcludeFileTypes(excludeFileTypes),
)

// Initialize the CLI
Expand Down
7 changes: 4 additions & 3 deletions cmd/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ func Test_flags(t *testing.T) {
Args []string
ExpectedExit int
}{
{"flags set", []string{"--exclude-dirs=subdir", "--reporter=json", "."}, 0},
{"flags set", []string{"--exclude-dirs=subdir", "--reporter=junit", "."}, 1},
{"flags set", []string{"/path/does/not/exit"}, 1},
{"blank", []string{}, 0},
{"flags set, json reporter", []string{"--exclude-dirs=subdir", "--reporter=json", "."}, 0},
{"flags set, junit reported", []string{"--exclude-dirs=subdir", "--reporter=junit", "."}, 1},
{"bad path", []string{"/path/does/not/exit"}, 1},
{"exclude file types set", []string{"--exclude-file-types=json", "."}, 0},
}
for _, tc := range cases {
// this call is required because otherwise flags panics,
Expand Down
22 changes: 21 additions & 1 deletion pkg/finder/finder_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package finder

import (
"fmt"
"testing"

"github.com/Boeing/config-file-validator/pkg/filetype"
"github.com/Boeing/config-file-validator/pkg/validator"
"testing"
)

func Test_fsFinder(t *testing.T) {
Expand Down Expand Up @@ -40,6 +42,24 @@ func Test_fsFinderExcludeDirs(t *testing.T) {
}
}

func Test_fsFinderExcludeFileTypes(t *testing.T) {
fsFinder := FileSystemFinderInit(
WithPathRoot("../../test/fixtures/exclude-file-types"),
WithExcludeFileTypes([]string{"json"}),
)

files, err := fsFinder.Find()

if len(files) != 1 {
fmt.Println(files)
t.Errorf("Wrong amount of files, expected 1 got %d", len(files))
}

if err != nil {
t.Errorf("Unable to find files")
}
}

func Test_fsFinderCustomTypes(t *testing.T) {
jsonFileType := filetype.FileType{
Name: "json",
Expand Down
42 changes: 33 additions & 9 deletions pkg/finder/fsfinder.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package finder

import (
"github.com/Boeing/config-file-validator/pkg/filetype"
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/Boeing/config-file-validator/pkg/filetype"
)

type FileSystemFinder struct {
PathRoot string
FileTypes []filetype.FileType
ExcludeDirs []string
PathRoot string
FileTypes []filetype.FileType
ExcludeDirs []string
ExcludeFileTypes []string
}

type FSFinderOptions func(*FileSystemFinder)
Expand All @@ -36,6 +39,13 @@ func WithExcludeDirs(excludeDirs []string) FSFinderOptions {
}
}

// WithExcludeFileTypes adds excluded file types to FSFinder.
func WithExcludeFileTypes(types []string) FSFinderOptions {
return func(fsf *FileSystemFinder) {
fsf.ExcludeFileTypes = types
}
}

func FileSystemFinderInit(opts ...FSFinderOptions) *FileSystemFinder {
var defaultExludeDirs []string
defaultPathRoot := "."
Expand Down Expand Up @@ -78,14 +88,16 @@ func (fsf FileSystemFinder) Find() ([]FileMetadata, error) {
}

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

for _, fileType := range fsf.FileTypes {
for _, extension := range fileType.Extensions {
// filepath.Ext() returns the extension name with a dot
// so it needs to be prepended to the FileType extension
// in order to match
if ("." + extension) == walkFileExtension {
if extension == walkFileExtension {
fileMetadata := FileMetadata{dirEntry.Name(), path, fileType}
matchingFiles = append(matchingFiles, fileMetadata)
}
Expand All @@ -102,3 +114,15 @@ func (fsf FileSystemFinder) Find() ([]FileMetadata, error) {

return matchingFiles, nil
}

// isExtensionExcluded returns true if extension exists in exclude list.
// TODO: refactor to slices.Contains when project will be updated to go 1.21.
func (fsf FileSystemFinder) isExtensionExcluded(ext string) bool {
for _, typ := range fsf.ExcludeFileTypes {
if typ == ext {
return true
}
}

return false
}
1 change: 1 addition & 0 deletions test/fixtures/exclude-file-types/excluded.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.

0 comments on commit ed46ce0

Please sign in to comment.