10 // TestString is a Stringer-like function that outputs a string that can
11 // be used to easily compare multiple Config structures in unit tests.
13 // This function has no practical use outside of unit tests and debugging.
14 func (c *Config) TestString() string {
20 if len(c.Modules) > 0 {
21 buf.WriteString("Modules:\n\n")
22 buf.WriteString(modulesStr(c.Modules))
23 buf.WriteString("\n\n")
26 if len(c.Variables) > 0 {
27 buf.WriteString("Variables:\n\n")
28 buf.WriteString(variablesStr(c.Variables))
29 buf.WriteString("\n\n")
32 if len(c.ProviderConfigs) > 0 {
33 buf.WriteString("Provider Configs:\n\n")
34 buf.WriteString(providerConfigsStr(c.ProviderConfigs))
35 buf.WriteString("\n\n")
38 if len(c.Resources) > 0 {
39 buf.WriteString("Resources:\n\n")
40 buf.WriteString(resourcesStr(c.Resources))
41 buf.WriteString("\n\n")
44 if len(c.Outputs) > 0 {
45 buf.WriteString("Outputs:\n\n")
46 buf.WriteString(outputsStr(c.Outputs))
50 return strings.TrimSpace(buf.String())
53 func terraformStr(t *Terraform) string {
56 if b := t.Backend; b != nil {
57 result += fmt.Sprintf("backend (%s)\n", b.Type)
59 keys := make([]string, 0, len(b.RawConfig.Raw))
60 for k, _ := range b.RawConfig.Raw {
61 keys = append(keys, k)
65 for _, k := range keys {
66 result += fmt.Sprintf(" %s\n", k)
70 return strings.TrimSpace(result)
73 func modulesStr(ms []*Module) string {
75 order := make([]int, 0, len(ms))
76 ks := make([]string, 0, len(ms))
77 mapping := make(map[string]int)
78 for i, m := range ms {
84 for _, k := range ks {
85 order = append(order, mapping[k])
88 for _, i := range order {
90 result += fmt.Sprintf("%s\n", m.Id())
92 ks := make([]string, 0, len(m.RawConfig.Raw))
93 for k, _ := range m.RawConfig.Raw {
98 result += fmt.Sprintf(" source = %s\n", m.Source)
100 for _, k := range ks {
101 result += fmt.Sprintf(" %s\n", k)
105 return strings.TrimSpace(result)
108 func outputsStr(os []*Output) string {
109 ns := make([]string, 0, len(os))
110 m := make(map[string]*Output)
111 for _, o := range os {
112 ns = append(ns, o.Name)
118 for _, n := range ns {
121 result += fmt.Sprintf("%s\n", n)
123 if len(o.DependsOn) > 0 {
124 result += fmt.Sprintf(" dependsOn\n")
125 for _, d := range o.DependsOn {
126 result += fmt.Sprintf(" %s\n", d)
130 if len(o.RawConfig.Variables) > 0 {
131 result += fmt.Sprintf(" vars\n")
132 for _, rawV := range o.RawConfig.Variables {
134 str := rawV.FullKey()
137 case *ResourceVariable:
143 result += fmt.Sprintf(" %s: %s\n", kind, str)
147 if o.Description != "" {
148 result += fmt.Sprintf(" description\n %s\n", o.Description)
152 return strings.TrimSpace(result)
155 func localsStr(ls []*Local) string {
156 ns := make([]string, 0, len(ls))
157 m := make(map[string]*Local)
158 for _, l := range ls {
159 ns = append(ns, l.Name)
165 for _, n := range ns {
168 result += fmt.Sprintf("%s\n", n)
170 if len(l.RawConfig.Variables) > 0 {
171 result += fmt.Sprintf(" vars\n")
172 for _, rawV := range l.RawConfig.Variables {
174 str := rawV.FullKey()
177 case *ResourceVariable:
183 result += fmt.Sprintf(" %s: %s\n", kind, str)
188 return strings.TrimSpace(result)
191 // This helper turns a provider configs field into a deterministic
192 // string value for comparison in tests.
193 func providerConfigsStr(pcs []*ProviderConfig) string {
196 ns := make([]string, 0, len(pcs))
197 m := make(map[string]*ProviderConfig)
198 for _, n := range pcs {
199 ns = append(ns, n.Name)
204 for _, n := range ns {
207 result += fmt.Sprintf("%s\n", n)
209 keys := make([]string, 0, len(pc.RawConfig.Raw))
210 for k, _ := range pc.RawConfig.Raw {
211 keys = append(keys, k)
215 for _, k := range keys {
216 result += fmt.Sprintf(" %s\n", k)
219 if len(pc.RawConfig.Variables) > 0 {
220 result += fmt.Sprintf(" vars\n")
221 for _, rawV := range pc.RawConfig.Variables {
223 str := rawV.FullKey()
226 case *ResourceVariable:
232 result += fmt.Sprintf(" %s: %s\n", kind, str)
237 return strings.TrimSpace(result)
240 // This helper turns a resources field into a deterministic
241 // string value for comparison in tests.
242 func resourcesStr(rs []*Resource) string {
244 order := make([]int, 0, len(rs))
245 ks := make([]string, 0, len(rs))
246 mapping := make(map[string]int)
247 for i, r := range rs {
253 for _, k := range ks {
254 order = append(order, mapping[k])
257 for _, i := range order {
259 result += fmt.Sprintf(
264 ks := make([]string, 0, len(r.RawConfig.Raw))
265 for k, _ := range r.RawConfig.Raw {
270 for _, k := range ks {
271 result += fmt.Sprintf(" %s\n", k)
274 if len(r.Provisioners) > 0 {
275 result += fmt.Sprintf(" provisioners\n")
276 for _, p := range r.Provisioners {
278 if p.When != ProvisionerWhenCreate {
279 when = fmt.Sprintf(" (%s)", p.When.String())
282 result += fmt.Sprintf(" %s%s\n", p.Type, when)
284 if p.OnFailure != ProvisionerOnFailureFail {
285 result += fmt.Sprintf(" on_failure = %s\n", p.OnFailure.String())
288 ks := make([]string, 0, len(p.RawConfig.Raw))
289 for k, _ := range p.RawConfig.Raw {
294 for _, k := range ks {
295 result += fmt.Sprintf(" %s\n", k)
300 if len(r.DependsOn) > 0 {
301 result += fmt.Sprintf(" dependsOn\n")
302 for _, d := range r.DependsOn {
303 result += fmt.Sprintf(" %s\n", d)
307 if len(r.RawConfig.Variables) > 0 {
308 result += fmt.Sprintf(" vars\n")
310 ks := make([]string, 0, len(r.RawConfig.Variables))
311 for k, _ := range r.RawConfig.Variables {
316 for _, k := range ks {
317 rawV := r.RawConfig.Variables[k]
319 str := rawV.FullKey()
322 case *ResourceVariable:
328 result += fmt.Sprintf(" %s: %s\n", kind, str)
333 return strings.TrimSpace(result)
336 // This helper turns a variables field into a deterministic
337 // string value for comparison in tests.
338 func variablesStr(vs []*Variable) string {
340 ks := make([]string, 0, len(vs))
341 m := make(map[string]*Variable)
342 for _, v := range vs {
343 ks = append(ks, v.Name)
348 for _, k := range ks {
353 required = " (required)"
357 if v.DeclaredType != "" {
358 declaredType = fmt.Sprintf(" (%s)", v.DeclaredType)
361 if v.Default == nil || v.Default == "" {
364 if v.Description == "" {
368 result += fmt.Sprintf(
369 "%s%s%s\n %v\n %s\n",
377 return strings.TrimSpace(result)