From 9f04f40640aee726bfd070c77d77ba7177e0551b Mon Sep 17 00:00:00 2001 From: Alex Quick Date: Sat, 16 Sep 2017 17:55:04 -0400 Subject: [PATCH 1/2] Be more careful with TestRoundtrip --- godotenv_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/godotenv_test.go b/godotenv_test.go index 47b0c35..654ee20 100644 --- a/godotenv_test.go +++ b/godotenv_test.go @@ -351,20 +351,20 @@ func TestWrite(t *testing.T) { } func TestRoundtrip(t *testing.T) { - fixtures := []string{"equals.env", "exported.env", "invalid1.env", "plain.env", "quoted.env"} + fixtures := []string{"equals.env", "exported.env", "plain.env", "quoted.env"} for _, fixture := range fixtures { fixtureFilename := fmt.Sprintf("fixtures/%s", fixture) env, err := readFile(fixtureFilename) if err != nil { - continue + t.Errorf("Expected '%s' to read without error (%v)", fixtureFilename, err) } rep, err := Marshal(env) if err != nil { - continue + t.Errorf("Expected '%s' to Marshal (%v)", fixtureFilename, err) } roundtripped, err := Unmarshal(rep) if err != nil { - continue + t.Errorf("Expected '%s' to Mashal and Unmarshal (%v)", fixtureFilename, err) } if !reflect.DeepEqual(env, roundtripped) { t.Errorf("Expected '%s' to roundtrip as '%v', got '%v' instead", fixtureFilename, env, roundtripped) From 3dd2dbe832c5bf3f32b969aa3e3e3ebbc9dcc40d Mon Sep 17 00:00:00 2001 From: Alex Quick Date: Sat, 16 Sep 2017 17:58:01 -0400 Subject: [PATCH 2/2] 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) {