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

symlink: drop go 1.13, and use testing.Cleanup() #55

Merged
merged 1 commit into from
Feb 25, 2021
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
62 changes: 24 additions & 38 deletions symlink/fs_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func testSymlink(tmpdir, path, expected, scope string) error {
}

func TestFollowSymlinkAbsolute(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we use t.TempDir() here? https://golang.org/pkg/testing/#T.TempDir

Ahh, this is because they are only supported since 1.15, so we need to wait at least until 1.14 is no longer supported.

Makes sense to add a TODO maybe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, can add a TODO

(didn't know about the t.TempDir() until you mentioned it recently. I should catch up more on new features 😂)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't use this shiny new t.TempDir() anyway until 1.14 goes out of fashion...


if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/a/d", target: "/b"}}); err != nil {
t.Fatal(err)
}
Expand All @@ -64,8 +64,7 @@ func TestFollowSymlinkAbsolute(t *testing.T) {
}

func TestFollowSymlinkRelativePath(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)

if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/i", target: "a"}}); err != nil {
t.Fatal(err)
Expand All @@ -76,8 +75,7 @@ func TestFollowSymlinkRelativePath(t *testing.T) {
}

func TestFollowSymlinkSkipSymlinksOutsideScope(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)

if err := makeFs(tmpdir, []dirOrLink{
{path: "linkdir", target: "realdir"},
Expand All @@ -97,8 +95,7 @@ func TestFollowSymlinkInvalidScopePathPair(t *testing.T) {
}

func TestFollowSymlinkLastLink(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)

if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/a/d", target: "/b"}}); err != nil {
t.Fatal(err)
Expand All @@ -109,8 +106,7 @@ func TestFollowSymlinkLastLink(t *testing.T) {
}

func TestFollowSymlinkRelativeLinkChangeScope(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)

if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/a/e", target: "../b"}}); err != nil {
t.Fatal(err)
Expand All @@ -126,8 +122,7 @@ func TestFollowSymlinkRelativeLinkChangeScope(t *testing.T) {
}

func TestFollowSymlinkDeepRelativeLinkChangeScope(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)

if err := makeFs(tmpdir, []dirOrLink{{path: "testdata/fs/a/f", target: "../../../../test"}}); err != nil {
t.Fatal(err)
Expand All @@ -147,8 +142,7 @@ func TestFollowSymlinkDeepRelativeLinkChangeScope(t *testing.T) {
}

func TestFollowSymlinkRelativeLinkChain(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)

// avoid letting symlink g (pointed at by symlink h) take out of scope
// TODO: we should probably normalize to scope here because ../[....]/root
Expand All @@ -165,8 +159,7 @@ func TestFollowSymlinkRelativeLinkChain(t *testing.T) {
}

func TestFollowSymlinkBreakoutPath(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)

// avoid letting symlink -> ../directory/file escape from scope
// normalize to "testdata/fs/j"
Expand All @@ -179,8 +172,7 @@ func TestFollowSymlinkBreakoutPath(t *testing.T) {
}

func TestFollowSymlinkToRoot(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)

// make sure we don't allow escaping to /
// normalize to dir
Expand All @@ -193,8 +185,7 @@ func TestFollowSymlinkToRoot(t *testing.T) {
}

func TestFollowSymlinkSlashDotdot(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)
tmpdir = filepath.Join(tmpdir, "dir", "subdir")

// make sure we don't allow escaping to /
Expand All @@ -208,8 +199,7 @@ func TestFollowSymlinkSlashDotdot(t *testing.T) {
}

func TestFollowSymlinkDotdot(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)
tmpdir = filepath.Join(tmpdir, "dir", "subdir")

// make sure we stay in scope without leaking information
Expand All @@ -224,8 +214,7 @@ func TestFollowSymlinkDotdot(t *testing.T) {
}

func TestFollowSymlinkRelativePath2(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)

if err := makeFs(tmpdir, []dirOrLink{{path: "bar/foo", target: "baz/target"}}); err != nil {
t.Fatal(err)
Expand All @@ -236,8 +225,7 @@ func TestFollowSymlinkRelativePath2(t *testing.T) {
}

func TestFollowSymlinkScopeLink(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)

if err := makeFs(tmpdir, []dirOrLink{
{path: "root2"},
Expand All @@ -252,8 +240,7 @@ func TestFollowSymlinkScopeLink(t *testing.T) {
}

func TestFollowSymlinkRootScope(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)

expected, err := filepath.EvalSymlinks(tmpdir)
if err != nil {
Expand Down Expand Up @@ -283,8 +270,7 @@ func TestFollowSymlinkEmpty(t *testing.T) {
}

func TestFollowSymlinkCircular(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)

if err := makeFs(tmpdir, []dirOrLink{{path: "root/foo", target: "foo"}}); err != nil {
t.Fatal(err)
Expand All @@ -306,8 +292,7 @@ func TestFollowSymlinkCircular(t *testing.T) {
}

func TestFollowSymlinkComplexChainWithTargetPathsContainingLinks(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)

if err := makeFs(tmpdir, []dirOrLink{
{path: "root2"},
Expand All @@ -327,8 +312,7 @@ func TestFollowSymlinkComplexChainWithTargetPathsContainingLinks(t *testing.T) {
}

func TestFollowSymlinkBreakoutNonExistent(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)

if err := makeFs(tmpdir, []dirOrLink{
{path: "root/slash", target: "/"},
Expand All @@ -342,8 +326,7 @@ func TestFollowSymlinkBreakoutNonExistent(t *testing.T) {
}

func TestFollowSymlinkNoLexicalCleaning(t *testing.T) {
tmpdir, cleanup := mkTempDir(t)
defer cleanup()
tmpdir := mkTempDir(t)

if err := makeFs(tmpdir, []dirOrLink{
{path: "root/sym", target: "/foo/bar"},
Expand All @@ -356,11 +339,14 @@ func TestFollowSymlinkNoLexicalCleaning(t *testing.T) {
}
}

func mkTempDir(t *testing.T) (string, func()) {
// TODO use testing.TempDir() instead (https://golang.org/pkg/testing/#T.TempDir)
// once we no longer test on Go 1.14 and older.
func mkTempDir(t *testing.T) string {
t.Helper()
tmpdir, err := ioutil.TempDir("", t.Name())
if err != nil {
t.Fatal(err)
}
return tmpdir, func() { _ = os.RemoveAll(tmpdir) }
t.Cleanup(func() { _ = os.RemoveAll(tmpdir) })
return tmpdir
}
2 changes: 1 addition & 1 deletion symlink/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/moby/sys/symlink

go 1.13
go 1.14

require golang.org/x/sys v0.0.0-20200922070232-aee5d888a860