2
2
mirror of https://github.com/octoleo/restic.git synced 2024-05-28 06:30:53 +00:00
restic/tree_test.go

94 lines
1.8 KiB
Go
Raw Normal View History

2014-12-05 20:45:49 +00:00
package restic_test
2014-09-23 20:39:12 +00:00
import (
2014-12-06 21:01:54 +00:00
"encoding/json"
2014-09-23 20:39:12 +00:00
"io/ioutil"
"os"
"path/filepath"
"testing"
2014-12-06 21:01:54 +00:00
"github.com/restic/restic"
2015-04-09 19:15:48 +00:00
. "github.com/restic/restic/test"
2014-09-23 20:39:12 +00:00
)
var testFiles = []struct {
name string
content []byte
}{
{"foo", []byte("bar")},
{"bar/foo2", []byte("bar2")},
{"bar/bla/blubb", []byte("This is just a test!\n")},
}
// prepareDir creates a temporary directory and returns it.
func prepareDir(t *testing.T) string {
2015-04-26 12:46:15 +00:00
tempdir, err := ioutil.TempDir(*TestTempDir, "restic-test-")
2015-04-09 19:15:48 +00:00
OK(t, err)
2014-09-23 20:39:12 +00:00
for _, test := range testFiles {
file := filepath.Join(tempdir, test.name)
dir := filepath.Dir(file)
if dir != "." {
2015-04-09 19:15:48 +00:00
OK(t, os.MkdirAll(dir, 0755))
2014-09-23 20:39:12 +00:00
}
f, err := os.Create(file)
defer func() {
2015-04-09 19:15:48 +00:00
OK(t, f.Close())
2014-09-23 20:39:12 +00:00
}()
2015-04-09 19:15:48 +00:00
OK(t, err)
2014-09-23 20:39:12 +00:00
_, err = f.Write(test.content)
2015-04-09 19:15:48 +00:00
OK(t, err)
2014-09-23 20:39:12 +00:00
}
return tempdir
}
func TestTree(t *testing.T) {
dir := prepareDir(t)
2014-09-23 20:39:12 +00:00
defer func() {
2015-04-26 12:46:15 +00:00
if *TestCleanup {
2015-04-09 19:15:48 +00:00
OK(t, os.RemoveAll(dir))
2014-09-23 20:39:12 +00:00
}
}()
}
2014-12-06 21:01:54 +00:00
var testNodes = []restic.Node{
restic.Node{Name: "normal"},
restic.Node{Name: "with backslashes \\zzz"},
restic.Node{Name: "test utf-8 föbärß"},
restic.Node{Name: "test invalid \x00\x01\x02\x03\x04"},
restic.Node{Name: "test latin1 \x75\x6d\x6c\xe4\xfc\x74\xf6\x6e\xdf\x6e\x6c\x6c"},
}
func TestNodeMarshal(t *testing.T) {
for i, n := range testNodes {
data, err := json.Marshal(&n)
2015-04-09 19:15:48 +00:00
OK(t, err)
2014-12-06 21:01:54 +00:00
var node restic.Node
err = json.Unmarshal(data, &node)
2015-04-09 19:15:48 +00:00
OK(t, err)
2014-12-06 21:01:54 +00:00
if n.Name != node.Name {
t.Fatalf("Node %d: Names are not equal, want: %q got: %q", i, n.Name, node.Name)
}
}
}
func TestNodeComparison(t *testing.T) {
fi, err := os.Lstat("tree_test.go")
2015-04-09 19:15:48 +00:00
OK(t, err)
node, err := restic.NodeFromFileInfo("foo", fi)
2015-04-09 19:15:48 +00:00
OK(t, err)
n2 := *node
2015-04-09 19:15:48 +00:00
Assert(t, node.Equals(n2), "nodes aren't equal")
n2.Size -= 1
2015-04-09 19:15:48 +00:00
Assert(t, !node.Equals(n2), "nodes are equal")
}