Skip to content

Commit

Permalink
configfile: sort members in setEnv (#2417)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcurtis authored Nov 22, 2024
1 parent c0331c7 commit 3c4df04
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 71 deletions.
15 changes: 13 additions & 2 deletions internal/devconfig/configfile/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package configfile

import (
"bytes"
"cmp"
"regexp"
"slices"

Expand Down Expand Up @@ -430,7 +431,10 @@ func (c *configAST) createMemberIfMissing(key string) *hujson.ObjectMember {
i := c.memberIndex(c.root.Value.(*hujson.Object), key)
if i == -1 {
c.root.Value.(*hujson.Object).Members = append(c.root.Value.(*hujson.Object).Members, hujson.ObjectMember{
Name: hujson.Value{Value: hujson.String(key)},
Name: hujson.Value{
Value: hujson.String(key),
BeforeExtra: []byte{'\n'},
},
})
i = len(c.root.Value.(*hujson.Object).Members) - 1
}
Expand All @@ -441,10 +445,17 @@ func mapToObjectMembers(env map[string]string) []hujson.ObjectMember {
members := make([]hujson.ObjectMember, 0, len(env))
for k, v := range env {
members = append(members, hujson.ObjectMember{
Name: hujson.Value{Value: hujson.String(k)},
Name: hujson.Value{
Value: hujson.String(k),
BeforeExtra: []byte{'\n'},
},
Value: hujson.Value{Value: hujson.String(v)},
})
}
// Make the order deterministic so we don't keep moving fields around.
slices.SortFunc(members, func(a, b hujson.ObjectMember) int {
return cmp.Compare(a.Name.Value.(hujson.Literal).String(), b.Name.Value.(hujson.Literal).String())
})
return members
}

Expand Down
69 changes: 0 additions & 69 deletions internal/devconfig/configfile/ast_test.go

This file was deleted.

72 changes: 72 additions & 0 deletions internal/devconfig/configfile/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,78 @@ func TestSetAllowInsecure(t *testing.T) {
}
}

func TestSetEnv(t *testing.T) {
in, want := parseConfigTxtarTest(t, `
-- in --
{}
-- want --
{
"env": {
"BAZ": "qux",
"FOO": "bar"
}
}`)

in.SetEnv(map[string]string{
"FOO": "bar",
"BAZ": "qux",
})
if diff := cmp.Diff(want, in.Bytes(), optParseHujson()); diff != "" {
t.Errorf("wrong parsed config json (-want +got):\n%s", diff)
}
if diff := cmp.Diff(want, in.Bytes()); diff != "" {
t.Errorf("wrong raw config hujson (-want +got):\n%s", diff)
}
}

func TestSetEnvExisting(t *testing.T) {
in, want := parseConfigTxtarTest(t, `
-- in --
{
"env": {
"EXISTING": "value"
}
}
-- want --
{
"env": {
"FOO": "bar"
}
}`)

in.SetEnv(map[string]string{
"FOO": "bar",
})
if diff := cmp.Diff(want, in.Bytes(), optParseHujson()); diff != "" {
t.Errorf("wrong parsed config json (-want +got):\n%s", diff)
}
if diff := cmp.Diff(want, in.Bytes()); diff != "" {
t.Errorf("wrong raw config hujson (-want +got):\n%s", diff)
}
}

func TestSetEnvClear(t *testing.T) {
in, want := parseConfigTxtarTest(t, `
-- in --
{
"env": {
"EXISTING": "value"
}
}
-- want --
{
"env": {}
}`)

in.SetEnv(map[string]string{})
if diff := cmp.Diff(want, in.Bytes(), optParseHujson()); diff != "" {
t.Errorf("wrong parsed config json (-want +got):\n%s", diff)
}
if diff := cmp.Diff(want, in.Bytes()); diff != "" {
t.Errorf("wrong raw config hujson (-want +got):\n%s", diff)
}
}

func TestNixpkgsValidation(t *testing.T) {
testCases := map[string]struct {
commit string
Expand Down

0 comments on commit 3c4df04

Please sign in to comment.