Skip to content

Update pattern for templated golden files #14

Open
@gtrevg

Description

@gtrevg

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!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions