Skip to content

Commit 144189c

Browse files
authored
Merge pull request #42 from alexquick/feature-sorted-output
Sort output of Marshal/Write
2 parents 9739509 + 3dd2dbe commit 144189c

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,23 +348,26 @@ 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) {
354-
fixtures := []string{"equals.env", "exported.env", "invalid1.env", "plain.env", "quoted.env"}
357+
fixtures := []string{"equals.env", "exported.env", "plain.env", "quoted.env"}
355358
for _, fixture := range fixtures {
356359
fixtureFilename := fmt.Sprintf("fixtures/%s", fixture)
357360
env, err := readFile(fixtureFilename)
358361
if err != nil {
359-
continue
362+
t.Errorf("Expected '%s' to read without error (%v)", fixtureFilename, err)
360363
}
361364
rep, err := Marshal(env)
362365
if err != nil {
363-
continue
366+
t.Errorf("Expected '%s' to Marshal (%v)", fixtureFilename, err)
364367
}
365368
roundtripped, err := Unmarshal(rep)
366369
if err != nil {
367-
continue
370+
t.Errorf("Expected '%s' to Mashal and Unmarshal (%v)", fixtureFilename, err)
368371
}
369372
if !reflect.DeepEqual(env, roundtripped) {
370373
t.Errorf("Expected '%s' to roundtrip as '%v', got '%v' instead", fixtureFilename, env, roundtripped)

0 commit comments

Comments
 (0)