diff --git a/pkg/yqlib/operator_load.go b/pkg/yqlib/operator_load.go index 70e77e5c..eda63b72 100644 --- a/pkg/yqlib/operator_load.go +++ b/pkg/yqlib/operator_load.go @@ -48,7 +48,7 @@ func loadYaml(filename string, decoder Decoder) (*CandidateNode, error) { } else { sequenceNode := &CandidateNode{Node: &yaml.Node{Kind: yaml.SequenceNode}} for doc := documents.Front(); doc != nil; doc = doc.Next() { - sequenceNode.Node.Content = append(sequenceNode.Node.Content, doc.Value.(*CandidateNode).Node) + sequenceNode.Node.Content = append(sequenceNode.Node.Content, unwrapDoc(doc.Value.(*CandidateNode).Node)) } return sequenceNode, nil } diff --git a/pkg/yqlib/operator_load_test.go b/pkg/yqlib/operator_load_test.go index 616be363..619b9685 100644 --- a/pkg/yqlib/operator_load_test.go +++ b/pkg/yqlib/operator_load_test.go @@ -5,6 +5,22 @@ import ( ) var loadScenarios = []expressionScenario{ + { + skipDoc: true, + description: "Load empty file", + expression: `load("../../examples/empty.yaml")`, + expected: []string{ + "D0, P[], (!!null)::\n", + }, + }, + { + skipDoc: true, + description: "Load multiple documents", + expression: `load("../../examples/multiple_docs_small.yaml")`, + expected: []string{ + "D0, P[], ()::- a: Easy! as one two three\n- another:\n document: here\n- - 1\n - 2\n", + }, + }, { description: "Simple example", document: `{myFile: "../../examples/thing.yml"}`, diff --git a/test/utils.go b/test/utils.go index 7072cc27..f270c5b3 100644 --- a/test/utils.go +++ b/test/utils.go @@ -23,17 +23,29 @@ func ParseData(rawData string) yaml.Node { return parsedData } +func printDifference(t *testing.T, expectedValue interface{}, actualValue interface{}) { + opts := []write.Option{write.TerminalColor()} + var differenceBuffer bytes.Buffer + expectedString := fmt.Sprintf("%v", expectedValue) + actualString := fmt.Sprintf("%v", actualValue) + if err := diff.Text("expected", "actual", expectedString, actualString, bufio.NewWriter(&differenceBuffer), opts...); err != nil { + t.Error(err) + } else { + t.Error(differenceBuffer.String()) + } +} + func AssertResult(t *testing.T, expectedValue interface{}, actualValue interface{}) { t.Helper() if expectedValue != actualValue { - t.Error("Expected <", expectedValue, "> but got <", actualValue, ">", fmt.Sprintf("%T", actualValue)) + printDifference(t, expectedValue, actualValue) } } func AssertResultComplex(t *testing.T, expectedValue interface{}, actualValue interface{}) { t.Helper() if !reflect.DeepEqual(expectedValue, actualValue) { - t.Error("\nExpected <", expectedValue, ">\nbut got <", actualValue, ">", fmt.Sprintf("%T", actualValue)) + printDifference(t, expectedValue, actualValue) } } @@ -41,7 +53,7 @@ func AssertResultComplexWithContext(t *testing.T, expectedValue interface{}, act t.Helper() if !reflect.DeepEqual(expectedValue, actualValue) { t.Error(context) - t.Error("\nExpected <", expectedValue, ">\nbut got <", actualValue, ">", fmt.Sprintf("%T", actualValue)) + printDifference(t, expectedValue, actualValue) } }