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

fix: handle builds with trimpath enabled #120

Merged
merged 4 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ jobs:
go-version: 1.21.x
- name: Run Tests
run: make test
- name: Run Tests
run: make test-trimpath
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 7 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -411,21 +411,16 @@ 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 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

This library used [Jest Snapshoting](https://jestjs.io/docs/snapshot-testing) and [Cupaloy](https://github.com/bradleyjkemp/cupaloy) as inspiration.

- 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.
7 changes: 2 additions & 5 deletions examples/matchSnapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/gkampitakis/go-snaps/snaps"
Expand Down Expand Up @@ -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")
})

Expand Down
7 changes: 5 additions & 2 deletions snaps/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,15 @@ func snapshotPath(c *Config, tName string, isStandalone bool) (string, string) {
callerFilename := baseCaller(3)

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
}
Expand Down
26 changes: 26 additions & 0 deletions snaps/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"path/filepath"
"runtime"
"runtime/debug"
"slices"
"strings"
"sync"

Expand All @@ -22,6 +24,7 @@ var (
defaultConfig = Config{
snapsDir: "__snapshots__",
}
isTrimBathBuild = trimPathBuild()
)

const (
Expand Down Expand Up @@ -138,3 +141,26 @@ func shouldCreate(u *bool) bool {

return true
}

// trimPathBuild checks if the build has trimpath setting true
func trimPathBuild() bool {
keys := []string{"-trimpath", "--trimpath"}
goFlags := strings.Split(os.Getenv("GOFLAGS"), " ")

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() == ""
}