Skip to content

Commit 3dd2dbe

Browse files
committed
sort output of Write/Marshal
1 parent 9f04f40 commit 3dd2dbe

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

godotenv.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os"
2222
"os/exec"
2323
"regexp"
24+
"sort"
2425
"strings"
2526
)
2627

@@ -165,6 +166,7 @@ func Marshal(envMap map[string]string) (string, error) {
165166
for k, v := range envMap {
166167
lines = append(lines, fmt.Sprintf(`%s="%s"`, k, doubleQuoteEscape(v)))
167168
}
169+
sort.Strings(lines)
168170
return strings.Join(lines, "\n"), nil
169171
}
170172

godotenv_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ func TestWrite(t *testing.T) {
348348
writeAndCompare(`key=va'lu'e`, `key="va'lu'e"`)
349349
// newlines, backslashes, and some other special chars are escaped
350350
writeAndCompare(`foo="$ba\n\r\\r!"`, `foo="\$ba\n\r\\r\!"`)
351+
// lines should be sorted
352+
writeAndCompare("foo=bar\nbaz=buzz", "baz=\"buzz\"\nfoo=\"bar\"")
353+
351354
}
352355

353356
func TestRoundtrip(t *testing.T) {

0 commit comments

Comments
 (0)