From 06e944dcb620742b1687eb4d1f76d59b4a642169 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Mon, 7 Feb 2022 11:26:48 +1100 Subject: [PATCH] Added github action fix for parsing xml --- acceptance_tests/inputs-format.sh | 18 ++++++++++++++++++ pkg/yqlib/decoder_xml.go | 5 +++++ pkg/yqlib/xml_test.go | 31 ++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/acceptance_tests/inputs-format.sh b/acceptance_tests/inputs-format.sh index 0727150d..4c76cd2e 100755 --- a/acceptance_tests/inputs-format.sh +++ b/acceptance_tests/inputs-format.sh @@ -22,4 +22,22 @@ EOM assertEquals "$expected" "$X" } +testInputXmlGithubAction() { + cat >test.yml <BiBi +EOL + + read -r -d '' expected << EOM +cat: + +content: BiBi + +legs: "4" +EOM + + X=$(cat /dev/null | ./yq e -p=xml test.yml) + assertEquals "$expected" "$X" + + X=$(cat /dev/null | ./yq ea -p=xml test.yml) + assertEquals "$expected" "$X" +} + source ./scripts/shunit2 \ No newline at end of file diff --git a/pkg/yqlib/decoder_xml.go b/pkg/yqlib/decoder_xml.go index 25b0f891..04c8f9e5 100644 --- a/pkg/yqlib/decoder_xml.go +++ b/pkg/yqlib/decoder_xml.go @@ -126,6 +126,9 @@ func (dec *xmlDecoder) convertToYamlNode(n *xmlNode) (*yaml.Node, error) { return dec.createMap(n) } scalar := createScalarNode(n.Data, n.Data) + if n.Data == "" { + scalar = createScalarNode(nil, "") + } log.Debug("scalar headC: %v, footC: %v", n.HeadComment, n.FootComment) scalar.HeadComment = dec.processComment(n.HeadComment) scalar.LineComment = dec.processComment(n.LineComment) @@ -149,6 +152,8 @@ func (dec *xmlDecoder) Decode(rootYamlNode *yaml.Node) error { if err != nil { return err + } else if firstNode.Tag == "!!null" { + return io.EOF } rootYamlNode.Kind = yaml.DocumentNode rootYamlNode.Content = []*yaml.Node{firstNode} diff --git a/pkg/yqlib/xml_test.go b/pkg/yqlib/xml_test.go index 01a51f15..3e9d5ae4 100644 --- a/pkg/yqlib/xml_test.go +++ b/pkg/yqlib/xml_test.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "fmt" + "io" "strings" "testing" @@ -18,7 +19,7 @@ func decodeXml(t *testing.T, s formatScenario) *CandidateNode { node := &yaml.Node{} err := decoder.Decode(node) - if err != nil { + if err != nil && err != io.EOF { t.Error(err, "fail to decode", s.input) } @@ -266,6 +267,34 @@ var xmlScenarios = []formatScenario{ expected: expectedDecodeYamlWithComments, scenarioType: "decode", }, + { + description: "Empty doc", + skipDoc: true, + input: "", + expected: "D0, P[], ()::null\n", + scenarioType: "decode", + }, + { + description: "Empty single node", + skipDoc: true, + input: "", + expected: "D0, P[], (doc)::a:\n", + scenarioType: "decode", + }, + { + description: "Empty close node", + skipDoc: true, + input: "", + expected: "D0, P[], (doc)::a:\n", + scenarioType: "decode", + }, + { + description: "Nested empty", + skipDoc: true, + input: "", + expected: "D0, P[], (doc)::a:\n b:\n", + scenarioType: "decode", + }, { description: "Parse xml: with comments subchild", skipDoc: true,