sort output of Write/Marshal

pull/42/head
Alex Quick 2017-09-16 17:58:01 -04:00
parent 9f04f40640
commit 3dd2dbe832
2 changed files with 5 additions and 0 deletions

View File

@ -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
}

View File

@ -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) {