@@ -7,81 +7,22 @@ import (
77 "fmt"
88
99 "github.com/hashicorp/hcl/v2"
10+
1011 "github.com/hashicorp/terraform/internal/configs"
11- "github.com/hashicorp/terraform/internal/dag"
1212 "github.com/hashicorp/terraform/internal/moduletest"
1313 hcltest "github.com/hashicorp/terraform/internal/moduletest/hcl"
14- "github.com/hashicorp/terraform/internal/states"
15- "github.com/hashicorp/terraform/internal/terraform"
16- "github.com/hashicorp/terraform/internal/tfdiags"
1714)
1815
19- type GraphNodeExecutable interface {
20- Execute (ctx * EvalContext ) tfdiags.Diagnostics
21- }
22-
23- // TestFileState is a helper struct that just maps a run block to the state that
24- // was produced by the execution of that run block.
25- type TestFileState struct {
26- Run * moduletest.Run
27- State * states.State
28- }
29-
30- // TestConfigTransformer is a GraphTransformer that adds all the test runs,
31- // and the variables defined in each run block, to the graph.
32- type TestConfigTransformer struct {
33- File * moduletest.File
34- }
35-
36- func (t * TestConfigTransformer ) Transform (g * terraform.Graph ) error {
37- // This map tracks the state of each run in the file. If multiple runs
38- // have the same state key, they will share the same state.
39- statesMap := make (map [string ]* TestFileState )
40-
41- // a root config node that will add the file states to the context
42- rootConfigNode := t .addRootConfigNode (g , statesMap )
43-
44- for _ , v := range g .Vertices () {
45- node , ok := v .(* NodeTestRun )
46- if ! ok {
47- continue
48- }
49- key := node .run .GetStateKey ()
50- if _ , exists := statesMap [key ]; ! exists {
51- state := & TestFileState {
52- Run : nil ,
53- State : states .NewState (),
54- }
55- statesMap [key ] = state
56- }
57-
58- // Connect all the test runs to the config node, so that the config node
59- // is executed before any of the test runs.
60- g .Connect (dag .BasicEdge (node , rootConfigNode ))
61- }
62-
63- return nil
64- }
65-
66- func (t * TestConfigTransformer ) addRootConfigNode (g * terraform.Graph , statesMap map [string ]* TestFileState ) * dynamicNode {
67- rootConfigNode := & dynamicNode {
68- eval : func (ctx * EvalContext ) tfdiags.Diagnostics {
69- var diags tfdiags.Diagnostics
70- ctx .FileStates = statesMap
71- return diags
72- },
73- }
74- g .Add (rootConfigNode )
75- return rootConfigNode
76- }
77-
7816// TransformConfigForRun transforms the run's module configuration to include
7917// the providers and variables from its block and the test file.
8018//
8119// In practice, this actually just means performing some surgery on the
8220// available providers. We want to copy the relevant providers from the test
8321// file into the configuration. We also want to process the providers so they
8422// use variables from the file instead of variables from within the test file.
23+ //
24+ // TODO(liamcervante): Push provider initialisation into the test graph
25+ // so we don't have to transform the config in this way anymore.
8526func TransformConfigForRun (ctx * EvalContext , run * moduletest.Run , file * moduletest.File ) hcl.Diagnostics {
8627 var diags hcl.Diagnostics
8728
0 commit comments