Skip to content

Commit efd0a1d

Browse files
authored
fix: the data parent directory is missing (#70)
1 parent 2884073 commit efd0a1d

File tree

7 files changed

+59
-29
lines changed

7 files changed

+59
-29
lines changed

cmd/run.go

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"path"
78
"path/filepath"
89
"strings"
910
"sync"
@@ -218,6 +219,7 @@ func (o *runOption) runSuite(suite string, dataContext map[string]interface{}, c
218219
o.limiter.Accept()
219220

220221
ctxWithTimeout, _ := context.WithTimeout(ctx, o.requestTimeout)
222+
ctxWithTimeout = context.WithValue(ctxWithTimeout, runner.ContextKey("").ParentDir(), path.Dir(suite))
221223

222224
simpleRunner := runner.NewSimpleTestCaseRunner()
223225
simpleRunner.WithTestReporter(o.reporter)

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ require (
4444
golang.org/x/text v0.8.0 // indirect
4545
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
4646
google.golang.org/protobuf v1.30.0 // indirect
47-
gopkg.in/yaml.v2 v2.3.0 // indirect
47+
gopkg.in/yaml.v2 v2.4.0 // indirect
4848
gopkg.in/yaml.v3 v3.0.1 // indirect
4949
)

go.sum

+2-5
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
4141
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
4242
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
4343
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
44-
github.com/linuxsuren/go-fake-runtime v0.0.0-20230413085645-15e77ab55dbd h1:2Avir30WOgcDqG3sA4hlW4bC4c/tgseAUntPhf5JQ6E=
45-
github.com/linuxsuren/go-fake-runtime v0.0.0-20230413085645-15e77ab55dbd/go.mod h1:zmh6J78hSnWZo68faMA2eKOdaEp8eFbERHi3ZB9xHCQ=
46-
github.com/linuxsuren/go-fake-runtime v0.0.0-20230426143116-85f55baf088f h1:QvE5qCxn6uNxIpF4dnX8O172J12+iOFBcmQ52rvhFFU=
47-
github.com/linuxsuren/go-fake-runtime v0.0.0-20230426143116-85f55baf088f/go.mod h1:zmh6J78hSnWZo68faMA2eKOdaEp8eFbERHi3ZB9xHCQ=
4844
github.com/linuxsuren/go-fake-runtime v0.0.0-20230426144714-1a7a0d160d3f h1:TfAzkLxq/agwMBbccTx/f/dlmFWIBLWRGCWjI4IOlK8=
4945
github.com/linuxsuren/go-fake-runtime v0.0.0-20230426144714-1a7a0d160d3f/go.mod h1:zmh6J78hSnWZo68faMA2eKOdaEp8eFbERHi3ZB9xHCQ=
5046
github.com/linuxsuren/unstructured v0.0.1 h1:ilUA8MUYbR6l9ebo/YPV2bKqlf62bzQursDSE+j00iU=
@@ -137,8 +133,9 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogR
137133
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
138134
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
139135
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
140-
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
141136
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
137+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
138+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
142139
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
143140
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
144141
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/runner/simple.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,27 @@ func NewSimpleTestCaseRunner() TestCaseRunner {
168168
WithExecer(fakeruntime.DefaultExecer{})
169169
}
170170

171+
// ContextKey is the alias type of string for context key
172+
type ContextKey string
173+
174+
// NewContextKeyBuilder returns an emtpy context key
175+
func NewContextKeyBuilder() ContextKey {
176+
return ContextKey("")
177+
}
178+
179+
// ParentDir returns the key of the parsent directory
180+
func (c ContextKey) ParentDir() ContextKey {
181+
return ContextKey("parentDir")
182+
}
183+
184+
// GetContextValueOrEmpty returns the value of the context key, if not exist, return empty string
185+
func (c ContextKey) GetContextValueOrEmpty(ctx context.Context) string {
186+
if ctx.Value(c) != nil {
187+
return ctx.Value(c).(string)
188+
}
189+
return ""
190+
}
191+
171192
// RunTestCase is the main entry point of a test case
172193
func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataContext interface{}, ctx context.Context) (output interface{}, err error) {
173194
r.log.Info("start to run: '%s'\n", testcase.Name)
@@ -196,7 +217,8 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
196217
},
197218
}
198219

199-
if err = testcase.Request.Render(dataContext); err != nil {
220+
contextDir := NewContextKeyBuilder().ParentDir().GetContextValueOrEmpty(ctx)
221+
if err = testcase.Request.Render(dataContext, contextDir); err != nil {
200222
return
201223
}
202224

pkg/runner/simple_test.go

+27-19
Original file line numberDiff line numberDiff line change
@@ -219,25 +219,25 @@ func TestTestCase(t *testing.T) {
219219
assert.Contains(t, err.Error(), "failed to get field")
220220
},
221221
},
222-
// {
223-
// name: "verify failed",
224-
// testCase: &atest.TestCase{
225-
// Request: atest.Request{
226-
// API: urlFoo,
227-
// },
228-
// Expect: atest.Response{
229-
// Verify: []string{
230-
// "len(data.items) > 0",
231-
// },
232-
// },
233-
// },
234-
// prepare: defaultPrepare,
235-
// verify: func(t *testing.T, output interface{}, err error) {
236-
// if assert.NotNil(t, err) {
237-
// assert.Contains(t, err.Error(), "failed to verify")
238-
// }
239-
// },
240-
// },
222+
{
223+
name: "verify failed",
224+
testCase: &atest.TestCase{
225+
Request: atest.Request{
226+
API: urlFoo,
227+
},
228+
Expect: atest.Response{
229+
Verify: []string{
230+
"len(data.items) > 0",
231+
},
232+
},
233+
},
234+
prepare: defaultPrepare,
235+
verify: func(t *testing.T, output interface{}, err error) {
236+
if assert.NotNil(t, err) {
237+
assert.Contains(t, err.Error(), "failed to verify")
238+
}
239+
},
240+
},
241241
{
242242
name: "failed to compile",
243243
testCase: &atest.TestCase{
@@ -437,6 +437,14 @@ func TestRunJob(t *testing.T) {
437437
}
438438
}
439439

440+
func TestContextKey(t *testing.T) {
441+
assert.Equal(t, ContextKey("parentDir"), NewContextKeyBuilder().ParentDir())
442+
443+
ctx := context.WithValue(context.Background(), NewContextKeyBuilder().ParentDir(), "/tmp")
444+
assert.Equal(t, "/tmp", NewContextKeyBuilder().ParentDir().GetContextValueOrEmpty(ctx))
445+
assert.Empty(t, ContextKey("fake").GetContextValueOrEmpty(ctx))
446+
}
447+
440448
const defaultSchemaForTest = `{"properties": {
441449
"name": {"type": "string"},
442450
"age": {"type": "integer"}

pkg/testing/parser.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"net/url"
1010
"os"
11+
"path"
1112
"strings"
1213

1314
"github.com/ghodss/yaml"
@@ -70,7 +71,7 @@ func ParseTestCaseFromData(data []byte) (testCase *TestCase, err error) {
7071
}
7172

7273
// Render injects the template based context
73-
func (r *Request) Render(ctx interface{}) (err error) {
74+
func (r *Request) Render(ctx interface{}, dataDir string) (err error) {
7475
// template the API
7576
var result string
7677
if result, err = render.Render("api", r.API, ctx); err == nil {
@@ -83,7 +84,7 @@ func (r *Request) Render(ctx interface{}) (err error) {
8384
// read body from file
8485
if r.BodyFromFile != "" {
8586
var data []byte
86-
if data, err = os.ReadFile(r.BodyFromFile); err != nil {
87+
if data, err = os.ReadFile(path.Join(dataDir, r.BodyFromFile)); err != nil {
8788
return
8889
}
8990
r.Body = strings.TrimSpace(string(data))

pkg/testing/parser_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func TestRequestRender(t *testing.T) {
151151
}}
152152
for _, tt := range tests {
153153
t.Run(tt.name, func(t *testing.T) {
154-
err := tt.request.Render(tt.ctx)
154+
err := tt.request.Render(tt.ctx, "")
155155
if assert.Equal(t, tt.hasErr, err != nil, err) && tt.verify != nil {
156156
tt.verify(t, tt.request)
157157
}

0 commit comments

Comments
 (0)