From 5cf0adcc5b1f05537c68234dba216e9a0882a705 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 9 Jun 2026 14:23:11 +1000 Subject: [PATCH] Adding some tests --- pkg/yqlib/candidate_node_test.go | 25 +++++++++++++++++++ .../operators/anchor-and-alias-operators.md | 2 +- .../headers/anchor-and-alias-operators.md | 2 +- pkg/yqlib/goccy_yaml_test.go | 2 +- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/pkg/yqlib/candidate_node_test.go b/pkg/yqlib/candidate_node_test.go index 58f436d7..0e8878bc 100644 --- a/pkg/yqlib/candidate_node_test.go +++ b/pkg/yqlib/candidate_node_test.go @@ -106,6 +106,31 @@ func TestCreateScalarNodeScenarios(t *testing.T) { } } +type createStringScalarNodeScenario struct { + stringValue string + expectedTag string +} + +var createStringScalarNodeScenarios = []createStringScalarNodeScenario{ + { + stringValue: "yourKey", + expectedTag: "!!str", + }, + { + stringValue: "<<", + expectedTag: "!!merge", + }, +} + +func TestCreateStringScalarNodeScenarios(t *testing.T) { + for _, tt := range createStringScalarNodeScenarios { + actual := createStringScalarNode(tt.stringValue) + test.AssertResultWithContext(t, tt.stringValue, actual.Value, fmt.Sprintf("Value for: %v", tt.stringValue)) + test.AssertResultWithContext(t, tt.expectedTag, actual.Tag, fmt.Sprintf("Tag for: %v", tt.stringValue)) + test.AssertResultWithContext(t, ScalarNode, actual.Kind, fmt.Sprintf("Kind for: %v", tt.stringValue)) + } +} + func TestGetKeyForMapValue(t *testing.T) { key := createStringScalarNode("yourKey") n := CandidateNode{Key: key, Value: "meow", document: 3} diff --git a/pkg/yqlib/doc/operators/anchor-and-alias-operators.md b/pkg/yqlib/doc/operators/anchor-and-alias-operators.md index a7ecdb55..1d33f737 100644 --- a/pkg/yqlib/doc/operators/anchor-and-alias-operators.md +++ b/pkg/yqlib/doc/operators/anchor-and-alias-operators.md @@ -2,7 +2,7 @@ Use the `alias` and `anchor` operators to read and write yaml aliases and anchors. The `explode` operator normalises a yaml file (dereference (or expands) aliases and remove anchor names). -`yq` supports merge aliases (like `<<: *blah`) however this is no longer in the standard yaml spec (1.2) and so `yq` will automatically add the `!!merge` tag to these nodes as it is effectively a custom tag. +`yq` supports merge keys (like `<<: *blah`) from YAML 1.1. These are no longer part of the YAML 1.2 standard, but remain common in practice. Plain `<<:` keys are recognised as merge keys and round-trip as `<<:` without an explicit `!!merge` tag. When the source uses an explicit `!!merge` tag, that is preserved on output. Internally, when `yq` synthesises a `<<` map key (for example during merge operations), it tags the key as `!!merge` rather than `!!str`. ## NOTE --yaml-fix-merge-anchor-to-spec flag diff --git a/pkg/yqlib/doc/operators/headers/anchor-and-alias-operators.md b/pkg/yqlib/doc/operators/headers/anchor-and-alias-operators.md index 610c364c..1fc7fae3 100644 --- a/pkg/yqlib/doc/operators/headers/anchor-and-alias-operators.md +++ b/pkg/yqlib/doc/operators/headers/anchor-and-alias-operators.md @@ -2,7 +2,7 @@ Use the `alias` and `anchor` operators to read and write yaml aliases and anchors. The `explode` operator normalises a yaml file (dereference (or expands) aliases and remove anchor names). -`yq` supports merge aliases (like `<<: *blah`) however this is no longer in the standard yaml spec (1.2) and so `yq` will automatically add the `!!merge` tag to these nodes as it is effectively a custom tag. +`yq` supports merge keys (like `<<: *blah`) from YAML 1.1. These are no longer part of the YAML 1.2 standard, but remain common in practice. Plain `<<:` keys are recognised as merge keys and round-trip as `<<:` without an explicit `!!merge` tag. When the source uses an explicit `!!merge` tag, that is preserved on output. Internally, when `yq` synthesises a `<<` map key (for example during merge operations), it tags the key as `!!merge` rather than `!!str`. ## NOTE --yaml-fix-merge-anchor-to-spec flag diff --git a/pkg/yqlib/goccy_yaml_test.go b/pkg/yqlib/goccy_yaml_test.go index e2c74a83..338e8e54 100644 --- a/pkg/yqlib/goccy_yaml_test.go +++ b/pkg/yqlib/goccy_yaml_test.go @@ -230,7 +230,7 @@ var goccyYamlFormatScenarios = []formatScenario{ description: "merge anchor", skipDoc: true, input: "a: &remember\n c: mike\nb:\n <<: *remember", - expected: "a: &remember\n c: mike\nb:\n <<: *remember\n", + expected: "a: &remember\n c: mike\nb:\n <<: *remember\n", }, { description: "custom tag",