grim/resticide

move the test loading to it's own file
develop
2015-11-05, Gary Kramlich
1972afe4841c
Parents dfddac46d37b
Children defe8693fb86
move the test loading to it's own file
  • +88 -0
    loader.go
  • +1 -1
    main.go
  • +0 -81
    test.go
  • --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/loader.go Thu Nov 05 23:17:14 2015 -0600
    @@ -0,0 +1,88 @@
    +package main
    +
    +import (
    + "encoding/json"
    + "fmt"
    + "io/ioutil"
    + "os"
    + "path/filepath"
    + "strings"
    +
    + "github.com/xeipuuv/gojsonschema"
    +)
    +
    +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) ([]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 TestData
    + 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
    +}
    --- a/main.go Thu Nov 05 23:11:11 2015 -0600
    +++ b/main.go Thu Nov 05 23:17:14 2015 -0600
    @@ -11,7 +11,7 @@
    flag.Parse()
    - tests, err := find_tests(*test_dir)
    + tests, err := LoadTests(*test_dir)
    if err != nil {
    fmt.Printf("error %s", err.Error())
    --- a/test.go Thu Nov 05 23:11:11 2015 -0600
    +++ b/test.go Thu Nov 05 23:17:14 2015 -0600
    @@ -2,13 +2,8 @@
    import (
    "bytes"
    - "encoding/json"
    - "fmt"
    "io/ioutil"
    "net/http"
    - "os"
    - "path/filepath"
    - "strings"
    "time"
    "github.com/xeipuuv/gojsonschema"
    @@ -51,82 +46,6 @@
    Duration time.Duration
    }
    -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 find_tests(path string) ([]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 TestData
    - 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
    -}
    -
    func (req *TestRequest)buildRequest() (*http.Request, error) {
    body := bytes.NewBufferString(req.Body)