Example #1
0
class MemoryPathTestCase(AbstractFilePathTestCase):

    def subdir(self, *dirname):
        for head in heads(dirname):
            self.fs._dirs.add(head)
        self.fs._dirs.add(dirname)

    def subfile(self, *dirname):
        for head in heads(dirname):
            self.fs._dirs.add(head)
        return self.fs.open(dirname)

    def setUp(self):
        self.fs = MemoryFS()

        AbstractFilePathTestCase.setUp(self)

        self.path = MemoryPath(fs=self.fs)
        self.root = self.path
        self.all = self.fs._dirs | set(self.fs._store.keys())
        self.all = set(format_memory_path(p, "/") for p in self.all)

    def test_removeDirectory(self):
        """
        L{MemoryPath.remove} on a L{MemoryPath} that refers to a
        directory will recursively delete its contents.
        """
        self.assertTrue(self.path.isdir())
        self.assertTrue(self.path.exists())

        foo = self.path.child("foo")
        foo.setContent("Hi!")
        baz = self.path.descendant(["bar", "baz"])
        baz.setContent("Bye!")

        self.path.remove()
        self.assertFalse(self.path.exists())
        self.assertFalse(foo.exists())
        self.assertFalse(baz.exists())

    def test_removeFile(self):
        """
        L{MemoryPath.remove} on a L{MemoryPath} that refers to a
        file simply deletes the file.
        """
        path = self.path.child("file")
        path.setContent("Hello!")
        self.assertTrue(path.isfile())
        self.assertTrue(path.exists())

        path.remove()
        self.assertFalse(path.exists())

    def test_removeNonExistant(self):
        """
        L{MemoryPath.remove} on a L{MemoryPath} that does not exist
        raises an error.
        """
        path = self.path.child("file")
        self.assertFalse(path.exists())

        with self.assertRaises(PathError) as e:
            path.remove()
        self.assertEqual(e.exception.errno, errno.ENOENT)
class MemoryPathTestCase(AbstractFilePathTestCase):
    def subdir(self, *dirname):
        for head in heads(dirname):
            self.fs._dirs.add(head)
        self.fs._dirs.add(dirname)

    def subfile(self, *dirname):
        for head in heads(dirname):
            self.fs._dirs.add(head)
        return self.fs.open(dirname)

    def setUp(self):
        self.fs = MemoryFS()

        AbstractFilePathTestCase.setUp(self)

        self.path = MemoryPath(fs=self.fs)
        self.root = self.path
        self.all = self.fs._dirs | set(self.fs._store.keys())
        self.all = set(format_memory_path(p, "/") for p in self.all)

    def test_removeDirectory(self):
        """
        L{MemoryPath.remove} on a L{MemoryPath} that refers to a
        directory will recursively delete its contents.
        """
        self.assertTrue(self.path.isdir())
        self.assertTrue(self.path.exists())

        foo = self.path.child("foo")
        foo.setContent("Hi!")

        bar = self.path.child("bar")
        bar.createDirectory()
        baz = bar.child("baz")
        baz.setContent("Bye!")

        self.path.remove()
        self.assertFalse(self.path.exists())
        self.assertFalse(foo.exists())
        self.assertFalse(baz.exists())

    def test_removeFile(self):
        """
        L{MemoryPath.remove} on a L{MemoryPath} that refers to a
        file simply deletes the file.
        """
        path = self.path.child("file")
        path.setContent("Hello!")
        self.assertTrue(path.isfile())
        self.assertTrue(path.exists())

        path.remove()
        self.assertFalse(path.exists())

    def test_removeNonExistant(self):
        """
        L{MemoryPath.remove} on a L{MemoryPath} that does not exist
        raises an error.
        """
        path = self.path.child("file")
        self.assertFalse(path.exists())

        with self.assertRaises(PathError) as e:
            path.remove()
        self.assertEqual(e.exception.errno, errno.ENOENT)

    def test_getUserID(self):
        """
        L{MemoryPath.getUserID} on a L{MemoryPath} returns the current user's
        ID by default.

        """

        self.assertEqual(self.path.getUserID(), os.getuid())

    def test_getUserID_after_set(self):
        self.fs.chown(self.path, uid=1234, gid=-1)
        self.assertEqual(self.path.getUserID(), 1234)