From 5c8a9abac64f96182e0ff70f71b7801440e7abdb Mon Sep 17 00:00:00 2001 From: Tiexin Guo Date: Tue, 27 Aug 2024 05:09:45 +0800 Subject: [PATCH] test: improve osutil.mkdir test coverage (#492) Added two more test cases to improve test coverage. If the path already exists but it's not a directory, a `syscall.ENOTDIR` error is expected. --- internals/osutil/mkdir_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/internals/osutil/mkdir_test.go b/internals/osutil/mkdir_test.go index 5f388bbb..6983e26d 100644 --- a/internals/osutil/mkdir_test.go +++ b/internals/osutil/mkdir_test.go @@ -64,6 +64,16 @@ func (mkdirSuite) TestExistNotOK(c *check.C) { c.Assert(err, check.ErrorMatches, `.*: file exists`) } +func (mkdirSuite) TestExistsButNotDir(c *check.C) { + tmpDir := c.MkDir() + + _, err := os.Create(tmpDir + "/foo") + c.Assert(err, check.IsNil) + + err = osutil.Mkdir(tmpDir+"/foo", 0o755, nil) + c.Assert(err, check.ErrorMatches, `.*: not a directory`) +} + func (mkdirSuite) TestDirEndWithSlash(c *check.C) { tmpDir := c.MkDir() @@ -112,6 +122,18 @@ func (mkdirSuite) TestMakeParentsAndExistNotOK(c *check.C) { c.Assert(err, check.ErrorMatches, `.*: file exists`) } +func (mkdirSuite) TestParentExistsButNotDir(c *check.C) { + tmpDir := c.MkDir() + + _, err := os.Create(tmpDir + "/foo") + c.Assert(err, check.IsNil) + + err = osutil.Mkdir(tmpDir+"/foo/bar/", 0o755, &osutil.MkdirOptions{ + MakeParents: true, + }) + c.Assert(err, check.ErrorMatches, `.*: not a directory`) +} + func (mkdirSuite) TestChmod(c *check.C) { oldmask := syscall.Umask(0022) defer syscall.Umask(oldmask)