Skip to content

Commit

Permalink
fixup! contenthash: switch recursive rootPath implementation to be it…
Browse files Browse the repository at this point in the history
…erative

Signed-off-by: Aleksa Sarai <[email protected]>
  • Loading branch information
cyphar committed Jun 5, 2024
1 parent 34a4919 commit 88f9fe8
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions cache/contenthash/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package contenthash
import (
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 88f9fe8

Please sign in to comment.