Skip to content

Commit eb60b2c

Browse files
committed
target main.tf in module if file exists
1 parent 9bf7172 commit eb60b2c

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

schema/module_schema.go

+13
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ func schemaForDependentModuleBlock(localName string, modMeta *module.Meta) (*sch
9494
if len(modMeta.Files) > 0 {
9595
filename := modMeta.Files[0]
9696

97+
if sliceContains(modMeta.Files, "main.tf") {
98+
filename = "main.tf"
99+
}
100+
97101
bodySchema.Targets = &schema.Target{
98102
Path: lang.Path{
99103
Path: modMeta.Path,
@@ -109,3 +113,12 @@ func schemaForDependentModuleBlock(localName string, modMeta *module.Meta) (*sch
109113

110114
return bodySchema, nil
111115
}
116+
117+
func sliceContains(slice []string, value string) bool {
118+
for _, val := range slice {
119+
if val == value {
120+
return true
121+
}
122+
}
123+
return false
124+
}

schema/module_schema_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/google/go-cmp/cmp"
77
"github.com/hashicorp/hcl-lang/lang"
88
"github.com/hashicorp/hcl-lang/schema"
9+
"github.com/hashicorp/hcl/v2"
910
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
1011
"github.com/hashicorp/terraform-schema/module"
1112
"github.com/zclconf/go-cty-debug/ctydebug"
@@ -166,3 +167,86 @@ func TestSchemaForDependentModuleBlock_basic(t *testing.T) {
166167
t.Fatalf("schema mismatch: %s", diff)
167168
}
168169
}
170+
171+
func TestSchemaForDependentModuleBlock_Target(t *testing.T) {
172+
type testCase struct {
173+
name string
174+
meta *module.Meta
175+
expectedSchema *schema.BodySchema
176+
}
177+
178+
testCases := []testCase{
179+
{
180+
"without main.tf",
181+
&module.Meta{
182+
Path: "./local",
183+
Variables: map[string]module.Variable{},
184+
Outputs: map[string]module.Output{},
185+
Files: []string{"a_file.tf", "b_file.tf"},
186+
},
187+
&schema.BodySchema{
188+
Attributes: map[string]*schema.AttributeSchema{},
189+
TargetableAs: []*schema.Targetable{
190+
{
191+
Address: lang.Address{
192+
lang.RootStep{Name: "module"},
193+
lang.AttrStep{Name: "refname"},
194+
},
195+
ScopeId: refscope.ModuleScope,
196+
AsType: cty.Object(map[string]cty.Type{}),
197+
NestedTargetables: []*schema.Targetable{},
198+
},
199+
},
200+
Targets: &schema.Target{
201+
Path: lang.Path{Path: "./local", LanguageID: "terraform"},
202+
Range: hcl.Range{
203+
Filename: "a_file.tf",
204+
Start: hcl.InitialPos,
205+
End: hcl.InitialPos,
206+
},
207+
},
208+
},
209+
},
210+
{
211+
"with main.tf",
212+
&module.Meta{
213+
Path: "./local",
214+
Variables: map[string]module.Variable{},
215+
Outputs: map[string]module.Output{},
216+
Files: []string{"a_file.tf", "main.tf"},
217+
},
218+
&schema.BodySchema{
219+
Attributes: map[string]*schema.AttributeSchema{},
220+
TargetableAs: []*schema.Targetable{
221+
{
222+
Address: lang.Address{
223+
lang.RootStep{Name: "module"},
224+
lang.AttrStep{Name: "refname"},
225+
},
226+
ScopeId: refscope.ModuleScope,
227+
AsType: cty.Object(map[string]cty.Type{}),
228+
NestedTargetables: []*schema.Targetable{},
229+
},
230+
},
231+
Targets: &schema.Target{
232+
Path: lang.Path{Path: "./local", LanguageID: "terraform"},
233+
Range: hcl.Range{
234+
Filename: "main.tf",
235+
Start: hcl.InitialPos,
236+
End: hcl.InitialPos,
237+
},
238+
},
239+
},
240+
},
241+
}
242+
243+
for _, tc := range testCases {
244+
depSchema, err := schemaForDependentModuleBlock("refname", tc.meta)
245+
if err != nil {
246+
t.Fatal(err)
247+
}
248+
if diff := cmp.Diff(tc.expectedSchema, depSchema, ctydebug.CmpOptions); diff != "" {
249+
t.Fatalf("schema mismatch: %s", diff)
250+
}
251+
}
252+
}

0 commit comments

Comments
 (0)