From 88f9fe8909aff4af13f02abee6e18355c8146cd9 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Wed, 5 Jun 2024 01:02:17 +1000 Subject: [PATCH] fixup! contenthash: switch recursive rootPath implementation to be iterative Signed-off-by: Aleksa Sarai --- cache/contenthash/path_test.go | 45 ++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/cache/contenthash/path_test.go b/cache/contenthash/path_test.go index 6b11ac13e92a4..a6d895a5f15e8 100644 --- a/cache/contenthash/path_test.go +++ b/cache/contenthash/path_test.go @@ -3,7 +3,9 @@ package contenthash import ( "fmt" "os" + "path" "path/filepath" + "runtime" "testing" "github.com/stretchr/testify/require" @@ -26,8 +28,8 @@ func TestRootPathSymlinks(t *testing.T) { symlink(t, tmpdir, "link1/notaloop", "../target") symlink(t, tmpdir, "link1/final", "/target") // |- link2 - // |- link1 -> /link1/./ - // |- link1sub -> /link1/sub/./ + // |- link1 -> /link1/ + // |- link1sub -> /link1/sub/ // |- notaloop -> ./link1sub/../notaloop // |- notaloop_abs -> /link2/link1sub/../notaloop // |- notaloop2 -> ./link1/../link1/notaloop @@ -37,8 +39,8 @@ func TestRootPathSymlinks(t *testing.T) { // |- target2 -> ./link1/../link1/final // |- target2_abs -> /link2/link1/../link1/final mkdirAll(t, tmpdir, "link2") - symlink(t, tmpdir, "link2/link1sub", "/link1/sub/") symlink(t, tmpdir, "link2/link1", "/link1/") + symlink(t, tmpdir, "link2/link1sub", "/link1/sub/") symlink(t, tmpdir, "link2/notaloop", "./link1sub/../notaloop") symlink(t, tmpdir, "link2/notaloop_abs", "/link2/link1sub/../notaloop") symlink(t, tmpdir, "link2/notaloop2", "./link1/../link1/notaloop") @@ -109,21 +111,50 @@ func TestRootPathSymlinks(t *testing.T) { resolvedPath, err := rootPath(tmpdir, test.path, test.followTrailing, nil) require.NoError(t, err) - expectedPath := filepath.Join(tmpdir, test.expected) + expectedPath := filepath.Join(tmpdir, filepath.FromSlash(test.expected)) require.Equal(t, expectedPath, resolvedPath) }) } } func mkdirAll(t *testing.T, root, path string) { + path = filepath.FromSlash(path) + err := os.MkdirAll(filepath.Join(root, path), 0755) require.NoError(t, err) } -func symlink(t *testing.T, root, path, target string) { - dir, _ := filepath.Split(path) +func symlink(t *testing.T, root, linkname, target string) { + linkname = filepath.FromSlash(linkname) + + // We need to add a dummy drive letter to emulate absolute symlinks on + // Windows. + if runtime.GOOS == "windows" && path.IsAbs(target) { + target = "Z:" + filepath.FromSlash(target) + } else { + target = filepath.FromSlash(target) + } + + dir, _ := filepath.Split(linkname) mkdirAll(t, root, dir) - err := os.Symlink(target, filepath.Join(root, path)) + fullLinkname := filepath.Join(root, linkname) + + err := os.Symlink(target, fullLinkname) require.NoError(t, err) + + // Windows seems to automatically change our /foo/../bar symlinks to /bar, + // causing some tests to fail. Technically we only care about this symlink + // behaviour on Linux (since we implemented it this way to keep Linux + // compatibility), so if our symlink has the wrong target we can just skip + // the test. + actualTarget, err := os.Readlink(fullLinkname) + require.NoError(t, err) + if actualTarget != target { + fn := t.Skipf + if runtime.GOOS != "windows" { + fn = t.Fatalf + } + fn("created link had the wrong contents -- %s -> %s", target, actualTarget) + } }