Skip to content

Commit

Permalink
more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
acabarbaye committed Sep 5, 2024
1 parent 1e9f82a commit 6fe3b07
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
7 changes: 7 additions & 0 deletions utils/filesystem/filepath.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func FilePathParentsOnFilesystem(fs FS, fp string) (parents []string) {
return
}
path := elements[0]
if path == "" {
elements = elements[1:]
if len(elements) <= 1 {
return
}
path = elements[0]
}
parents = append(parents, path)
for i := 1; i < len(elements)-1; i++ {
path = strings.Join([]string{path, elements[i]}, string(fs.PathSeparator()))
Expand Down
37 changes: 33 additions & 4 deletions utils/filesystem/filepath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/ARM-software/golang-utils/utils/commonerrors"
"github.com/ARM-software/golang-utils/utils/commonerrors/errortest"
"github.com/ARM-software/golang-utils/utils/platform"
)

func TestFilepathStem(t *testing.T) {
Expand All @@ -30,24 +31,52 @@ func TestFilepathStem(t *testing.T) {
}

func TestFilepathParents(t *testing.T) {

tests := []struct {
type PathTest struct {
path string
expectedParents []string
}{
}
tests := []PathTest{
{},
{
path: " ",
expectedParents: nil,
},
{
path: "/",
expectedParents: nil,
},
{
path: ".",
expectedParents: nil,
},
{
path: "./",
expectedParents: nil,
},
{
path: "./blah",
expectedParents: []string{"."},
},
{
path: filepath.Join("a", "great", "fake", "path", "blah"),
expectedParents: []string{"a", filepath.Join("a", "great"), filepath.Join("a", "great", "fake"), filepath.Join("a", "great", "fake", "path")},
},
{
path: "/foo/bar/setup.py",
expectedParents: []string{filepath.Join(`foo`), filepath.Join(`foo`, `bar`)},
},
}

if platform.IsWindows() {
tests = append(tests, PathTest{
path: "C:/foo/bar/setup.py",
expectedParents: []string{"C:", filepath.Join(`C:`, `\foo`), filepath.Join(`C:`, `\foo`, `bar`)},
},
})
} else {
tests = append(tests, PathTest{
path: "C:/foo/bar/setup.py",
expectedParents: []string{"C:", filepath.Join(`C:`, `foo`), filepath.Join(`C:`, `foo`, `bar`)},
})
}
for _, tt := range tests {
t.Run(tt.path, func(t *testing.T) {
Expand Down

0 comments on commit 6fe3b07

Please sign in to comment.