Merge pull request #5469 from TamerTas/master

Refactor examples_test.walkJSONFiles
This commit is contained in:
Robert Bailey 2015-03-16 08:07:05 -07:00
commit 7186b05682

View File

@ -22,6 +22,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
"testing" "testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@ -70,30 +71,27 @@ func validateObject(obj runtime.Object) (errors []error) {
} }
func walkJSONFiles(inDir string, fn func(name, path string, data []byte)) error { func walkJSONFiles(inDir string, fn func(name, path string, data []byte)) error {
err := filepath.Walk(inDir, func(path string, info os.FileInfo, err error) error { return filepath.Walk(inDir, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }
if info.IsDir() && path != inDir { if info.IsDir() && path != inDir {
return filepath.SkipDir return filepath.SkipDir
} }
name := filepath.Base(path)
ext := filepath.Ext(name) file := filepath.Base(path)
if ext != "" { if ext := filepath.Ext(file); ext == ".json" || ext == ".yaml" {
name = name[:len(name)-len(ext)]
}
if !(ext == ".json" || ext == ".yaml") {
return nil
}
glog.Infof("Testing %s", path) glog.Infof("Testing %s", path)
data, err := ioutil.ReadFile(path) data, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
return err return err
} }
name := strings.TrimSuffix(file, ext)
fn(name, path, data) fn(name, path, data)
}
return nil return nil
}) })
return err
} }
func TestExampleObjectSchemas(t *testing.T) { func TestExampleObjectSchemas(t *testing.T) {