deterministic order for walk, fixes tests

This commit is contained in:
Shreyas Goenka 2024-05-20 23:14:44 +02:00
parent 09975a02ef
commit 6302e58965
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
4 changed files with 16 additions and 13 deletions

View File

@ -30,17 +30,6 @@ func TestRootLoad(t *testing.T) {
assert.Equal(t, "basic", root.Bundle.Name)
}
func TestDuplicateIdOnMergeReturnsErrorForJobAndJob(t *testing.T) {
root, diags := Load("./testdata/duplicate_resource_name_in_subconfiguration_job_and_job/databricks.yml")
require.NoError(t, diags.Error())
other, diags := Load("./testdata/duplicate_resource_name_in_subconfiguration_job_and_job/resources.yml")
require.NoError(t, diags.Error())
err := root.Merge(other)
assert.ErrorContains(t, err, "multiple resources named foo (job at ./testdata/duplicate_resource_name_in_subconfiguration_job_and_job/databricks.yml:10:7, job at ./testdata/duplicate_resource_name_in_subconfiguration_job_and_job/resources.yml:4:7)")
}
func TestInitializeVariables(t *testing.T) {
fooDefault := "abc"
root := &Root{

View File

@ -20,7 +20,7 @@ func TestValidateUniqueResourceIdentifiers(t *testing.T) {
},
{
name: "duplicate_resource_names_in_root_job_and_experiment",
errorMsg: "multiple resources named foo (jobs.foo at validate/duplicate_resource_names_in_root_job_and_experiment/databricks.yml:10:7, experiments.foo at validate/duplicate_resource_names_in_root_job_and_experiment/databricks.yml:18:7)",
errorMsg: "multiple resources named foo (experiments.foo at validate/duplicate_resource_names_in_root_job_and_experiment/databricks.yml:18:7, jobs.foo at validate/duplicate_resource_names_in_root_job_and_experiment/databricks.yml:10:7)",
},
{
name: "duplicate_resource_name_in_subconfiguration",

View File

@ -4,6 +4,7 @@ import (
"fmt"
"maps"
"slices"
"sort"
)
// Pair represents a single key-value pair in a Mapping.
@ -51,6 +52,17 @@ func (m Mapping) Pairs() []Pair {
return m.pairs
}
// SortedPairs returns all the key-value pairs in the Mapping, sorted by key in
// lexicographic order.
func (m Mapping) SortedPairs() []Pair {
pairs := make([]Pair, len(m.pairs))
copy(pairs, m.pairs)
sort.Slice(pairs, func(i, j int) bool {
return pairs[i].Key.MustString() < pairs[j].Key.MustString()
})
return pairs
}
// Len returns the number of key-value pairs in the Mapping.
func (m Mapping) Len() int {
return len(m.pairs)

View File

@ -35,7 +35,9 @@ func walk(v Value, p Path, fn func(p Path, v Value) (Value, error)) (Value, erro
case KindMap:
m := v.MustMap()
out := newMappingWithSize(m.Len())
for _, pair := range m.Pairs() {
// Iterate over the pairs in sorted order to ensure a deterministic order.
for _, pair := range m.SortedPairs() {
pk := pair.Key
pv := pair.Value
nv, err := walk(pv, append(p, Key(pk.MustString())), fn)