[pypy-commit] pypy rposix-for-3: Add tests for mkdirat(), openat(), unlinkat()

rlamy pypy.commits at gmail.com
Tue Mar 22 14:28:31 EDT 2016


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: rposix-for-3
Changeset: r83271:49faef350e25
Date: 2016-03-22 18:27 +0000
http://bitbucket.org/pypy/pypy/changeset/49faef350e25/

Log:	Add tests for mkdirat(), openat(), unlinkat()

diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py
--- a/rpython/rlib/rposix.py
+++ b/rpython/rlib/rposix.py
@@ -1877,7 +1877,7 @@
 
     @enforceargs(s_Str0, int, int, int, typecheck=False)
     def openat(path, flags, mode, dir_fd=AT_FDCWD):
-        fd = c_openat(path, flags, mode, dir_fd)
+        fd = c_openat(dir_fd, path, flags, mode)
         return handle_posix_error('open', fd)
 
 if HAVE_MKFIFOAT:
diff --git a/rpython/rlib/test/test_rposix.py b/rpython/rlib/test/test_rposix.py
--- a/rpython/rlib/test/test_rposix.py
+++ b/rpython/rlib/test/test_rposix.py
@@ -7,6 +7,10 @@
 import errno
 import py
 
+def rposix_requires(funcname):
+    return py.test.mark.skipif(not hasattr(rposix, funcname),
+        reason="Requires rposix.%s()" % funcname)
+
 class TestPosixFunction:
     def test_access(self):
         filename = str(udir.join('test_access.txt'))
@@ -99,11 +103,25 @@
     def test_mkdir(self):
         filename = str(udir.join('test_mkdir.dir'))
         rposix.mkdir(filename, 0)
-        exc = py.test.raises(OSError, rposix.mkdir, filename, 0)
-        assert exc.value.errno == errno.EEXIST
+        with py.test.raises(OSError) as excinfo:
+            rposix.mkdir(filename, 0)
+        assert excinfo.value.errno == errno.EEXIST
         if sys.platform == 'win32':
             assert exc.type is WindowsError
 
+    @rposix_requires('mkdirat')
+    def test_mkdirat(self):
+        relpath = 'test_mkdirat.dir'
+        filename = str(udir.join(relpath))
+        dirfd = os.open(os.path.dirname(filename), os.O_RDONLY)
+        try:
+            rposix.mkdirat(relpath, 0, dir_fd=dirfd)
+            with py.test.raises(OSError) as excinfo:
+                rposix.mkdirat(relpath, 0, dir_fd=dirfd)
+            assert excinfo.value.errno == errno.EEXIST
+        finally:
+            os.close(dirfd)
+
     def test_strerror(self):
         assert rposix.strerror(2) == os.strerror(2)
 
@@ -448,6 +466,38 @@
     def _get_filename(self):
         return str(udir.join('test_open_ascii'))
 
+    @rposix_requires('openat')
+    def test_openat(self):
+        def f(dirfd):
+            try:
+                fd = rposix.openat('test_open_ascii', os.O_RDONLY, 0777, dirfd)
+                try:
+                    text = os.read(fd, 50)
+                    return text
+                finally:
+                    os.close(fd)
+            except OSError:
+                return ''
+
+        dirfd = os.open(os.path.dirname(self.ufilename), os.O_RDONLY)
+        try:
+            assert ll_to_string(interpret(f, [dirfd])) == "test"
+        finally:
+            os.close(dirfd)
+
+    @rposix_requires('unlinkat')
+    def test_unlinkat(self):
+        def f(dirfd):
+            return rposix.unlinkat('test_open_ascii', dir_fd=dirfd)
+
+        dirfd = os.open(os.path.dirname(self.ufilename), os.O_RDONLY)
+        try:
+            interpret(f, [dirfd])
+        finally:
+            os.close(dirfd)
+        assert not os.path.exists(self.ufilename)
+
+
 class TestPosixUnicode(BasePosixUnicodeOrAscii):
     def _get_filename(self):
         return (unicode(udir.join('test_open')) +


More information about the pypy-commit mailing list