Skip to content

[Feature Request]: allow signaling what custom snapshots to expect #142

@G-Rath

Description

@G-Rath

🚀 Feature Proposal

Currently if you have custom snapshot files these will be considered obsolete if the related Match function is not called during the test, such as because you're using -run or there is an error beforehand.

It could be useful if we can explicitly provide a list of snapshot files for snaps.Clean to not consider obsolete.

For example over in osv.dev we're adding a bunch of snapshot-based tests that run on input files that I'm having go to multiple snapshot files to break things up - this means we can determine the snapshot files that will be used i.e.

func listCassetteSnapshotFiles() ([]string, error) {
	files, err := os.ReadDir("./testdata/cassettes")

	if err != nil {
		return nil, err
	}

	snapshots := []string{}
	for _, file := range files {
		if file.IsDir() {
			continue
		}

		name, _, _ := strings.Cut(file.Name(), ".")

		snapshots = append(snapshots, "./__snapshots_/cassette_"+name+".snap")
	}

	return snapshots, nil
}

func TestMain(m *testing.M) {
	err := vcr.CleanCassettes()
	if err != nil {
		fmt.Println("Error cleaning cassettes:", err)
		os.Exit(1)
	}

	m.Run()

	extraSnapshotFiles, err := listCassetteSnapshotFiles()
	if err != nil {
		fmt.Println("Error cleaning cassettes:", err)
		os.Exit(1)
	}

	dirty, err := snaps.Clean(m, snaps.CleanOpts{
		Sort:               true,
		ExtraSnapshotFiles: extraSnapshotFiles,
	})

	if err != nil {
		fmt.Println("Error cleaning snaps:", err)
		os.Exit(1)
	}
	if dirty {
		fmt.Println("Some snapshots were outdated.")
		os.Exit(1)
	}
}

Motivation

No response

Example

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions