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: resolve relative path of snapshot correct #105

Merged
merged 1 commit into from
Jul 19, 2024
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
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
Loading