Skip to content

Commit 43ded37

Browse files
committed
Fix regression in the test framework
1 parent feb2a9c commit 43ded37

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

pkg/app/app.go

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (app *App) Run(cmd string, args map[string]interface{}, opts map[string]int
5050
f = fs[0]
5151
}
5252

53-
jr, err := app.Job(nil, cmd, args, opts, f, true)
53+
jr, err := app.Job(nil, nil, cmd, args, opts, f, true)
5454
if err != nil {
5555
return nil, err
5656
}
@@ -63,14 +63,14 @@ func (app *App) Run(cmd string, args map[string]interface{}, opts map[string]int
6363
return res, nil
6464
}
6565

66-
func (app *App) run(l *EventLogger, cmd string, args map[string]interface{}, streamOutput bool) (*Result, error) {
66+
func (app *App) run(jobCtx *JobContext, l *EventLogger, cmd string, args map[string]interface{}, streamOutput bool) (*Result, error) {
6767
if l != nil {
6868
if err := l.LogRun(cmd, args); err != nil {
6969
return nil, err
7070
}
7171
}
7272

73-
jr, err := app.Job(l, cmd, args, args, nil, streamOutput)
73+
jr, err := app.Job(jobCtx, l, cmd, args, args, nil, streamOutput)
7474
if err != nil {
7575
if cmd != "" {
7676
return nil, xerrors.Errorf("job %q: %w", cmd, err)
@@ -91,7 +91,7 @@ func (app *App) run(l *EventLogger, cmd string, args map[string]interface{}, str
9191
return res, nil
9292
}
9393

94-
func (app *App) Job(l *EventLogger, cmd string, args map[string]interface{}, opts map[string]interface{}, f SetOptsFunc, streamOutput bool) (func() (*Result, error), error) {
94+
func (app *App) Job(jobCtx *JobContext, l *EventLogger, cmd string, args map[string]interface{}, opts map[string]interface{}, f SetOptsFunc, streamOutput bool) (func() (*Result, error), error) {
9595
jobByName := app.JobByName
9696

9797
j, cmdDefined := jobByName[cmd]
@@ -107,13 +107,22 @@ func (app *App) Job(l *EventLogger, cmd string, args map[string]interface{}, opt
107107
return func() (*Result, error) {
108108
cc := app.Config
109109

110+
// execMatcher is the only object that is inherited from the parent to the child jobContext
111+
var execMatcher *execMatcher
112+
113+
if jobCtx != nil {
114+
execMatcher = jobCtx.execMatcher
115+
}
116+
110117
jobCtx, err := app.createJobContext(cc, j, args, opts, f)
111118
if err != nil {
112119
app.PrintError(err)
113120

114121
return nil, err
115122
}
116123

124+
jobCtx.execMatcher = execMatcher
125+
117126
jobEvalCtx := jobCtx.evalContext
118127

119128
if l == nil {
@@ -296,33 +305,43 @@ type Command struct {
296305
Interactive bool
297306
}
298307

299-
func (app *App) execCmd(cmd Command, log bool) (*Result, error) {
308+
func (app *App) execCmd(ctx *JobContext, cmd Command, log bool) (*Result, error) {
309+
var execM *execMatcher
310+
311+
if ctx != nil {
312+
execM = ctx.execMatcher
313+
}
314+
315+
if execM == nil {
316+
execM = &execMatcher{}
317+
}
318+
300319
// If we have one ore more pending exec expectations, never run the actual command.
301320
// Instead, do validate the execCmd run against the expectation.
302-
if len(app.expectedExecs) > 0 {
303-
app.execInvocationCount++
321+
if len(execM.expectedExecs) > 0 {
322+
execM.execInvocationCount++
304323

305-
expectation := app.expectedExecs[0]
324+
expectation := execM.expectedExecs[0]
306325

307326
if cmd.Name != expectation.Command {
308-
return nil, fmt.Errorf("unexpected exec %d: expected command %q, got %q", app.execInvocationCount, expectation.Command, cmd.Name)
327+
return nil, fmt.Errorf("unexpected exec %d: expected command %q, got %q", execM.execInvocationCount, expectation.Command, cmd.Name)
309328
}
310329

311330
if diff := cmp.Diff(expectation.Args, cmd.Args); diff != "" {
312-
return nil, fmt.Errorf("unexpected exec %d: expected args %v, got %v", app.execInvocationCount, expectation.Args, cmd.Args)
331+
return nil, fmt.Errorf("unexpected exec %d: expected args %v, got %v", execM.execInvocationCount, expectation.Args, cmd.Args)
313332
}
314333

315334
if diff := cmp.Diff(expectation.Dir, cmd.Dir); diff != "" {
316-
return nil, fmt.Errorf("unexpected exec %d: expected dir %q, got %q", app.execInvocationCount, expectation.Dir, cmd.Dir)
335+
return nil, fmt.Errorf("unexpected exec %d: expected dir %q, got %q", execM.execInvocationCount, expectation.Dir, cmd.Dir)
317336
}
318337

319338
// Pop the successful command expectation so that on next execCmd call, we can
320339
// use expectedExecs[0] as the next expectation to be checked.
321-
app.expectedExecs = app.expectedExecs[1:]
340+
execM.expectedExecs = execM.expectedExecs[1:]
322341

323342
return &Result{Validated: true}, nil
324-
} else if app.execInvocationCount > 0 {
325-
return nil, fmt.Errorf("unexpected exec %d: fix the test by adding an expect block for this exec, or fix the test target: %v", app.execInvocationCount+1, cmd)
343+
} else if execM.execInvocationCount > 0 {
344+
return nil, fmt.Errorf("unexpected exec %d: fix the test by adding an expect block for this exec, or fix the test target: %v", execM.execInvocationCount+1, cmd)
326345
}
327346

328347
env := map[string]string{}
@@ -465,7 +484,7 @@ func (app *App) execJob(l *EventLogger, j JobSpec, jobCtx *JobContext, streamOut
465484
c.Interactive = true
466485
}
467486

468-
res, err = app.execCmd(c, streamOutput)
487+
res, err = app.execCmd(jobCtx, c, streamOutput)
469488
if err := l.LogExec(cmd, args); err != nil {
470489
return nil, err
471490
}
@@ -717,9 +736,10 @@ func (app *App) execTestCase(t Test, c Case) (*Result, error) {
717736
jobCtx := &JobContext{
718737
evalContext: ctx,
719738
globalArgs: map[string]interface{}{},
739+
execMatcher: &execMatcher{},
720740
}
721741

722-
app.expectedExecs = nil
742+
expectedExecs := []expectedExec{}
723743

724744
for _, e := range t.ExpectedExecs {
725745
var cmd string
@@ -742,13 +762,15 @@ func (app *App) execTestCase(t Test, c Case) (*Result, error) {
742762
}
743763
}
744764

745-
app.expectedExecs = append(app.expectedExecs, expectedExec{
765+
expectedExecs = append(expectedExecs, expectedExec{
746766
Command: cmd,
747767
Args: args,
748768
Dir: dir,
749769
})
750770
}
751771

772+
jobCtx.execMatcher.expectedExecs = expectedExecs
773+
752774
res, err := app.runJobAndUpdateContext(nil, jobCtx, eitherJobRun{static: &t.Run}, new(sync.Mutex), true)
753775

754776
if res == nil && err != nil {
@@ -849,7 +871,7 @@ func (app *App) dispatchRunJob(l *EventLogger, jobCtx *JobContext, run eitherJob
849871
}
850872
}
851873

852-
return app.run(l, jobRun.Name, jobRun.Args, streamOutput)
874+
return app.run(jobCtx, l, jobRun.Name, jobRun.Args, streamOutput)
853875
}
854876

855877
func cloneEvalContext(c *hcl2.EvalContext) *hcl2.EvalContext {
@@ -904,7 +926,7 @@ func (app *App) execMultiRun(l *EventLogger, jobCtx *JobContext, r *DependsOn, s
904926
return nil, err
905927
}
906928

907-
res, err := app.run(l, r.Name, args, streamOutput)
929+
res, err := app.run(jobCtx, l, r.Name, args, streamOutput)
908930
if err != nil {
909931
return res, err
910932
}
@@ -925,7 +947,7 @@ func (app *App) execMultiRun(l *EventLogger, jobCtx *JobContext, r *DependsOn, s
925947
return nil, err
926948
}
927949

928-
res, err := app.run(l, r.Name, args, streamOutput)
950+
res, err := app.run(jobCtx, l, r.Name, args, streamOutput)
929951
if err != nil {
930952
return res, err
931953
}
@@ -1247,6 +1269,13 @@ type JobContext struct {
12471269
evalContext *hcl2.EvalContext
12481270

12491271
globalArgs map[string]interface{}
1272+
1273+
execMatcher *execMatcher
1274+
}
1275+
1276+
type execMatcher struct {
1277+
execInvocationCount int
1278+
expectedExecs []expectedExec
12501279
}
12511280

12521281
func (c *JobContext) WithEvalContext(evalCtx *hcl2.EvalContext) JobContext {
@@ -1457,7 +1486,7 @@ func (app *App) getConfigs(jobCtx *JobContext, cc *HCL2Config, j JobSpec, confTy
14571486
return cty.NilVal, err
14581487
}
14591488

1460-
res, err := app.run(nil, source.Name, args, false)
1489+
res, err := app.run(jobCtx, nil, source.Name, args, false)
14611490
if err != nil {
14621491
return cty.NilVal, xerrors.Errorf("%s %q: source %d: %w", confType, confSpec.Name, sourceIdx, err)
14631492
}

pkg/app/app_shim.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func (app *App) ExportBinary(srcDir, dstFile string) error {
3737
}
3838

3939
_, err = app.execCmd(
40+
nil,
4041
Command{
4142
Name: "sh",
4243
Args: []string{"-c", fmt.Sprintf("cd %s; go build -o %s %s", tmpDir, absDstFile, tmpDir)},
@@ -133,6 +134,7 @@ func main() {
133134
}
134135

135136
_, err = app.execCmd(
137+
nil,
136138
Command{
137139
Name: "sh",
138140
Args: []string{"-c", fmt.Sprintf("cd %s; go mod init %s && go get github.com/rakyll/statik && statik -src=%s", dstDir, moduleName, fs.VendorPrefix)},
@@ -147,6 +149,7 @@ func main() {
147149
variantVer := os.Getenv("VARIANT_BUILD_VER")
148150
if variantVer != "" {
149151
_, err = app.execCmd(
152+
nil,
150153
Command{
151154
Name: "sh",
152155
Args: []string{"-c", fmt.Sprintf("cd %s; go mod edit -require=github.com/mumoshu/variant2@%s", dstDir, variantVer)},
@@ -162,6 +165,7 @@ func main() {
162165
variantReplace := os.Getenv("VARIANT_BUILD_VARIANT_REPLACE")
163166
if variantReplace != "" {
164167
_, err = app.execCmd(
168+
nil,
165169
Command{
166170
Name: "sh",
167171
Args: []string{"-c", fmt.Sprintf("cd %s; go mod edit -replace github.com/mumoshu/variant2@%s=%s", dstDir, variantVer, variantReplace)},
@@ -192,6 +196,7 @@ func main() {
192196

193197
for _, modReplace := range modReplaces {
194198
_, err = app.execCmd(
199+
nil,
195200
Command{
196201
Name: "sh",
197202
Args: []string{"-c", fmt.Sprintf("cd %s; go mod edit -replace %s", dstDir, modReplace)},

pkg/app/types.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,6 @@ type App struct {
234234

235235
Trace string
236236

237-
execInvocationCount int
238-
239-
expectedExecs []expectedExec
240-
241237
sourceClient *source.Client
242238

243239
initMu sync.Mutex

0 commit comments

Comments
 (0)