MCPcopy
hub / github.com/opentofu/opentofu / buildTestModules

Function buildTestModules

internal/configs/config_build.go:52–142  ·  view source on GitHub ↗
(ctx context.Context, root *Config, walker ModuleWalker)

Source from the content-addressed store, hash-verified

50}
51
52func buildTestModules(ctx context.Context, root *Config, walker ModuleWalker) hcl.Diagnostics {
53 var diags hcl.Diagnostics
54
55 for name, file := range root.Module.Tests {
56 for _, run := range file.Runs {
57 if run.Module == nil {
58 continue
59 }
60
61 // We want to make sure the path for the testing modules are unique
62 // so we create a dedicated path for them.
63 //
64 // Some examples:
65 // - file: main.tftest.hcl, run: setup - test.main.setup
66 // - file: tests/main.tftest.hcl, run: setup - test.tests.main.setup
67
68 dir := filepath.Dir(name)
69 base := filepath.Base(name)
70
71 path := addrs.Module{}
72 path = append(path, "test")
73 if dir != "." {
74 path = append(path, strings.Split(dir, "/")...)
75 }
76 path = append(path, strings.TrimSuffix(base, ".tftest.hcl"), run.Name)
77 req := ModuleRequest{
78 Name: run.Name,
79 Path: path,
80 SourceAddr: run.Module.Source,
81 SourceAddrRange: run.Module.SourceDeclRange,
82 VersionConstraint: run.Module.Version,
83 Parent: root,
84 Call: NewStaticModuleCall(
85 path,
86 run.Module.DeclRange,
87 func(v *Variable) (cty.Value, hcl.Diagnostics) {
88 // Handle the case where this is overridden in the test run block
89 expr, isOverridden := run.Variables[v.Name]
90 if isOverridden {
91 identifier := StaticIdentifier{
92 Module: path,
93 Subject: fmt.Sprintf("var.%s", v.Name),
94 DeclRange: expr.Range(),
95 }
96 return root.Module.StaticEvaluator.Evaluate(ctx, expr, identifier)
97 }
98
99 // If we haven't had it overridden in a run block, fall back to trying our best
100 // but we do have defaults for some that we can use.
101 if v.Default != cty.NilVal {
102 return v.Default, nil
103 }
104 return cty.DynamicVal, nil
105 },
106 root.Module.SourceDir,
107 root.Module.StaticEvaluator.call.workspace,
108 ),
109

Callers 1

BuildConfigFunction · 0.85

Calls 5

NewStaticModuleCallFunction · 0.85
loadModuleFunction · 0.85
rebaseChildModuleFunction · 0.85
RangeMethod · 0.65
EvaluateMethod · 0.65

Tested by

no test coverage detected