diff --git a/properties.go b/properties.go index 03abec2..33c108c 100644 --- a/properties.go +++ b/properties.go @@ -539,21 +539,32 @@ func (p *Properties) WriteComment(w io.Writer, prefix string, enc Encoding) (n i if prefix != "" { if comments, ok := p.c[key]; ok { - // add a blank line between entries but not at the top - if len(comments) > 0 && n > 0 { - x, err = fmt.Fprintln(w) - if err != nil { - return + // don't print comments if they are all empty + allEmpty := true + for _, c := range comments { + if c != "" { + allEmpty = false + break } - n += x } - for _, c := range comments { - x, err = fmt.Fprintf(w, "%s%s\n", prefix, encode(c, "", enc)) - if err != nil { - return + if !allEmpty { + // add a blank line between entries but not at the top + if len(comments) > 0 && n > 0 { + x, err = fmt.Fprintln(w) + if err != nil { + return + } + n += x + } + + for _, c := range comments { + x, err = fmt.Fprintf(w, "%s%s\n", prefix, encode(c, "", enc)) + if err != nil { + return + } + n += x } - n += x } } } diff --git a/properties_test.go b/properties_test.go index 6f1c5c1..36ff293 100644 --- a/properties_test.go +++ b/properties_test.go @@ -116,6 +116,7 @@ var commentTests = []struct { comments []string }{ {"key=value", "key", "value", nil}, + {"#\nkey=value", "key", "value", []string{""}}, {"#comment\nkey=value", "key", "value", []string{"comment"}}, {"# comment\nkey=value", "key", "value", []string{"comment"}}, {"# comment\nkey=value", "key", "value", []string{"comment"}}, @@ -176,6 +177,8 @@ var writeCommentTests = []struct { }{ // ISO-8859-1 tests {"key = value", "key = value\n", "ISO-8859-1"}, + {"#\nkey = value", "key = value\n", "ISO-8859-1"}, + {"#\n#\n#\nkey = value", "key = value\n", "ISO-8859-1"}, {"# comment\nkey = value", "# comment\nkey = value\n", "ISO-8859-1"}, {"\n# comment\nkey = value", "# comment\nkey = value\n", "ISO-8859-1"}, {"# comment\n\nkey = value", "# comment\nkey = value\n", "ISO-8859-1"}, @@ -247,11 +250,11 @@ var parsedDurationTests = []struct { def, value time.Duration }{ // valid values - {"key = -1ns", "key", 999, -1*time.Nanosecond}, - {"key = 300ms", "key", 999, 300*time.Millisecond}, - {"key = 5s", "key", 999, 5*time.Second}, - {"key = 3h", "key", 999, 3*time.Hour}, - {"key = 2h45m", "key", 999, 2*time.Hour+45*time.Minute}, + {"key = -1ns", "key", 999, -1 * time.Nanosecond}, + {"key = 300ms", "key", 999, 300 * time.Millisecond}, + {"key = 5s", "key", 999, 5 * time.Second}, + {"key = 3h", "key", 999, 3 * time.Hour}, + {"key = 2h45m", "key", 999, 2*time.Hour + 45*time.Minute}, // invalid values {"key = 0xff", "key", 999, 999},