diff --git a/.editorconfig b/.editorconfig index 50b04ac..e54d215 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 diff --git a/terraform/load.go b/terraform/load.go index 7cfa1f7..162c59e 100644 --- a/terraform/load.go +++ b/terraform/load.go @@ -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) } diff --git a/terraform/load_test.go b/terraform/load_test.go index 984acd5..cbce6e8 100644 --- a/terraform/load_test.go +++ b/terraform/load_test.go @@ -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 diff --git a/terraform/testdata/inputs-crlf/variables.tf b/terraform/testdata/inputs-crlf/variables.tf index 2333cc8..6304af7 100644 --- a/terraform/testdata/inputs-crlf/variables.tf +++ b/terraform/testdata/inputs-crlf/variables.tf @@ -1,4 +1,4 @@ -variable "multi-line-lf" { +variable "multi-line-crlf" { type = string description = <<-EOT The quick brown fox jumps diff --git a/terraform/testdata/inputs-lf/variables.tf b/terraform/testdata/inputs-lf/variables.tf index b2cbf52..fbc0958 100644 --- a/terraform/testdata/inputs-lf/variables.tf +++ b/terraform/testdata/inputs-lf/variables.tf @@ -1,4 +1,4 @@ -variable "multi-line-crlf" { +variable "multi-line-lf" { type = string description = <<-EOT The quick brown fox jumps diff --git a/terraform/testdata/outputs-crlf/outputs.tf b/terraform/testdata/outputs-crlf/outputs.tf new file mode 100644 index 0000000..18266be --- /dev/null +++ b/terraform/testdata/outputs-crlf/outputs.tf @@ -0,0 +1,7 @@ +output "multi-line-crlf" { + value = "foo" + description = <<-EOT + The quick brown fox jumps + over the lazy dog + EOT +} diff --git a/terraform/testdata/outputs-lf/outputs.tf b/terraform/testdata/outputs-lf/outputs.tf new file mode 100644 index 0000000..ab5daf6 --- /dev/null +++ b/terraform/testdata/outputs-lf/outputs.tf @@ -0,0 +1,7 @@ +output "multi-line-lf" { + value = "foo" + description = <<-EOT + The quick brown fox jumps + over the lazy dog + EOT +}