-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathporcelain.go
129 lines (124 loc) · 3.92 KB
/
porcelain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
package gdt
import (
"io"
"os"
"github.com/gdt-dev/gdt/api"
gdtcontext "github.com/gdt-dev/gdt/context"
jsonfix "github.com/gdt-dev/gdt/fixture/json"
"github.com/gdt-dev/gdt/plugin"
_ "github.com/gdt-dev/gdt/plugin/exec"
"github.com/gdt-dev/gdt/scenario"
"github.com/gdt-dev/gdt/suite"
)
var (
// RegisterPlugin registers a plugin with gdt's set of known plugins.
//
// Generally only plugin authors will ever need to call this function. It is
// not required for normal use of gdt or any known plugin.
RegisterPlugin = plugin.Register
// RegisterFixture registers a named fixtures with the context
RegisterFixture = gdtcontext.RegisterFixture
// NewContext returns a new `context.Context` that can be passed to a
// `scenario.Run` or `suite.Run` pointer receivers.
NewContext = gdtcontext.New
// WithFixtures sets a context's Fixtures
WithFixtures = gdtcontext.WithFixtures
// WithDebug informs gdt to output extra debugging information. You can
// supply zero or more `io.Writer` objects to the function.
//
// If no `io.Writer` objects are supplied, gdt will output debug messages
// using the `testing.T.Log[f]()` function. This means that you will only
// get these debug messages if you call the `go test` tool with the `-v`
// option (either as `go test -v` or with `go test -v=test2json`.
//
// ```go
// func TestExample(t *testing.T) {
// require := require.New(t)
// fp := filepath.Join("testdata", "example.yaml")
// f, err := os.Open(fp)
// require.Nil(err)
//
// ctx := gdt.NewContext(gdt.WithDebug())
// s, err := scenario.FromReader(
// f,
// scenario.WithPath(fp),
// scenario.WithContext(ctx),
// )
// require.Nil(err)
// require.NotNil(s)
//
// err = s.Run(ctx, t)
// require.Nil(err)
// }
// ```
//
// If you want gdt to log extra debugging information about tests and
// assertions to a different file or collecting buffer, pass it a context
// with a debug `io.Writer`:
//
// ```go
// f := ioutil.TempFile("", "mytest*.log")
// ctx := gdt.NewContext(gdt.WithDebug(f))
// ```
//
// ```go
// var b bytes.Buffer
// w := bufio.NewWriter(&b)
// ctx := gdt.NewContext(gdt.WithDebug(w))
// ```
//
// you can then inspect the debug "log" and do whatever you'd like with it.
WithDebug = gdtcontext.WithDebug
// SetDebug sets gdt's debug logging to the supplied `io.Writer`.
//
// The `writers` parameters is optional. If no `io.Writer` objects are
// supplied, gdt will output debug messages using the `testing.T.Log[f]()`
// function. This means that you will only get these debug messages if you
// call the `go test` tool with the `-v` option (either as `go test -v` or
// with `go test -v=test2json`.
SetDebug = gdtcontext.SetDebug
// NewJSONFixture takes a string, some bytes or an io.Reader and returns a
// new api.Fixture that can have its state queried via JSONPath
NewJSONFixture = jsonfix.New
)
// From returns a new `api.Runnable` from an `io.Reader`, a string file or
// directory path, or the raw bytes of YAML content describing a scenario or
// suite.
func From(source interface{}) (api.Runnable, error) {
switch src := source.(type) {
case io.Reader:
s, err := scenario.FromReader(src)
if err != nil {
return nil, err
}
return suite.FromScenario(s), nil
case string:
f, err := os.Open(src)
if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
return nil, err
}
if fi.IsDir() {
return suite.FromDir(src)
}
s, err := scenario.FromReader(f, scenario.WithPath(src))
if err != nil {
return nil, err
}
return suite.FromScenario(s), nil
case []byte:
s, err := scenario.FromBytes(src)
if err != nil {
return nil, err
}
return suite.FromScenario(s), nil
default:
return nil, api.UnknownSourceType(source)
}
}