Giter VIP home page Giter VIP logo

go-snaps's People

Contributors

g-rath avatar gkampitakis avatar parasyris avatar renovate-bot avatar zregvart avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

go-snaps's Issues

Snapshots combined with using `-count`

Issue

This could be a bug or a feature - I'm still actually digging into this but wanted to open an issue because you might be able to dig into it faster (if you've not already come across this!)

It looks like when running with -count (i.e. go test -count 5 ./...) in some (if not all?) cases snapshots get counted multiple times for the same test rather than recompared - i.e. the snapshot gets updated with [TestRun/#00 - n] where n is the number of snapshots called so far across all runs.

I think this is because the test ids are using a global registry + mutex and that -count is working within the same process, it just calls the test functions multiple times.

I think this could be worked around by exposing a way of manually clearing/resetting the registry, which could be called before every test in TestMain, but I've not yet tested that to confirm.

I'm also not sure if something like golang/go#64883 would make it possible for the library to handle this automatically.

[Bug]: Nested arrays keys

Description

When trying to this library with a complex json. for example:

Attempting to match.Any("#.components.#.properties.value") yields a bad snapshot.
modifying to match.Any("0.components.#.properties.value") yields a good but incomplete matched snapshot.

A solution for prerendering the matchers based on the input is non-functional. (creating a matcher per nested of nested matcher).

It's probably due to limitations with gjson - tidwall/gjson#267
As you figured, snapshots can be (and probably are) complex, nested arrays within an array is very common, this yields using a snapshot almost useless.

We can try fixing it ourselves, parsing the '.#.' and running through a loop with '.0.' ... '.n.' new matchers to workaround this.
wdyt?

[TestGenerateSync/happy:cyclonedx_mono_with_and_go_trivy - 1]
{
 "$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json",
 "bomFormat": "CycloneDX",
 "components": [
  {
   "bom-ref": "<Any value>",
   "name": "go.mod",
   "properties": [
    {
     "name": "aquasecurity:trivy:Class",
     "value": "lang-pkgs"
    },
    {
     "name": "aquasecurity:trivy:Type",
     "value": "gomod"
    }
   ],
   "type": "application"
  },
  {
   "bom-ref": "<Any value>",
   "name": "github.com/davecgh/go-spew",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "github.com/davecgh/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/github.com/davecgh/[email protected]",
   "type": "library",
   "version": "1.1.0"
  },
  {
   "bom-ref": "<Any value>",
   "name": "github.com/jmespath/go-jmespath/internal/testify",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "github.com/jmespath/go-jmespath/internal/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/github.com/jmespath/go-jmespath/internal/[email protected]",
   "type": "library",
   "version": "1.5.1"
  },
  {
   "bom-ref": "<Any value>",
   "licenses": [
    {
     "license": {
      "name": "Apache-2.0"
     }
    }
   ],
   "name": "github.com/jmespath/go-jmespath",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "github.com/jmespath/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/github.com/jmespath/[email protected]",
   "type": "library",
   "version": "0.4.0"
  },
  {
   "bom-ref": "<Any value>",
   "licenses": [
    {
     "license": {
      "name": "BSD-2-Clause"
     }
    }
   ],
   "name": "github.com/pkg/errors",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "github.com/pkg/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/github.com/pkg/[email protected]",
   "type": "library",
   "version": "0.9.1"
  },
  {
   "bom-ref": "<Any value>",
   "name": "github.com/pmezard/go-difflib",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "github.com/pmezard/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/github.com/pmezard/[email protected]",
   "type": "library",
   "version": "1.0.0"
  },
  {
   "bom-ref": "<Any value>",
   "name": "github.com/stretchr/objx",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "github.com/stretchr/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/github.com/stretchr/[email protected]",
   "type": "library",
   "version": "0.1.0"
  },
  {
   "bom-ref": "<Any value>",
   "name": "github.com/yuin/goldmark",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "github.com/yuin/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/github.com/yuin/[email protected]",
   "type": "library",
   "version": "1.4.13"
  },
  {
   "bom-ref": "<Any value>",
   "name": "golang.org/x/crypto",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "golang.org/x/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/golang.org/x/[email protected]",
   "type": "library",
   "version": "0.0.0-20210921155107-089bfa567519"
  },
  {
   "bom-ref": "<Any value>",
   "name": "golang.org/x/mod",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "golang.org/x/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/golang.org/x/[email protected]",
   "type": "library",
   "version": "0.6.0-dev.0.20220419223038-86c51ed26bb4"
  },
  {
   "bom-ref": "<Any value>",
   "name": "golang.org/x/net",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "golang.org/x/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/golang.org/x/[email protected]",
   "type": "library",
   "version": "0.1.0"
  },
  {
   "bom-ref": "<Any value>",
   "name": "golang.org/x/sync",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "golang.org/x/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/golang.org/x/[email protected]",
   "type": "library",
   "version": "0.0.0-20220722155255-886fb9371eb4"
  },
  {
   "bom-ref": "<Any value>",
   "name": "golang.org/x/sys",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "golang.org/x/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/golang.org/x/[email protected]",
   "type": "library",
   "version": "0.1.0"
  },
  {
   "bom-ref": "<Any value>",
   "name": "golang.org/x/term",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "golang.org/x/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/golang.org/x/[email protected]",
   "type": "library",
   "version": "0.1.0"
  },
  {
   "bom-ref": "<Any value>",
   "name": "golang.org/x/text",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "golang.org/x/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/golang.org/x/[email protected]",
   "type": "library",
   "version": "0.4.0"
  },
  {
   "bom-ref": "<Any value>",
   "name": "golang.org/x/tools",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "golang.org/x/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/golang.org/x/[email protected]",
   "type": "library",
   "version": "0.1.12"
  },
  {
   "bom-ref": "<Any value>",
   "name": "golang.org/x/xerrors",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "golang.org/x/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/golang.org/x/[email protected]",
   "type": "library",
   "version": "0.0.0-20190717185122-a985d3407aa7"
  },
  {
   "bom-ref": "<Any value>",
   "name": "gopkg.in/check.v1",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "gopkg.in/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/gopkg.in/[email protected]",
   "type": "library",
   "version": "0.0.0-20161208181325-20d25e280405"
  },
  {
   "bom-ref": "<Any value>",
   "name": "gopkg.in/yaml.v2",
   "properties": [
    {
     "name": "aquasecurity:trivy:PkgID",
     "value": "gopkg.in/[email protected]"
    },
    {
     "name": "aquasecurity:trivy:PkgType",
     "value": "gomod"
    }
   ],
   "purl": "pkg:golang/gopkg.in/[email protected]",
   "type": "library",
   "version": "2.2.8"
  }
 ],
 "dependencies": [
  {
   "dependsOn": [
    "a0fedf37-f446-4629-ab6c-fbebd72d5034"
   ],
   "ref": "4dacd68b-b238-4db3-a1b1-d388034af718"
  },
  {
   "dependsOn": [
    "pkg:golang/github.com/davecgh/[email protected]",
    "pkg:golang/github.com/jmespath/[email protected]",
    "pkg:golang/github.com/pkg/[email protected]",
    "pkg:golang/github.com/pmezard/[email protected]",
    "pkg:golang/github.com/stretchr/[email protected]",
    "pkg:golang/github.com/yuin/[email protected]",
    "pkg:golang/golang.org/x/[email protected]",
    "pkg:golang/golang.org/x/[email protected]",
    "pkg:golang/golang.org/x/[email protected]",
    "pkg:golang/golang.org/x/[email protected]",
    "pkg:golang/golang.org/x/[email protected]",
    "pkg:golang/golang.org/x/[email protected]",
    "pkg:golang/golang.org/x/[email protected]",
    "pkg:golang/golang.org/x/[email protected]",
    "pkg:golang/golang.org/x/[email protected]",
    "pkg:golang/gopkg.in/[email protected]",
    "pkg:golang/gopkg.in/[email protected]"
   ],
   "ref": "a0fedf37-f446-4629-ab6c-fbebd72d5034"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/github.com/davecgh/[email protected]"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/github.com/jmespath/go-jmespath/internal/[email protected]"
  },
  {
   "dependsOn": [
    "pkg:golang/github.com/jmespath/go-jmespath/internal/[email protected]"
   ],
   "ref": "pkg:golang/github.com/jmespath/[email protected]"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/github.com/pkg/[email protected]"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/github.com/pmezard/[email protected]"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/github.com/stretchr/[email protected]"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/github.com/yuin/[email protected]"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/golang.org/x/[email protected]"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/golang.org/x/[email protected]"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/golang.org/x/[email protected]"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/golang.org/x/[email protected]"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/golang.org/x/[email protected]"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/golang.org/x/[email protected]"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/golang.org/x/[email protected]"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/golang.org/x/[email protected]"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/golang.org/x/[email protected]"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/gopkg.in/[email protected]"
  },
  {
   "dependsOn": [],
   "ref": "pkg:golang/gopkg.in/[email protected]"
  }
 ],
 "metadata": {
  "component": {
   "bom-ref": "<Any value>",
   "name": "testdata/aws-sdk-go",
   "properties": [
    {
     "name": "aquasecurity:trivy:SchemaVersion",
     "value": "2"
    }
   ],
   "type": "application"
  },
  "timestamp": "<Any value>",
  "tools": [
   {
    "name": "trivy",
    "vendor": "aquasecurity",
    "version": "0.47.0"
   }
  ]
 },
 "serialNumber": "<Any value>",
 "specVersion": "1.5",
 "version": 1,
 "vulnerabilities": []
}
---

Steps to Reproduce

Run with attached JSON (or simplified version) or any [{ ... , arr: [ { id: 123 } ]}] with a "#.arr.#.id" matcher.

Expected Behavior

No response

[Bug]: Cryptic error message for snaps saved using "CRLF" instead of "LF" line ending

Description

Pulling the snaps on a Windows and running the tests on a Docker (Linux) environment produces the following error:-

--- FAIL: TestFoo (0.01s)
  --- FAIL: TestFoo/test_snapshot (0.01s)
      foo_test.go:34: snapshot not found

This error is cryptic because suggests that the file doesn't exist when it is actually the wrong format. This can happen when developer clones a repository from Github and attempts to run the tests in a Docker container.

Steps to Reproduce

  1. Go to snapshot folder.
  2. Select a snap
  3. Change the line ending from LF to CRLF on a Windows machine
  4. Attempt to run the tests in a Docker container or some kind of Linux environement

Expected Behavior

Proposal: Update the docs to highlight this issue and maybe change the error message to be less cryptic. i.e.

--- FAIL: TestFoo (0.01s)
  --- FAIL: TestFoo/test_snapshot (0.01s)
      foo_test.go:34: snapshot not found or wrong format

Proposed changes to docs under notes. (Tried to create a PR but my branch was not accepted):

5. If you are running this package on both a Windows and Linux environment, you may encounter the following error:-

\```bash
--- FAIL: TestFoo (0.01s)
  --- FAIL: TestFoo/test_snapshot (0.01s)
      foo_test.go:34: snapshot not found
\```

this is because the snapshots are saved with a different line ending on each platform (LF on Linux, CRLF on Windows). To fix this, you can use the [dos2unix](https://pkgs.alpinelinux.org/package/edge/community/x86/dos2unix) tool to convert the line endings to LF on Linux

Identify outdated snapshots

If we remove a snapshot from the testing code the snapshot entry becomes outdated. Ideally, the library should identify the outdated snaps and on update remove them

[Bug]: race condition with cleanup and parallel

Description

It seems there's a race condition with cleaning up snapshots when using parallel tests that means if a snapshot changes there's a chance any of the snapshots in the same file could be duplicates, moved around, etc.

Steps to Reproduce

package main

import (
	"fmt"
	"math/rand"
	"os"
	"strings"
	"testing"
	"time"

	"github.com/gkampitakis/go-snaps/snaps"
)

var runCount = 2
var testsCount = 10

func init() {
	rand.Seed(time.Now().UnixNano())
}

var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

func randSeq(n int) string {
	b := make([]rune, n)
	for i := range b {
		b[i] = letters[rand.Intn(len(letters))]
	}
	return string(b)
}

func TestMain(m *testing.M) {
	for i := 0; i < runCount; i++ {
		code := m.Run()

		snaps.Clean(m, snaps.CleanOpts{Sort: true})

		b, err := os.ReadFile("__snapshots__/main_test.snap")

		if err != nil {
			if i == 0 {
				// ignore on the first run, since it should generate the snapshot
				continue
			}

			panic(err)
		}

		content := string(b)

		for i := 0; i < testsCount; i++ {
			header := fmt.Sprintf("[TestSnaps/#%02d - 1]", i)
			count := strings.Count(content, header)

			if count > 1 {
				code = 1

				fmt.Printf("Saw '%s' %d times\n", header, count)
			}
		}

		if code != 0 {
			os.Exit(code)
		}
	}

	os.Exit(0)
}

func TestSnaps(t *testing.T) {
	t.Parallel()

	type args struct {
		str string
	}
	type testCase struct {
		name string
		args args
	}

	tests := make([]testCase, 0, testsCount)

	for i := 0; i < testsCount; i++ {
		tests = append(tests, testCase{args: args{str: randSeq(10)}})
	}

	for _, tt := range tests {
		tt := tt
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			snaps.MatchSnapshot(t, tt.args.str)
		})
	}
}

Run with UPDATE_SNAPS=true go test ./...:

โฏ UPDATE_SNAPS=true go test ./...
PASS

Snapshot Summary

โœŽ 10 snapshots added

PASS

Snapshot Summary

โœŽ 12 snapshots added
โœŽ 8 snapshots updated

Saw '[TestSnaps/#05 - 1]' 2 times
Saw '[TestSnaps/#08 - 1]' 2 times
FAIL    github.com/g-rath/go-snaps-testing      0.003s
FAIL

Running it multiple times will compound the result:

โฏ UPDATE_SNAPS=true go test ./...
PASS

Snapshot Summary

โœŽ 7 snapshots added
โœŽ 3 snapshots updated

Saw '[TestSnaps/#01 - 1]' 3 times
Saw '[TestSnaps/#03 - 1]' 3 times
Saw '[TestSnaps/#04 - 1]' 2 times
Saw '[TestSnaps/#05 - 1]' 2 times
Saw '[TestSnaps/#06 - 1]' 2 times
Saw '[TestSnaps/#07 - 1]' 3 times
Saw '[TestSnaps/#08 - 1]' 4 times
Saw '[TestSnaps/#09 - 1]' 2 times
FAIL    github.com/g-rath/go-snaps-testing      0.004s
FAIL

Note that you can reproduce this without the looping, it just makes it a lot easier - but importantly my TestMain here is not interfering with the cleanup function.

Commenting out the t.Parallel calls resolves the issue

Expected Behavior

The snapshot order is stable, and snapshots are not duplicated.

Is snapshort sorting a thing?

Issue

I find it useful for the snapshots to be sorted.
In the scenario where you change something so a ton of snapshots are obsolete, but some of the tests actually find some bugs that need to be fixed.
For my sanity I then prefer to just regenerate the snapshots that are just obsolete and leave the ones that are broken.
The easiest way in my mind to accomplish that is to just delete the snapshots that are outdated and let them be regenerated. However the order of the snapshots changes when that is done so the diff in git will be larger than is necessary.
It seems like go-snaps just appends any new snapshots to the end of the file

I imagine that the intended way to do this is to run each test manually with UPDATE_SNAPS set, but that's more time consuming in these cases than just modifying the snapshot directly.

Would adding sorting or some other mechanism to make this usecase easier be reasonable?
Jest snapshots for example are sorted from my experience so I can do exactly what I outlined above

Testcase is intially written, but fails on updating the test

Hi,

if I create the test case, everything is fine... if I update it, it is updated in a wrong way so that from this point on all tests will fail. It only is possible to fix if I delete the snaps and create them from scratch...

--- FAIL: Test_UpdateSecret (0.00s)
    mocks.go:24: 
         Snapshot 
         Received 
        
        >>>SecretsManagerMockClient.PutSecretValue.Input
        {
          ClientRequestToken: "4547532a137611d83958d17095c6c2d38ae0036a760c3b79c9dd5957d1c20cf2.yaml",
          SecretId: "arn:${Partition}:secretsmanager:::secret:${Region}:${Account}:secret:${SecretId}",
          SecretString: <sensitive>
        }

image

Do you need more information?

[Feature Request]: MatchYAML

๐Ÿš€ Feature Proposal

Considering the existence of the MatchJSON function, I suggest to introduce a similar implementation for YAML files: MatchYAML.

If agreeable, I would like to collaborate implementing this feature.

Motivation

I'm using this library for a project where I need to handle some data via JSON and YAML and I'd like to have the same functionality that I've with MatchJSON

Example

No response

[Feature Request ]: Add env variable to error if .snap is missing

๐Ÿš€ Feature Proposal

We want to make sure that .snaps files are always committed alongside each test. In case a test that relies on a snapshot, in CI, instead of creating the .snap file upon run, fail automatically.

Motivation

No response

Example

In CI we want to set an env variable FAIL_ON_MISSING_SNAP=true and if a .snap is missing when snaps.MatchJSON (or similar), instead of creating/updating the .snap file, the test fails.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • fix(deps): update all dependencies (github.com/gkampitakis/ciinfo, go)

Detected dependencies

github-actions
.github/workflows/go.yml
  • actions/setup-go v3
  • actions/checkout v3
  • golangci/golangci-lint-action v3
  • actions/checkout v3
  • actions/setup-go v3
gomod
go.mod
  • go 1.16
  • github.com/gkampitakis/ciinfo v0.1.0
  • github.com/kr/pretty v0.3.0
  • github.com/sergi/go-diff v1.2.0

  • Check this box to trigger a request for Renovate to run again on this repository

Issues with diffs

Hi,

coming from typescript and jest I really like your work and can save a lot of lines of code in my unit tests! But I have an edge case where the snapshot/match logic does not work.

This is the initial snapshot:

[Test_GetS3FileContent - 1]
{
    "key1": "ENC[AES256_GCM,data:ArSjC0Vd,iv:NFrIj1ekM2dU8JCU6NpmnHFmwcFaEQagCw/fxKV2b6A=,tag:GrGgeNmtqFuyiTjKJ/78kw==,type:str]",
    "key2": "ENC[AES256_GCM,data:NeoandY=,iv:2oCcBk0R/XxvH/O+aaCIa7V9pFsy7dhCKZ70ebjc5Ds=,tag:GVZXUFFwbpMVY/KCLNENAQ==,type:float]",
    "key3": "ENC[AES256_GCM,data:y4tBaNg=,iv:mHuYrtf0Ke/oHBcunpgdPpzaYkBx7UjMcOQgpmVc8/Q=,tag:cuEg2UE17Po6opfpH8ikog==,type:bool]",
    "sops": {
        "kms": null,
        "gcp_kms": null,
        "azure_kv": null,
        "hc_vault": null,
        "age": [
            {
                "recipient": "age1djllw2pzuprrqc0en5m8vc8k5ge3tm0f6g7cj0c0glfzp44vdc4ql8ngvu",
                "enc": "-----BEGIN AGE ENCRYPTED FILE-----\nYWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBOWlAxekVNNXdjenV5cmdR\nZDlQU3N5WC9oeENUbUpYcWxkeU9kVS9TeUR3Cm5zbGhIL0J6WG9TWi9KR3ZUTVph\nblpiUDZtQklnWlRrSGZzTXlZN2Z6WjgKLS0tIDI3ZHNHR0NmWTMzdTg0dEtXcU93\nbEdNWndKdWROWGdLdHAwTGFtRGlsaDQKV7HJG629rPWDBY046Hxj4utxUkex3SwU\nVUQRX00p6r9ffc+iC5DGUm/KOketAHunO4Kn0uOS4WHg+Jg2Cwu72Q==\n-----END AGE ENCRYPTED FILE-----\n"
            }
        ],
        "lastmodified": "2022-03-29T21:02:32Z",
        "mac": "ENC[AES256_GCM,data:2vmzwW74OSJq18GEcScpMH76AWrhJdENlKw2vRzGrQfPixTNcrwJ0neTH+BvGKfR9OlLR2EZTheRphQ0Ikb4SdhfInoDjHo+0jXN7RQ4neTlko4j/YRVek61oePgAKAqcmvL2IhzRGO2VR8nDPuJuvRCmP5wOMOBkerydRKHeDc=,iv:NHWPNWDmuhekJUIJpC1cDJ51lLKTsXLE6dC4tpw/qSI=,tag:yV5ngRdIYZfUgj6ntsY5CQ==,type:str]",
        "pgp": null,
        "unencrypted_suffix": "_unencrypted",
        "version": "3.7.2"
    }
}

The second run already fails:

2022/04/03 12:18:34 Downloaded 1403 bytes
    main_test.go:31: 
         Snapshot 
         Received 
        
        {
            "key1": "ENC[AES256_GCM,data:ArSjC0Vd,iv:NFrIj1ekM2dU8JCU6NpmnHFmwcFaEQagCw/fxKV2b6A=,tag:GrGgeNmtqFuyiTjKJ/78kw==,type:str]",
            "key2": "ENC[AES256_GCM,data:NeoandY=,iv:2oCcBk0R/XxvH/O+aaCIa7V9pFsy7dhCKZ70ebjc5Ds=,tag:GVZXUFFwbpMVY/KCLNENAQ==,type:float]",
            "key3": "ENC[AES256_GCM,data:y4tBaNg=,iv:mHuYrtf0Ke/oHBcunpgdPpzaYkBx7UjMcOQgpmVc8/Q=,tag:cuEg2UE17Po6opfpH8ikog==,type:bool]",
            "sops": {
                "kms": null,
                "gcp_kms": null,
                "azure_kv": null,
                "hc_vault": null,
                "age": [
                    {
                        "recipient": "age1djllw2pzuprrqc0en5m8vc8k5ge3tm0f6g7cj0c0glfzp44vdc4ql8ngvu",
                        "enc": "-----BEGIN AGE ENCRYPTED FILE-----\nYWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBOWlAxekVNNXdjenV5cmdR\nZDlQU3N5WC9oeENUbUpYcWxkeU9kVS9TeUR3Cm5zbGhIL0J6WG9TWi9KR3ZUTVph\nblpiUDZtQklnWlRrSGZzTXlZN2Z6WjgKLS0tIDI3ZHNHR0NmWTMzdTg0dEtXcU93\nbEdNWndKdWROWGdLdHAwTGFtRGlsaDQKV7HJG629rPWDBY046Hxj4utxUkex3SwU\nVUQRX00p6r9ffc+iC5DGUm/KOketAHunO4Kn0uOS4WHg+Jg2Cwu72Q==\n-----END AGE ENCRYPTED FILE-----\n"
                    }
                ],
                "lastmodified": "2022-03-29T21:02:32Z",
                "mac": "ENC[AES256_GCM,data:2vmzwW74OSJq18GEcScpMH76AWrhJdENlKw2vRzGrQfPixTNcrwJ0neTH+BvGKfR9OlLR2EZTheRphQ0Ikb4SdhfInoDjHo+0jXN7RQ4neTlko4j/YRVek61oePgAKAqcmvL2IhzRGO2VR8nDPuJuvRCmP5wOMOBkerydRKHeDc=,iv:NHWPNWDmuhekJUIJpC1cDJ51lLKTsXLE6dC4tpw/qSI=,tag:yV5ngRdIYZfUgj6ntsY5CQ==,type:str]",
                "pgp": null,
                "unencrypted_suffix": "_unencrypted",
                "version": "3.7.2"
            }
        }

An image can tell more than thousand words:
image

And after updating the snapshot 2 more times, the snapshot looks like:

[Test_GetS3FileContent - 1]
{
    "key1": "ENC[AES256_GCM,data:ArSjC0Vd,iv:NFrIj1ekM2dU8JCU6NpmnHFmwcFaEQagCw/fxKV2b6A=,tag:GrGgeNmtqFuyiTjKJ/78kw==,type:str]",
    "key2": "ENC[AES256_GCM,data:NeoandY=,iv:2oCcBk0R/XxvH/O+aaCIa7V9pFsy7dhCKZ70ebjc5Ds=,tag:GVZXUFFwbpMVY/KCLNENAQ==,type:float]",
    "key3": "ENC[AES256_GCM,data:y4tBaNg=,iv:mHuYrtf0Ke/oHBcunpgdPpzaYkBx7UjMcOQgpmVc8/Q=,tag:cuEg2UE17Po6opfpH8ikog==,type:bool]",
    "sops": {
        "kms": null,
        "gcp_kms": null,
        "azure_kv": null,
        "hc_vault": null,
        "age": [
            {
                "recipient": "age1djllw2pzuprrqc0en5m8vc8k5ge3tm0f6g7cj0c0glfzp44vdc4ql8ngvu",
                "enc": "-----BEGIN AGE ENCRYPTED FILE-----\nYWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBOWlAxekVNNXdjenV5cmdR\nZDlQU3N5WC9oeENUbUpYcWxkeU9kVS9TeUR3Cm5zbGhIL0J6WG9TWi9KR3ZUTVph\nblpiUDZtQklnWlRrSGZzTXlZN2Z6WjgKLS0tIDI3ZHNHR0NmWTMzdTg0dEtXcU93\nbEdNWndKdWROWGdLdHAwTGFtRGlsaDQKV7HJG629rPWDBY046Hxj4utxUkex3SwU\nVUQRX00p6r9ffc+iC5DGUm/KOketAHunO4Kn0uOS4WHg+Jg2Cwu72Q==\n-----END AGE ENCRYPTED FILE-----\n"
            }
        ],
        "lastmodified": "2022-03-29T21:02:32Z",
        "mac": "ENC[AES256_GCM,data:2vmzwW74OSJq18GEcScpMH76AWrhJdENlKw2vRzGrQfPixTNcrwJ0neTH+BvGKfR9OlLR2EZTheRphQ0Ikb4SdhfInoDjHo+0jXN7RQ4neTlko4j/YRVek61oePgAKAqcmvL2IhzRGO2VR8nDPuJuvRCmP5wOMOBkerydRKHeDc=,iv:NHWPNWDmuhekJUIJpC1cDJ51lLKTsXLE6dC4tpw/qSI=,tag:yV5ngRdIYZfUgj6ntsY5CQ==,type:str]",
        "pgp": null,
        "unencrypted_suffix": "_unencrypted",
        "version": "3.7.2"
    }
}
-----BEGIN AGE ENCRYPTED FILE-----\nYWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBOWlAxekVNNXdjenV5cmdR\nZDlQU3N5WC9oeENUbUpYcWxkeU9kVS9TeUR3Cm5zbGhIL0J6WG9TWi9KR3ZUTVph\nblpiUDZtQklnWlRrSGZzTXlZN2Z6WjgKLS0tIDI3ZHNHR0NmWTMzdTg0dEtXcU93\nbEdNWndKdWROWGdLdHAwTGFtRGlsaDQKV7HJG629rPWDBY046Hxj4utxUkex3SwU\nVUQRX00p6r9ffc+iC5DGUm/KOketAHunO4Kn0uOS4WHg+Jg2Cwu72Q==\n-----END AGE ENCRYPTED FILE-----\n"
            }
        ],
        "lastmodified": "2022-03-29T21:02:32Z",
        "mac": "ENC[AES256_GCM,data:2vmzwW74OSJq18GEcScpMH76AWrhJdENlKw2vRzGrQfPixTNcrwJ0neTH+BvGKfR9OlLR2EZTheRphQ0Ikb4SdhfInoDjHo+0jXN7RQ4neTlko4j/YRVek61oePgAKAqcmvL2IhzRGO2VR8nDPuJuvRCmP5wOMOBkerydRKHeDc=,iv:NHWPNWDmuhekJUIJpC1cDJ51lLKTsXLE6dC4tpw/qSI=,tag:yV5ngRdIYZfUgj6ntsY5CQ==,type:str]",
        "pgp": null,
        "unencrypted_suffix": "_unencrypted",
        "version": "3.7.2"
    }
}
-----BEGIN AGE ENCRYPTED FILE-----\nYWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBOWlAxekVNNXdjenV5cmdR\nZDlQU3N5WC9oeENUbUpYcWxkeU9kVS9TeUR3Cm5zbGhIL0J6WG9TWi9KR3ZUTVph\nblpiUDZtQklnWlRrSGZzTXlZN2Z6WjgKLS0tIDI3ZHNHR0NmWTMzdTg0dEtXcU93\nbEdNWndKdWROWGdLdHAwTGFtRGlsaDQKV7HJG629rPWDBY046Hxj4utxUkex3SwU\nVUQRX00p6r9ffc+iC5DGUm/KOketAHunO4Kn0uOS4WHg+Jg2Cwu72Q==\n-----END AGE ENCRYPTED FILE-----\n"
            }
        ],
        "lastmodified": "2022-03-29T21:02:32Z",
        "mac": "ENC[AES256_GCM,data:2vmzwW74OSJq18GEcScpMH76AWrhJdENlKw2vRzGrQfPixTNcrwJ0neTH+BvGKfR9OlLR2EZTheRphQ0Ikb4SdhfInoDjHo+0jXN7RQ4neTlko4j/YRVek61oePgAKAqcmvL2IhzRGO2VR8nDPuJuvRCmP5wOMOBkerydRKHeDc=,iv:NHWPNWDmuhekJUIJpC1cDJ51lLKTsXLE6dC4tpw/qSI=,tag:yV5ngRdIYZfUgj6ntsY5CQ==,type:str]",
        "pgp": null,
        "unencrypted_suffix": "_unencrypted",
        "version": "3.7.2"
    }
}

So I think something within this line is breaking the snapshot/diff logic:

"enc": "-----BEGIN AGE ENCRYPTED FILE-----\nYWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBOWlAxekVNNXdjenV5cmdR\nZDlQU3N5WC9oeENUbUpYcWxkeU9kVS9TeUR3Cm5zbGhIL0J6WG9TWi9KR3ZUTVph\nblpiUDZtQklnWlRrSGZzTXlZN2Z6WjgKLS0tIDI3ZHNHR0NmWTMzdTg0dEtXcU93\nbEdNWndKdWROWGdLdHAwTGFtRGlsaDQKV7HJG629rPWDBY046Hxj4utxUkex3SwU\nVUQRX00p6r9ffc+iC5DGUm/KOketAHunO4Kn0uOS4WHg+Jg2Cwu72Q==\n-----END AGE ENCRYPTED FILE-----\n"

Maybe you have an idea what's wrong?

Further testing showed, that replacing "-----" with "" is a possible workaround

Consider Adding Named Snapshots

๐Ÿš€ Feature Proposal

A way to substitute the simple incrementing number in snapshot files, e.g. TestName - 1, TestName - 2, etc. with a proper name. I'd be willing to PR this.

Motivation

The clarity of snapshots being named TestName - 1, TestName - 2, and so on can be subpar in the case of a test doing something like this:

func TestFindGoodAndBad(t *testing.T) {
    good, bad := findGoodAndBad(...)

    snaps.MatchSnapshot(t, good)
    snaps.MatchSnapshot(t, bad)
}

I'm aware of the form snaps.MatchSnapshot(t, good, bad) where it'll only create one entry but that version doesn't even indicate where one value starts and the other one ends.

Example

One or multiple of these APIs would be added:

// Useful for consistency with other options
snaps.WithConfig(snaps.Name("foo")).MatchSnapshot(...)

// Useful because it's probably more common to change than other options
snaps.Name("bar").MatchSnapshot(...)

// Useful to be more DRY when reusing the same base configuration multiple time
config := snaps.WithConfig(otherOptions)

config.Name("lorem").MatchSnapshot(...)
config.Name("ipsum").MatchSnapshot(...)

As for how config.Name("foo") being used more than once would work I could go two ways:

  1. It's simply an error to use the same name twice.
  2. An entry named TestName - foo/1 and TestName - foo/2 and so on is generated.

[Bug]: snapshots of big slices could not be loaded back

Description

The bufferio scanner instanced in getPrevSnapshot() uses default arguments, which means that the maximum token size for scanning a single line caps at 64*1024 bytes.

I've a test snapshot storing a slice of 5k string elements, which gets dumped into a single line of ~130k characters.
As a temporary workaround I'm splitting out the slice in smaller chunks and snapshotting each one of them, but I think the snapshot should consider the MaxScanTokenSize limit and break the dumped data into multiple lines.

Steps to Reproduce

  • Generate a slice whose content would be dumped into a single line of >65K characters
  • use it to produce a snapshot
  • attempt to reload the snapshot, getPrevSnapshot() will return errSnapNotFound

Expected Behavior

The snapshot creation should take into account the scan buffer size limits, and split data among multiple lines.

[Feature Request ]: Configurable snapshot file

๐Ÿš€ Feature Proposal

Configurable snapshot file

Motivation

It will be nice to have the ability to save a snapshot to a different file. This way if you have a test that generates a lot of snapshots, you can easily check and see what adds each one, and also makes easier to review the diff in git.

Example

BEFORE:

			snaps.MatchJSON(t, data,
				match.Custom("payload.id", matchers.MatchAny[float64]),
				match.Custom("payload.created_at", matchers.MatchTime(time.RFC3339Nano)),
				match.Custom("payload.updated_at", matchers.MatchTime(time.RFC3339Nano)),
			)

AFTER


			snaps.MatchJSON(t, "test_1.snap", data,
				match.Custom("payload.id", matchers.MatchAny[float64]),
				match.Custom("payload.created_at", matchers.MatchTime(time.RFC3339Nano)),
				match.Custom("payload.updated_at", matchers.MatchTime(time.RFC3339Nano)),
			)

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Location: renovate.json
Error type: The renovate configuration file contains some invalid settings
Message: Invalid schedule: 'Invalid schedule: Failed to parse "every week"'

Allow configuration of snapshot file names / ending

Hi,
at the moment I am struggling with the phenomenon that jest removes the go-snaps test files. Nothing I can change?!
So might it be possible to add a parameter to rename the snapshots to maybe .go-snaps?

On the other side, I finally managed to make jest irgnore the go snaps:

"coveragePathIgnorePatterns": [
      "/node_modules/",
      "/lambda/" <- here are the go snaps
],
"testPathIgnorePatterns": [
      "/node_modules/",
      "/lambda/" <- here are the go snaps
],

[Bug]: CRLF not respected in snaps

Description

I'm writing a formatter and a testing suite of files alongside it that also needs to validate line endings (LF, CRLF) of string outputs or file reads. From my understanding, I think the snaps are only written in LF which leads to my snaps not matching my bytes output as they sometimes use CRLF.

I understand this is a bit of a complicated messy edge case, so I'm fine with no solution. Just wanted to let you know that line endings are inconsistently handled which does break tests. My only workaround is to disable CRLF testing in the meantime.

Steps to Reproduce

Read any CRLF file into a snap and it will replace all line endings into LF. Read the CRLF file again to match with the snap and it will fail. Note my snaps actually mix both LF and CRLF in the same file so that might be another complicated factor to handle.

Expected Behavior

No response

Add tests

Right now there are no tests except the manual examples. โ˜ ๏ธ

Some unit tests have been added, mostly to catch any regressions. Could have been written in better way and push coverage close to 100%

Behaviour changed? Secrets now redacted?

Hi,

did you changed something regarding secret values? My tests change from

[Test_UpdateSecret - 1]
>>>SecretsManagerMockClient.PutSecretValue.Input
{
  ClientRequestToken: "4547532a137611d83958d17095c6c2d38ae0036a760c3b79c9dd5957d1c20cf2",
  SecretId: "arn:${Partition}:secretsmanager:${Region}:${Account}:secret:${SecretId}",
  SecretString: "some-secret-data"
}

to

[Test_UpdateSecret - 1]
>>>SecretsManagerMockClient.PutSecretValue.Input
{
  ClientRequestToken: "4547532a137611d83958d17095c6c2d38ae0036a760c3b79c9dd5957d1c20cf2",
  SecretId: "arn::secretsmanager:::secret:",
  SecretString: <sensitive>
}

Can I disable the replacing with ? I think no one stores real secrets in tests...

[Feature Request ]: support gomock matcher

๐Ÿš€ Feature Proposal

I have tried to wrote a test.Matcher implement, but the snapDirAndName getting the real path by the caller. So I have to implement my matcher in each package to ensure that the save path of the snaps are correct.

So is there a way to replace the path? I thought it would be possible to add an option to modify the skip number of baseCaller

Motivation

Match json data in gomock arguments.

Example

myStub.EXPECT().DoSomething(mysnaps.MatchJSON())

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

๐Ÿš€ 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.