From 942376782772711ecca6da5c66ce2c747b410247 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 23 Jan 2016 17:42:26 +0100 Subject: [PATCH] Update test generate script, add tests to membackend --- backend/local/backend_test.go | 22 +++++++-------- backend/mem_backend_prepare_test.go | 37 +++++++++++++++++++++++++ backend/mem_backend_test.go | 18 ++++++++---- backend/test/backend_test.go | 20 ++++++++++++++ backend/test/generate_backend_tests.go | 37 ++++++++++++++++++++----- backend/test/tests_test.go | 38 ++++++++++++++++++++++++-- 6 files changed, 147 insertions(+), 25 deletions(-) create mode 100644 backend/mem_backend_prepare_test.go create mode 100644 backend/test/backend_test.go diff --git a/backend/local/backend_test.go b/backend/local/backend_test.go index 8dea61892..8f521bced 100644 --- a/backend/local/backend_test.go +++ b/backend/local/backend_test.go @@ -1,5 +1,5 @@ // DO NOT EDIT! -// generated at 2016-23-01 17:06:38 +0100 CET +// generated at 2016-23-01 17:41:09 +0100 CET package local_test import ( @@ -8,13 +8,13 @@ import ( "github.com/restic/restic/backend/test" ) -func TestCreate(t *testing.T) { test.Create(t) } -func TestOpen(t *testing.T) { test.Open(t) } -func TestLocation(t *testing.T) { test.Location(t) } -func TestConfig(t *testing.T) { test.Config(t) } -func TestGetReader(t *testing.T) { test.GetReader(t) } -func TestLoad(t *testing.T) { test.Load(t) } -func TestWrite(t *testing.T) { test.Write(t) } -func TestGeneric(t *testing.T) { test.Generic(t) } -func TestDelete(t *testing.T) { test.Delete(t) } -func TestCleanup(t *testing.T) { test.Cleanup(t) } +func TestLocalBackendCreate(t *testing.T) { test.Create(t) } +func TestLocalBackendOpen(t *testing.T) { test.Open(t) } +func TestLocalBackendLocation(t *testing.T) { test.Location(t) } +func TestLocalBackendConfig(t *testing.T) { test.Config(t) } +func TestLocalBackendGetReader(t *testing.T) { test.GetReader(t) } +func TestLocalBackendLoad(t *testing.T) { test.Load(t) } +func TestLocalBackendWrite(t *testing.T) { test.Write(t) } +func TestLocalBackendGeneric(t *testing.T) { test.Generic(t) } +func TestLocalBackendDelete(t *testing.T) { test.Delete(t) } +func TestLocalBackendCleanup(t *testing.T) { test.Cleanup(t) } diff --git a/backend/mem_backend_prepare_test.go b/backend/mem_backend_prepare_test.go new file mode 100644 index 000000000..8ddeaa72c --- /dev/null +++ b/backend/mem_backend_prepare_test.go @@ -0,0 +1,37 @@ +package backend_test + +import ( + "errors" + + "github.com/restic/restic/backend" + "github.com/restic/restic/backend/test" +) + +var be backend.Backend + +//go:generate go run test/generate_backend_tests.go -testfile test/tests.go -output mem_backend_test.go -prefix MemBackend + +func init() { + test.CreateFn = func() (backend.Backend, error) { + if be != nil { + return nil, errors.New("temporary memory backend dir already exists") + } + + be = backend.NewMemoryBackend() + + return be, nil + } + + test.OpenFn = func() (backend.Backend, error) { + if be == nil { + return nil, errors.New("repository not initialized") + } + + return be, nil + } + + test.CleanupFn = func() error { + be = nil + return nil + } +} diff --git a/backend/mem_backend_test.go b/backend/mem_backend_test.go index c5b43415c..83e869990 100644 --- a/backend/mem_backend_test.go +++ b/backend/mem_backend_test.go @@ -1,12 +1,20 @@ +// DO NOT EDIT! +// generated at 2016-23-01 17:41:47 +0100 CET package backend_test import ( "testing" - "github.com/restic/restic/backend" + "github.com/restic/restic/backend/test" ) -func TestMemoryBackend(t *testing.T) { - be := backend.NewMemoryBackend() - testBackend(be, t) -} +func TestMemBackendCreate(t *testing.T) { test.Create(t) } +func TestMemBackendOpen(t *testing.T) { test.Open(t) } +func TestMemBackendLocation(t *testing.T) { test.Location(t) } +func TestMemBackendConfig(t *testing.T) { test.Config(t) } +func TestMemBackendGetReader(t *testing.T) { test.GetReader(t) } +func TestMemBackendLoad(t *testing.T) { test.Load(t) } +func TestMemBackendWrite(t *testing.T) { test.Write(t) } +func TestMemBackendGeneric(t *testing.T) { test.Generic(t) } +func TestMemBackendDelete(t *testing.T) { test.Delete(t) } +func TestMemBackendCleanup(t *testing.T) { test.Cleanup(t) } diff --git a/backend/test/backend_test.go b/backend/test/backend_test.go new file mode 100644 index 000000000..4c11a5ac3 --- /dev/null +++ b/backend/test/backend_test.go @@ -0,0 +1,20 @@ +// DO NOT EDIT! +// generated at 2016-23-01 17:41:27 +0100 CET +package test_test + +import ( + "testing" + + "github.com/restic/restic/backend/test" +) + +func TestTestBackendCreate(t *testing.T) { test.Create(t) } +func TestTestBackendOpen(t *testing.T) { test.Open(t) } +func TestTestBackendLocation(t *testing.T) { test.Location(t) } +func TestTestBackendConfig(t *testing.T) { test.Config(t) } +func TestTestBackendGetReader(t *testing.T) { test.GetReader(t) } +func TestTestBackendLoad(t *testing.T) { test.Load(t) } +func TestTestBackendWrite(t *testing.T) { test.Write(t) } +func TestTestBackendGeneric(t *testing.T) { test.Generic(t) } +func TestTestBackendDelete(t *testing.T) { test.Delete(t) } +func TestTestBackendCleanup(t *testing.T) { test.Cleanup(t) } diff --git a/backend/test/generate_backend_tests.go b/backend/test/generate_backend_tests.go index 64c89159d..2286ba2dd 100644 --- a/backend/test/generate_backend_tests.go +++ b/backend/test/generate_backend_tests.go @@ -14,12 +14,15 @@ import ( "regexp" "text/template" "time" + "unicode" + "unicode/utf8" ) var data struct { - Package string - Funcs []string - Timestamp string + Package string + PackagePrefix string + Funcs []string + Timestamp string } var testTemplate = ` @@ -33,12 +36,15 @@ import ( "github.com/restic/restic/backend/test" ) -{{ range $f := .Funcs }}func Test{{ $f }}(t *testing.T){ test.{{ $f }}(t) } +{{ $prefix := .PackagePrefix }} +{{ range $f := .Funcs }}func Test{{ $prefix }}{{ $f }}(t *testing.T){ test.{{ $f }}(t) } {{ end }} ` var testFile = flag.String("testfile", "../test/tests.go", "file to search test functions in") var outputFile = flag.String("output", "backend_test.go", "output file to write generated code to") +var packageName = flag.String("package", "", "the package name to use") +var prefix = flag.String("prefix", "", "test function prefix") func errx(err error) { if err == nil { @@ -84,6 +90,15 @@ func generateOutput(wr io.Writer, data interface{}) { errx(cmd.Wait()) } +func packageTestFunctionPrefix(pkg string) string { + if pkg == "" { + return "" + } + + r, n := utf8.DecodeRuneInString(pkg) + return string(unicode.ToUpper(r)) + pkg[n:] +} + func init() { flag.Parse() } @@ -95,17 +110,25 @@ func main() { os.Exit(1) } - packageName := filepath.Base(dir) + pkg := *packageName + if pkg == "" { + pkg = filepath.Base(dir) + } f, err := os.Create(*outputFile) errx(err) - data.Package = packageName + "_test" + data.Package = pkg + "_test" + if *prefix != "" { + data.PackagePrefix = *prefix + } else { + data.PackagePrefix = packageTestFunctionPrefix(pkg) + "Backend" + } data.Funcs = findTestFunctions() data.Timestamp = time.Now().Format("2006-02-01 15:04:05 -0700 MST") generateOutput(f, data) errx(f.Close()) - fmt.Printf("wrote backend tests for package %v\n", packageName) + fmt.Printf("wrote backend tests for package %v to %v\n", data.Package, *outputFile) } diff --git a/backend/test/tests_test.go b/backend/test/tests_test.go index fbdba9534..c901e6658 100644 --- a/backend/test/tests_test.go +++ b/backend/test/tests_test.go @@ -1,3 +1,37 @@ -package test +package test_test -// func Test +import ( + "errors" + + "github.com/restic/restic/backend" + "github.com/restic/restic/backend/test" +) + +var be backend.Backend + +//go:generate go run ../test/generate_backend_tests.go + +func init() { + test.CreateFn = func() (backend.Backend, error) { + if be != nil { + return nil, errors.New("temporary memory backend dir already exists") + } + + be = backend.NewMemoryBackend() + + return be, nil + } + + test.OpenFn = func() (backend.Backend, error) { + if be == nil { + return nil, errors.New("repository not initialized") + } + + return be, nil + } + + test.CleanupFn = func() error { + be = nil + return nil + } +}