mounter: add .pp() method returning a pathlib.Path instead of str
Signed-off-by: Olivier Gayot <olivier.gayot@canonical.com>
This commit is contained in:
parent
cd234d416a
commit
59704a1f1e
|
@ -56,11 +56,15 @@ class AbsolutePathError(Exception):
|
|||
|
||||
|
||||
class _MountBase:
|
||||
def p(self, *args: Union[str, Path]) -> Path:
|
||||
def pp(self, *args: Union[str, Path]) -> Path:
|
||||
"""Same as p() but returns a pathlib.Path."""
|
||||
for a in args:
|
||||
if Path(a).is_absolute():
|
||||
raise AbsolutePathError("no absolute paths here please")
|
||||
return str(Path(self.mountpoint).joinpath(*args))
|
||||
return Path(self.mountpoint).joinpath(*args)
|
||||
|
||||
def p(self, *args: Union[str, Path]) -> str:
|
||||
return str(self.pp(*args))
|
||||
|
||||
def write(self, path, content):
|
||||
with open(self.p(path), "w") as fp:
|
||||
|
|
|
@ -34,21 +34,26 @@ class Test_MountBase(SubiTestCase):
|
|||
self.mountbase = _MountBase()
|
||||
self.mountbase.mountpoint = "/target"
|
||||
|
||||
def test_p(self):
|
||||
def test_pp(self):
|
||||
mnt = self.mountbase
|
||||
|
||||
self.assertEqual("/target/d1", mnt.p("d1"))
|
||||
self.assertEqual("/target/d1/d2/d3/d4", mnt.p("d1", "d2/d3", "d4"))
|
||||
self.assertEqual(Path("/target/d1"), mnt.pp("d1"))
|
||||
self.assertEqual(Path("/target/d1/d2/d3/d4"), mnt.pp("d1", "d2/d3", "d4"))
|
||||
|
||||
# Mix of strings and paths should produce the same result.
|
||||
self.assertEqual(mnt.p("d1", "d2/d3"), mnt.p("d1", Path("d2/d3")))
|
||||
self.assertEqual(mnt.p("d1", "d2/d3"), mnt.p(Path("d1"), "d2/d3"))
|
||||
self.assertEqual(mnt.pp("d1", "d2/d3"), mnt.pp("d1", Path("d2/d3")))
|
||||
self.assertEqual(mnt.pp("d1", "d2/d3"), mnt.pp(Path("d1"), "d2/d3"))
|
||||
|
||||
def test_p__absolute(self):
|
||||
def test_pp__absolute(self):
|
||||
with self.assertRaises(AbsolutePathError):
|
||||
self.mountbase.p("a", "/b")
|
||||
self.mountbase.pp("a", "/b")
|
||||
with self.assertRaises(AbsolutePathError):
|
||||
self.mountbase.p("a", pathlib.Path("/b"))
|
||||
self.mountbase.pp("a", Path("/b"))
|
||||
|
||||
def test_p(self):
|
||||
self.assertEqual(self.mountbase.p("foo", "bar"), "/target/foo/bar")
|
||||
with self.assertRaises(AbsolutePathError):
|
||||
self.mountbase.p("foo", "/bar")
|
||||
|
||||
|
||||
class TestMounter(SubiTestCase):
|
||||
|
|
Loading…
Reference in New Issue