Skip to content

Commit 6abad45

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

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-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

+108
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,110 @@ 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+
"no target",
181+
&module.Meta{
182+
Path: "./local",
183+
Variables: map[string]module.Variable{},
184+
Outputs: map[string]module.Output{},
185+
Files: nil,
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: nil,
201+
},
202+
},
203+
{
204+
"without main.tf",
205+
&module.Meta{
206+
Path: "./local",
207+
Variables: map[string]module.Variable{},
208+
Outputs: map[string]module.Output{},
209+
Files: []string{"a_file.tf", "b_file.tf"},
210+
},
211+
&schema.BodySchema{
212+
Attributes: map[string]*schema.AttributeSchema{},
213+
TargetableAs: []*schema.Targetable{
214+
{
215+
Address: lang.Address{
216+
lang.RootStep{Name: "module"},
217+
lang.AttrStep{Name: "refname"},
218+
},
219+
ScopeId: refscope.ModuleScope,
220+
AsType: cty.Object(map[string]cty.Type{}),
221+
NestedTargetables: []*schema.Targetable{},
222+
},
223+
},
224+
Targets: &schema.Target{
225+
Path: lang.Path{Path: "./local", LanguageID: "terraform"},
226+
Range: hcl.Range{
227+
Filename: "a_file.tf",
228+
Start: hcl.InitialPos,
229+
End: hcl.InitialPos,
230+
},
231+
},
232+
},
233+
},
234+
{
235+
"with main.tf",
236+
&module.Meta{
237+
Path: "./local",
238+
Variables: map[string]module.Variable{},
239+
Outputs: map[string]module.Output{},
240+
Files: []string{"a_file.tf", "main.tf"},
241+
},
242+
&schema.BodySchema{
243+
Attributes: map[string]*schema.AttributeSchema{},
244+
TargetableAs: []*schema.Targetable{
245+
{
246+
Address: lang.Address{
247+
lang.RootStep{Name: "module"},
248+
lang.AttrStep{Name: "refname"},
249+
},
250+
ScopeId: refscope.ModuleScope,
251+
AsType: cty.Object(map[string]cty.Type{}),
252+
NestedTargetables: []*schema.Targetable{},
253+
},
254+
},
255+
Targets: &schema.Target{
256+
Path: lang.Path{Path: "./local", LanguageID: "terraform"},
257+
Range: hcl.Range{
258+
Filename: "main.tf",
259+
Start: hcl.InitialPos,
260+
End: hcl.InitialPos,
261+
},
262+
},
263+
},
264+
},
265+
}
266+
267+
for _, tc := range testCases {
268+
depSchema, err := schemaForDependentModuleBlock("refname", tc.meta)
269+
if err != nil {
270+
t.Fatal(err)
271+
}
272+
if diff := cmp.Diff(tc.expectedSchema, depSchema, ctydebug.CmpOptions); diff != "" {
273+
t.Fatalf("schema mismatch: %s", diff)
274+
}
275+
}
276+
}

0 commit comments

Comments
 (0)