I'm trying to leverage the goldie.AssertWithTemplate feature along with the pattern of using the -update flag when the file doesn't exist.
In regards to the template golden file, I would expect the process to be something like:
Write unit test
Run go test and see it fails with the use -update flag message
Run go test -update and see the example.golden file gets created
Replace Golden in the generated file with {{ .Type }}
Run go test and see everything passes
Write some table driven tests that iterate through many different types
At that point, everything seems good. Then we
Create some more unit tests in that package
Need to generate the initial golden files for those tests, so we run go test -update
Run go test to validate
Tests fail because the templated example.golden file is now overwritten with a single value and doesn't retain the initial {{ .Type }} that we put in there.
I can imagine that it would be relatively simple to go back and change the one overwritten .Type variable in this case. It is a bit more complex, though, if you had a bunch of variables throughout a large .golden file.
Do you have any thoughts on how to preserve the original template variables while doing an -update? One idea is that on -update, the template code could be smart enough to replace all scalar values in the data passed in with {{ .Dot.Path.To.Key }}. That way, when .Type writes itself out to the golden file, it'll end up writing itself out as {{ .Type }} once again, thus preserving the template variables while updating all the other content of the golden file.
First, thanks for building this package!
I'm trying to leverage the
goldie.AssertWithTemplate
feature along with the pattern of using the-update
flag when the file doesn't exist.In regards to the template golden file, I would expect the process to be something like:
go test
and see it fails with theuse -update flag
messagego test -update
and see theexample.golden
file gets createdGolden
in the generated file with{{ .Type }}
go test
and see everything passesAt that point, everything seems good. Then we
go test -update
go test
to validateexample.golden
file is now overwritten with a single value and doesn't retain the initial{{ .Type }}
that we put in there.I can imagine that it would be relatively simple to go back and change the one overwritten
.Type
variable in this case. It is a bit more complex, though, if you had a bunch of variables throughout a large.golden
file.Do you have any thoughts on how to preserve the original template variables while doing an
-update
? One idea is that on-update
, the template code could be smart enough to replace all scalar values in the data passed in with{{ .Dot.Path.To.Key }}
. That way, when.Type
writes itself out to the golden file, it'll end up writing itself out as{{ .Type }}
once again, thus preserving the template variables while updating all the other content of the golden file.Thanks!