sters / yaml-diff

Make a diff between 2 yaml files.
MIT License
30 stars 7 forks source link

bug with yaml string wrapping syntax #29

Open dionysius opened 1 year ago

dionysius commented 1 year ago

Testcase:

package main

import (
    "testing"

    "github.com/sters/yaml-diff/yamldiff"
)

var yaml1 = `
value: |-
  foo
  bar
  baz
  special
    multiline
`

var yaml2 = `
value: "foo\nbar\nbaz\n\
special\n\
\  multiline"
`

func Test_PodYamlDiff(t *testing.T) {
    t.Parallel()

    yaml1Source, err := yamldiff.Load(yaml1)
    if err != nil {
        t.Error(err)
    }

    yaml2Source, err := yamldiff.Load(yaml2)
    if err != nil {
        t.Error(err)
    }

    diffs := yamldiff.Do(yaml1Source, yaml2Source)
    if len(diffs) > 0 {
        for _, diff := range diffs {
            if diff.Status() != yamldiff.DiffStatusSame {
                t.Log("diff detected\n" + diff.Dump())
                t.Fail()
            }
        }
    }
}

Result:

--- FAIL: Test_PodYamlDiff (0.00s)
    .../main_test.go:41: diff detected
        - value: "foo
        bar
        baz
        special
          multiline"
        + value: "foo
        bar
        baz
        \ special
        \ \  multiline"

FAIL

The handling of \n in strings seems to be fine, the issue seems to be:

I ran into a diff issue after using yq to manipulate some yaml files. The problem is not yq tho, the resulting yaml seems to be a correct yaml. If you copy those two yamls to e.g. https://www.yamldiff.com/ it doesn't yield a diff: Screenshot_20221121_143822

dionysius commented 1 year ago

Replaced yq with gojq for the manipulation steps since I anyway wanted to move away from python helpers. gojq --yaml-input --yaml-output seems not to do word wrapping so I don't run into this issue anymore. Bug is still valid though.

sters commented 1 year ago

@dionysius It seems caused by goccy/go-yaml. I'm trying to fix it here: https://github.com/goccy/go-yaml/pull/342