remove root node from view; fix tests

This commit is contained in:
Alex Goodman 2018-06-24 12:49:47 -04:00
parent 3590a7cf46
commit c9b1d3dd3c
No known key found for this signature in database
GPG Key ID: 05328C611D8A520E
6 changed files with 99 additions and 111 deletions

View File

@ -6,7 +6,7 @@ import (
func TestAssignDiffType(t *testing.T) {
tree := NewFileTree()
node, err := tree.AddPath("/usr", BlankFileChangeInfo("/usr"))
node, err := tree.AddPath("/usr", *BlankFileChangeInfo("/usr"))
if err != nil {
t.Errorf("Expected no error from fetching path. got: %v", err)
}

View File

@ -12,7 +12,7 @@ import (
)
const (
AttributeFormat = "%s%s %10s %10s "
AttributeFormat = "%7s%1s %10s %10s "
)
type FileNode struct {

View File

@ -12,14 +12,14 @@ func TestAddChild(t *testing.T) {
Path: "stufffffs",
}
one := tree.Root.AddChild("first node!", &payload)
one := tree.Root.AddChild("first node!", payload)
two := tree.Root.AddChild("nil node!", nil)
two := tree.Root.AddChild("nil node!", FileInfo{})
tree.Root.AddChild("third node!", nil)
two.AddChild("forth, one level down...", nil)
two.AddChild("fifth, one level down...", nil)
two.AddChild("fifth, one level down...", nil)
tree.Root.AddChild("third node!", FileInfo{})
two.AddChild("forth, one level down...", FileInfo{})
two.AddChild("fifth, one level down...", FileInfo{})
two.AddChild("fifth, one level down...", FileInfo{})
expected, actual = 5, tree.Size
if expected != actual {
@ -36,17 +36,14 @@ func TestAddChild(t *testing.T) {
t.Errorf("Expected 'twos' number of children to be %d got %d.", expected, actual)
}
expectedFC := &FileInfo{
expectedFC := FileInfo{
Path: "stufffffs",
}
actualFC := one.Data.FileInfo
if *expectedFC != *actualFC {
if expectedFC.Path != actualFC.Path {
t.Errorf("Expected 'ones' payload to be %+v got %+v.", expectedFC, actualFC)
}
if two.Data.FileInfo != nil {
t.Errorf("Expected 'twos' payload to be nil got %+v.", two.Data.FileInfo)
}
}
@ -54,11 +51,11 @@ func TestRemoveChild(t *testing.T) {
var expected, actual int
tree := NewFileTree()
tree.Root.AddChild("first", nil)
two := tree.Root.AddChild("nil", nil)
tree.Root.AddChild("third", nil)
forth := two.AddChild("forth", nil)
two.AddChild("fifth", nil)
tree.Root.AddChild("first", FileInfo{})
two := tree.Root.AddChild("nil", FileInfo{})
tree.Root.AddChild("third", FileInfo{})
forth := two.AddChild("forth", FileInfo{})
two.AddChild("fifth", FileInfo{})
forth.Remove()
@ -87,7 +84,7 @@ func TestRemoveChild(t *testing.T) {
func TestPath(t *testing.T) {
expected := "/etc/nginx/nginx.conf"
tree := NewFileTree()
node, _ := tree.AddPath(expected, nil)
node, _ := tree.AddPath(expected, FileInfo{})
actual := node.Path()
if expected != actual {
@ -97,8 +94,8 @@ func TestPath(t *testing.T) {
func TestIsWhiteout(t *testing.T) {
tree1 := NewFileTree()
p1, _ := tree1.AddPath("/etc/nginx/public1", nil)
p2, _ := tree1.AddPath("/etc/nginx/.wh.public2", nil)
p1, _ := tree1.AddPath("/etc/nginx/public1", FileInfo{})
p2, _ := tree1.AddPath("/etc/nginx/.wh.public2", FileInfo{})
if p1.IsWhiteout() != false {
t.Errorf("Expected Path '%s' to **not** be a whiteout file", p1.Name)
@ -111,15 +108,15 @@ func TestIsWhiteout(t *testing.T) {
func TestDiffTypeFromAddedChildren(t *testing.T) {
tree := NewFileTree()
node, _ := tree.AddPath("/usr", BlankFileChangeInfo("/usr"))
node, _ := tree.AddPath("/usr", *BlankFileChangeInfo("/usr"))
node.Data.DiffType = Unchanged
info1 := BlankFileChangeInfo("/usr/bin")
node, _ = tree.AddPath("/usr/bin", info1)
node, _ = tree.AddPath("/usr/bin", *info1)
node.Data.DiffType = Added
info2 := BlankFileChangeInfo("/usr/bin2")
node, _ = tree.AddPath("/usr/bin2", info2)
node, _ = tree.AddPath("/usr/bin2", *info2)
node.Data.DiffType = Removed
tree.Root.Children["usr"].deriveDiffType(Unchanged)
@ -130,14 +127,14 @@ func TestDiffTypeFromAddedChildren(t *testing.T) {
}
func TestDiffTypeFromRemovedChildren(t *testing.T) {
tree := NewFileTree()
node, _ := tree.AddPath("/usr", BlankFileChangeInfo("/usr"))
node, _ := tree.AddPath("/usr", *BlankFileChangeInfo("/usr"))
info1 := BlankFileChangeInfo("/usr/.wh.bin")
node, _ = tree.AddPath("/usr/.wh.bin", info1)
node, _ = tree.AddPath("/usr/.wh.bin", *info1)
node.Data.DiffType = Removed
info2 := BlankFileChangeInfo("/usr/.wh.bin2")
node, _ = tree.AddPath("/usr/.wh.bin2", info2)
node, _ = tree.AddPath("/usr/.wh.bin2", *info2)
node.Data.DiffType = Removed
tree.Root.Children["usr"].deriveDiffType(Unchanged)

View File

@ -32,7 +32,7 @@ func NewFileTree() (tree *FileTree) {
return tree
}
func (tree *FileTree) String() string {
func (tree *FileTree) String(showAttributes bool) string {
var renderTreeLine func(string, []bool, bool, bool) string
var walkTree func(*FileNode, []bool, int) string
@ -73,7 +73,10 @@ func (tree *FileTree) String() string {
}
last := idx == (len(node.Children) - 1)
showCollapsed := child.Data.ViewInfo.Collapsed && len(child.Children) > 0
result += child.MetadataString() + " " + renderTreeLine(child.String(), spaces, last, showCollapsed)
if showAttributes {
result += child.MetadataString() + " "
}
result += renderTreeLine(child.String(), spaces, last, showCollapsed)
if len(child.Children) > 0 && !child.Data.ViewInfo.Collapsed {
spacesChild := append(spaces, last)
result += walkTree(child, spacesChild, depth+1)
@ -82,7 +85,7 @@ func (tree *FileTree) String() string {
return result
}
return "." + newLine + walkTree(tree.Root, []bool{}, 0)
return walkTree(tree.Root, []bool{}, 0)
}
func (tree *FileTree) Copy() *FileTree {

View File

@ -15,9 +15,6 @@ func stringInSlice(a string, list []string) bool {
}
func AssertDiffType(node *FileNode, expectedDiffType DiffType) error {
if node.Data.FileInfo == nil {
return fmt.Errorf("expected *FileInfo but got nil at Path %s", node.Path())
}
if node.Data.DiffType != expectedDiffType {
return fmt.Errorf("Expecting node at %s to have DiffType %v, but had %v", node.Path(), expectedDiffType, node.Data.DiffType)
}
@ -26,18 +23,18 @@ func AssertDiffType(node *FileNode, expectedDiffType DiffType) error {
func TestPrintTree(t *testing.T) {
tree := NewFileTree()
tree.Root.AddChild("first node!", nil)
two := tree.Root.AddChild("second node!", nil)
tree.Root.AddChild("third node!", nil)
two.AddChild("forth, one level down...", nil)
tree.Root.AddChild("first node!", FileInfo{})
two := tree.Root.AddChild("second node!", FileInfo{})
tree.Root.AddChild("third node!", FileInfo{})
two.AddChild("forth, one level down...", FileInfo{})
expected := `.
first node!
expected :=
` first node!
second node!
forth, one level down...
third node!
`
actual := tree.String()
actual := tree.String(false)
if expected != actual {
t.Errorf("Expected tree string:\n--->%s<---\nGot:\n--->%s<---", expected, actual)
@ -47,15 +44,15 @@ func TestPrintTree(t *testing.T) {
func TestAddPath(t *testing.T) {
tree := NewFileTree()
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.AddPath("/etc/nginx/nginx.conf", FileInfo{})
tree.AddPath("/etc/nginx/public", FileInfo{})
tree.AddPath("/var/run/systemd", FileInfo{})
tree.AddPath("/var/run/bashful", FileInfo{})
tree.AddPath("/tmp", FileInfo{})
tree.AddPath("/tmp/nonsense", FileInfo{})
expected := `.
etc
expected :=
` etc
nginx
nginx.conf
public
@ -66,7 +63,7 @@ func TestAddPath(t *testing.T) {
bashful
systemd
`
actual := tree.String()
actual := tree.String(false)
if expected != actual {
t.Errorf("Expected tree string:\n--->%s<---\nGot:\n--->%s<---", expected, actual)
@ -76,18 +73,18 @@ func TestAddPath(t *testing.T) {
func TestRemovePath(t *testing.T) {
tree := NewFileTree()
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.AddPath("/etc/nginx/nginx.conf", FileInfo{})
tree.AddPath("/etc/nginx/public", FileInfo{})
tree.AddPath("/var/run/systemd", FileInfo{})
tree.AddPath("/var/run/bashful", FileInfo{})
tree.AddPath("/tmp", FileInfo{})
tree.AddPath("/tmp/nonsense", FileInfo{})
tree.RemovePath("/var/run/bashful")
tree.RemovePath("/tmp")
expected := `.
etc
expected :=
` etc
nginx
nginx.conf
public
@ -95,7 +92,7 @@ func TestRemovePath(t *testing.T) {
run
systemd
`
actual := tree.String()
actual := tree.String(false)
if expected != actual {
t.Errorf("Expected tree string:\n--->%s<---\nGot:\n--->%s<---", expected, actual)
@ -111,20 +108,20 @@ func TestStack(t *testing.T) {
tree1 := NewFileTree()
tree1.AddPath("/etc/nginx/public", nil)
tree1.AddPath(payloadKey, nil)
tree1.AddPath("/var/run/bashful", nil)
tree1.AddPath("/tmp", nil)
tree1.AddPath("/tmp/nonsense", nil)
tree1.AddPath("/etc/nginx/public", FileInfo{})
tree1.AddPath(payloadKey, FileInfo{})
tree1.AddPath("/var/run/bashful", FileInfo{})
tree1.AddPath("/tmp", FileInfo{})
tree1.AddPath("/tmp/nonsense", FileInfo{})
tree2 := NewFileTree()
// add new files
tree2.AddPath("/etc/nginx/nginx.conf", nil)
tree2.AddPath("/etc/nginx/nginx.conf", FileInfo{})
// modify current files
tree2.AddPath(payloadKey, &payloadValue)
tree2.AddPath(payloadKey, payloadValue)
// whiteout the following files
tree2.AddPath("/var/run/.wh.bashful", nil)
tree2.AddPath("/.wh.tmp", nil)
tree2.AddPath("/var/run/.wh.bashful", FileInfo{})
tree2.AddPath("/.wh.tmp", FileInfo{})
err := tree1.Stack(tree2)
@ -132,8 +129,8 @@ func TestStack(t *testing.T) {
t.Errorf("Could not stack refTrees: %v", err)
}
expected := `.
etc
expected :=
` etc
nginx
nginx.conf
public
@ -147,11 +144,11 @@ func TestStack(t *testing.T) {
t.Errorf("Expected '%s' to still exist, but it doesn't", payloadKey)
}
if *node.Data.FileInfo != payloadValue {
if node.Data.FileInfo.Path != payloadValue.Path {
t.Errorf("Expected '%s' value to be %+v but got %+v", payloadKey, payloadValue, node.Data.FileInfo)
}
actual := tree1.String()
actual := tree1.String(false)
if expected != actual {
t.Errorf("Expected tree string:\n--->%s<---\nGot:\n--->%s<---", expected, actual)
@ -161,18 +158,18 @@ func TestStack(t *testing.T) {
func TestCopy(t *testing.T) {
tree := NewFileTree()
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.AddPath("/etc/nginx/nginx.conf", FileInfo{})
tree.AddPath("/etc/nginx/public", FileInfo{})
tree.AddPath("/var/run/systemd", FileInfo{})
tree.AddPath("/var/run/bashful", FileInfo{})
tree.AddPath("/tmp", FileInfo{})
tree.AddPath("/tmp/nonsense", FileInfo{})
tree.RemovePath("/var/run/bashful")
tree.RemovePath("/tmp")
expected := `.
etc
expected :=
` etc
nginx
nginx.conf
public
@ -182,7 +179,7 @@ func TestCopy(t *testing.T) {
`
NewFileTree := tree.Copy()
actual := NewFileTree.String()
actual := NewFileTree.String(false)
if expected != actual {
t.Errorf("Expected tree string:\n--->%s<---\nGot:\n--->%s<---", expected, actual)
@ -201,18 +198,14 @@ func TestCompareWithNoChanges(t *testing.T) {
Typeflag: 1,
MD5sum: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}
lowerTree.AddPath(value, &fakeData)
upperTree.AddPath(value, &fakeData)
lowerTree.AddPath(value, fakeData)
upperTree.AddPath(value, fakeData)
}
lowerTree.Compare(upperTree)
asserter := func(n *FileNode) error {
if n.Path() == "/" {
return nil
}
if n.Data.FileInfo == nil {
t.Errorf("Expected *FileInfo but got nil")
return fmt.Errorf("expected *FileInfo but got nil")
}
if (n.Data.DiffType) != Unchanged {
t.Errorf("Expecting node at %s to have DiffType unchanged, but had %v", n.Path(), n.Data.DiffType)
}
@ -231,7 +224,7 @@ func TestCompareWithAdds(t *testing.T) {
upperPaths := [...]string{"/etc", "/etc/sudoers", "/usr", "/etc/hosts", "/usr/bin", "/usr/bin/bash"}
for _, value := range lowerPaths {
lowerTree.AddPath(value, &FileInfo{
lowerTree.AddPath(value, FileInfo{
Path: value,
Typeflag: 1,
MD5sum: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
@ -239,7 +232,7 @@ func TestCompareWithAdds(t *testing.T) {
}
for _, value := range upperPaths {
upperTree.AddPath(value, &FileInfo{
upperTree.AddPath(value, FileInfo{
Path: value,
Typeflag: 1,
MD5sum: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
@ -291,12 +284,12 @@ func TestCompareWithChanges(t *testing.T) {
paths := [...]string{"/etc", "/usr", "/etc/hosts", "/etc/sudoers", "/usr/bin"}
for _, value := range paths {
lowerTree.AddPath(value, &FileInfo{
lowerTree.AddPath(value, FileInfo{
Path: value,
Typeflag: 1,
MD5sum: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
})
upperTree.AddPath(value, &FileInfo{
upperTree.AddPath(value, FileInfo{
Path: value,
Typeflag: 1,
MD5sum: [16]byte{1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0},
@ -348,7 +341,7 @@ func TestCompareWithRemoves(t *testing.T) {
Typeflag: 1,
MD5sum: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}
lowerTree.AddPath(value, &fakeData)
lowerTree.AddPath(value, fakeData)
}
for _, value := range upperPaths {
@ -357,7 +350,7 @@ func TestCompareWithRemoves(t *testing.T) {
Typeflag: 1,
MD5sum: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}
upperTree.AddPath(value, &fakeData)
upperTree.AddPath(value, fakeData)
}
lowerTree.Compare(upperTree)
@ -397,12 +390,12 @@ func TestCompareWithRemoves(t *testing.T) {
func TestStackRange(t *testing.T) {
tree := NewFileTree()
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.AddPath("/etc/nginx/nginx.conf", FileInfo{})
tree.AddPath("/etc/nginx/public", FileInfo{})
tree.AddPath("/var/run/systemd", FileInfo{})
tree.AddPath("/var/run/bashful", FileInfo{})
tree.AddPath("/tmp", FileInfo{})
tree.AddPath("/tmp/nonsense", FileInfo{})
tree.RemovePath("/var/run/bashful")
tree.RemovePath("/tmp")
@ -418,7 +411,7 @@ func TestStackRange(t *testing.T) {
Typeflag: 1,
MD5sum: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}
lowerTree.AddPath(value, &fakeData)
lowerTree.AddPath(value, fakeData)
}
for _, value := range upperPaths {
@ -427,7 +420,7 @@ func TestStackRange(t *testing.T) {
Typeflag: 1,
MD5sum: [16]byte{1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0},
}
upperTree.AddPath(value, &fakeData)
upperTree.AddPath(value, fakeData)
}
trees := []*FileTree{lowerTree, upperTree, tree}
StackRange(trees, 2)
@ -445,7 +438,7 @@ func TestRemoveOnIterate(t *testing.T) {
Typeflag: 1,
MD5sum: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}
node, err := tree.AddPath(value, &fakeData)
node, err := tree.AddPath(value, fakeData)
if err == nil && stringInSlice(node.Path(), []string{"/etc"}) {
node.Data.ViewInfo.Hidden = true
}
@ -458,12 +451,12 @@ func TestRemoveOnIterate(t *testing.T) {
return nil
}, nil)
expected := `.
usr
expected :=
` usr
bin
something
`
actual := tree.String()
actual := tree.String(false)
if expected != actual {
t.Errorf("Expected tree string:\n--->%s<---\nGot:\n--->%s<---", expected, actual)
}

View File

@ -136,16 +136,11 @@ func (view *FileTreeView) getAbsPositionNode() (node *filetree.FileNode) {
var evaluator func(*filetree.FileNode) bool
var dfsCounter int
// special case: the root node is never visited
if view.TreeIndex == 0 {
return view.ModelTree.Root
}
visiter = func(curNode *filetree.FileNode) error {
dfsCounter++
if dfsCounter == view.TreeIndex {
node = curNode
}
dfsCounter++
return nil
}
@ -205,7 +200,7 @@ func (view *FileTreeView) KeyHelp() string {
func (view *FileTreeView) Render() error {
// print the tree to the view
lines := strings.Split(view.ViewTree.String(), "\n")
lines := strings.Split(view.ViewTree.String(true), "\n")
view.gui.Update(func(g *gocui.Gui) error {
view.view.Clear()
for idx, line := range lines {