Skip to content

Commit 3c4df04

Browse files
authored
configfile: sort members in setEnv (#2417)
1 parent c0331c7 commit 3c4df04

File tree

3 files changed

+85
-71
lines changed

3 files changed

+85
-71
lines changed

internal/devconfig/configfile/ast.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package configfile
22

33
import (
44
"bytes"
5+
"cmp"
56
"regexp"
67
"slices"
78

@@ -430,7 +431,10 @@ func (c *configAST) createMemberIfMissing(key string) *hujson.ObjectMember {
430431
i := c.memberIndex(c.root.Value.(*hujson.Object), key)
431432
if i == -1 {
432433
c.root.Value.(*hujson.Object).Members = append(c.root.Value.(*hujson.Object).Members, hujson.ObjectMember{
433-
Name: hujson.Value{Value: hujson.String(key)},
434+
Name: hujson.Value{
435+
Value: hujson.String(key),
436+
BeforeExtra: []byte{'\n'},
437+
},
434438
})
435439
i = len(c.root.Value.(*hujson.Object).Members) - 1
436440
}
@@ -441,10 +445,17 @@ func mapToObjectMembers(env map[string]string) []hujson.ObjectMember {
441445
members := make([]hujson.ObjectMember, 0, len(env))
442446
for k, v := range env {
443447
members = append(members, hujson.ObjectMember{
444-
Name: hujson.Value{Value: hujson.String(k)},
448+
Name: hujson.Value{
449+
Value: hujson.String(k),
450+
BeforeExtra: []byte{'\n'},
451+
},
445452
Value: hujson.Value{Value: hujson.String(v)},
446453
})
447454
}
455+
// Make the order deterministic so we don't keep moving fields around.
456+
slices.SortFunc(members, func(a, b hujson.ObjectMember) int {
457+
return cmp.Compare(a.Name.Value.(hujson.Literal).String(), b.Name.Value.(hujson.Literal).String())
458+
})
448459
return members
449460
}
450461

internal/devconfig/configfile/ast_test.go

Lines changed: 0 additions & 69 deletions
This file was deleted.

internal/devconfig/configfile/file_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,78 @@ func TestSetAllowInsecure(t *testing.T) {
684684
}
685685
}
686686

687+
func TestSetEnv(t *testing.T) {
688+
in, want := parseConfigTxtarTest(t, `
689+
-- in --
690+
{}
691+
-- want --
692+
{
693+
"env": {
694+
"BAZ": "qux",
695+
"FOO": "bar"
696+
}
697+
}`)
698+
699+
in.SetEnv(map[string]string{
700+
"FOO": "bar",
701+
"BAZ": "qux",
702+
})
703+
if diff := cmp.Diff(want, in.Bytes(), optParseHujson()); diff != "" {
704+
t.Errorf("wrong parsed config json (-want +got):\n%s", diff)
705+
}
706+
if diff := cmp.Diff(want, in.Bytes()); diff != "" {
707+
t.Errorf("wrong raw config hujson (-want +got):\n%s", diff)
708+
}
709+
}
710+
711+
func TestSetEnvExisting(t *testing.T) {
712+
in, want := parseConfigTxtarTest(t, `
713+
-- in --
714+
{
715+
"env": {
716+
"EXISTING": "value"
717+
}
718+
}
719+
-- want --
720+
{
721+
"env": {
722+
"FOO": "bar"
723+
}
724+
}`)
725+
726+
in.SetEnv(map[string]string{
727+
"FOO": "bar",
728+
})
729+
if diff := cmp.Diff(want, in.Bytes(), optParseHujson()); diff != "" {
730+
t.Errorf("wrong parsed config json (-want +got):\n%s", diff)
731+
}
732+
if diff := cmp.Diff(want, in.Bytes()); diff != "" {
733+
t.Errorf("wrong raw config hujson (-want +got):\n%s", diff)
734+
}
735+
}
736+
737+
func TestSetEnvClear(t *testing.T) {
738+
in, want := parseConfigTxtarTest(t, `
739+
-- in --
740+
{
741+
"env": {
742+
"EXISTING": "value"
743+
}
744+
}
745+
-- want --
746+
{
747+
"env": {}
748+
}`)
749+
750+
in.SetEnv(map[string]string{})
751+
if diff := cmp.Diff(want, in.Bytes(), optParseHujson()); diff != "" {
752+
t.Errorf("wrong parsed config json (-want +got):\n%s", diff)
753+
}
754+
if diff := cmp.Diff(want, in.Bytes()); diff != "" {
755+
t.Errorf("wrong raw config hujson (-want +got):\n%s", diff)
756+
}
757+
}
758+
687759
func TestNixpkgsValidation(t *testing.T) {
688760
testCases := map[string]struct {
689761
commit string

0 commit comments

Comments
 (0)