From 3dd2dbe832c5bf3f32b969aa3e3e3ebbc9dcc40d Mon Sep 17 00:00:00 2001 From: Alex Quick Date: Sat, 16 Sep 2017 17:58:01 -0400 Subject: [PATCH] sort output of Write/Marshal --- godotenv.go | 2 ++ godotenv_test.go | 3 +++ 2 files changed, 5 insertions(+) diff --git a/godotenv.go b/godotenv.go index 2710572..48ae78c 100644 --- a/godotenv.go +++ b/godotenv.go @@ -21,6 +21,7 @@ import ( "os" "os/exec" "regexp" + "sort" "strings" ) @@ -165,6 +166,7 @@ func Marshal(envMap map[string]string) (string, error) { for k, v := range envMap { lines = append(lines, fmt.Sprintf(`%s="%s"`, k, doubleQuoteEscape(v))) } + sort.Strings(lines) return strings.Join(lines, "\n"), nil } diff --git a/godotenv_test.go b/godotenv_test.go index 654ee20..91f4363 100644 --- a/godotenv_test.go +++ b/godotenv_test.go @@ -348,6 +348,9 @@ func TestWrite(t *testing.T) { writeAndCompare(`key=va'lu'e`, `key="va'lu'e"`) // newlines, backslashes, and some other special chars are escaped writeAndCompare(`foo="$ba\n\r\\r!"`, `foo="\$ba\n\r\\r\!"`) + // lines should be sorted + writeAndCompare("foo=bar\nbaz=buzz", "baz=\"buzz\"\nfoo=\"bar\"") + } func TestRoundtrip(t *testing.T) {