google / go-cmp

Package for comparing Go values in tests
BSD 3-Clause "New" or "Revised" License
4.08k stars 209 forks source link

Inconsistent whitespace produced by cmp.Diff #344

Closed akupila closed 7 months ago

akupila commented 7 months ago

I ran into an issue when using cmp.Diff output in an example test. The test would sometimes fail, sometimes not. The output looked the same, so it was a bit tricky to find what was happening.

Turns out cmp.Diff can sometimes prefix the output with a space (0x20) and sometimes with NBSP (0xC2 0xA0):

module github.com/akupila/cmpdiff

go 1.21.3

require github.com/google/go-cmp v0.6.0
package example

import (
    "encoding/hex"
    "fmt"
    "testing"

    "github.com/google/go-cmp/cmp"
)

type Struct struct {
    Value int
}

func TestDiff(t *testing.T) {
    a := Struct{Value: 1}
    b := Struct{Value: 2}
    diff := cmp.Diff(a, b)

    // Sometimes, the first character is a Space (0x20), sometimes it's a NBSP (0xC2 0xA0)
    fmt.Println(hex.EncodeToString([]byte(diff)))
}

I'm not able to make the behavior change when using -count, but a subsequent run can produce different values in what appears to be random:

$ go test -v -count=5
=== RUN   TestDiff
20206578616d706c652e5374727563747b0a2d200956616c75653a20312c0a2b200956616c75653a20322c0a20207d0a
--- PASS: TestDiff (0.00s)
=== RUN   TestDiff
20206578616d706c652e5374727563747b0a2d200956616c75653a20312c0a2b200956616c75653a20322c0a20207d0a
--- PASS: TestDiff (0.00s)
=== RUN   TestDiff
20206578616d706c652e5374727563747b0a2d200956616c75653a20312c0a2b200956616c75653a20322c0a20207d0a
--- PASS: TestDiff (0.00s)
=== RUN   TestDiff
20206578616d706c652e5374727563747b0a2d200956616c75653a20312c0a2b200956616c75653a20322c0a20207d0a
--- PASS: TestDiff (0.00s)
=== RUN   TestDiff
20206578616d706c652e5374727563747b0a2d200956616c75653a20312c0a2b200956616c75653a20322c0a20207d0a
--- PASS: TestDiff (0.00s)
PASS
ok      github.com/akupila/cmpdiff      0.155s
$ go test -v -count=5
=== RUN   TestDiff
c2a0c2a06578616d706c652e5374727563747b0a2dc2a00956616c75653a20312c0a2bc2a00956616c75653a20322c0ac2a0c2a07d0a
--- PASS: TestDiff (0.00s)
=== RUN   TestDiff
c2a0c2a06578616d706c652e5374727563747b0a2dc2a00956616c75653a20312c0a2bc2a00956616c75653a20322c0ac2a0c2a07d0a
--- PASS: TestDiff (0.00s)
=== RUN   TestDiff
c2a0c2a06578616d706c652e5374727563747b0a2dc2a00956616c75653a20312c0a2bc2a00956616c75653a20322c0ac2a0c2a07d0a
--- PASS: TestDiff (0.00s)
=== RUN   TestDiff
c2a0c2a06578616d706c652e5374727563747b0a2dc2a00956616c75653a20312c0a2bc2a00956616c75653a20322c0ac2a0c2a07d0a
--- PASS: TestDiff (0.00s)
=== RUN   TestDiff
c2a0c2a06578616d706c652e5374727563747b0a2dc2a00956616c75653a20312c0a2bc2a00956616c75653a20322c0ac2a0c2a07d0a
--- PASS: TestDiff (0.00s)
PASS
ok      github.com/akupila/cmpdiff      0.156s
Go env

``` $ go env GO111MODULE='' GOARCH='arm64' GOBIN='' GOCACHE='/Users/akupila/Library/Caches/go-build' GOENV='/Users/akupila/Library/Application Support/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='arm64' GOHOSTOS='darwin' GOINSECURE='' GOMODCACHE='/Users/akupila/go/pkg/mod' GONOPROXY='github.com/GetStream' GONOSUMDB='github.com/GetStream' GOOS='darwin' GOPATH='/Users/akupila/go' GOPRIVATE='github.com/GetStream' GOPROXY='https://proxy.golang.org,direct' GOROOT='/nix/store/1fxz1flmv4a4m5pvjmmzxlaznjzybjcp-go-1.21.3/share/go' GOSUMDB='sum.golang.org' GOTMPDIR='' GOTOOLCHAIN='auto' GOTOOLDIR='/nix/store/1fxz1flmv4a4m5pvjmmzxlaznjzybjcp-go-1.21.3/share/go/pkg/tool/darwin_arm64' GOVCS='' GOVERSION='go1.21.3' GCCGO='gccgo' AR='ar' CC='clang' CXX='clang++' CGO_ENABLED='1' GOMOD='/Users/akupila/scratch/cmpdiff/go.mod' GOWORK='' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g' CGO_FFLAGS='-O2 -g' CGO_LDFLAGS='-O2 -g' PKG_CONFIG='pkg-config' GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/v9/8tv8ybjx47g9j_gdrfndk8xr0000gn/T/go-build2610279179=/tmp/go-build -gno-record-gcc-switches -fno-common' ```

dsnet commented 7 months ago

Hi @akupila, sorry to hear about your struggle. This is working as intended. The documentation on Diff says:

Do not depend on this output being stable. If you need the ability to programmatically interpret the difference, consider using a custom Reporter.

The output of Diff has in the past and will continue to change because it is intended to evolve to be more and more humanly readable. Codifying it's output in something like an example test would mean that the test is brittle and breaks whenever Diff's output changes again. The instability of the output was done to deliberately make it harder to accidentally assume that the output was stable.

akupila commented 7 months ago

Oh, i didn't notice that in the docs, but it makes sense. Thanks for this, I'll use a custom Reporter 👍

Thanks also for all the work you've put into go-cmp 🙂