2016-03-10 21:15:55 +00:00
|
|
|
package mtree
|
|
|
|
|
|
|
|
import (
|
2016-04-05 21:16:44 +00:00
|
|
|
"io/ioutil"
|
2016-03-10 21:15:55 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-04-05 21:16:44 +00:00
|
|
|
var (
|
2017-12-13 19:20:29 +00:00
|
|
|
testFiles = []struct {
|
|
|
|
Name string
|
|
|
|
Counts map[EntryType]int
|
|
|
|
Len int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "testdata/source.mtree",
|
|
|
|
Counts: map[EntryType]int{
|
|
|
|
FullType: 0,
|
|
|
|
RelativeType: 45,
|
|
|
|
CommentType: 37,
|
|
|
|
SpecialType: 7,
|
|
|
|
DotDotType: 17,
|
|
|
|
BlankType: 34,
|
|
|
|
},
|
|
|
|
Len: int64(7887),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "testdata/source.casync-mtree",
|
|
|
|
Counts: map[EntryType]int{
|
|
|
|
FullType: 744,
|
|
|
|
RelativeType: 56,
|
|
|
|
CommentType: 37,
|
|
|
|
SpecialType: 7,
|
|
|
|
DotDotType: 17,
|
|
|
|
BlankType: 34,
|
|
|
|
},
|
|
|
|
Len: int64(168439),
|
|
|
|
},
|
2016-04-05 21:16:44 +00:00
|
|
|
}
|
|
|
|
)
|
2016-03-10 21:15:55 +00:00
|
|
|
|
|
|
|
func TestParser(t *testing.T) {
|
2017-12-13 19:20:29 +00:00
|
|
|
for i, tf := range testFiles {
|
|
|
|
_ = i
|
2016-03-10 21:15:55 +00:00
|
|
|
func() {
|
2017-12-13 19:20:29 +00:00
|
|
|
fh, err := os.Open(tf.Name)
|
2016-03-10 21:15:55 +00:00
|
|
|
if err != nil {
|
2016-03-11 20:53:20 +00:00
|
|
|
t.Error(err)
|
2016-03-10 21:15:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer fh.Close()
|
|
|
|
|
2016-03-11 20:53:20 +00:00
|
|
|
dh, err := ParseSpec(fh)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2016-03-10 21:15:55 +00:00
|
|
|
}
|
2017-12-13 19:20:29 +00:00
|
|
|
/*
|
|
|
|
if i == 1 {
|
|
|
|
buf, err := xml.MarshalIndent(dh, "", " ")
|
|
|
|
if err == nil {
|
|
|
|
t.Error(string(buf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2016-04-05 21:16:44 +00:00
|
|
|
gotNums := countTypes(dh)
|
2017-12-13 19:20:29 +00:00
|
|
|
for typ, num := range tf.Counts {
|
2016-04-05 21:16:44 +00:00
|
|
|
if gNum, ok := gotNums[typ]; ok {
|
|
|
|
if num != gNum {
|
|
|
|
t.Errorf("for type %s: expected %d, got %d", typ, num, gNum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-11 20:53:20 +00:00
|
|
|
|
2016-04-05 21:16:44 +00:00
|
|
|
i, err := dh.WriteTo(ioutil.Discard)
|
2016-03-11 20:53:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2016-03-10 21:15:55 +00:00
|
|
|
}
|
2017-12-13 19:20:29 +00:00
|
|
|
if i != tf.Len {
|
|
|
|
t.Errorf("expected to write %d, but wrote %d", tf.Len, i)
|
2016-04-05 21:16:44 +00:00
|
|
|
}
|
2016-03-11 20:53:20 +00:00
|
|
|
|
2016-03-10 21:15:55 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
2016-04-05 21:16:44 +00:00
|
|
|
|
|
|
|
func countTypes(dh *DirectoryHierarchy) map[EntryType]int {
|
|
|
|
nT := map[EntryType]int{}
|
|
|
|
for i := range dh.Entries {
|
|
|
|
typ := dh.Entries[i].Type
|
|
|
|
if _, ok := nT[typ]; !ok {
|
|
|
|
nT[typ] = 1
|
|
|
|
} else {
|
|
|
|
nT[typ]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nT
|
|
|
|
}
|