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

[Feature Request]: Expose ability to update snapshot via the API #77

Closed
wagoodman opened this issue Sep 28, 2023 · 2 comments
Closed

[Feature Request]: Expose ability to update snapshot via the API #77

wagoodman opened this issue Sep 28, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@wagoodman
Copy link

wagoodman commented Sep 28, 2023

🚀 Feature Proposal

It would be great to be able to update snapshots more programmatically:

	cfg := snaps.WithConfig(snaps.Update(true))
	cfg.MatchSnapshot(t, myTestSubject)

In this way you could have other test conditions / mechanisms that enforce when a snapshot can and should be updated.

Motivation

Today we use the flag package which hooks nicely into the standard test utils:

package spdxjson

import (
	"flag"
	"testing"
)

var updateSnapshot = flag.Bool("update-spdx-json-snapshot", false, "update the *.golden files for spdx-json encoders")

func Test_Something(t *testing.T) {
	...
	if *updateSnapshot {
		// do something...
	}
	...
}

Which can be used like so:

go test ./path/to/package -update-spdx-json-snapshot

See:

This has the nice quality that:

  • just by running tests as usual you cannot write out a snapshot
  • you can continually update a snapshot without needing to scope the go test command with the correct env var while developing on a narrow feature that isn't stable yet

Example

Combining the motivation and the feature request:

package spdxjson

import (
	"flag"
	"testing"
)

var updateSnapshot = flag.Bool("update-spdx-json-snapshot", false, "update the *.golden files for spdx-json encoders")

func Test_Something(t *testing.T) {
	...
	cfg := snaps.WithConfig(snaps.Update(*updateSnapshot))
	cfg.MatchSnapshot(t, myTestSubject)
	...
}

In which case the flags are the only way to update snapshot files.

@wagoodman wagoodman added the enhancement New feature or request label Sep 28, 2023
@gkampitakis
Copy link
Owner

hey 👋 thanks a lot for opening this issue. I really like this idea. I created a draft pr to support this. I am thinking that the priority should be CI > snaps.Update > UPDATE_SNAPS . So if running on a CI the snaps won't update and will fail. wdyt ?

@gkampitakis
Copy link
Owner

Have added this on #78 please tell me if that works for you. Again thanks for opening this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants