Always convert CRLF to LF for output description

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
This commit is contained in:
Khosrow Moossavi
2022-01-07 19:09:55 -05:00
parent 05d432ed44
commit e72f215e3e
7 changed files with 52 additions and 3 deletions

View File

@@ -40,5 +40,8 @@ trim_trailing_whitespace = false
[**/inputs-crlf/variables.tf]
end_of_line = crlf
[**/outputs-crlf/outputs.tf]
end_of_line = crlf
[Makefile]
indent_style = tab

View File

@@ -278,7 +278,8 @@ func loadOutputs(tfmodule *tfconfig.Module, config *print.Config) ([]*Output, er
}
}
for _, o := range tfmodule.Outputs {
description := o.Description
// convert CRLF to LF early on (https://github.com/terraform-docs/terraform-docs/issues/584)
description := strings.ReplaceAll(o.Description, "\r\n", "\n")
if description == "" && config.Settings.ReadComments {
description = loadComments(o.Pos.Filename, o.Pos.Line)
}

View File

@@ -618,6 +618,37 @@ func TestLoadOutputs(t *testing.T) {
}
}
func TestLoadOutputsLineEnding(t *testing.T) {
tests := []struct {
name string
path string
expected string
}{
{
name: "load module outputs from file with lf line ending",
path: "outputs-lf",
expected: "The quick brown fox jumps\nover the lazy dog\n",
},
{
name: "load module outputs from file with crlf line ending",
path: "outputs-crlf",
expected: "The quick brown fox jumps\nover the lazy dog\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
config := print.NewConfig()
module, _ := loadModule(filepath.Join("testdata", tt.path))
outputs, _ := loadOutputs(module, config)
assert.Equal(1, len(outputs))
assert.Equal(tt.expected, string(outputs[0].Description))
})
}
}
func TestLoadOutputsValues(t *testing.T) {
type expected struct {
outputs int

View File

@@ -1,4 +1,4 @@
variable "multi-line-lf" {
variable "multi-line-crlf" {
type = string
description = <<-EOT
The quick brown fox jumps

View File

@@ -1,4 +1,4 @@
variable "multi-line-crlf" {
variable "multi-line-lf" {
type = string
description = <<-EOT
The quick brown fox jumps

View File

@@ -0,0 +1,7 @@
output "multi-line-crlf" {
value = "foo"
description = <<-EOT
The quick brown fox jumps
over the lazy dog
EOT
}

View File

@@ -0,0 +1,7 @@
output "multi-line-lf" {
value = "foo"
description = <<-EOT
The quick brown fox jumps
over the lazy dog
EOT
}