Commits

holger krekel committed d3fb01f

fix issue124 - make test reporting more resilient against tests changing FD 1

Comments (0)

Files changed (4)

 Changes between 1.3.4 and 1.4.0.dev0
 ==================================================
 
+- fix issue124 - make reporting more resilient against tests opening
+  files on filedescriptor 1 (stdout).
 - fix issue109 - sibling conftest.py files will not be loaded.
   (and Directory collectors cannot be customized anymore from a Directory's
   conftest.py - this needs to happen at least one level up).

py/_plugin/pytest_terminal.py

 """
 import py
 import sys
+import os
 
 def pytest_addoption(parser):
     group = parser.getgroup("terminal reporting", "reporting", after="general")
                help="don't cut any tracebacks (default is to cut).")
 
 def pytest_configure(config):
-    if config.option.showfuncargs:
-        return
     if config.option.collectonly:
         reporter = CollectonlyReporter(config)
     else:
         self.curdir = py.path.local()
         if file is None:
             file = py.std.sys.stdout
+            # we try hard to make printing resilient against 
+            # later changes on FD level.
+            if hasattr(os, 'dup') and hasattr(file, 'fileno'):
+                try:
+                    newfd = os.dup(file.fileno())
+                except ValueError:
+                    pass
+                else:
+                    file = os.fdopen(newfd, file.mode, 1)
         self._tw = py.io.TerminalWriter(file)
         self.currentfspath = None
         self.reportchars = getreportopt(config)

testing/plugin/test_pytest_capture.py

     result.stdout.fnmatch_lines([
         "*1 skipped*"
     ])
-

testing/plugin/test_pytest_terminal.py

             "line2",
             "*hello: info*",
         ])
+
+needsosdup = py.test.mark.xfail("not hasattr(os, 'dup')")
+def test_fdopen_kept_alive_issue124(testdir):
+    testdir.makepyfile("""
+        import os, sys
+        k = []
+        def test_open_file_and_keep_alive(capfd):
+            stdout = os.fdopen(1, 'w', 1)
+            k.append(stdout)
+
+        def test_close_kept_alive_file():
+            stdout = k.pop()
+            stdout.close()
+    """)
+    result = testdir.runpytest("-s")
+    result.stdout.fnmatch_lines([
+        "*2 passed*"
+    ])