devtools/qunit.py
changeset 5779 916ddfd72ac2
parent 5742 74c19dac29cf
child 5780 07c1d64dff34
equal deleted inserted replaced
5775:34195230dc2a 5779:916ddfd72ac2
     1 import os, os.path as osp
     1 import os, os.path as osp
     2 import signal
     2 import signal
     3 from tempfile import mkdtemp, NamedTemporaryFile
     3 from tempfile import mkdtemp, NamedTemporaryFile, TemporaryFile
     4 import tempfile
     4 import tempfile
     5 from Queue import Queue, Empty
     5 from Queue import Queue, Empty
     6 from subprocess import Popen, check_call
     6 from subprocess import Popen, check_call, CalledProcessError
     7 from shutil import rmtree, copy as copyfile
     7 from shutil import rmtree, copy as copyfile
     8 from uuid import uuid4 
     8 from uuid import uuid4 
     9 
     9 
    10 # imported by default to simplify further import statements
    10 # imported by default to simplify further import statements
    11 from logilab.common.testlib import unittest_main, with_tempdir, InnerTest
    11 from logilab.common.testlib import unittest_main, with_tempdir, InnerTest
    13 import os
    13 import os
    14 import cubicweb
    14 import cubicweb
    15 from cubicweb.view import StartupView
    15 from cubicweb.view import StartupView
    16 from cubicweb.web.controller import Controller
    16 from cubicweb.web.controller import Controller
    17 from cubicweb.devtools.httptest import CubicWebServerTC
    17 from cubicweb.devtools.httptest import CubicWebServerTC
       
    18 
       
    19 
       
    20 class VerboseCalledProcessError(CalledProcessError):
       
    21 
       
    22     def __init__(self, returncode, command, stdout, stderr):
       
    23         super(VerboseCalledProcessError, self).__init__(returncode, command)
       
    24         self.stdout = stdout
       
    25         self.stderr = stderr
       
    26 
       
    27     def __str__(self):
       
    28         str = [ super(VerboseCalledProcessError, self).__str__()]
       
    29         if self.stdout.strip():
       
    30             str.append('******************')
       
    31             str.append('* process stdout *')
       
    32             str.append('******************')
       
    33             str.append(self.stdout)
       
    34         if self.stderr.strip():
       
    35             str.append('******************')
       
    36             str.append('* process stderr *')
       
    37             str.append('******************')
       
    38             str.append(self.stderr)
       
    39         return '\n'.join(str)
       
    40 
       
    41 
    18 
    42 
    19 class FirefoxHelper(object):
    43 class FirefoxHelper(object):
    20 
    44 
    21     profile_name_mask = 'PYTEST_PROFILE_%(uid)s'
    45     profile_name_mask = 'PYTEST_PROFILE_%(uid)s'
    22 
    46 
    24         self._process = None
    48         self._process = None
    25         self._tmp_dir = mkdtemp()
    49         self._tmp_dir = mkdtemp()
    26         self._profile_data = {'uid': uuid4()}
    50         self._profile_data = {'uid': uuid4()}
    27         self._profile_name = self.profile_name_mask % self._profile_data
    51         self._profile_name = self.profile_name_mask % self._profile_data
    28         fnull = open(os.devnull, 'w')
    52         fnull = open(os.devnull, 'w')
    29         check_call(['firefox', '-no-remote', '-CreateProfile',
    53         stdout = TemporaryFile()
    30                     '%s %s' % (self._profile_name, self._tmp_dir)],
    54         stderr = TemporaryFile()
    31                               stdout=fnull, stderr=fnull)
    55         try:
    32         if url is not None:
    56           check_call(['firefox', '-no-remote', '-CreateProfile',
    33             self.start(url)
    57                       '%s %s' % (self._profile_name, self._tmp_dir)],
       
    58                                 stdout=stdout, stderr=stderr)
       
    59         except CalledProcessError, cpe:
       
    60             stdout.seek(0)
       
    61             stderr.seek(0)
       
    62             raise VerboseCalledProcessError(cpe.returncode, cpe.cmd, stdout.read(), stderr.read())
    34 
    63 
    35 
    64 
    36     def start(self, url):
    65     def start(self, url):
    37         self.stop()
    66         self.stop()
    38         fnull = open(os.devnull, 'w')
    67         fnull = open(os.devnull, 'w')