Skip to content

Commit

Permalink
fix(paths): trim leading and trailing whitespaces for file system pat…
Browse files Browse the repository at this point in the history
…hs (#162)

* Remove Leading And Trailing Whitespaces For Paths

* Add Test For Leading And Trailing Whitespaces

* Add More Test Cases For Whitespaces In Middle of Path
  • Loading branch information
mahadzaryab1 authored Aug 15, 2024
1 parent 10a6def commit 77d6a24
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
69 changes: 69 additions & 0 deletions pkg/finder/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,75 @@ func Test_FileFinderBadPath(t *testing.T) {
}
}

func Test_FileFinderPathWithWhitespaces(t *testing.T) {
tests := []struct {
name string
path string
expectErr bool
}{
{
name: "no whitespace",
path: "../../test/fixtures/subdir",
},
{
name: "leading whitespace",
path: " ../../test/fixtures/subdir",
},
{
name: "trailing whitespace",
path: "../../test/fixtures/subdir ",
},
{
name: "leading and trailing whitespace",
path: " ../../test/fixtures/subdir ",
},
{
name: "whitespace in middle of path",
path: "../../test/ fixtures /subdir",
expectErr: true,
},
{
name: "leading whitespace + whitespace in middle of path",
path: " ../../test/ fixtures /subdir",
expectErr: true,
},
{
name: "trailing whitespace + whitespace in middle of path",
path: "../../test/ fixtures /subdir ",
expectErr: true,
},
{
name: "leading and trailing whitespace + whitespace in middle of path",
path: " ../../test/ fixtures /subdir ",
expectErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fsFinder := FileSystemFinderInit(
WithPathRoots(tt.path),
)

files, err := fsFinder.Find()

if tt.expectErr {
if err == nil {
t.Errorf("Error should be thrown for bad path")
}
} else {
if len(files) < 1 {
t.Errorf("Unable to find file")
}

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

func Benchmark_Finder(b *testing.B) {
fsFinder := FileSystemFinderInit(
WithPathRoots("../../test/fixtures/"),
Expand Down
4 changes: 3 additions & 1 deletion pkg/finder/fsfinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ 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, seen)
// remove all leading and trailing whitespace
trimmedPathRoot := strings.TrimSpace(pathRoot)
matches, err := fsf.findOne(trimmedPathRoot, seen)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 77d6a24

Please sign in to comment.