Browse Source

handle bad args and more unit tests

changes/49/376949/1
Corey Goldberg 8 years ago
parent
commit
f87d693855
  1. 49
      xvfbwrapper/test_xvfb.py
  2. 10
      xvfbwrapper/xvfbwrapper.py

49
xvfbwrapper/test_xvfb.py

@ -4,8 +4,14 @@
from .xvfbwrapper import Xvfb
import os
import sys
import unittest
try:
from StringIO import StringIO # Python 2.7 compat
except ImportError:
from io import StringIO
import pep8
@ -18,7 +24,15 @@ class TestXvfb(unittest.TestCase):
self.assertEqual(':%d' % xvfb.vdisplay_num, os.environ['DISPLAY'])
self.assertIsNot(None, xvfb.proc)
def test_start_with_args(self):
def test_stop(self):
orig_display = os.environ['DISPLAY']
xvfb = Xvfb()
xvfb.start()
self.assertNotEqual(orig_display, os.environ['DISPLAY'])
xvfb.stop()
self.assertEqual(orig_display, os.environ['DISPLAY'])
def test_start_with_kwargs(self):
w = 800
h = 600
depth = 16
@ -29,15 +43,36 @@ class TestXvfb(unittest.TestCase):
self.assertEqual(h, xvfb.height)
self.assertEqual(depth, xvfb.colordepth)
self.assertEqual(os.environ['DISPLAY'], ':%d' % xvfb.vdisplay_num)
self.assertIsNotNone(xvfb.proc)
def test_start_with_arbitrary_kwarg(self):
xvfb = Xvfb(nolisten='tcp')
self.addCleanup(xvfb.stop)
xvfb.start()
self.assertEqual(os.environ['DISPLAY'], ':%d' % xvfb.vdisplay_num)
self.assertIsNot(None, xvfb.proc)
def test_stop(self):
orig = os.environ['DISPLAY']
xvfb = Xvfb()
def test_start_with_bad_kwarg_param(self):
orig_display = os.environ['DISPLAY']
out = StringIO()
sys.stdout = out
xvfb = Xvfb(nolisten='badvalue')
self.addCleanup(xvfb.stop)
xvfb.start()
self.assertNotEqual(orig, os.environ['DISPLAY'])
xvfb.stop()
self.assertEqual(orig, os.environ['DISPLAY'])
self.assertEqual(orig_display, os.environ['DISPLAY'])
self.assertIsNone(xvfb.proc)
self.assertIn('Error: Xvfb did not start', out.getvalue())
def test_start_with_bad_kwarg(self):
orig_display = os.environ['DISPLAY']
out = StringIO()
sys.stdout = out
xvfb = Xvfb(badarg='foo')
self.addCleanup(xvfb.stop)
xvfb.start()
self.assertEqual(orig_display, os.environ['DISPLAY'])
self.assertIsNone(xvfb.proc)
self.assertIn('Error: Xvfb did not start', out.getvalue())
class Pep8ConformanceTestCase(unittest.TestCase):

10
xvfbwrapper/xvfbwrapper.py

@ -43,9 +43,15 @@ class Xvfb:
self.proc = subprocess.Popen(self.xvfb_cmd,
stdout=open(os.devnull),
stderr=open(os.devnull))
stderr=open(os.devnull),
)
time.sleep(0.1) # give Xvfb time to start
self._redirect_display(self.vdisplay_num)
ret_code = self.proc.poll()
if ret_code is None:
self._redirect_display(self.vdisplay_num)
else:
self.proc = None
print('Error: Xvfb did not start')
def stop(self):
self._redirect_display(self.old_display_num)

Loading…
Cancel
Save