Remove *testing.T from IndexDeps constructor; use Fataler interface

Change-Id: I1cfcc2fa8059dca3f4e082c1dae896974d3d0e02
This commit is contained in:
Brad Fitzpatrick 2012-11-04 12:27:45 +01:00
parent 17a3faf2df
commit 933375019d
1 changed files with 29 additions and 12 deletions

View File

@ -25,6 +25,7 @@ import (
"strings"
"testing"
"time"
"log"
"camlistore.org/pkg/blobref"
"camlistore.org/pkg/index"
@ -34,6 +35,7 @@ import (
"camlistore.org/pkg/test"
)
// An IndexDeps is a helper for populating and querying an Index for tests.
type IndexDeps struct {
Index *index.Index
@ -46,7 +48,17 @@ type IndexDeps struct {
now time.Time // fake clock, nanos since epoch
t *testing.T
Fataler // optional means of failing.
}
type Fataler interface {
Fatalf(format string, args ...interface{})
}
type logFataler struct{}
func (logFataler) Fatalf(format string, args ...interface{}) {
log.Fatalf(format, args...)
}
func (id *IndexDeps) Get(key string) string {
@ -70,7 +82,7 @@ func (id *IndexDeps) uploadAndSignMap(m schema.Map) *blobref.BlobRef {
m["camliSigner"] = id.SignerBlobRef
unsigned, err := m.JSON()
if err != nil {
id.t.Fatal("uploadAndSignMap: " + err.Error())
id.Fatalf("uploadAndSignMap: " + err.Error())
}
sr := &jsonsign.SignRequest{
UnsignedJSON: unsigned,
@ -79,12 +91,12 @@ func (id *IndexDeps) uploadAndSignMap(m schema.Map) *blobref.BlobRef {
}
signed, err := sr.Sign()
if err != nil {
id.t.Fatal("problem signing: " + err.Error())
id.Fatalf("problem signing: " + err.Error())
}
tb := &test.Blob{Contents: signed}
_, err = id.Index.ReceiveBlob(tb.BlobRef(), tb.Reader())
if err != nil {
id.t.Fatalf("problem indexing blob: %v\nblob was:\n%s", err, signed)
id.Fatalf("problem indexing blob: %v\nblob was:\n%s", err, signed)
}
return tb.BlobRef()
}
@ -123,7 +135,7 @@ func (id *IndexDeps) UploadFile(fileName string, contents string) (fileRef, whol
wholeRef = cb.BlobRef()
_, err := id.Index.ReceiveBlob(wholeRef, cb.Reader())
if err != nil {
id.t.Fatal(err)
id.Fatalf("UploadFile.ReceiveBlob: %v", err)
}
m := schema.NewFileMap(fileName)
@ -134,7 +146,7 @@ func (id *IndexDeps) UploadFile(fileName string, contents string) (fileRef, whol
}})
fjson, err := m.JSON()
if err != nil {
id.t.Fatal(err)
id.Fatalf("UploadFile.JSON: %v", err)
}
fb := &test.Blob{Contents: fjson}
id.BlobSource.AddBlob(fb)
@ -170,7 +182,9 @@ func findGoPathPackage(pkg string) string {
panic(fmt.Sprintf("package %q not found in GOPATH(s) of %q", pkg, gp))
}
func NewIndexDeps(t *testing.T, index *index.Index) *IndexDeps {
// NewIndexDeps returns an IndexDeps helper for populating and working
// with the provided index for tests.
func NewIndexDeps(index *index.Index) *IndexDeps {
secretRingFile := filepath.Join(findGoPathPackage("camlistore.org"), "pkg", "jsonsign", "testdata", "test-secring.gpg")
pubKey := &test.Blob{Contents: `-----BEGIN PGP PUBLIC KEY BLOCK-----
@ -192,12 +206,12 @@ Enpn/oOOfYFa5h0AFndZd1blMvruXfdAobjVABEBAAE=
},
SignerBlobRef: pubKey.BlobRef(),
now: time.Unix(1322443956, 123456),
t: t,
Fataler: logFataler{},
}
// Add dev-camput's test key public key, keyid 26F5ABDA,
// blobref sha1-ad87ca5c78bd0ce1195c46f7c98e6025abbaf007
if g, w := id.SignerBlobRef.String(), "sha1-ad87ca5c78bd0ce1195c46f7c98e6025abbaf007"; g != w {
t.Fatalf("unexpected signer blobref; got signer = %q; want %q", g, w)
id.Fatalf("unexpected signer blobref; got signer = %q; want %q", g, w)
}
id.PublicKeyFetcher.AddBlob(pubKey)
id.Index.KeyFetcher = id.PublicKeyFetcher
@ -206,7 +220,8 @@ Enpn/oOOfYFa5h0AFndZd1blMvruXfdAobjVABEBAAE=
}
func Index(t *testing.T, initIdx func() *index.Index) {
id := NewIndexDeps(t, initIdx())
id := NewIndexDeps(initIdx())
id.Fataler = t
pn := id.NewPermanode()
t.Logf("uploaded permanode %q", pn)
br1 := id.SetAttribute(pn, "foo", "foo1")
@ -369,7 +384,8 @@ func Index(t *testing.T, initIdx func() *index.Index) {
}
func PathsOfSignerTarget(t *testing.T, initIdx func() *index.Index) {
id := NewIndexDeps(t, initIdx())
id := NewIndexDeps(initIdx())
id.Fataler = t
pn := id.NewPermanode()
t.Logf("uploaded permanode %q", pn)
@ -419,7 +435,8 @@ func PathsOfSignerTarget(t *testing.T, initIdx func() *index.Index) {
}
func Files(t *testing.T, initIdx func() *index.Index) {
id := NewIndexDeps(t, initIdx())
id := NewIndexDeps(initIdx())
id.Fataler = t
fileRef, wholeRef := id.UploadFile("foo.html", "<html>I am an html file.</html>")
t.Logf("uploaded fileref %q, wholeRef %q", fileRef, wholeRef)
id.dumpIndex(t)