Skip to content

Commit

Permalink
report all deps errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dsa0x committed Jan 16, 2025
1 parent cbb7214 commit 754bc4e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
7 changes: 4 additions & 3 deletions internal/backend/local/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,13 @@ func (runner *TestFileRunner) Test(file *moduletest.File) {
file.Status = file.Status.Merge(moduletest.Pass)
}

// Build the graph for the file. Currently, we build this serially to maintain
// the existing sequential functionality of test runs. In the future, we could
// optimize this by parallelizing runs that do not depend on each other.
// Build the graph for the file.
b := terraformtest.TestGraphBuilder{File: file}
graph, diags := b.Build(addrs.RootModuleInstance)
file.Diagnostics = file.Diagnostics.Append(diags)
if diags.HasErrors() {
return
}

// walk and execute the graph
diags = runner.walkGraph(graph)
Expand Down
1 change: 1 addition & 0 deletions internal/command/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ func TestTest_Runs(t *testing.T) {
expectedOut: []string{"3 passed, 1 failed."},
expectedErr: []string{"Test assertion failed", "resource renamed without moved block"},
code: 1,
},
"simple_testdata": {
expectedOut: []string{" passed, 0 failed."},
code: 0,
Expand Down
6 changes: 4 additions & 2 deletions internal/terraformtest/node_test_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ package terraformtest
import (
"fmt"

"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/moduletest"
)

type NodeTestRun struct {
file *moduletest.File
run *moduletest.Run
file *moduletest.File
run *moduletest.Run
config *configs.Config
}

func (n *NodeTestRun) Run() *moduletest.Run {
Expand Down
56 changes: 54 additions & 2 deletions internal/terraformtest/transform_test_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
package terraformtest

import (
"errors"
"fmt"
"strings"

"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/backend/backendrun"
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/dag"
"github.com/hashicorp/terraform/internal/lang/langrefs"
"github.com/hashicorp/terraform/internal/moduletest"
"github.com/hashicorp/terraform/internal/terraform"
)
Expand All @@ -21,16 +27,62 @@ type TestRunTransformer struct {

func (t *TestRunTransformer) Transform(g *terraform.Graph) error {
var prev *NodeTestRun
var errs []error
runsSoFar := make(map[string]*NodeTestRun)
for _, run := range t.File.Runs {
node := &NodeTestRun{run: run, file: t.File}
// If we're testing a specific configuration, we need to use that
config := t.config
if run.Config.ConfigUnderTest != nil {
config = run.Config.ConfigUnderTest
}

node := &NodeTestRun{run: run, file: t.File, config: config}
g.Add(node)
if prev != nil {
g.Connect(dag.BasicEdge(node, prev))
}
prev = node

// Connect the run to all the other runs that it depends on
refs, err := getRefs(run)
if err != nil {
return err
}
for _, ref := range refs {
subjectStr := ref.Subject.String()
if !strings.HasPrefix(subjectStr, "run.") {
continue
}
runName := strings.TrimPrefix(subjectStr, "run.")
if runName == "" {
continue
}
dependency, ok := runsSoFar[runName]
if !ok {
errs = append(errs, fmt.Errorf("dependency `run.%s` not found for run %q", runName, run.Name))
continue
}
g.Connect(dag.BasicEdge(node, dependency))
}
runsSoFar[run.Name] = node
}

return nil
return errors.Join(errs...)
}

func getRefs(run *moduletest.Run) ([]*addrs.Reference, error) {
refs, refDiags := run.GetReferences()
if refDiags.HasErrors() {
return nil, refDiags.Err()
}
for _, expr := range run.Config.Variables {
moreRefs, moreDiags := langrefs.ReferencesInExpr(addrs.ParseRefFromTestingScope, expr)
if moreDiags.HasErrors() {
return nil, moreDiags.Err()
}
refs = append(refs, moreRefs...)
}
return refs, nil
}

// -------------------------------------------------------- CloseTestGraphTransformer --------------------------------------------------------
Expand Down

0 comments on commit 754bc4e

Please sign in to comment.