Merge second: Copy trees (#3)
This commit is contained in:
parent
18bb252d10
commit
0d2b6551b3
3
tree.go
3
tree.go
@ -18,6 +18,7 @@ const (
|
|||||||
collapsedItem = "⊕ "
|
collapsedItem = "⊕ "
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type FileTree struct {
|
type FileTree struct {
|
||||||
root *Node
|
root *Node
|
||||||
size int
|
size int
|
||||||
@ -51,7 +52,9 @@ func NewNode(parent *Node, name string, data *FileChangeInfo) (node *Node) {
|
|||||||
node.data = data
|
node.data = data
|
||||||
node.children = make(map[string]*Node)
|
node.children = make(map[string]*Node)
|
||||||
node.parent = parent
|
node.parent = parent
|
||||||
|
if parent != nil {
|
||||||
node.tree = parent.tree
|
node.tree = parent.tree
|
||||||
|
}
|
||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
|
|
||||||
|
33
tree_test.go
33
tree_test.go
@ -105,7 +105,6 @@ func TestPrintTree(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestAddPath(t *testing.T) {
|
func TestAddPath(t *testing.T) {
|
||||||
tree := NewTree()
|
tree := NewTree()
|
||||||
tree.AddPath("/etc/nginx/nginx.conf", nil)
|
tree.AddPath("/etc/nginx/nginx.conf", nil)
|
||||||
@ -189,7 +188,6 @@ func TestIsWhiteout(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestStack(t *testing.T) {
|
func TestStack(t *testing.T) {
|
||||||
payloadKey := "/var/run/systemd"
|
payloadKey := "/var/run/systemd"
|
||||||
payloadValue := FileChangeInfo{
|
payloadValue := FileChangeInfo{
|
||||||
@ -245,3 +243,34 @@ func TestStack(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCopy(t *testing.T) {
|
||||||
|
tree := NewTree()
|
||||||
|
tree.AddPath("/etc/nginx/nginx.conf", nil)
|
||||||
|
tree.AddPath("/etc/nginx/public", nil)
|
||||||
|
tree.AddPath("/var/run/systemd", nil)
|
||||||
|
tree.AddPath("/var/run/bashful", nil)
|
||||||
|
tree.AddPath("/tmp", nil)
|
||||||
|
tree.AddPath("/tmp/nonsense", nil)
|
||||||
|
|
||||||
|
tree.RemovePath("/var/run/bashful")
|
||||||
|
tree.RemovePath("/tmp")
|
||||||
|
|
||||||
|
expected := `.
|
||||||
|
├── etc
|
||||||
|
│ └── nginx
|
||||||
|
│ ├── nginx.conf
|
||||||
|
│ └── public
|
||||||
|
└── var
|
||||||
|
└── run
|
||||||
|
└── systemd
|
||||||
|
`
|
||||||
|
|
||||||
|
newTree := tree.Copy()
|
||||||
|
actual := newTree.String()
|
||||||
|
|
||||||
|
if expected != actual {
|
||||||
|
t.Errorf("Expected tree string:\n--->%s<---\nGot:\n--->%s<---", expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user