Check that all indexer packages have all the required tests

Change-Id: I3c189060bce299235828a9d57a7bb679b05adffd
This commit is contained in:
mpl 2012-03-28 10:33:20 +02:00
parent 6c6dcdc8a1
commit 95b9622271
1 changed files with 95 additions and 0 deletions

View File

@ -17,6 +17,13 @@ limitations under the License.
package index_test
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
"testing"
"camlistore.org/pkg/index"
@ -47,3 +54,91 @@ func TestPathsOfSignerTarget_Memory(t *testing.T) {
func TestFiles_Memory(t *testing.T) {
indextest.Files(t, index.ExpNewMemoryIndex)
}
var (
// those dirs are not packages implementing indexers,
// hence we do not want to check them.
excludedDirs = []string{"indextest", "testdata", "mysql"}
// A map is used in hasAllRequiredTests to note which required
// tests have been found in a package, by setting the corresponding
// booleans to true. Those are the keys for this map.
requiredTests = []string{"TestIndex_", "TestPathsOfSignerTarget_", "TestFiles_"}
)
// This function checks that all the functions using the tests
// defined in indextest, namely:
// TestIndex_, TestPathOfSignerTarget_, TestFiles_
// do exist in the provided package.
func hasAllRequiredTests(path string, t *testing.T) error {
tests := make(map[string]bool)
for _, v := range requiredTests {
tests[v] = false
}
dir, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
names, err := dir.Readdirnames(-1)
if err != nil {
t.Fatal(err)
}
defer dir.Close()
for _, name := range names {
if !strings.HasSuffix(name, "_test.go") {
continue
}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filepath.Join(path, name), nil, 0)
if err != nil {
t.Fatalf("%v: %v", filepath.Join(path, name), err)
}
ast.Inspect(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.FuncDecl:
name := x.Name.Name
for k, _ := range tests {
if strings.HasPrefix(name, k) {
tests[k] = true
}
}
}
return true
})
}
for k, v := range tests {
if !v {
return fmt.Errorf("%v not implemented in %v", k, path)
}
}
return nil
}
// For each package implementing an indexer, this checks that
// all the required tests are present in its test suite.
func TestIndexerTestsCompleteness(t *testing.T) {
cwd, err := os.Open(".")
if err != nil {
t.Fatal(err)
}
defer cwd.Close()
files, err := cwd.Readdir(-1)
if err != nil {
t.Fatal(err)
}
Files:
for _, file := range files {
if !file.IsDir() {
continue
}
for _, v := range excludedDirs {
if file.Name() == v {
continue Files
}
}
if err = hasAllRequiredTests(file.Name(), t); err != nil {
t.Fatal(err)
}
}
}