add test for formatting/rejecting relative path in filetree
This commit is contained in:
parent
ed57565910
commit
17742e7b6c
@ -2,6 +2,7 @@ package filetree
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -240,8 +241,12 @@ func (tree *FileTree) GetNode(path string) (*FileNode, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AddPath adds a new node to the tree with the given payload
|
// AddPath adds a new node to the tree with the given payload
|
||||||
func (tree *FileTree) AddPath(path string, data FileInfo) (*FileNode, []*FileNode, error) {
|
func (tree *FileTree) AddPath(filepath string, data FileInfo) (*FileNode, []*FileNode, error) {
|
||||||
nodeNames := strings.Split(strings.Trim(path, "/"), "/")
|
filepath = path.Clean(filepath)
|
||||||
|
if filepath == "." {
|
||||||
|
return nil, nil, fmt.Errorf("cannot add relative path '%s'", filepath)
|
||||||
|
}
|
||||||
|
nodeNames := strings.Split(strings.Trim(filepath, "/"), "/")
|
||||||
node := tree.Root
|
node := tree.Root
|
||||||
addedNodes := make([]*FileNode, 0)
|
addedNodes := make([]*FileNode, 0)
|
||||||
for idx, name := range nodeNames {
|
for idx, name := range nodeNames {
|
||||||
@ -264,7 +269,7 @@ func (tree *FileTree) AddPath(path string, data FileInfo) (*FileNode, []*FileNod
|
|||||||
|
|
||||||
if node == nil {
|
if node == nil {
|
||||||
// the child could not be added
|
// the child could not be added
|
||||||
return node, addedNodes, fmt.Errorf(fmt.Sprintf("could not add child node: '%s' (path:'%s')", name, path))
|
return node, addedNodes, fmt.Errorf(fmt.Sprintf("could not add child node: '%s' (path:'%s')", name, filepath))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,6 +125,40 @@ func TestStringBetween(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRejectPurelyRelativePath(t *testing.T) {
|
||||||
|
tree := NewFileTree()
|
||||||
|
_, _, err := tree.AddPath("./etc/nginx/nginx.conf", FileInfo{})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not setup test: %v", err)
|
||||||
|
}
|
||||||
|
_, _, err = tree.AddPath("./", FileInfo{})
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected to reject relative path, but did not")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddRelativePath(t *testing.T) {
|
||||||
|
tree := NewFileTree()
|
||||||
|
_, _, err := tree.AddPath("./etc/nginx/nginx.conf", FileInfo{})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not setup test: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected :=
|
||||||
|
`└── etc
|
||||||
|
└── nginx
|
||||||
|
└── nginx.conf
|
||||||
|
`
|
||||||
|
actual := tree.String(false)
|
||||||
|
|
||||||
|
if expected != actual {
|
||||||
|
t.Errorf("Expected tree string:\n--->%s<---\nGot:\n--->%s<---", expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestAddPath(t *testing.T) {
|
func TestAddPath(t *testing.T) {
|
||||||
tree := NewFileTree()
|
tree := NewFileTree()
|
||||||
_, _, err := tree.AddPath("/etc/nginx/nginx.conf", FileInfo{})
|
_, _, err := tree.AddPath("/etc/nginx/nginx.conf", FileInfo{})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user