Skip to content

Commit

Permalink
fix: resolve relative path of snapshot correct
Browse files Browse the repository at this point in the history
  • Loading branch information
gkampitakis committed Jul 19, 2024
1 parent c578aa7 commit b836c8d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions snaps/matchSnapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestMatchSnapshot(t *testing.T) {
"+ Received + 2\x1b[0m\n\n\x1b[38;5;52m\x1b[48;5;225m- int(10)\x1b[0m\n\x1b[38;5;52m\x1b[48;5;225m" +
"- hello world\x1b[0m\n\x1b[38;5;22m\x1b[48;5;159m+ int(100)\x1b[0m\n\x1b[38;5;22m\x1b[48;5;159m" +
"+ bye world\x1b[0m\n\n\x1b[2mat " + filepath.FromSlash(
"../__snapshots__/matchSnapshot_test.snap:2",
"__snapshots__/matchSnapshot_test.snap:2",
) +
"\n\x1b[0m"

Expand Down Expand Up @@ -227,7 +227,7 @@ func TestMatchSnapshot(t *testing.T) {
"- hello world----\x1b[0m\n\x1b[38;5;52m\x1b[48;5;225m- ---\x1b[0m\n\x1b[38;5;22m\x1b[48;5;159m" +
"+ int(100)\x1b[0m\n\x1b[38;5;22m\x1b[48;5;159m+ bye world----\x1b[0m\n\x1b[38;5;22m\x1b[48;5;159m" +
"+ --\x1b[0m\n\n\x1b[2mat " + filepath.FromSlash(
"../__snapshots__/matchSnapshot_test.snap:2",
"__snapshots__/matchSnapshot_test.snap:2",
) +
"\n\x1b[0m"

Expand Down
8 changes: 4 additions & 4 deletions snaps/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,20 +255,20 @@ Returns the relative path of the caller and the snapshot path.
*/
func snapshotPath(c *config) (string, string) {
// skips current func, the wrapper match* and the exported Match* func
callerPath := baseCaller(3)
callerFilename := baseCaller(3)

dir := c.snapsDir
if !filepath.IsAbs(dir) {
dir = filepath.Join(filepath.Dir(callerPath), c.snapsDir)
dir = filepath.Join(filepath.Dir(callerFilename), c.snapsDir)
}

filename := c.filename
if filename == "" {
base := filepath.Base(callerPath)
base := filepath.Base(callerFilename)
filename = strings.TrimSuffix(base, filepath.Ext(base))
}
snapPath := filepath.Join(dir, filename+snapsExt)
snapPathRel, _ := filepath.Rel(callerPath, snapPath)
snapPathRel, _ := filepath.Rel(filepath.Dir(callerFilename), snapPath)

return snapPath, snapPathRel
}
Expand Down
6 changes: 3 additions & 3 deletions snaps/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func TestSnapPathAndFile(t *testing.T) {
}()

test.Contains(t, snapPath, filepath.FromSlash("/snaps/__snapshots__"))
test.Contains(t, snapPathRel, filepath.FromSlash("../__snapshots__/snapshot_test.snap"))
test.Contains(t, snapPathRel, filepath.FromSlash("__snapshots__/snapshot_test.snap"))
})

t.Run("should return path and file from config", func(t *testing.T) {
Expand All @@ -177,7 +177,7 @@ func TestSnapPathAndFile(t *testing.T) {

// returns the current file's path /snaps/*
test.Contains(t, snapPath, filepath.FromSlash("/snaps/my_snapshot_dir"))
test.Contains(t, snapPathRel, filepath.FromSlash("../my_snapshot_dir/my_file.snap"))
test.Contains(t, snapPathRel, filepath.FromSlash("my_snapshot_dir/my_file.snap"))
})

t.Run("should return absolute path", func(t *testing.T) {
Expand All @@ -198,7 +198,7 @@ func TestSnapPathAndFile(t *testing.T) {
}()

test.Contains(t, snapPath, filepath.FromSlash("/path_to/my_snapshot_dir"))
test.Contains(t, snapPathRel, filepath.FromSlash("/path_to/my_snapshot_dir/my_file.snap"))
test.Contains(t, snapPathRel, filepath.FromSlash("path_to/my_snapshot_dir/my_file.snap"))
})
}

Expand Down

0 comments on commit b836c8d

Please sign in to comment.