@@ -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
855877func 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
12521281func (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 }
0 commit comments