mirror of
https://github.com/terraform-docs/terraform-docs.git
synced 2026-03-27 12:58:35 +07:00
Add one extra special variable the `content`:
- `{{ .Module }}`
As opposed to the other variables, which are generated sections based on
a selected formatter, the `{{ .Module }}` variable is just a `struct`
representing a Terraform module.
It can be used to build highly complex and highly customized content:
```yaml
content: |-
## Resources
{{ range .Module.Resources }}
- {{ .GetMode }}.{{ .Spec }} ({{ .Position.Filename }}#{{ .Position.Line }})
{{- end }}
```
Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
104 lines
2.1 KiB
Go
104 lines
2.1 KiB
Go
/*
|
|
Copyright 2021 The terraform-docs Authors.
|
|
|
|
Licensed under the MIT license (the "License"); you may not
|
|
use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at the LICENSE file in
|
|
the root directory of this source tree.
|
|
*/
|
|
|
|
package format
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/terraform-docs/terraform-docs/internal/testutil"
|
|
"github.com/terraform-docs/terraform-docs/print"
|
|
)
|
|
|
|
func TestTfvarsHcl(t *testing.T) {
|
|
tests := map[string]struct {
|
|
config print.Config
|
|
}{
|
|
// Base
|
|
"Base": {
|
|
config: testutil.WithSections(),
|
|
},
|
|
"Empty": {
|
|
config: testutil.WithDefaultSections(
|
|
testutil.With(func(c *print.Config) {
|
|
c.ModuleRoot = "empty"
|
|
}),
|
|
),
|
|
},
|
|
|
|
// Settings
|
|
"EscapeCharacters": {
|
|
config: testutil.With(func(c *print.Config) {
|
|
c.Settings.Escape = true
|
|
}),
|
|
},
|
|
"PrintDescription": {
|
|
config: testutil.WithSections(
|
|
testutil.With(func(c *print.Config) {
|
|
c.Settings.Description = true
|
|
}),
|
|
),
|
|
},
|
|
"SortByName": {
|
|
config: testutil.WithSections(
|
|
testutil.With(func(c *print.Config) {
|
|
c.Sort.Enabled = true
|
|
c.Sort.By = print.SortName
|
|
}),
|
|
),
|
|
},
|
|
"SortByRequired": {
|
|
config: testutil.WithSections(
|
|
testutil.With(func(c *print.Config) {
|
|
c.Sort.Enabled = true
|
|
c.Sort.By = print.SortRequired
|
|
}),
|
|
),
|
|
},
|
|
"SortByType": {
|
|
config: testutil.WithSections(
|
|
testutil.With(func(c *print.Config) {
|
|
c.Sort.Enabled = true
|
|
c.Sort.By = print.SortType
|
|
}),
|
|
),
|
|
},
|
|
|
|
// No section
|
|
"NoInputs": {
|
|
config: testutil.WithSections(
|
|
testutil.With(func(c *print.Config) {
|
|
c.Sections.Inputs = false
|
|
}),
|
|
),
|
|
},
|
|
}
|
|
for name, tt := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
expected, err := testutil.GetExpected("tfvars", "hcl-"+name)
|
|
assert.Nil(err)
|
|
|
|
module, err := testutil.GetModule(&tt.config)
|
|
assert.Nil(err)
|
|
|
|
formatter := NewTfvarsHCL(&tt.config)
|
|
|
|
err = formatter.Generate(module)
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(expected, formatter.Content())
|
|
})
|
|
}
|
|
}
|