Skip to content

Commit

Permalink
Merge pull request #658 from k1LoW/root-trace
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW authored Oct 26, 2023
2 parents 59744ae + 19caeaa commit 4427cf3
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 4 deletions.
4 changes: 4 additions & 0 deletions book.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type book struct {
t *testing.T
included bool
force bool
trace bool
failFast bool
skipIncluded bool
openApi3DocLocations []string
Expand Down Expand Up @@ -538,6 +539,9 @@ func (bk *book) merge(loaded *book) error {
if !bk.force {
bk.force = loaded.force
}
if !bk.trace {
bk.trace = loaded.trace
}
bk.loop = loaded.loop
bk.openApi3DocLocations = loaded.openApi3DocLocations
bk.grpcNoTLS = loaded.grpcNoTLS
Expand Down
5 changes: 4 additions & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ func (rnr *dbRunner) run(ctx context.Context, q *dbQuery, s *step) error {
return err
}
// Override trace
if q.trace == nil && rnr.trace != nil && *rnr.trace {
switch {
case q.trace == nil && rnr.trace == nil:
q.trace = &o.trace
case q.trace == nil && rnr.trace != nil:
q.trace = rnr.trace
}
tc, err := q.generateTraceStmtComment(s)
Expand Down
5 changes: 4 additions & 1 deletion grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ func (rnr *grpcRunner) run(ctx context.Context, r *grpcRequest, s *step) error {
return fmt.Errorf("cannot find method: %s", key)
}
// Override trace
if r.trace == nil && rnr.trace != nil && *rnr.trace {
switch {
case r.trace == nil && rnr.trace == nil:
r.trace = &o.trace
case r.trace == nil && rnr.trace != nil:
r.trace = rnr.trace
}
if err := r.setTraceHeader(s); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,10 @@ func (rnr *httpRunner) run(ctx context.Context, r *httpRequest, s *step) error {
}

// Override trace
if r.trace == nil && rnr.trace != nil && *rnr.trace {
switch {
case r.trace == nil && rnr.trace == nil:
r.trace = &o.trace
case r.trace == nil && rnr.trace != nil:
r.trace = rnr.trace
}
if err := r.setTraceHeader(s); err != nil {
Expand Down
1 change: 1 addition & 0 deletions include.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (o *operator) newNestedOperator(parent *step, opts ...Option) (*operator, e
popts = append(popts, Profile(o.profile))
popts = append(popts, SkipTest(o.skipTest))
popts = append(popts, Force(o.force))
popts = append(popts, Trace(o.trace))
for k, f := range o.store.funcs {
popts = append(popts, Func(k, f))
}
Expand Down
2 changes: 2 additions & 0 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type operator struct {
thisT *testing.T
parent *step
force bool
trace bool
failFast bool
included bool
ifCond string
Expand Down Expand Up @@ -443,6 +444,7 @@ func New(opts ...Option) (*operator, error) {
t: bk.t,
thisT: bk.t,
force: bk.force,
trace: bk.trace,
failFast: bk.failFast,
included: bk.included,
ifCond: bk.ifCond,
Expand Down
41 changes: 41 additions & 0 deletions operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,47 @@ func TestStoreKeys(t *testing.T) {
}
}

func TestTrace(t *testing.T) {
tests := []struct {
book string
}{
{"testdata/book/http.yml"},
{"testdata/book/grpc.yml"},
{"testdata/book/db.yml"},
}
ctx := context.Background()
t.Setenv("DEBUG", "false")
for _, tt := range tests {
tt := tt
t.Run(tt.book, func(t *testing.T) {
buf := new(bytes.Buffer)
ts := testutil.HTTPServer(t)
t.Setenv("TEST_HTTP_END_POINT", ts.URL)
_, dsn := testutil.SQLite(t)
t.Setenv("TEST_DB_DSN", dsn)
tg := testutil.GRPCServer(t, false, false)
id := "1234567890"
opts := []Option{
Book(tt.book),
GrpcRunner("greq", tg.Conn()),
Capture(NewDebugger(buf)),
Trace(true),
}
o, err := New(opts...)
if err != nil {
t.Fatal(err)
}
o.id = id
if err := o.Run(ctx); err != nil {
t.Error(err)
}
if !strings.Contains(buf.String(), id) {
t.Error("no trace")
}
})
}
}

func TestLoop(t *testing.T) {
tests := []struct {
book string
Expand Down
10 changes: 10 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,16 @@ func Force(enable bool) Option {
}
}

// Trace - Trace all steps by default.
func Trace(enable bool) Option {
return func(bk *book) error {
if !bk.trace {
bk.trace = enable
}
return nil
}
}

// HTTPOpenApi3 - Set the path of OpenAPI Document for HTTP runners.
// Deprecated: Use HTTPOpenApi3s instead.
func HTTPOpenApi3(l string) Option {
Expand Down
5 changes: 5 additions & 0 deletions runbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type runbook struct {
Loop any `yaml:"loop,omitempty"`
Concurrency string `yaml:"concurrency,omitempty"`
Force bool `yaml:"force,omitempty"`
Trace bool `yaml:"trace,omitempty"`

useMap bool
stepKeys []string
Expand All @@ -66,6 +67,7 @@ type runbookMapped struct {
Loop any `yaml:"loop,omitempty"`
Concurrency string `yaml:"concurrency,omitempty"`
Force bool `yaml:"force,omitempty"`
Trace bool `yaml:"trace,omitempty"`
}

func NewRunbook(desc string) *runbook {
Expand Down Expand Up @@ -122,6 +124,7 @@ func parseRunbookMapped(b []byte, rb *runbook) error {
rb.If = m.If
rb.SkipTest = m.SkipTest
rb.Force = m.Force
rb.Trace = m.Trace

keys := map[string]struct{}{}
for _, s := range m.Steps {
Expand Down Expand Up @@ -183,6 +186,7 @@ func (rb *runbook) MarshalYAML() (any, error) {
m.If = rb.If
m.SkipTest = rb.SkipTest
m.Force = rb.Force
m.Trace = rb.Trace
ms := yaml.MapSlice{}
for i, k := range rb.stepKeys {
ms = append(ms, yaml.MapItem{
Expand Down Expand Up @@ -371,6 +375,7 @@ func (rb *runbook) toBook() (*book, error) {
bk.ifCond = rb.If
bk.skipTest = rb.SkipTest
bk.force = rb.Force
bk.trace = rb.Trace
if rb.Loop != nil {
bk.loop, err = newLoop(rb.Loop)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion runner_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestUseCookie(t *testing.T) {
}
}

func TestTrace(t *testing.T) {
func TestHTTPTrace(t *testing.T) {
c := &httpRunnerConfig{}
want := true
opt := HTTPTrace(want)
Expand Down

0 comments on commit 4427cf3

Please sign in to comment.