Update test generate script, add tests to membackend

This commit is contained in:
Alexander Neumann 2016-01-23 17:42:26 +01:00
parent e966df3fed
commit 9423767827
6 changed files with 147 additions and 25 deletions

View File

@ -1,5 +1,5 @@
// DO NOT EDIT! // 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 package local_test
import ( import (
@ -8,13 +8,13 @@ import (
"github.com/restic/restic/backend/test" "github.com/restic/restic/backend/test"
) )
func TestCreate(t *testing.T) { test.Create(t) } func TestLocalBackendCreate(t *testing.T) { test.Create(t) }
func TestOpen(t *testing.T) { test.Open(t) } func TestLocalBackendOpen(t *testing.T) { test.Open(t) }
func TestLocation(t *testing.T) { test.Location(t) } func TestLocalBackendLocation(t *testing.T) { test.Location(t) }
func TestConfig(t *testing.T) { test.Config(t) } func TestLocalBackendConfig(t *testing.T) { test.Config(t) }
func TestGetReader(t *testing.T) { test.GetReader(t) } func TestLocalBackendGetReader(t *testing.T) { test.GetReader(t) }
func TestLoad(t *testing.T) { test.Load(t) } func TestLocalBackendLoad(t *testing.T) { test.Load(t) }
func TestWrite(t *testing.T) { test.Write(t) } func TestLocalBackendWrite(t *testing.T) { test.Write(t) }
func TestGeneric(t *testing.T) { test.Generic(t) } func TestLocalBackendGeneric(t *testing.T) { test.Generic(t) }
func TestDelete(t *testing.T) { test.Delete(t) } func TestLocalBackendDelete(t *testing.T) { test.Delete(t) }
func TestCleanup(t *testing.T) { test.Cleanup(t) } func TestLocalBackendCleanup(t *testing.T) { test.Cleanup(t) }

View File

@ -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
}
}

View File

@ -1,12 +1,20 @@
// DO NOT EDIT!
// generated at 2016-23-01 17:41:47 +0100 CET
package backend_test package backend_test
import ( import (
"testing" "testing"
"github.com/restic/restic/backend" "github.com/restic/restic/backend/test"
) )
func TestMemoryBackend(t *testing.T) { func TestMemBackendCreate(t *testing.T) { test.Create(t) }
be := backend.NewMemoryBackend() func TestMemBackendOpen(t *testing.T) { test.Open(t) }
testBackend(be, 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) }

View File

@ -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) }

View File

@ -14,12 +14,15 @@ import (
"regexp" "regexp"
"text/template" "text/template"
"time" "time"
"unicode"
"unicode/utf8"
) )
var data struct { var data struct {
Package string Package string
Funcs []string PackagePrefix string
Timestamp string Funcs []string
Timestamp string
} }
var testTemplate = ` var testTemplate = `
@ -33,12 +36,15 @@ import (
"github.com/restic/restic/backend/test" "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 }} {{ end }}
` `
var testFile = flag.String("testfile", "../test/tests.go", "file to search test functions in") 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 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) { func errx(err error) {
if err == nil { if err == nil {
@ -84,6 +90,15 @@ func generateOutput(wr io.Writer, data interface{}) {
errx(cmd.Wait()) 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() { func init() {
flag.Parse() flag.Parse()
} }
@ -95,17 +110,25 @@ func main() {
os.Exit(1) os.Exit(1)
} }
packageName := filepath.Base(dir) pkg := *packageName
if pkg == "" {
pkg = filepath.Base(dir)
}
f, err := os.Create(*outputFile) f, err := os.Create(*outputFile)
errx(err) 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.Funcs = findTestFunctions()
data.Timestamp = time.Now().Format("2006-02-01 15:04:05 -0700 MST") data.Timestamp = time.Now().Format("2006-02-01 15:04:05 -0700 MST")
generateOutput(f, data) generateOutput(f, data)
errx(f.Close()) 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)
} }

View File

@ -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
}
}