Merge pull request #102 from sgotti/fix_path_list_ending_slash

util: Fix PathList output when path ends with slashes
This commit is contained in:
Simone Gotti 2019-09-09 16:12:08 +02:00 committed by GitHub
commit 30192d5f0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 1 deletions

View File

@ -42,6 +42,7 @@ func PathHierarchy(p string) []string {
// I.E. for a path like "path/to/file" or "/path/to/file" it'll return a slice of these elements:
// "path", "to", "file"
func PathList(p string) []string {
p = path.Clean(p)
paths := []string{}
for {
paths = append([]string{path.Base(p)}, paths...)

View File

@ -14,7 +14,39 @@
package util
import "testing"
import (
"reflect"
"testing"
)
func TestPathList(t *testing.T) {
tests := []struct {
in string
out []string
}{
{"/path", []string{"path"}},
{"path", []string{"path"}},
{"/path/", []string{"path"}},
{"path/", []string{"path"}},
{"//path/", []string{"path"}},
{"///path///", []string{"path"}},
{"/path/to/file", []string{"path", "to", "file"}},
{"path/to/file", []string{"path", "to", "file"}},
{"/path/to/file/", []string{"path", "to", "file"}},
{"path/to/file/", []string{"path", "to", "file"}},
{"path/to/file///", []string{"path", "to", "file"}},
{"///path///to///file///", []string{"path", "to", "file"}},
}
for _, tt := range tests {
t.Run("test is parent path", func(t *testing.T) {
out := PathList(tt.in)
if !reflect.DeepEqual(out, tt.out) {
t.Errorf("got %q but wanted: %q", out, tt.out)
}
})
}
}
func TestIsParentPath(t *testing.T) {
tests := []struct {