Skip to content

Commit

Permalink
Merge pull request #587 from khos2ow/output-crlf
Browse files Browse the repository at this point in the history
Always convert CRLF to LF for output description
  • Loading branch information
khos2ow committed Jan 8, 2022
2 parents 05d432e + e72f215 commit f5c7c6f
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion terraform/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
31 changes: 31 additions & 0 deletions terraform/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion terraform/testdata/inputs-crlf/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "multi-line-lf" {
variable "multi-line-crlf" {
type = string
description = <<-EOT
The quick brown fox jumps
Expand Down
2 changes: 1 addition & 1 deletion terraform/testdata/inputs-lf/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "multi-line-crlf" {
variable "multi-line-lf" {
type = string
description = <<-EOT
The quick brown fox jumps
Expand Down
7 changes: 7 additions & 0 deletions terraform/testdata/outputs-crlf/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "multi-line-crlf" {
value = "foo"
description = <<-EOT
The quick brown fox jumps
over the lazy dog
EOT
}
7 changes: 7 additions & 0 deletions terraform/testdata/outputs-lf/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "multi-line-lf" {
value = "foo"
description = <<-EOT
The quick brown fox jumps
over the lazy dog
EOT
}

0 comments on commit f5c7c6f

Please sign in to comment.