2016-03-16 19:59:34 +00:00
|
|
|
package mtree
|
|
|
|
|
|
|
|
import (
|
2016-03-17 21:16:46 +00:00
|
|
|
"io/ioutil"
|
2016-04-06 16:45:08 +00:00
|
|
|
"os"
|
2016-03-16 19:59:34 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWalk(t *testing.T) {
|
2016-03-17 21:16:46 +00:00
|
|
|
dh, err := Walk(".", nil, append(DefaultKeywords, "sha1"))
|
2016-03-16 19:59:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-04-06 16:45:08 +00:00
|
|
|
numEntries = countTypes(dh)
|
2016-03-18 17:38:32 +00:00
|
|
|
|
2016-03-17 21:16:46 +00:00
|
|
|
fh, err := ioutil.TempFile("", "walk.")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-04-06 16:45:08 +00:00
|
|
|
defer os.Remove(fh.Name())
|
|
|
|
defer fh.Close()
|
2016-03-17 21:16:46 +00:00
|
|
|
|
|
|
|
if _, err = dh.WriteTo(fh); err != nil {
|
2016-04-06 16:45:08 +00:00
|
|
|
t.Fatal(err)
|
2016-03-17 21:16:46 +00:00
|
|
|
}
|
2016-04-06 16:45:08 +00:00
|
|
|
if _, err := fh.Seek(0, 0); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if dh, err = ParseSpec(fh); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for k, v := range countTypes(dh) {
|
|
|
|
if numEntries[k] != v {
|
|
|
|
t.Errorf("for type %s: expected %d, got %d", k, numEntries[k], v)
|
|
|
|
}
|
2016-03-16 19:59:34 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-18 03:43:02 +00:00
|
|
|
|
|
|
|
func TestWalkDirectory(t *testing.T) {
|
|
|
|
dh, err := Walk(".", []ExcludeFunc{ExcludeNonDirectories}, []Keyword{"type"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range dh.Entries {
|
|
|
|
for _, keyval := range dh.Entries[i].AllKeys() {
|
|
|
|
if dh.Entries[i].Type == FullType || dh.Entries[i].Type == RelativeType {
|
|
|
|
if keyval.Keyword() == "type" && keyval.Value() != "dir" {
|
|
|
|
t.Errorf("expected only directories, but %q is a %q", dh.Entries[i].Name, keyval.Value())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|