cupaloy icon indicating copy to clipboard operation
cupaloy copied to clipboard

Single vs double spaced output

Open mattharrigan opened this issue 3 years ago • 6 comments

I am getting inconsistent test failures. The difference is sometimes there are single spaces in the snapshot of a variable, and sometimes there are double spaces. Any ideas on why or how to fix? Thanks.

From go.sum

github.com/bradleyjkemp/cupaloy v2.3.0+incompatible h1:UafIjBvWQmS9i/xRg+CamMrnLTKNzo+bdmT/oH34c2Y=
github.com/bradleyjkemp/cupaloy v2.3.0+incompatible/go.mod h1:Au1Xw1sgaJ5iSFktEhYsS0dbQiS1B0/XMXl+42y9Ilk=
...
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

mattharrigan avatar Jan 25 '21 21:01 mattharrigan

Hey @mattharrigan do you have an example of a test where you're seeing this inconsistency? By double spaces are you meaning between the snapshots of variables or actually within the snapshot of a single variable?

Sorry for not replying sooner, the GitHub notification for this is showing as 6 hours ago 🤔

bradleyjkemp avatar Mar 10 '21 12:03 bradleyjkemp

Hey @bradleyjkemp, I'm getting the same issue. The snapshot output for all tests seem to change from single / double spaces at random.

    --- FAIL: Test_Repository/get_opening_hours (0.00s)
        client_test.go:223: snapshot not equal:
            --- Previous
            +++ Current
            @@ -1,2 +1,2 @@
            -(*api.GetOpeningHoursSpecificationResponse)(specification:{dayOfWeek:2  opens:"11:00"  closes:"22:00"  validFrom:"3000-11-01T00:00:00Z"  validThrough:"3000-12-01T00:00:00Z"})
            +(*api.GetOpeningHoursSpecificationResponse)(specification:{dayOfWeek:2 opens:"11:00" closes:"22:00" validFrom:"3000-11-01T00:00:00Z" validThrough:"3000-12-01T00:00:00Z"})
{
	name: "get opening hours",
	test: func(t *testing.T) {
		ctx := context.Background()
		hours, err := repository.GetOpeningHoursSpecification(ctx, &api.GetOpeningHoursSpecificationRequest{
			VenueId: UUID,
			Date:    "3000-11-11T00:00:00Z",
		})
		require.NoError(t, err)

		cupaloy.SnapshotT(t, hours)
	},
},

cobbinma avatar Mar 16 '21 09:03 cobbinma

Ah are you using protobuf by any chance?

The default protobuf String() method intentionally introduces randomness: https://github.com/golang/protobuf/issues/1269

Cupaloy shouldn't really be using the String() method by default but it'd be a breaking change to disable the behaviour. Cupaloy should probably have an option to disable this though. An alternative is to marshal your protobuf message yourself before passing it to Cupaloy

bradleyjkemp avatar Mar 16 '21 12:03 bradleyjkemp

thanks @bradleyjkemp 👍 , didn't know about that protobuf behaviour

cobbinma avatar Mar 16 '21 16:03 cobbinma

cupaloy.New(cupaloy.UseStringerMethods(false)).SnapshotT(t, hours)

cobbinma avatar Mar 17 '21 10:03 cobbinma

Ah awesome there already is an option for disabling String() methods!

bradleyjkemp avatar Mar 17 '21 10:03 bradleyjkemp