|
6 | 6 | "github.com/google/go-cmp/cmp"
|
7 | 7 | "github.com/hashicorp/hcl-lang/lang"
|
8 | 8 | "github.com/hashicorp/hcl-lang/schema"
|
| 9 | + "github.com/hashicorp/hcl/v2" |
9 | 10 | "github.com/hashicorp/terraform-schema/internal/schema/refscope"
|
10 | 11 | "github.com/hashicorp/terraform-schema/module"
|
11 | 12 | "github.com/zclconf/go-cty-debug/ctydebug"
|
@@ -166,3 +167,86 @@ func TestSchemaForDependentModuleBlock_basic(t *testing.T) {
|
166 | 167 | t.Fatalf("schema mismatch: %s", diff)
|
167 | 168 | }
|
168 | 169 | }
|
| 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