@@ -13,7 +13,7 @@ import (
13
13
"github.com/zclconf/go-cty/cty"
14
14
)
15
15
16
- func TestLinksInFile (t * testing.T ) {
16
+ func TestLinksInFileBlock (t * testing.T ) {
17
17
resourceLabelSchema := []* schema.LabelSchema {
18
18
{Name : "type" , IsDepKey : true },
19
19
{Name : "name" },
@@ -101,6 +101,91 @@ func TestLinksInFile(t *testing.T) {
101
101
}
102
102
}
103
103
104
+ func TestLinksInFileAttribute (t * testing.T ) {
105
+ resourceLabelSchema := []* schema.LabelSchema {
106
+ {Name : "name" },
107
+ }
108
+ blockSchema := & schema.BlockSchema {
109
+ Labels : resourceLabelSchema ,
110
+ Description : lang .Markdown ("My special block" ),
111
+ Body : & schema.BodySchema {
112
+ Attributes : map [string ]* schema.AttributeSchema {
113
+ "num_attr" : {Expr : schema .LiteralTypeOnly (cty .Number )},
114
+ "source" : {
115
+ Expr : schema .LiteralTypeOnly (cty .String ),
116
+ Description : lang .PlainText ("Special attribute" ),
117
+ IsDepKey : true ,
118
+ },
119
+ },
120
+ },
121
+ DependentBody : map [schema.SchemaKey ]* schema.BodySchema {
122
+ schema .NewSchemaKey (schema.DependencyKeys {
123
+ Attributes : []schema.AttributeDependent {
124
+ {
125
+ Name : "source" ,
126
+ Expr : schema.ExpressionValue {
127
+ Static : cty .StringVal ("example.com/source" ),
128
+ },
129
+ },
130
+ },
131
+ }): {
132
+ DocsLink : & schema.DocsLink {URL : "https://example.com/some/source" },
133
+ },
134
+ },
135
+ }
136
+ bodySchema := & schema.BodySchema {
137
+ Blocks : map [string ]* schema.BlockSchema {
138
+ "myblock" : blockSchema ,
139
+ },
140
+ }
141
+ testConfig := []byte (`myblock "example" {
142
+ source = "example.com/source"
143
+ num_attr = 4
144
+ }
145
+ ` )
146
+
147
+ f , pDiags := hclsyntax .ParseConfig (testConfig , "test.tf" , hcl .InitialPos )
148
+ if len (pDiags ) > 0 {
149
+ t .Fatal (pDiags )
150
+ }
151
+
152
+ d := testPathDecoder (t , & PathContext {
153
+ Schema : bodySchema ,
154
+ Files : map [string ]* hcl.File {
155
+ "test.tf" : f ,
156
+ },
157
+ })
158
+
159
+ links , err := d .LinksInFile ("test.tf" )
160
+ if err != nil {
161
+ t .Fatal (err )
162
+ }
163
+
164
+ expectedLinks := []lang.Link {
165
+ {
166
+ URI : "https://example.com/some/source" ,
167
+ Range : hcl.Range {
168
+ Filename : "test.tf" ,
169
+ Start : hcl.Pos {
170
+ Line : 2 ,
171
+ Column : 12 ,
172
+ Byte : 31 ,
173
+ },
174
+ End : hcl.Pos {
175
+ Line : 2 ,
176
+ Column : 32 ,
177
+ Byte : 51 ,
178
+ },
179
+ },
180
+ },
181
+ }
182
+
183
+ diff := cmp .Diff (expectedLinks , links )
184
+ if diff != "" {
185
+ t .Fatalf ("unexpected links: %s" , diff )
186
+ }
187
+ }
188
+
104
189
func TestLinksInFile_json (t * testing.T ) {
105
190
f , pDiags := json .Parse ([]byte (`{
106
191
"customblock": {
@@ -117,6 +202,7 @@ func TestLinksInFile_json(t *testing.T) {
117
202
},
118
203
})
119
204
205
+ // We never want to provide links in JSON configs
120
206
_ , err := d .LinksInFile ("test.tf.json" )
121
207
unknownFormatErr := & UnknownFileFormatError {}
122
208
if ! errors .As (err , & unknownFormatErr ) {
0 commit comments