1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2024-11-22 00:15:39 +00:00
go-mtree/walk_test.go
Aleksa Sarai e22043cb86
walk: implement FsEval hooks
In certain circumstances (such as the manifest generation of a
filesystem as an unprivileged user) it is important to provide hooks
that override the default os.* implementation of filesystem-related
functions.

In order to avoid merging too much code from outside projects (such as
umoci) this is implemented by providing FsEval hooks to Walk() and
Check(). This allows for users of go-mtree to modify how filesystem
checks are done, without compromising the simplicity of go-mtree's code.

Signed-off-by: Aleksa Sarai <asarai@suse.de>
2016-12-14 16:26:31 +11:00

54 lines
1.1 KiB
Go

package mtree
import (
"io/ioutil"
"os"
"testing"
)
func TestWalk(t *testing.T) {
dh, err := Walk(".", nil, append(DefaultKeywords, "sha1"), nil)
if err != nil {
t.Fatal(err)
}
numEntries = countTypes(dh)
fh, err := ioutil.TempFile("", "walk.")
if err != nil {
t.Fatal(err)
}
defer os.Remove(fh.Name())
defer fh.Close()
if _, err = dh.WriteTo(fh); err != nil {
t.Fatal(err)
}
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)
}
}
}
func TestWalkDirectory(t *testing.T) {
dh, err := Walk(".", []ExcludeFunc{ExcludeNonDirectories}, []Keyword{"type"}, nil)
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())
}
}
}
}
}