grim/resticide

merging
develop
2015-11-09, Gary Kramlich
6bdbec3f6a40
merging
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/xeipuuv/gojsonschema"
)
type test_data struct {
Name string `json:"name"`
Request TestRequest `json:"request"`
Requests []TestRequest `json:"requests"`
}
func load_schema() (*gojsonschema.Schema, error) {
schema_bytes, err := Asset("assets/schema.json")
if err != nil {
return nil, err
}
schema_string := string(schema_bytes[:])
schema_loader := gojsonschema.NewStringLoader(schema_string)
return gojsonschema.NewSchema(schema_loader)
}
func LoadTests(path string, reporter *Reporter) ([]Test, error) {
schema, err := load_schema()
if err != nil {
return nil, err
}
tests := []Test{}
err = filepath.Walk(path, func(path string, _ os.FileInfo, _ error) error {
stat, err := os.Stat(path)
if os.IsNotExist(err) {
return nil
}
if !stat.Mode().IsRegular() {
return nil
}
ext := filepath.Ext(path)
if ext != ".json" {
return nil
}
fp, err := ioutil.ReadFile(path)
if err != nil {
return nil
}
var data test_data
err = json.Unmarshal(fp, &data)
if err != nil {
return nil
}
result, err := schema.Validate(gojsonschema.NewGoLoader(data))
if err != nil {
return nil
}
if result.Valid() {
var test Test
test.Name = data.Name
if data.Requests != nil {
test.Requests = data.Requests
} else {
test.Requests = []TestRequest{}
test.Requests = append(test.Requests, data.Request)
}
tests = append(tests, test)
} else {
fmt.Printf("%s failed validation\n", path)
for _, desc := range result.Errors() {
fmt.Printf(" %s\n", desc)
}
fmt.Printf(strings.Repeat("-", 4) + "\n")
}
return nil
})
return tests, err
}