Skip to content

Commit 4374d2b

Browse files
committed
To avoid confusion, do not allow the defer marked step to refer to the store with the previous key.
1 parent 7b83818 commit 4374d2b

17 files changed

+69
-43
lines changed

Diff for: bind.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ func (rnr *bindRunner) Run(ctx context.Context, s *step, first bool) error {
2525
store := o.store.toMap()
2626
store[storeRootKeyIncluded] = o.included
2727
if first {
28-
store[storeRootKeyPrevious] = o.store.latest()
28+
if !s.deferred {
29+
store[storeRootKeyPrevious] = o.store.latest()
30+
}
2931
} else {
30-
store[storeRootKeyPrevious] = o.store.previous()
32+
if !s.deferred {
33+
store[storeRootKeyPrevious] = o.store.previous()
34+
}
3135
store[storeRootKeyCurrent] = o.store.latest()
3236
}
3337
keys := lo.Keys(cond)

Diff for: cdp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (rnr *cdpRunner) Renew() error {
8888

8989
func (rnr *cdpRunner) Run(ctx context.Context, s *step) error {
9090
o := s.parent
91-
cas, err := parseCDPActions(s.cdpActions, o.expandBeforeRecord)
91+
cas, err := parseCDPActions(s.cdpActions, s, o.expandBeforeRecord)
9292
if err != nil {
9393
return fmt.Errorf("failed to parse: %w", err)
9494
}

Diff for: db.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func normalizeDSN(dsn string) string {
8484

8585
func (rnr *dbRunner) Run(ctx context.Context, s *step) error {
8686
o := s.parent
87-
e, err := o.expandBeforeRecord(s.dbQuery)
87+
e, err := o.expandBeforeRecord(s.dbQuery, s)
8888
if err != nil {
8989
return err
9090
}

Diff for: dbg.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ func (c *completer) do(d prompt.Document) ([]prompt.Suggest, pstrings.RuneNumber
9595
// print
9696
store := c.step.parent.store.toMap()
9797
store[storeRootKeyIncluded] = c.step.parent.included
98-
store[storeRootKeyPrevious] = c.step.parent.store.latest()
98+
if !c.step.deferred {
99+
store[storeRootKeyPrevious] = c.step.parent.store.latest()
100+
}
99101
keys := storeKeys(store)
100102
for _, k := range keys {
101103
if strings.HasPrefix(k, w) {
@@ -193,7 +195,9 @@ L:
193195
}
194196
store := s.parent.store.toMapForDbg()
195197
store[storeRootKeyIncluded] = s.parent.included
196-
store[storeRootKeyPrevious] = s.parent.store.latest()
198+
if !s.deferred {
199+
store[storeRootKeyPrevious] = s.parent.store.latest()
200+
}
197201
e, err := Eval(cmd[1], store)
198202
if err != nil {
199203
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err.Error())
@@ -244,7 +248,9 @@ L:
244248
case "variables", "v":
245249
store := s.parent.store.toMapForDbg()
246250
store[storeRootKeyIncluded] = s.parent.included
247-
store[storeRootKeyPrevious] = s.parent.store.latest()
251+
if !s.deferred {
252+
store[storeRootKeyPrevious] = s.parent.store.latest()
253+
}
248254
keys := lo.Keys(store)
249255
sort.Strings(keys)
250256
for _, k := range keys {

Diff for: dump.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ func (rnr *dumpRunner) Run(ctx context.Context, s *step, first bool) error {
3434
store := o.store.toMap()
3535
store[storeRootKeyIncluded] = o.included
3636
if first {
37-
store[storeRootKeyPrevious] = o.store.latest()
37+
if !s.deferred {
38+
store[storeRootKeyPrevious] = o.store.latest()
39+
}
3840
} else {
39-
store[storeRootKeyPrevious] = o.store.previous()
41+
if !s.deferred {
42+
store[storeRootKeyPrevious] = o.store.previous()
43+
}
4044
store[storeRootKeyCurrent] = o.store.latest()
4145
}
4246
if r.out == "" {

Diff for: exec.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (rnr *execRunner) Run(ctx context.Context, s *step) error {
4848
}
4949
globalScopes.mu.RUnlock()
5050
o := s.parent
51-
e, err := o.expandBeforeRecord(s.execCommand)
51+
e, err := o.expandBeforeRecord(s.execCommand, s)
5252
if err != nil {
5353
return err
5454
}

Diff for: grpc.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (rnr *grpcRunner) Close() error {
131131

132132
func (rnr *grpcRunner) Run(ctx context.Context, s *step) error {
133133
o := s.parent
134-
req, err := parseGrpcRequest(s.grpcRequest, o.expandBeforeRecord)
134+
req, err := parseGrpcRequest(s.grpcRequest, s, o.expandBeforeRecord)
135135
if err != nil {
136136
return err
137137
}
@@ -732,7 +732,7 @@ func setHeaders(ctx context.Context, h metadata.MD) context.Context {
732732
func (rnr *grpcRunner) setMessage(req proto.Message, message map[string]any, s *step) error {
733733
o := s.parent
734734
// Lazy expand due to the possibility of computing variables between multiple messages.
735-
e, err := o.expandBeforeRecord(message)
735+
e, err := o.expandBeforeRecord(message, s)
736736
if err != nil {
737737
return err
738738
}

Diff for: http.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ func isLocalhost(domain string) (bool, error) {
374374

375375
func (rnr *httpRunner) Run(ctx context.Context, s *step) error {
376376
o := s.parent
377-
e, err := o.expandBeforeRecord(s.httpRequest)
377+
e, err := o.expandBeforeRecord(s.httpRequest, s)
378378
if err != nil {
379379
return err
380380
}

Diff for: include.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ func (rnr *includeRunner) Run(ctx context.Context, s *step) error {
7878
// Store before record
7979
store := o.store.toMap()
8080
store[storeRootKeyIncluded] = o.included
81-
store[storeRootKeyPrevious] = o.store.latest()
81+
if !s.deferred {
82+
store[storeRootKeyPrevious] = o.store.latest()
83+
}
8284

8385
nodes, err := s.expandNodes()
8486
if err != nil {
@@ -96,7 +98,7 @@ func (rnr *includeRunner) Run(ctx context.Context, s *step) error {
9698
switch ov := v.(type) {
9799
case string:
98100
var vv any
99-
vv, err = o.expandBeforeRecord(ov)
101+
vv, err = o.expandBeforeRecord(ov, s)
100102
if err != nil {
101103
return err
102104
}
@@ -106,7 +108,7 @@ func (rnr *includeRunner) Run(ctx context.Context, s *step) error {
106108
}
107109
params[k] = evv
108110
case map[string]any, []any:
109-
vv, err := o.expandBeforeRecord(ov)
111+
vv, err := o.expandBeforeRecord(ov, s)
110112
if err != nil {
111113
return err
112114
}
@@ -133,7 +135,7 @@ func (rnr *includeRunner) Run(ctx context.Context, s *step) error {
133135
switch ov := v.(type) {
134136
case string:
135137
var vv any
136-
vv, err = o.expandBeforeRecord(ov)
138+
vv, err = o.expandBeforeRecord(ov, s)
137139
if err != nil {
138140
return err
139141
}
@@ -143,7 +145,7 @@ func (rnr *includeRunner) Run(ctx context.Context, s *step) error {
143145
}
144146
oo.store.vars[k] = evv
145147
case map[string]any, []any:
146-
vv, err := o.expandBeforeRecord(ov)
148+
vv, err := o.expandBeforeRecord(ov, s)
147149
if err != nil {
148150
return err
149151
}

Diff for: operator.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (op *operator) runStep(ctx context.Context, idx int, s *step) error {
175175
op.Debugln("")
176176
}
177177
if s.ifCond != "" {
178-
tf, err := op.expandCondBeforeRecord(s.ifCond)
178+
tf, err := op.expandCondBeforeRecord(s.ifCond, s)
179179
if err != nil {
180180
return err
181181
}
@@ -345,7 +345,9 @@ func (op *operator) runStep(ctx context.Context, idx int, s *step) error {
345345
if s.loop.Until != "" {
346346
store := op.store.toMap()
347347
store[storeRootKeyIncluded] = op.included
348-
store[storeRootKeyPrevious] = op.store.previous()
348+
if !s.deferred {
349+
store[storeRootKeyPrevious] = op.store.previous()
350+
}
349351
store[storeRootKeyCurrent] = op.store.latest()
350352
tf, err := EvalWithTrace(s.loop.Until, store)
351353
if err != nil {
@@ -1180,7 +1182,7 @@ func (op *operator) runInternal(ctx context.Context) (rerr error) {
11801182

11811183
// if
11821184
if op.ifCond != "" {
1183-
tf, err := op.expandCondBeforeRecord(op.ifCond)
1185+
tf, err := op.expandCondBeforeRecord(op.ifCond, &step{})
11841186
if err != nil {
11851187
rerr = err
11861188
return
@@ -1313,18 +1315,22 @@ func (op *operator) stepName(i int) string {
13131315
}
13141316

13151317
// expandBeforeRecord - expand before the runner records the result.
1316-
func (op *operator) expandBeforeRecord(in any) (any, error) {
1318+
func (op *operator) expandBeforeRecord(in any, s *step) (any, error) {
13171319
store := op.store.toMap()
13181320
store[storeRootKeyIncluded] = op.included
1319-
store[storeRootKeyPrevious] = op.store.latest()
1321+
if !s.deferred {
1322+
store[storeRootKeyPrevious] = op.store.latest()
1323+
}
13201324
return EvalExpand(in, store)
13211325
}
13221326

13231327
// expandCondBeforeRecord - expand condition before the runner records the result.
1324-
func (op *operator) expandCondBeforeRecord(ifCond string) (bool, error) {
1328+
func (op *operator) expandCondBeforeRecord(ifCond string, s *step) (bool, error) {
13251329
store := op.store.toMap()
13261330
store[storeRootKeyIncluded] = op.included
1327-
store[storeRootKeyPrevious] = op.store.latest()
1331+
if !s.deferred {
1332+
store[storeRootKeyPrevious] = op.store.latest()
1333+
}
13281334
return EvalCond(ifCond, store)
13291335
}
13301336

Diff for: operator_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func TestExpand(t *testing.T) {
147147
o.store.steps = tt.steps
148148
o.store.vars = tt.vars
149149

150-
got, err := o.expandBeforeRecord(tt.in)
150+
got, err := o.expandBeforeRecord(tt.in, &step{})
151151
if err != nil {
152152
t.Fatal(err)
153153
}

Diff for: parse.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func parseDBQuery(v map[string]any) (*dbQuery, error) {
144144
return q, nil
145145
}
146146

147-
func parseGrpcRequest(v map[string]any, expand func(any) (any, error)) (*grpcRequest, error) {
147+
func parseGrpcRequest(v map[string]any, s *step, expand func(any, *step) (any, error)) (*grpcRequest, error) {
148148
v = trimDelimiter(v)
149149
req := &grpcRequest{
150150
headers: metadata.MD{},
@@ -157,7 +157,7 @@ func parseGrpcRequest(v map[string]any, expand func(any) (any, error)) (*grpcReq
157157
return nil, fmt.Errorf("invalid request: %s", string(part))
158158
}
159159
for k, vv := range v {
160-
pe, err := expand(k)
160+
pe, err := expand(k, s)
161161
if err != nil {
162162
return nil, err
163163
}
@@ -177,7 +177,7 @@ func parseGrpcRequest(v map[string]any, expand func(any) (any, error)) (*grpcReq
177177
}
178178
hm, ok := vvv["headers"]
179179
if ok {
180-
hme, err := expand(hm)
180+
hme, err := expand(hm, s)
181181
if err != nil {
182182
return nil, err
183183
}
@@ -204,7 +204,7 @@ func parseGrpcRequest(v map[string]any, expand func(any) (any, error)) (*grpcReq
204204
}
205205
tm, ok := vvv["timeout"]
206206
if ok {
207-
tme, err := expand(tm)
207+
tme, err := expand(tm, s)
208208
if err != nil {
209209
return nil, err
210210
}
@@ -223,7 +223,7 @@ func parseGrpcRequest(v map[string]any, expand func(any) (any, error)) (*grpcReq
223223
ms, ok := mm.(string)
224224
if ok {
225225
// Only for string, variable expansion is acceptable.
226-
mm, err = expand(ms)
226+
mm, err = expand(ms, s)
227227
if err != nil {
228228
return nil, err
229229
}
@@ -242,7 +242,7 @@ func parseGrpcRequest(v map[string]any, expand func(any) (any, error)) (*grpcReq
242242
ms, ok := mm.(string)
243243
if ok {
244244
// Only for string, variable expansion is acceptable.
245-
mm, err = expand(ms)
245+
mm, err = expand(ms, s)
246246
if err != nil {
247247
return nil, err
248248
}
@@ -255,7 +255,7 @@ func parseGrpcRequest(v map[string]any, expand func(any) (any, error)) (*grpcReq
255255
// Only for string, variable expansion is acceptable.
256256
mms, ok := mm.(string)
257257
if ok {
258-
mm, err = expand(mms)
258+
mm, err = expand(mms, s)
259259
if err != nil {
260260
return nil, err
261261
}
@@ -295,7 +295,7 @@ func parseGrpcRequest(v map[string]any, expand func(any) (any, error)) (*grpcReq
295295
return req, nil
296296
}
297297

298-
func parseCDPActions(v map[string]any, expand func(any) (any, error)) (CDPActions, error) {
298+
func parseCDPActions(v map[string]any, s *step, expand func(any, *step) (any, error)) (CDPActions, error) {
299299
v = trimDelimiter(v)
300300
cas := CDPActions{}
301301
part, err := yaml.Marshal(v)
@@ -317,7 +317,7 @@ func parseCDPActions(v map[string]any, expand func(any) (any, error)) (CDPAction
317317
ca := CDPAction{
318318
Args: map[string]any{},
319319
}
320-
v, err := expand(v)
320+
v, err := expand(v, s)
321321
if err != nil {
322322
return nil, err
323323
}
@@ -352,14 +352,14 @@ func parseCDPActions(v map[string]any, expand func(any) (any, error)) (CDPAction
352352
return cas, nil
353353
}
354354

355-
func parseSSHCommand(v map[string]any, expand func(any) (any, error)) (*sshCommand, error) {
355+
func parseSSHCommand(v map[string]any, s *step, expand func(any, *step) (any, error)) (*sshCommand, error) {
356356
var err error
357357
part, err := yaml.Marshal(v)
358358
if err != nil {
359359
return nil, err
360360
}
361361
v = trimDelimiter(v)
362-
vv, err := expand(v)
362+
vv, err := expand(v, s)
363363
if err != nil {
364364
return nil, err
365365
}

Diff for: parse_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ my.custom.server.Service/Method:
378378
if err := yaml.Unmarshal([]byte(tt.in), &v); err != nil {
379379
t.Fatal(err)
380380
}
381-
got, err := parseGrpcRequest(v, o.expandBeforeRecord)
381+
got, err := parseGrpcRequest(v, &step{}, o.expandBeforeRecord)
382382
if err != nil {
383383
if !tt.wantErr {
384384
t.Error(err)

Diff for: runner_runner.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (rnr *runnerRunner) Run(ctx context.Context, s *step) error {
2727
default:
2828
return fmt.Errorf("only one runner can be defined: %v", s.runnerDefinition)
2929
}
30-
e, err := o.expandBeforeRecord(s.runnerDefinition)
30+
e, err := o.expandBeforeRecord(s.runnerDefinition, s)
3131
if err != nil {
3232
return err
3333
}

Diff for: ssh.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func (rnr *sshRunner) Close() error {
194194

195195
func (rnr *sshRunner) Run(ctx context.Context, s *step) error {
196196
o := s.parent
197-
cmd, err := parseSSHCommand(s.sshCommand, o.expandBeforeRecord)
197+
cmd, err := parseSSHCommand(s.sshCommand, s, o.expandBeforeRecord)
198198
if err != nil {
199199
return fmt.Errorf("invalid ssh command: %w", err)
200200
}

Diff for: step.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (s *step) expandNodes() (map[string]any, error) {
6060
return s.nodes, nil
6161
}
6262
o := s.parent
63-
nodes, err := o.expandBeforeRecord(s.rawStep)
63+
nodes, err := o.expandBeforeRecord(s.rawStep, s)
6464
if err != nil {
6565
return nil, err
6666
}

Diff for: test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ func (rnr *testRunner) Run(ctx context.Context, s *step, first bool) error {
3838
store := exprtrace.EvalEnv(o.store.toMap())
3939
store[storeRootKeyIncluded] = o.included
4040
if first {
41-
store[storeRootKeyPrevious] = o.store.latest()
41+
if !s.deferred {
42+
store[storeRootKeyPrevious] = o.store.latest()
43+
}
4244
} else {
43-
store[storeRootKeyPrevious] = o.store.previous()
45+
if !s.deferred {
46+
store[storeRootKeyPrevious] = o.store.previous()
47+
}
4448
store[storeRootKeyCurrent] = o.store.latest()
4549
}
4650
if err := rnr.run(ctx, cond, store, s, first); err != nil {

0 commit comments

Comments
 (0)