@@ -5,7 +5,6 @@ package local
5
5
6
6
import (
7
7
"context"
8
- "errors"
9
8
"fmt"
10
9
"log"
11
10
"path/filepath"
@@ -131,7 +130,10 @@ func (runner *TestSuiteRunner) Test() (moduletest.Status, tfdiags.Diagnostics) {
131
130
}
132
131
133
132
file := suite .Files [name ]
134
- evalCtx := graph .NewEvalContext ()
133
+ // The eval context inherits the cancelled context from the runner.
134
+ // This allows the eval context to stop the graph walk if the runner
135
+ // requests a hard stop.
136
+ evalCtx := graph .NewEvalContext (runner .CancelledCtx )
135
137
136
138
for _ , run := range file .Runs {
137
139
// Pre-initialise the prior outputs, so we can easily tell between
@@ -254,8 +256,6 @@ type TestFileRunner struct {
254
256
EvalContext * graph.EvalContext
255
257
}
256
258
257
- var graphTerminatedError = errors .New ("graph walk terminated" )
258
-
259
259
func (runner * TestFileRunner ) Test (file * moduletest.File ) {
260
260
log .Printf ("[TRACE] TestFileRunner: executing test file %s" , file .Name )
261
261
@@ -288,7 +288,8 @@ func (runner *TestFileRunner) Test(file *moduletest.File) {
288
288
// The error the user receives will just be:
289
289
// Failure! 0 passed, 1 failed.
290
290
// exit status 1
291
- if diags .HasErrors () && diags .Err ().Error () == graphTerminatedError .Error () {
291
+ if runner .EvalContext .Cancelled () {
292
+ file .UpdateStatus (moduletest .Error )
292
293
log .Printf ("[TRACE] TestFileRunner: graph walk terminated for %s" , file .Name )
293
294
return
294
295
}
@@ -300,16 +301,15 @@ func (runner *TestFileRunner) Test(file *moduletest.File) {
300
301
func (runner * TestFileRunner ) walkGraph (g * terraform.Graph ) tfdiags.Diagnostics {
301
302
sem := runner .Suite .semaphore
302
303
303
- // We'll use this context to cancel the walk if the test is stopped.
304
- // There is currently no mechanism by the graph for stopping the entire
305
- // graph walk, so we'll just cancel the context and let the walk function
306
- // return early for each node.
307
- ctx , cancel := context .WithCancelCause (context .Background ())
308
-
309
304
// Walk the graph.
310
305
walkFn := func (v dag.Vertex ) (diags tfdiags.Diagnostics ) {
311
- if ctx .Err () != nil {
312
- // If the context was cancelled, the node should just return immediately.
306
+ if runner .EvalContext .Cancelled () {
307
+ // If the graph walk has been cancelled, the node should just return immediately.
308
+ // For now, this means a hard stop has been requested, in this case we don't
309
+ // even stop to mark future test runs as having been skipped. They'll
310
+ // just show up as pending in the printed summary. We will quickly
311
+ // just mark the overall file status has having errored to indicate
312
+ // it was interrupted.
313
313
return
314
314
}
315
315
@@ -371,17 +371,6 @@ func (runner *TestFileRunner) walkGraph(g *terraform.Graph) tfdiags.Diagnostics
371
371
file := runNode .File ()
372
372
run := runNode .Run ()
373
373
374
- if runner .Suite .Cancelled {
375
- // This means a hard stop has been requested, in this case we don't
376
- // even stop to mark future tests as having been skipped. They'll
377
- // just show up as pending in the printed summary. We will quickly
378
- // just mark the overall file status has having errored to indicate
379
- // it was interrupted.
380
- file .UpdateStatus (moduletest .Error )
381
- cancel (graphTerminatedError )
382
- return
383
- }
384
-
385
374
if runner .Suite .Stopped {
386
375
// Then the test was requested to be stopped, so we just mark each
387
376
// following test as skipped, print the status, and move on.
@@ -445,23 +434,7 @@ func (runner *TestFileRunner) walkGraph(g *terraform.Graph) tfdiags.Diagnostics
445
434
return
446
435
}
447
436
448
- // We want to either wait for the walk to complete or for the context to be
449
- // cancelled. If the context is cancelled, we'll return immediately.
450
- doneCh := make (chan tfdiags.Diagnostics )
451
- go func () {
452
- defer close (doneCh )
453
- doneCh <- g .AcyclicGraph .Walk (walkFn )
454
- }()
455
-
456
- select {
457
- case <- ctx .Done ():
458
- // If the context was cancelled, we'll just return the cause of the
459
- // cancellation. For now, this can only be the graphTerminatedError.
460
- return tfdiags.Diagnostics {}.Append (context .Cause (ctx ))
461
- case diags := <- doneCh :
462
- return diags
463
- }
464
-
437
+ return g .AcyclicGraph .Walk (walkFn )
465
438
}
466
439
467
440
func (runner * TestFileRunner ) run (run * moduletest.Run , file * moduletest.File , state * states.State ) (* states.State , bool ) {
0 commit comments