Skip to content

Commit e72f215

Browse files
committed
Always convert CRLF to LF for output description
Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
1 parent 05d432e commit e72f215

File tree

7 files changed

+52
-3
lines changed

7 files changed

+52
-3
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,8 @@ trim_trailing_whitespace = false
4040
[**/inputs-crlf/variables.tf]
4141
end_of_line = crlf
4242

43+
[**/outputs-crlf/outputs.tf]
44+
end_of_line = crlf
45+
4346
[Makefile]
4447
indent_style = tab

terraform/load.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ func loadOutputs(tfmodule *tfconfig.Module, config *print.Config) ([]*Output, er
278278
}
279279
}
280280
for _, o := range tfmodule.Outputs {
281-
description := o.Description
281+
// convert CRLF to LF early on (https://github.com/terraform-docs/terraform-docs/issues/584)
282+
description := strings.ReplaceAll(o.Description, "\r\n", "\n")
282283
if description == "" && config.Settings.ReadComments {
283284
description = loadComments(o.Pos.Filename, o.Pos.Line)
284285
}

terraform/load_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,37 @@ func TestLoadOutputs(t *testing.T) {
618618
}
619619
}
620620

621+
func TestLoadOutputsLineEnding(t *testing.T) {
622+
tests := []struct {
623+
name string
624+
path string
625+
expected string
626+
}{
627+
{
628+
name: "load module outputs from file with lf line ending",
629+
path: "outputs-lf",
630+
expected: "The quick brown fox jumps\nover the lazy dog\n",
631+
},
632+
{
633+
name: "load module outputs from file with crlf line ending",
634+
path: "outputs-crlf",
635+
expected: "The quick brown fox jumps\nover the lazy dog\n",
636+
},
637+
}
638+
for _, tt := range tests {
639+
t.Run(tt.name, func(t *testing.T) {
640+
assert := assert.New(t)
641+
642+
config := print.NewConfig()
643+
module, _ := loadModule(filepath.Join("testdata", tt.path))
644+
outputs, _ := loadOutputs(module, config)
645+
646+
assert.Equal(1, len(outputs))
647+
assert.Equal(tt.expected, string(outputs[0].Description))
648+
})
649+
}
650+
}
651+
621652
func TestLoadOutputsValues(t *testing.T) {
622653
type expected struct {
623654
outputs int

terraform/testdata/inputs-crlf/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
variable "multi-line-lf" {
1+
variable "multi-line-crlf" {
22
type = string
33
description = <<-EOT
44
The quick brown fox jumps

terraform/testdata/inputs-lf/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
variable "multi-line-crlf" {
1+
variable "multi-line-lf" {
22
type = string
33
description = <<-EOT
44
The quick brown fox jumps
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "multi-line-crlf" {
2+
value = "foo"
3+
description = <<-EOT
4+
The quick brown fox jumps
5+
over the lazy dog
6+
EOT
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "multi-line-lf" {
2+
value = "foo"
3+
description = <<-EOT
4+
The quick brown fox jumps
5+
over the lazy dog
6+
EOT
7+
}

0 commit comments

Comments
 (0)