Exemplo n.º 1
0
    def test_globbing_by_suffix_in_subdir(self):
        base = URI("memory:///") / "dir"
        base.mkdir()
        a = base / "a.foo"
        b = base / "b.bar"

        for f in [a, b]:
            with f.open("w") as outf:
                outf.write("foo")

        self.assertEqual([a], base.glob("a.*"))
Exemplo n.º 2
0
    def test_globbing_by_suffix_in_subdir(self):
        base = URI("memory:///") / "dir"
        base.mkdir()
        a = base / "a.foo"
        b = base / "b.bar"

        for f in [a, b]:
            with f.open("w") as outf:
                outf.write("foo")

        self.assertEqual([a], base.glob("a.*"))
Exemplo n.º 3
0
 def test_copy(self):
     p = URI(str(self.foo_dir))
     p.mkdir()
     dest = URI(self.bar_dir)
     p.copy(dest, recursive=True)
Exemplo n.º 4
0
 def test_mkdir(self):
     p = URI(str(self.foo_dir))
     p.mkdir()
Exemplo n.º 5
0
class MemoryFSTests(CleanupMemoryBeforeTestMixin, TestCase):

    def setUp(self):
        super(MemoryFSTests, self).setUp()
        self.temp_path = URI(tempfile.mktemp())
        self.temp_path.mkdir()
        self.root = URI("memory:///")


    def tearDown(self):
        if self.temp_path.isdir():
            self.temp_path.remove(recursive=True)
        super(MemoryFSTests, self).tearDown()


    def test_all(self):
        root = self.root
        assert root.isdir()

        subdir = root / "foo"

        subdir.mkdir()

        assert subdir.isdir()
        assert not subdir.isfile()

        out = subdir / "bar"

        with out.open("w") as outf:
            outf.write("foobar")

        assert not out.isdir()
        assert out.isfile()

        with out.open() as inf:
            content = inf.read()
            self.assertEqual(content, "foobar")


        assert subdir == root / "foo"

        time.sleep(.5)
        assert out.info().mtime < time.time()

        connection = subdir.connection
        out = StringIO()
        connection.dump(out)
        print((out.getvalue()))


    def test_write_text_read_binary(self):
        test_file = self.root / 'foo'
        with test_file.open('w') as text_proxy:
            text_proxy.write("spam & eggs")
        with test_file.open('rb') as binary_proxy:
            self.assertEqual(binary_proxy.read(), b"spam & eggs")


    def test_write_binary_read_text(self):
        test_file = self.root / 'foo'
        with test_file.open('wb') as binary_proxy:
            binary_proxy.write(b"spam & eggs")
        with test_file.open('r') as text_proxy:
            self.assertEqual(text_proxy.read(), "spam & eggs")


    def test_info_on_symlinks(self):
        root = self.root
        a_file = root / "a_file"
        a_link = root / "a_link"
        with a_file.open('w') as f:
            f.write("a" * 800)
        a_file.symlink(a_link)

        self.assertEqual(a_file.info().size, 800)
        self.assertEqual(a_link.info().size, 800)
        self.assertNotEqual(a_link.info(followlinks=False).size, 800)

        orig_info = a_file.info()
        new_info = a_file.info()
        new_info.mtime = new_info.mtime + 100
        a_link.info(new_info, followlinks=False)

        self.assertEqual(a_file.info().mtime, orig_info.mtime)
        self.assertEqual(a_link.info().mtime, orig_info.mtime)
        self.assertEqual(a_link.info(followlinks=False).mtime, new_info.mtime)


    def test_listdir_empty_root(self):
        root = self.root
        files = root.listdir()
        assert not files


    def test_listdir_empty_dir(self):
        root = self.root
        foo = root / 'foo'
        foo.mkdir()
        rootfiles = root.listdir()
        assert 'foo' in rootfiles
        foofiles = foo.listdir()
        assert not foofiles


    def test_walk(self):
        root = self.root
        foo = root / 'foo'
        foo.mkdir()
        bar = root / 'bar'
        bar.mkdir()
        foofile =  foo / 'foocontent.txt'
        with foofile.open('w') as fd:
            fd.write('foocontent')
        results = []
        for root, dirs, files in root.walk():
            results.append((root, dirs, files))
        assert len(results) == 3


    def test_next(self):
        root = self.root
        subdir = root / "foo"
        with subdir.open("w") as outf:
            outf.write("foo\nbar")

        with subdir.open() as inf:
            content = next(inf)
            self.assertEqual(content, "foo\n")
            content = next(inf)
            self.assertEqual(content, "bar")

        with subdir.open() as inf:
            for line in inf:
                self.assertIn(line, ["foo\n", "bar"])

    def test_exists_on_root(self):
        root = self.root
        assert root.exists()


    def test_root_of_non_existing_dir_exists(self):
        dir_path = URI("memory:///foo")
        assert dir_path.dirname().exists()


    def test_directory_cant_be_overwritten_by_file(self):
        base = URI("memory:///")
        d = base / "foo"
        d.mkdir()
        assert d.exists()

        try:
            with d.open("w") as outf:
                outf.write("foo")
        except IOError as e:
            self.assertEqual(e.errno, errno.EISDIR)
        else:
            assert False, "You shouldn't be able to ovewrite a directory like this"


    def test_copy_into_fs(self):
        root = self.root
        for item in ["foo", "bar"]:
            with (root/item).open("wb") as fd:
                fd.write(item.encode('utf-8'))
        root.copy(self.temp_path, recursive=True)
        content = self.temp_path.listdir()
        self.assertEqual(set(content), set(["foo", "bar"]))


    def test_cleanup_removes_lingering_locks(self):
        lockfile = self.root / "lockfile"
        with lockfile.open("w") as outf:
            outf.write(" ")

        lockfile._manipulate(mtime=lockfile.mtime() + 3, lock=True)
        CONNECTION_REGISTRY.cleanup(force=True)

        with lockfile.lock(fail_on_lock=True):
            pass
Exemplo n.º 6
0
 def test_copy(self):
     p = URI(str(self.foo_dir))
     p.mkdir()
     dest = URI(self.bar_dir)
     p.copy(dest, recursive=True)
Exemplo n.º 7
0
 def test_mkdir(self):
     p = URI(str(self.foo_dir))
     p.mkdir()
Exemplo n.º 8
0
class MemoryFSTests(CleanupMemoryBeforeTestMixin, TestCase):
    def setUp(self):
        super(MemoryFSTests, self).setUp()
        self.temp_path = URI(tempfile.mktemp())
        self.temp_path.mkdir()
        self.root = URI("memory:///")

    def tearDown(self):
        if self.temp_path.isdir():
            self.temp_path.remove(recursive=True)
        super(MemoryFSTests, self).tearDown()

    def test_all(self):
        root = self.root
        assert root.isdir()

        subdir = root / "foo"

        subdir.mkdir()

        assert subdir.isdir()
        assert not subdir.isfile()

        out = subdir / "bar"

        with out.open("w") as outf:
            outf.write("foobar")

        assert not out.isdir()
        assert out.isfile()

        with out.open() as inf:
            content = inf.read()
            self.assertEqual(content, "foobar")

        assert subdir == root / "foo"

        time.sleep(.5)
        assert out.info().mtime < time.time()

        connection = subdir.connection
        out = StringIO()
        connection.dump(out)
        print((out.getvalue()))

    def test_write_text_read_binary(self):
        test_file = self.root / 'foo'
        with test_file.open('w') as text_proxy:
            text_proxy.write("spam & eggs")
        with test_file.open('rb') as binary_proxy:
            self.assertEqual(binary_proxy.read(), b"spam & eggs")

    def test_write_binary_read_text(self):
        test_file = self.root / 'foo'
        with test_file.open('wb') as binary_proxy:
            binary_proxy.write(b"spam & eggs")
        with test_file.open('r') as text_proxy:
            self.assertEqual(text_proxy.read(), "spam & eggs")

    def test_info_on_symlinks(self):
        root = self.root
        a_file = root / "a_file"
        a_link = root / "a_link"
        with a_file.open('w') as f:
            f.write("a" * 800)
        a_file.symlink(a_link)

        self.assertEqual(a_file.info().size, 800)
        self.assertEqual(a_link.info().size, 800)
        self.assertNotEqual(a_link.info(followlinks=False).size, 800)

        orig_info = a_file.info()
        new_info = a_file.info()
        new_info.mtime = new_info.mtime + 100
        a_link.info(new_info, followlinks=False)

        self.assertEqual(a_file.info().mtime, orig_info.mtime)
        self.assertEqual(a_link.info().mtime, orig_info.mtime)
        self.assertEqual(a_link.info(followlinks=False).mtime, new_info.mtime)

    def test_listdir_empty_root(self):
        root = self.root
        files = root.listdir()
        assert not files

    def test_listdir_empty_dir(self):
        root = self.root
        foo = root / 'foo'
        foo.mkdir()
        rootfiles = root.listdir()
        assert 'foo' in rootfiles
        foofiles = foo.listdir()
        assert not foofiles

    def test_walk(self):
        root = self.root
        foo = root / 'foo'
        foo.mkdir()
        bar = root / 'bar'
        bar.mkdir()
        foofile = foo / 'foocontent.txt'
        with foofile.open('w') as fd:
            fd.write('foocontent')
        results = []
        for root, dirs, files in root.walk():
            results.append((root, dirs, files))
        assert len(results) == 3

    def test_next(self):
        root = self.root
        subdir = root / "foo"
        with subdir.open("w") as outf:
            outf.write("foo\nbar")

        with subdir.open() as inf:
            content = next(inf)
            self.assertEqual(content, "foo\n")
            content = next(inf)
            self.assertEqual(content, "bar")

        with subdir.open() as inf:
            for line in inf:
                self.assertIn(line, ["foo\n", "bar"])

    def test_exists_on_root(self):
        root = self.root
        assert root.exists()

    def test_root_of_non_existing_dir_exists(self):
        dir_path = URI("memory:///foo")
        assert dir_path.dirname().exists()

    def test_directory_cant_be_overwritten_by_file(self):
        base = URI("memory:///")
        d = base / "foo"
        d.mkdir()
        assert d.exists()

        try:
            with d.open("w") as outf:
                outf.write("foo")
        except IOError as e:
            self.assertEqual(e.errno, errno.EISDIR)
        else:
            assert False, "You shouldn't be able to ovewrite a directory like this"

    def test_copy_into_fs(self):
        root = self.root
        for item in ["foo", "bar"]:
            with (root / item).open("wb") as fd:
                fd.write(item.encode('utf-8'))
        root.copy(self.temp_path, recursive=True)
        content = self.temp_path.listdir()
        self.assertEqual(set(content), set(["foo", "bar"]))

    def test_cleanup_removes_lingering_locks(self):
        lockfile = self.root / "lockfile"
        with lockfile.open("w") as outf:
            outf.write(" ")

        lockfile._manipulate(mtime=lockfile.mtime() + 3, lock=True)
        CONNECTION_REGISTRY.cleanup(force=True)

        with lockfile.lock(fail_on_lock=True):
            pass