From 035d0aeb31766d05359dd2d76a88d4da248be08e Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Mon, 1 Aug 2016 21:30:46 +0200 Subject: [PATCH] Do not create duplicate content for tests --- src/restic/testing.go | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/restic/testing.go b/src/restic/testing.go index 384c45e5b..88c5615a5 100644 --- a/src/restic/testing.go +++ b/src/restic/testing.go @@ -1,6 +1,7 @@ package restic import ( + "encoding/json" "fmt" "io" "math/rand" @@ -34,10 +35,14 @@ func saveFile(t testing.TB, repo *repository.Repository, rd io.Reader) (blobs ba t.Fatalf("unable to save chunk in repo: %v", err) } - id, err := repo.SaveAndEncrypt(pack.Data, chunk.Data, nil) - if err != nil { - t.Fatalf("error saving chunk: %v", err) + id := backend.Hash(chunk.Data) + if !repo.Index().Has(id) { + _, err := repo.SaveAndEncrypt(pack.Data, chunk.Data, &id) + if err != nil { + t.Fatalf("error saving chunk: %v", err) + } } + blobs = append(blobs, id) } @@ -50,6 +55,23 @@ const ( maxNodes = 32 ) +func treeIsKnown(t testing.TB, repo *repository.Repository, tree *Tree) (bool, backend.ID) { + data, err := json.Marshal(tree) + if err != nil { + t.Fatalf("json.Marshal(tree) returned error: %v", err) + return false, backend.ID{} + } + data = append(data, '\n') + + // check if tree has been saved before + id := backend.Hash(data) + if repo.Index().Has(id) { + return true, id + } + + return false, id +} + // saveTree saves a tree of fake files in the repo and returns the ID. func saveTree(t testing.TB, repo *repository.Repository, seed int64, depth int) backend.ID { rnd := rand.NewSource(seed) @@ -88,6 +110,10 @@ func saveTree(t testing.TB, repo *repository.Repository, seed int64, depth int) tree.Nodes = append(tree.Nodes, node) } + if known, id := treeIsKnown(t, repo, &tree); known { + return id + } + id, err := repo.SaveJSON(pack.Tree, tree) if err != nil { t.Fatal(err)