mirror of
https://github.com/terraform-docs/terraform-docs.git
synced 2026-03-27 12:58:35 +07:00
* Introduce format interface and expose to public pkg * fix issues after merge * don't panic * Rename TFString back to String
305 lines
7.0 KiB
Go
305 lines
7.0 KiB
Go
package format
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/segmentio/terraform-docs/internal/module"
|
|
"github.com/segmentio/terraform-docs/internal/testutil"
|
|
"github.com/segmentio/terraform-docs/pkg/print"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestJson(t *testing.T) {
|
|
assert := assert.New(t)
|
|
settings := testutil.Settings().WithSections().Build()
|
|
|
|
expected, err := testutil.GetExpected("json", "json")
|
|
assert.Nil(err)
|
|
|
|
options := module.NewOptions()
|
|
module, err := testutil.GetModule(options)
|
|
assert.Nil(err)
|
|
|
|
printer := NewJSON(settings)
|
|
actual, err := printer.Print(module, settings)
|
|
|
|
assert.Nil(err)
|
|
assert.Equal(expected, actual)
|
|
}
|
|
|
|
func TestJsonSortByName(t *testing.T) {
|
|
assert := assert.New(t)
|
|
settings := testutil.Settings().WithSections().With(&print.Settings{
|
|
SortByName: true,
|
|
}).Build()
|
|
|
|
expected, err := testutil.GetExpected("json", "json-SortByName")
|
|
assert.Nil(err)
|
|
|
|
options := module.NewOptions().With(&module.Options{
|
|
SortBy: &module.SortBy{
|
|
Name: true,
|
|
},
|
|
})
|
|
module, err := testutil.GetModule(options)
|
|
assert.Nil(err)
|
|
|
|
printer := NewJSON(settings)
|
|
actual, err := printer.Print(module, settings)
|
|
|
|
assert.Nil(err)
|
|
assert.Equal(expected, actual)
|
|
}
|
|
|
|
func TestJsonSortByRequired(t *testing.T) {
|
|
assert := assert.New(t)
|
|
settings := testutil.Settings().WithSections().With(&print.Settings{
|
|
SortByName: true,
|
|
SortByRequired: true,
|
|
}).Build()
|
|
|
|
expected, err := testutil.GetExpected("json", "json-SortByRequired")
|
|
assert.Nil(err)
|
|
|
|
options := module.NewOptions().With(&module.Options{
|
|
SortBy: &module.SortBy{
|
|
Name: true,
|
|
Required: true,
|
|
},
|
|
})
|
|
module, err := testutil.GetModule(options)
|
|
assert.Nil(err)
|
|
|
|
printer := NewJSON(settings)
|
|
actual, err := printer.Print(module, settings)
|
|
|
|
assert.Nil(err)
|
|
assert.Equal(expected, actual)
|
|
}
|
|
|
|
func TestJsonNoHeader(t *testing.T) {
|
|
assert := assert.New(t)
|
|
settings := testutil.Settings().With(&print.Settings{
|
|
ShowHeader: false,
|
|
ShowProviders: true,
|
|
ShowInputs: true,
|
|
ShowOutputs: true,
|
|
}).Build()
|
|
|
|
expected, err := testutil.GetExpected("json", "json-NoHeader")
|
|
assert.Nil(err)
|
|
|
|
options := module.NewOptions()
|
|
module, err := testutil.GetModule(options)
|
|
assert.Nil(err)
|
|
|
|
printer := NewJSON(settings)
|
|
actual, err := printer.Print(module, settings)
|
|
|
|
assert.Nil(err)
|
|
assert.Equal(expected, actual)
|
|
}
|
|
|
|
func TestJsonNoProviders(t *testing.T) {
|
|
assert := assert.New(t)
|
|
settings := testutil.Settings().With(&print.Settings{
|
|
ShowHeader: true,
|
|
ShowProviders: false,
|
|
ShowInputs: true,
|
|
ShowOutputs: true,
|
|
}).Build()
|
|
|
|
expected, err := testutil.GetExpected("json", "json-NoProviders")
|
|
assert.Nil(err)
|
|
|
|
options := module.NewOptions()
|
|
module, err := testutil.GetModule(options)
|
|
assert.Nil(err)
|
|
|
|
printer := NewJSON(settings)
|
|
actual, err := printer.Print(module, settings)
|
|
|
|
assert.Nil(err)
|
|
assert.Equal(expected, actual)
|
|
}
|
|
|
|
func TestJsonNoInputs(t *testing.T) {
|
|
assert := assert.New(t)
|
|
settings := testutil.Settings().With(&print.Settings{
|
|
ShowHeader: true,
|
|
ShowProviders: true,
|
|
ShowInputs: false,
|
|
ShowOutputs: true,
|
|
}).Build()
|
|
|
|
expected, err := testutil.GetExpected("json", "json-NoInputs")
|
|
assert.Nil(err)
|
|
|
|
options := module.NewOptions()
|
|
module, err := testutil.GetModule(options)
|
|
assert.Nil(err)
|
|
|
|
printer := NewJSON(settings)
|
|
actual, err := printer.Print(module, settings)
|
|
|
|
assert.Nil(err)
|
|
assert.Equal(expected, actual)
|
|
}
|
|
|
|
func TestJsonNoOutputs(t *testing.T) {
|
|
assert := assert.New(t)
|
|
settings := testutil.Settings().With(&print.Settings{
|
|
ShowHeader: true,
|
|
ShowProviders: true,
|
|
ShowInputs: true,
|
|
ShowOutputs: false,
|
|
}).Build()
|
|
|
|
expected, err := testutil.GetExpected("json", "json-NoOutputs")
|
|
assert.Nil(err)
|
|
|
|
options := module.NewOptions()
|
|
module, err := testutil.GetModule(options)
|
|
assert.Nil(err)
|
|
|
|
printer := NewJSON(settings)
|
|
actual, err := printer.Print(module, settings)
|
|
|
|
assert.Nil(err)
|
|
assert.Equal(expected, actual)
|
|
}
|
|
|
|
func TestJsonOnlyHeader(t *testing.T) {
|
|
assert := assert.New(t)
|
|
settings := testutil.Settings().With(&print.Settings{
|
|
ShowHeader: true,
|
|
ShowProviders: false,
|
|
ShowInputs: false,
|
|
ShowOutputs: false,
|
|
}).Build()
|
|
|
|
expected, err := testutil.GetExpected("json", "json-OnlyHeader")
|
|
assert.Nil(err)
|
|
|
|
options := module.NewOptions()
|
|
module, err := testutil.GetModule(options)
|
|
assert.Nil(err)
|
|
|
|
printer := NewJSON(settings)
|
|
actual, err := printer.Print(module, settings)
|
|
|
|
assert.Nil(err)
|
|
assert.Equal(expected, actual)
|
|
}
|
|
|
|
func TestJsonOnlyProviders(t *testing.T) {
|
|
assert := assert.New(t)
|
|
settings := testutil.Settings().With(&print.Settings{
|
|
ShowHeader: false,
|
|
ShowProviders: true,
|
|
ShowInputs: false,
|
|
ShowOutputs: false,
|
|
}).Build()
|
|
|
|
expected, err := testutil.GetExpected("json", "json-OnlyProviders")
|
|
assert.Nil(err)
|
|
|
|
options := module.NewOptions()
|
|
module, err := testutil.GetModule(options)
|
|
assert.Nil(err)
|
|
|
|
printer := NewJSON(settings)
|
|
actual, err := printer.Print(module, settings)
|
|
|
|
assert.Nil(err)
|
|
assert.Equal(expected, actual)
|
|
}
|
|
|
|
func TestJsonOnlyInputs(t *testing.T) {
|
|
assert := assert.New(t)
|
|
settings := testutil.Settings().With(&print.Settings{
|
|
ShowHeader: false,
|
|
ShowProviders: false,
|
|
ShowInputs: true,
|
|
ShowOutputs: false,
|
|
}).Build()
|
|
|
|
expected, err := testutil.GetExpected("json", "json-OnlyInputs")
|
|
assert.Nil(err)
|
|
|
|
options := module.NewOptions()
|
|
module, err := testutil.GetModule(options)
|
|
assert.Nil(err)
|
|
|
|
printer := NewJSON(settings)
|
|
actual, err := printer.Print(module, settings)
|
|
|
|
assert.Nil(err)
|
|
assert.Equal(expected, actual)
|
|
}
|
|
|
|
func TestJsonOnlyOutputs(t *testing.T) {
|
|
assert := assert.New(t)
|
|
settings := testutil.Settings().With(&print.Settings{
|
|
ShowHeader: false,
|
|
ShowProviders: false,
|
|
ShowInputs: false,
|
|
ShowOutputs: true,
|
|
}).Build()
|
|
|
|
expected, err := testutil.GetExpected("json", "json-OnlyOutputs")
|
|
assert.Nil(err)
|
|
|
|
options := module.NewOptions()
|
|
module, err := testutil.GetModule(options)
|
|
assert.Nil(err)
|
|
|
|
printer := NewJSON(settings)
|
|
actual, err := printer.Print(module, settings)
|
|
|
|
assert.Nil(err)
|
|
assert.Equal(expected, actual)
|
|
}
|
|
|
|
func TestJsonEscapeCharacters(t *testing.T) {
|
|
assert := assert.New(t)
|
|
settings := testutil.Settings().WithSections().With(&print.Settings{
|
|
EscapeCharacters: true,
|
|
}).Build()
|
|
|
|
expected, err := testutil.GetExpected("json", "json-EscapeCharacters")
|
|
assert.Nil(err)
|
|
|
|
options := module.NewOptions()
|
|
module, err := testutil.GetModule(options)
|
|
assert.Nil(err)
|
|
|
|
printer := NewJSON(settings)
|
|
actual, err := printer.Print(module, settings)
|
|
|
|
assert.Nil(err)
|
|
assert.Equal(expected, actual)
|
|
}
|
|
func TestJsonOutputValues(t *testing.T) {
|
|
assert := assert.New(t)
|
|
settings := testutil.Settings().WithSections().With(&print.Settings{
|
|
OutputValues: true,
|
|
}).Build()
|
|
|
|
expected, err := testutil.GetExpected("json", "json-OutputValues")
|
|
assert.Nil(err)
|
|
|
|
options := module.NewOptions().With(&module.Options{
|
|
OutputValues: true,
|
|
OutputValuesPath: "output_values.json",
|
|
})
|
|
module, err := testutil.GetModule(options)
|
|
assert.Nil(err)
|
|
|
|
printer := NewJSON(settings)
|
|
actual, err := printer.Print(module, settings)
|
|
|
|
assert.Nil(err)
|
|
assert.Equal(expected, actual)
|
|
}
|