Do not emit warning on YAML anchor blocks (#1354)

## Changes
In 0.217.0 we started to emit warning on unknown fields in YAML
configuration but wrongly considered YAML anchor blocks as unknown
field.

This PR fixes this by skipping normalising of YAML blocks.

## Tests
Added regression tests
This commit is contained in:
Andrew Nester 2024-04-10 11:55:02 +02:00 committed by GitHub
parent d0642023cb
commit d914a1b1e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 68 additions and 9 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config/mutator" "github.com/databricks/cli/bundle/config/mutator"
"github.com/databricks/cli/bundle/phases" "github.com/databricks/cli/bundle/phases"
"github.com/databricks/cli/libs/diag"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -20,9 +21,18 @@ func load(t *testing.T, path string) *bundle.Bundle {
} }
func loadTarget(t *testing.T, path, env string) *bundle.Bundle { func loadTarget(t *testing.T, path, env string) *bundle.Bundle {
b, diags := loadTargetWithDiags(path, env)
require.NoError(t, diags.Error())
return b
}
func loadTargetWithDiags(path, env string) (*bundle.Bundle, diag.Diagnostics) {
ctx := context.Background() ctx := context.Background()
b, err := bundle.Load(ctx, path) b, err := bundle.Load(ctx, path)
require.NoError(t, err) if err != nil {
return nil, diag.FromErr(err)
}
diags := bundle.Apply(ctx, b, bundle.Seq( diags := bundle.Apply(ctx, b, bundle.Seq(
phases.LoadNamedTarget(env), phases.LoadNamedTarget(env),
mutator.RewriteSyncPaths(), mutator.RewriteSyncPaths(),
@ -30,6 +40,5 @@ func loadTarget(t *testing.T, path, env string) *bundle.Bundle {
mutator.MergeJobTasks(), mutator.MergeJobTasks(),
mutator.MergePipelineClusters(), mutator.MergePipelineClusters(),
)) ))
require.NoError(t, diags.Error()) return b, diags
return b
} }

View File

@ -0,0 +1,15 @@
bundle:
name: yaml_anchors_separate_block
tags: &custom_tags
Tag1: "Value1"
Tag2: "Value2"
Tag3: "Value3"
resources:
jobs:
my_job:
tasks:
- task_key: yaml_anchors_separate_block
tags:
<<: *custom_tags

View File

@ -19,6 +19,18 @@ func TestYAMLAnchors(t *testing.T) {
require.NotNil(t, t0) require.NotNil(t, t0)
require.NotNil(t, t1) require.NotNil(t, t1)
require.NotNil(t, t0.NewCluster)
require.NotNil(t, t1.NewCluster)
assert.Equal(t, "10.4.x-scala2.12", t0.NewCluster.SparkVersion) assert.Equal(t, "10.4.x-scala2.12", t0.NewCluster.SparkVersion)
assert.Equal(t, "10.4.x-scala2.12", t1.NewCluster.SparkVersion) assert.Equal(t, "10.4.x-scala2.12", t1.NewCluster.SparkVersion)
} }
func TestYAMLAnchorsNoWarnings(t *testing.T) {
_, diags := loadTargetWithDiags("./yaml_anchors", "default")
assert.Empty(t, diags)
}
func TestYAMLAnchorsSeparateBlockNoWarnings(t *testing.T) {
_, diags := loadTargetWithDiags("./yaml_anchors_separate_block", "default")
assert.Empty(t, diags)
}

View File

@ -89,14 +89,17 @@ func (n normalizeOptions) normalizeStruct(typ reflect.Type, src dyn.Value, seen
for _, pair := range src.MustMap().Pairs() { for _, pair := range src.MustMap().Pairs() {
pk := pair.Key pk := pair.Key
pv := pair.Value pv := pair.Value
index, ok := info.Fields[pk.MustString()] index, ok := info.Fields[pk.MustString()]
if !ok { if !ok {
if !pv.IsAnchor() {
diags = diags.Append(diag.Diagnostic{ diags = diags.Append(diag.Diagnostic{
Severity: diag.Warning, Severity: diag.Warning,
Summary: fmt.Sprintf("unknown field: %s", pk.MustString()), Summary: fmt.Sprintf("unknown field: %s", pk.MustString()),
Location: pk.Location(), Location: pk.Location(),
Path: path, Path: path,
}) })
}
continue continue
} }

View File

@ -659,3 +659,23 @@ func TestNormalizeFloatError(t *testing.T) {
Path: dyn.EmptyPath, Path: dyn.EmptyPath,
}, err[0]) }, err[0])
} }
func TestNormalizeAnchors(t *testing.T) {
type Tmp struct {
Foo string `json:"foo"`
}
var typ Tmp
vin := dyn.V(map[string]dyn.Value{
"foo": dyn.V("bar"),
"anchor": dyn.V("anchor").MarkAnchor(),
})
vout, err := Normalize(typ, vin)
assert.Len(t, err, 0)
// The field that can be mapped to the struct field is retained.
assert.Equal(t, map[string]any{
"foo": "bar",
}, vout.AsAny())
}