Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pattern for templated golden files #14

Open
gtrevg opened this issue Sep 9, 2019 · 0 comments
Open

Update pattern for templated golden files #14

gtrevg opened this issue Sep 9, 2019 · 0 comments

Comments

@gtrevg
Copy link
Contributor

gtrevg commented Sep 9, 2019

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:

  • 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.

Thanks!

@sebdah sebdah self-assigned this Oct 16, 2019
@sebdah sebdah removed their assignment Oct 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants