Skip to content

Commit

Permalink
fix: handle builds with trimpath enabled (#120)
Browse files Browse the repository at this point in the history
Co-authored-by: Andre Klärner <kandre+github@ak-online.be>
  • Loading branch information
gkampitakis and klaernie authored Feb 8, 2025
1 parent 00f3055 commit 8740883
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 19 deletions.
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() == ""
}

0 comments on commit 8740883

Please sign in to comment.