Skip to content

feat: updated AssertWithTemplate to generate fixtures with variables when using the -update flag#44

Merged
sebdah merged 4 commits intosebdah:masterfrom
pd93:improved-templating-support
Jul 9, 2025
Merged

feat: updated AssertWithTemplate to generate fixtures with variables when using the -update flag#44
sebdah merged 4 commits intosebdah:masterfrom
pd93:improved-templating-support

Conversation

@pd93
Copy link
Copy Markdown
Contributor

@pd93 pd93 commented May 21, 2025

Fixes #14 and resolves the problems mentioned in go-task/task#2265.

Currently, if you have a golden file containing variables, when you run go test . -update, the variables will be updated with the values given in the tests. This means that subsequent tests can fail depending on the data that is being passed in.

This PR updates the behavior of the AssertWithTemplate function so that it can generate new variables in the golden file based on the data type being passed in. This works by analysing the data using the reflect package and creating a map of values to their paths in the data type. The generated paths are based on go templating syntax so that when we update the golden file, we can perform a bytes.ReplaceAll on the data.

Example

func TestExample(t *testing.T) {
	g := goldie.New(t)
	wd, err := os.Getwd()
	require.NoError(t, err)
	data := map[string]any{
		"TEST_NAME": t.Name(),
		"TEST_DIR":  wd,
	}
	// "TestExample" matches "t.Name()" and so is replaced with the path to the variable
	g.AssertWithTemplate(t, "example", data, []byte("example TestExample"))
}

go test -update ./... Generates:

example {{.TEST_NAME}}

Notes

  • This works well with scalars, collections and nested structures
  • Values with the highest specificity will be replaced first
    • Basically values that are longer before shorter ones. e.g. "foobar" before "foo" so you don't end up with "{{.FOO}}bar"
  • Its worth considering if this is a backwards-compatiable change or not. Maybe we need a new function instead of updating AssertWithTemplate?
  • Small values in data will cause issues. For example, a map[string]string{"A": "a"} will cause all instances of the letter "a" to be replaced. This could be undesirable. I would argue the tester should just choose more specific strings and the problem goes away.

@pd93
Copy link
Copy Markdown
Contributor Author

pd93 commented Jul 3, 2025

@sebdah Sorry for the ping. Any thoughts on the open PRs? Would love to bring this into the go-task/task workflow if possible.

@sebdah sebdah self-assigned this Jul 5, 2025
@sebdah
Copy link
Copy Markdown
Owner

sebdah commented Jul 5, 2025

Thanks for this, I'll take a look today/tomorrow.

@sebdah sebdah self-requested a review July 5, 2025 08:45
Comment thread v2/goldie.go Outdated

// Meta takes any data structure and returns a map of the data structure's
// values to their paths. This allows us to replace values in the golden file
// with template variables that match the values. These values are
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfinished sentence

Comment thread v2/goldie.go
Comment thread v2/goldie.go Outdated
// Meta takes any data structure and returns a map of the data structure's
// values to their paths. This allows us to replace values in the golden file
// with template variables that match the values. These values are
func Meta(a interface{}) map[string]string {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this one private

Comment thread v2/goldie.go
Comment thread v2/assertions.go Outdated
@pd93 pd93 requested a review from sebdah July 8, 2025 17:19
Copy link
Copy Markdown
Owner

@sebdah sebdah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome - thanks for addressing

@sebdah sebdah merged commit 5f83565 into sebdah:master Jul 9, 2025
@sebdah sebdah mentioned this pull request Jul 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update pattern for templated golden files

2 participants