From 7929c467304df6fbfe134686c087aace10266bc5 Mon Sep 17 00:00:00 2001 From: gkampitakis Date: Tue, 28 Jan 2025 23:43:47 +0200 Subject: [PATCH 1/4] fix: handle builds with trimpath enabled --- snaps/snapshot.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/snaps/snapshot.go b/snaps/snapshot.go index c2dc810..76a4a17 100644 --- a/snaps/snapshot.go +++ b/snaps/snapshot.go @@ -7,6 +7,7 @@ import ( "io" "os" "path/filepath" + "runtime/debug" "strings" "sync" @@ -316,14 +317,18 @@ func getPrevStandaloneSnapshot(snapPath string) (string, error) { func snapshotPath(c *Config, tName string, isStandalone bool) (string, string) { // skips current func, the wrapper match* and the exported Match* func callerFilename := baseCaller(3) + isTrimBathBuild := trimPathBuild() dir := c.snapsDir - if !filepath.IsAbs(dir) { + if !filepath.IsAbs(dir) && !isTrimBathBuild { dir = filepath.Join(filepath.Dir(callerFilename), c.snapsDir) } snapPath := filepath.Join(dir, constructFilename(c, callerFilename, tName, isStandalone)) - snapPathRel, _ := filepath.Rel(filepath.Dir(callerFilename), snapPath) + snapPathRel := snapPath + if !isTrimBathBuild { + snapPathRel, _ = filepath.Rel(filepath.Dir(callerFilename), snapPath) + } return snapPath, snapPathRel } @@ -347,6 +352,21 @@ func constructFilename(c *Config, callerFilename, tName string, isStandalone boo return filename } +func trimPathBuild() bool { + bInfo, ok := debug.ReadBuildInfo() + if !ok { + return false + } + + for _, info := range bInfo.Settings { + if info.Key == "-trimpath" { + return info.Value == "true" + } + } + + return false +} + func unescapeEndChars(s string) string { ss := strings.Split(s, "\n") for idx, s := range ss { From e0e290c64f78793375c2c6eaee766ef1010ce23e Mon Sep 17 00:00:00 2001 From: gkampitakis Date: Wed, 29 Jan 2025 23:05:46 +0200 Subject: [PATCH 2/4] fix: detect -trimpath effectively --- Makefile | 3 +++ README.md | 19 +++++++------------ examples/matchSnapshot_test.go | 7 ++----- snaps/snapshot.go | 17 ----------------- snaps/utils.go | 9 +++++++++ 5 files changed, 21 insertions(+), 34 deletions(-) diff --git a/Makefile b/Makefile index 7adb3f2..ee76fbe 100644 --- a/Makefile +++ b/Makefile @@ -20,3 +20,6 @@ test: ## Run tests test-verbose: ## Run tests with verbose output go test -race -count=10 -shuffle on -v -cover ./... + +test-trimpath: ## Run tests with -trimpath + GOFLAGS=-trimpath go test -race -count=10 -shuffle on -v -cover ./examples diff --git a/README.md b/README.md index 207f103..2c432b7 100644 --- a/README.md +++ b/README.md @@ -34,9 +34,9 @@ - [Running tests on CI](#running-tests-on-ci) - [No Color](#no-color) - [Snapshots Structure](#snapshots-structure) +- [Known Limitations](#known-limitations) - [Acknowledgments](#acknowledgments) - [Contributing](./contributing.md) -- [Appendix](#appendix) ## Installation @@ -411,6 +411,12 @@ map[string]interface{}{ > [!NOTE] > If your snapshot data contain characters `---` at the start of a line followed by a new line, `go-snaps` will "escape" them and save them as `/-/-/-/` to differentiate them from termination characters. +## Known Limitations + +- When running a specific test file by specifying a path `go test ./my_test.go`, `go-snaps` can't track the path so it will mistakenly mark snapshots as obsolete. +- go-snaps doesn't handle CRLF line endings. If you are using Windows, you may need to convert the line endings to LF. +- go-snaps doesn't work when running with `go test -trimpath ./...`. If you want to use `go-snaps` and `trimpath` try passing from env with `GOFLAGS=-trimpath`. + ## Acknowledgments This library used [Jest Snapshoting](https://jestjs.io/docs/snapshot-testing) and [Cupaloy](https://github.com/bradleyjkemp/cupaloy) as inspiration. @@ -418,14 +424,3 @@ This library used [Jest Snapshoting](https://jestjs.io/docs/snapshot-testing) an - Jest is a full-fledged Javascript testing framework and has robust snapshoting features. - Cupaloy is a great and simple Golang snapshoting solution. - The [logo](https://github.com/MariaLetta/free-gophers-pack) was made by [MariaLetta](https://github.com/MariaLetta). - -## Appendix - -> [!WARNING] -> When running a specific test file by specifying a path `go test ./my_test.go`, `go-snaps` can't track the path so it will mistakenly mark snapshots as obsolete. - -> [!IMPORTANT] -> Snapshots should be treated as code. The snapshot artifact should be committed alongside code changes, and reviewed as part of your code review process - -> [!NOTE] -> go-snaps doesn't handle CRLF line endings. If you are using Windows, you may need to convert the line endings to LF. diff --git a/examples/matchSnapshot_test.go b/examples/matchSnapshot_test.go index 4918e5f..352b88f 100644 --- a/examples/matchSnapshot_test.go +++ b/examples/matchSnapshot_test.go @@ -5,8 +5,6 @@ import ( "flag" "fmt" "os" - "path/filepath" - "runtime" "testing" "github.com/gkampitakis/go-snaps/snaps" @@ -80,10 +78,9 @@ func TestMatchSnapshot(t *testing.T) { }) t.Run("should allow absolute path", func(t *testing.T) { - _, b, _, _ := runtime.Caller(0) - basepath := filepath.Dir(b) + dir, _ := os.Getwd() - snaps.WithConfig(snaps.Dir(basepath+"/absolute_path")). + snaps.WithConfig(snaps.Dir(dir+"/absolute_path")). MatchSnapshot(t, "supporting absolute path") }) diff --git a/snaps/snapshot.go b/snaps/snapshot.go index 76a4a17..8001b6a 100644 --- a/snaps/snapshot.go +++ b/snaps/snapshot.go @@ -7,7 +7,6 @@ import ( "io" "os" "path/filepath" - "runtime/debug" "strings" "sync" @@ -317,7 +316,6 @@ func getPrevStandaloneSnapshot(snapPath string) (string, error) { func snapshotPath(c *Config, tName string, isStandalone bool) (string, string) { // skips current func, the wrapper match* and the exported Match* func callerFilename := baseCaller(3) - isTrimBathBuild := trimPathBuild() dir := c.snapsDir if !filepath.IsAbs(dir) && !isTrimBathBuild { @@ -352,21 +350,6 @@ func constructFilename(c *Config, callerFilename, tName string, isStandalone boo return filename } -func trimPathBuild() bool { - bInfo, ok := debug.ReadBuildInfo() - if !ok { - return false - } - - for _, info := range bInfo.Settings { - if info.Key == "-trimpath" { - return info.Value == "true" - } - } - - return false -} - func unescapeEndChars(s string) string { ss := strings.Split(s, "\n") for idx, s := range ss { diff --git a/snaps/utils.go b/snaps/utils.go index 9b7bfcc..5148e10 100644 --- a/snaps/utils.go +++ b/snaps/utils.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "runtime" + "slices" "strings" "sync" @@ -22,6 +23,7 @@ var ( defaultConfig = Config{ snapsDir: "__snapshots__", } + isTrimBathBuild = trimPathBuild() ) const ( @@ -138,3 +140,10 @@ func shouldCreate(u *bool) bool { return true } + +// trimPathBuild checks if the build has trimpath setting true +func trimPathBuild() bool { + goFlags := strings.Split(os.Getenv("GOFLAGS"), " ") + + return slices.Contains(goFlags, "-trimpath") +} From 13ac7154c01e576a567041483b912927f55e2eef Mon Sep 17 00:00:00 2001 From: gkampitakis Date: Sat, 1 Feb 2025 12:51:30 +0200 Subject: [PATCH 3/4] fix: try to cover all cases --- .github/workflows/go.yml | 2 ++ snaps/utils.go | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 44f6f2c..f390844 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -49,3 +49,5 @@ jobs: go-version: 1.21.x - name: Run Tests run: make test + - name: Run Tests + run: make test-trimpath diff --git a/snaps/utils.go b/snaps/utils.go index 5148e10..7c441ca 100644 --- a/snaps/utils.go +++ b/snaps/utils.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "runtime" + "runtime/debug" "slices" "strings" "sync" @@ -143,7 +144,23 @@ func shouldCreate(u *bool) bool { // trimPathBuild checks if the build has trimpath setting true func trimPathBuild() bool { + keys := []string{"-trimpath", "--trimpath"} goFlags := strings.Split(os.Getenv("GOFLAGS"), " ") - return slices.Contains(goFlags, "-trimpath") + for _, flag := range goFlags { + if slices.Contains(keys, flag) { + return true + } + } + + bInfo, ok := debug.ReadBuildInfo() + if ok && len(bInfo.Settings) > 0 { + for _, info := range bInfo.Settings { + if slices.Contains(keys, info.Key) { + return info.Value == "true" + } + } + } + + return runtime.GOROOT() == "" } From 880289f7898ade64596a93c1e1e189875932f3e1 Mon Sep 17 00:00:00 2001 From: Georgios Kampitakis <50364739+gkampitakis@users.noreply.github.com> Date: Sat, 8 Feb 2025 10:35:53 +0200 Subject: [PATCH 4/4] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Andre Klärner --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2c432b7..dba22ea 100644 --- a/README.md +++ b/README.md @@ -415,7 +415,7 @@ map[string]interface{}{ - When running a specific test file by specifying a path `go test ./my_test.go`, `go-snaps` can't track the path so it will mistakenly mark snapshots as obsolete. - go-snaps doesn't handle CRLF line endings. If you are using Windows, you may need to convert the line endings to LF. -- go-snaps doesn't work when running with `go test -trimpath ./...`. If you want to use `go-snaps` and `trimpath` try passing from env with `GOFLAGS=-trimpath`. +- go-snaps cannot determine the snapshot path automatically when running with `go test -trimpath ./...`. It then instead relies on the current working directory to define the snapshot directory. If this is a problem in your use case you can set an absolute path with `snaps.WithConfig(snaps.Dir("/some/absolute/path"))` ## Acknowledgments