Changeset 36:4c964b691922 in pyenvjasmine for pyenvjasmine/runner.py


Ignore:
Timestamp:
Mar 23, 2018, 9:09:29 AM (6 years ago)
Author:
Mauro Molino <mauro@…>
Branch:
default
Phase:
public
Message:

Selectable engine (phantom with jasmine3, rhino with jasmine1)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pyenvjasmine/runner.py

    r33 r36  
    8989
    9090    watchdog.cancel()  # if it's still waiting to run
    91 
    9291    # if it timed out, success is False
    9392    success = (not kill_check.isSet()) and p.returncode >= 0
     
    9594    return (success, ''.join(stdout_l), stderr)
    9695
    97 
    9896class Runner(object):
    9997    """
     
    105103
    106104    def __init__(self, rootdir=None, testdir=None, configfile=None,
    107                  browser_configfile=None):
     105                 browser_configfile=None, testing_enviroment='phantomjs'):
    108106        """
    109107        Set up paths, by default everything is
     
    127125        self.configfile = configfile
    128126        self.browser_configfile = browser_configfile
    129         self.runner_html = os.path.join(here, 'runner.html')
    130 
    131     def run(self, spec=None, timeout=None):
    132         """
    133         Run the js tests with envjasmine, return success (true/false) and
    134         the captured stdout data
    135 
    136         spec: (relative) path to a spec file (run only that spec)
    137         timeout: Set it to a given number of seconds and the process running
    138                  the js tests will be killed passed that time
    139         """
     127        self.runner_js = os.path.join(here, 'run-jasmine3.js')
     128        self.testing_enviroment = testing_enviroment
    140129        environment = get_environment()
    141130        rhino_path = os.path.join(self.rootdir, 'lib', 'rhino', 'js.jar')
     
    143132        rootdir_param = '--rootDir=%s' % self.rootdir
    144133        testdir_param = '--testDir=%s' % self.testdir
     134
     135        # using a dictionary to parameterize the different engines
     136        self.envs = {
     137            'phantomjs' : {
     138                'command' : [
     139                    'phantomjs',
     140                    '--debug=true',
     141                    self.runner_js,
     142                    'browser.runner.html'
     143                ],
     144                'runner_html' : 'runner3.html',
     145                'failed_mark' : 'FAILED',
     146                'success_mark' : '0 failures'
     147            },
     148            'rhino' : {
     149                'command' : [
     150                    'java',
     151                    '-Duser.timezone=US/Eastern',
     152                    '-Dfile.encoding=utf-8',
     153                    '-jar',
     154                    rhino_path,
     155                    envjasmine_js_path,
     156                    '--disableColor',
     157                    environment,
     158                    rootdir_param,
     159                    testdir_param
     160                ],
     161                'runner_html' : 'runner.html',
     162                'failed_mark' : 'FAILED',
     163                'success_mark' : 'Failed: 0',
     164                'command_params_func' : self.command_params_rhino
     165            }
     166        }
     167        self.runner_html = os.path.join(here,
     168                              self.envs[self.testing_enviroment]['runner_html'])
     169
     170    def command_params_rhino(self, command):
     171        """
     172        Function specific to Rhino to add eventual arguments to the command line.
     173        The function is referenced in the dictionary "envs" with key
     174        "command_params_func", and for every engine that needs the same type of
     175        manipulation there will be a similar function with relative reference in
     176        the dictionary
     177        """
     178        if self.configfile and os.path.exists(self.configfile):
     179           command.append('--configFile=%s' % self.configfile)
     180        return command
     181
     182    def run(self, spec=None, timeout=None):
     183        """
     184        Run the js tests with envjasmine, return success (true/false) and
     185        the captured stdout data
     186
     187        spec: (relative) path to a spec file (run only that spec)
     188        timeout: Set it to a given number of seconds and the process running
     189                 the js tests will be killed passed that time
     190        """
    145191        if self.browser_configfile and os.path.exists(self.browser_configfile):
    146192            self.write_browser_htmlfile()
    147 
    148         command = [
    149             'java',
    150             '-Duser.timezone=US/Eastern',
    151             '-Dfile.encoding=utf-8',
    152             '-jar',
    153             rhino_path,
    154             envjasmine_js_path,
    155             '--disableColor',
    156             environment,
    157             rootdir_param,
    158             testdir_param
    159             ]
    160 
    161         if self.configfile and os.path.exists(self.configfile):
    162             command.append('--configFile=%s' % self.configfile)
    163 
     193        command = self.envs[self.testing_enviroment]['command']
     194        # Add eventual other parameters to the command by calling a
     195        # function specific for the selected engine (rhino, phantomjs, etc.)
     196        if 'command_params_func' in self.envs[self.testing_enviroment]:
     197            command = self.envs[self.testing_enviroment]['command_params_func'](command)
    164198        # if we were asked to test only some of the spec files,
    165199        # addd them to the command line:
     
    174208        stderr = subprocess.PIPE
    175209        input_data = ''
    176 
    177210        success, stdout, stderr = run_popen_with_timeout(
    178211            command, timeout, input_data, stdin, stdout, stderr
     
    186219
    187220    def did_test_pass(self, stdout):
    188         if 'FAILED' in stdout:
     221        if self.envs[self.testing_enviroment]['failed_mark'] in stdout:
    189222            # it can happen that a test fails because of some timing issues
    190223            # (timer error). In such case it may happen that the test does
     
    198231        # tests passing ok
    199232        for line in stdout.splitlines():
    200             if 'Failed' in line:
    201                 failed = line.split(':')[1].strip()
    202                 return failed == '0'
     233            if self.envs[self.testing_enviroment]['success_mark'] in line:
     234                return True
    203235        return False
    204236
Note: See TracChangeset for help on using the changeset viewer.