1 new changeset in pytest:

http://bitbucket.org/hpk42/pytest/changeset/81082bcf2765/
changeset:   81082bcf2765
user:        gutworth
date:        2011-07-13 00:09:14
summary:     fix assertion rewriting in read-only directories (refs #60)
affected #:  3 files (688 bytes)

--- a/CHANGELOG Tue Jul 12 23:09:03 2011 +0200
+++ b/CHANGELOG Tue Jul 12 17:09:14 2011 -0500
@@ -1,6 +1,7 @@
 Changes between 2.1.0 and 2.1.1.DEV
 ----------------------------------------------
 
+- fix assertion rewriting in read-only directories
 - fix issue59: provide system-out/err tags for junitxml output
 - fix assertion rewriting on boolean operations with 3 or more operands
 


--- a/_pytest/assertion/rewrite.py      Tue Jul 12 23:09:03 2011 +0200
+++ b/_pytest/assertion/rewrite.py      Tue Jul 12 17:09:14 2011 -0500
@@ -90,16 +90,26 @@
         # cached pyc is always a complete, valid pyc. Operations on it must be
         # atomic. POSIX's atomic rename comes in handy.
         cache_dir = os.path.join(fn_pypath.dirname, "__pycache__")
-        py.path.local(cache_dir).ensure(dir=True)
+        try:
+            py.path.local(cache_dir).ensure(dir=True)
+        except py.error.EACCES:
+            state.trace("read only directory: %r" % (fn_pypath.dirname,))
+            write = False
+        else:
+            write = True
         cache_name = fn_pypath.basename[:-3] + "." + PYTEST_TAG + ".pyc"
         pyc = os.path.join(cache_dir, cache_name)
+        # Notice that even if we're in a read-only directory, I'm going to 
check
+        # for a cached pyc. This may not be optimal...
         co = _read_pyc(fn_pypath, pyc)
         if co is None:
             state.trace("rewriting %r" % (fn,))
-            co = _make_rewritten_pyc(state, fn_pypath, pyc)
+            co = _rewrite_test(state, fn_pypath)
             if co is None:
-                # Probably a SyntaxError in the module.
+                # Probably a SyntaxError in the test.
                 return None
+            if write:
+                _make_rewritten_pyc(state, fn_pypath, pyc, co)
         else:
             state.trace("found cached rewritten pyc for %r" % (fn,))
         self.modules[name] = co, pyc
@@ -135,12 +145,8 @@
     finally:
         fp.close()
 
-def _make_rewritten_pyc(state, fn, pyc):
-    """Try to rewrite *fn* and dump the rewritten code to *pyc*.
-
-    Return the code object of the rewritten module on success. Return None if
-    there are problems parsing or compiling the module.
-    """
+def _rewrite_test(state, fn):
+    """Try to read and rewrite *fn* and return the code object."""
     try:
         source = fn.read("rb")
     except EnvironmentError:
@@ -159,6 +165,10 @@
         # assertion rewriting, but I don't know of a fast way to tell.
         state.trace("failed to compile: %r" % (fn,))
         return None
+    return co
+
+def _make_rewritten_pyc(state, fn, pyc, co):
+    """Try to dump rewritten code to *pyc*."""
     if sys.platform.startswith("win"):
         # Windows grants exclusive access to open files and doesn't have atomic
         # rename, so just write into the final file.


--- a/testing/test_assertrewrite.py     Tue Jul 12 23:09:03 2011 +0200
+++ b/testing/test_assertrewrite.py     Tue Jul 12 17:09:14 2011 -0500
@@ -266,3 +266,14 @@
                 return False
             assert myany(A() < 0)
         assert "<MY42 object>< 0" in getmsg(f)
+
+
+class TestRewriteOnImport:
+
+    def test_readonly(self, testdir):
+        sub = testdir.mkdir("testing")
+        sub.join("test_readonly.py").write("""
+def test_rewritten():
+    assert "@py_builtins" in globals()""")
+        sub.chmod(320)
+        assert testdir.runpytest().ret == 0

Repository URL: https://bitbucket.org/hpk42/pytest/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
_______________________________________________
py-svn mailing list
py-svn@codespeak.net
http://codespeak.net/mailman/listinfo/py-svn

Reply via email to