You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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"
)
typeStructstruct {
Valueint
}
funcTestDiff(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
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.
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
):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 env
The text was updated successfully, but these errors were encountered: