-
Notifications
You must be signed in to change notification settings - Fork 0
/
collections_test.go
83 lines (76 loc) · 2.61 KB
/
collections_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package thuder
import (
"fmt"
"os"
"path/filepath"
"testing"
)
func acceptAll(n *Node) bool {
return true
}
func TestGetAppliedTo(t *testing.T) {
cw, _ := filepath.Abs(".")
rootN, err := NewRootNode(cw, false)
if err != nil {
t.Fatal(err)
}
c := NewCollection(acceptAll)
err = c.Add(rootN)
if err != nil {
t.Fatal(err)
}
t.Run(fmt.Sprintf("test apply to self should be no-op, except for child dirs"), func(t *testing.T) {
deletes, changedfiles, _, err := c.GetAppliedTo(cw)
if err != nil {
t.Fatal(err)
}
if len(deletes)+len(changedfiles) != 0 {
t.Fatal(deletes, changedfiles, "should all be empty for no-op")
}
})
for _, n := range c.nodes {
n[0].fc.isDelete = true
break // fc is shared, so it only need to set once
}
t.Run(fmt.Sprintf("test apply of deletes"), func(t *testing.T) {
deletes, changedfiles, dirs, err := c.GetAppliedTo(cw)
if err != nil {
t.Fatal(err)
}
if len(changedfiles) != 0 {
t.Fatal(changedfiles, "should be empty for no-op")
}
if len(deletes)+len(dirs) != len(c.nodes) {
t.Fatal("all should be deletes or dirs", deletes, dirs, c)
}
})
c = NewCollection(acceptAll)
c.AddList(&FileContext{from: "/overwritten"}, []os.FileInfo{&testFileInfo{name: "AddFile"}})
c.AddList(&FileContext{from: "/overwritten"}, []os.FileInfo{&testFileInfo{name: "AddDir"}})
c.AddList(&FileContext{from: "/other"}, []os.FileInfo{&testFileInfo{name: "AddFile"}})
c.AddList(&FileContext{from: "/other"}, []os.FileInfo{&testFileInfo{name: "AddDir", dir: true}})
c.AddList(&FileContext{isDelete: true}, []os.FileInfo{&testFileInfo{name: "DeleteMe"}})
c.AddList(&FileContext{isDelete: true}, []os.FileInfo{&testFileInfo{name: "deleteme"}}) //double delete, to be ignored
c.AddList(&FileContext{}, []os.FileInfo{&testFileInfo{name: "AddDir", dir: true}})
c.AddList(&FileContext{isDelete: true}, []os.FileInfo{&testFileInfo{name: "AddDir2", dir: true}})
c.AddList(&FileContext{isDelete: true}, []os.FileInfo{&testFileInfo{name: "addFILE"}}) //delete of an added, to be ignored
c.AddList(&FileContext{}, []os.FileInfo{&testFileInfo{name: "AddFile"}})
c.AddList(&FileContext{}, []os.FileInfo{&testFileInfo{name: "addfile"}})
c.AddList(&FileContext{isDelete: true}, []os.FileInfo{&testFileInfo{name: "addFILE"}}) //delete of an added, is ignored
c.PrintTo(t.Logf)
t.Run(fmt.Sprintf("test apply of a sample collection"), func(t *testing.T) {
deletes, changedfiles, dirs, err := c.GetAppliedTo(cw)
if err != nil {
t.Fatal(err)
}
if len(deletes) != 0 {
t.Fatal(deletes)
}
if len(changedfiles) != 2 {
t.Fatal(changedfiles)
}
if len(dirs) != 2 {
t.Fatal(dirs)
}
})
}