From b836c8d5ad6ef27eb895374ed05e0b5a8f476026 Mon Sep 17 00:00:00 2001 From: Georgios Kampitakis Date: Fri, 19 Jul 2024 19:55:13 +0100 Subject: [PATCH] fix: resolve relative path of snapshot correct --- snaps/matchSnapshot_test.go | 4 ++-- snaps/snapshot.go | 8 ++++---- snaps/snapshot_test.go | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/snaps/matchSnapshot_test.go b/snaps/matchSnapshot_test.go index 186fa30..87c3a9d 100644 --- a/snaps/matchSnapshot_test.go +++ b/snaps/matchSnapshot_test.go @@ -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" @@ -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" diff --git a/snaps/snapshot.go b/snaps/snapshot.go index 8b0bcec..05c588f 100644 --- a/snaps/snapshot.go +++ b/snaps/snapshot.go @@ -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 } diff --git a/snaps/snapshot_test.go b/snaps/snapshot_test.go index cacbc56..eee23d3 100644 --- a/snaps/snapshot_test.go +++ b/snaps/snapshot_test.go @@ -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) { @@ -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) { @@ -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")) }) }