Changeset 12:b385d1bc24d6 in pyenvjasmine


Ignore:
Timestamp:
Apr 24, 2015, 8:47:01 AM (9 years ago)
Author:
Borja Lopez <borja@…>
Branch:
default
Phase:
public
Message:

Moved the included html code for the base runner template to a separate file
Cleanup, runner.py is now pep8-compliant, changed the setup of the testdir,
libdir and root paths in the init (a bit clearer now).

Location:
pyenvjasmine
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • pyenvjasmine/runner.py

    r0 r12  
    77    Get the environment parameter, depending on OS (Win/Unix).
    88    """
    9     if os.name == 'nt': # not tested!
     9    if os.name == 'nt':  # not tested!
    1010        environment = '--environment=WIN'
    1111    else:
     
    2323
    2424    def __init__(self, rootdir=None, testdir=None, configfile=None,
    25                     browser_configfile=None):
     25                 browser_configfile=None):
    2626        """
    2727        Set up paths, by default everything is
     
    3939                    the tests in browser.
    4040        """
    41         if rootdir is None:
    42             here = os.path.dirname(__file__)
    43             rootdir = os.path.join(here, 'envjasmine')
    44         if testdir is None:
    45             testdir = rootdir
    46         self.testdir = testdir
    47         self.rootdir = rootdir
     41        here = os.path.dirname(__file__)
     42        self.libdir = here
     43        self.rootdir = rootdir or os.path.join(here, 'envjasmine')
     44        self.testdir = testdir or self.rootdir
    4845        self.configfile = configfile
    4946        self.browser_configfile = browser_configfile
     47        self.runner_html = os.path.join(here, 'runner.html')
    5048
    5149    def run(self, spec=None, capture_output=True):
     
    5654        """
    5755        environment = get_environment()
    58         rhino_path = os.path.join(self.rootdir,
    59                                     'lib', 'rhino', 'js.jar')
    60         envjasmine_js_path = os.path.join(self.rootdir,
    61                                     'lib', 'envjasmine.js')
     56        rhino_path = os.path.join(self.rootdir, 'lib', 'rhino', 'js.jar')
     57        envjasmine_js_path = os.path.join(self.rootdir, 'lib', 'envjasmine.js')
    6258        rootdir_param = '--rootDir=%s' % self.rootdir
    6359        testdir_param = '--testDir=%s' % self.testdir
     
    6561            self.write_browser_htmlfile()
    6662
    67         command = ['java', '-Duser.timezone=US/Eastern',
    68                 '-Dfile.encoding=utf-8', '-jar', rhino_path,
    69                 envjasmine_js_path,
    70                 '--disableColor',
    71                 environment, rootdir_param, testdir_param
    72                 ]
     63        command = [
     64            'java',
     65            '-Duser.timezone=US/Eastern',
     66            '-Dfile.encoding=utf-8',
     67            '-jar',
     68            rhino_path,
     69            envjasmine_js_path,
     70            '--disableColor',
     71            environment,
     72            rootdir_param,
     73            testdir_param
     74            ]
     75
    7376        if self.configfile and os.path.exists(self.configfile):
    7477            command.append('--configFile=%s' % self.configfile)
     78
    7579        # if we were asked to test only some of the spec files,
    7680        # addd them to the command line:
     
    7983                spec = [spec]
    8084            command.extend(spec)
     85
    8186        shell = False
     87        stdout = None
     88        stderr = None
    8289        if capture_output:
     90            # override if we want to capture the output of the test run
    8391            stdout = subprocess.PIPE
    8492            stderr = subprocess.PIPE
    85         else:
    86             stdout = None
    87             stderr = None
    88         p = subprocess.Popen(command, shell=shell,
    89                             stdout=stdout, stderr=stderr)
     93
     94        p = subprocess.Popen(command, shell=shell, stdout=stdout,
     95                             stderr=stderr)
    9096        (res, stderr) = p.communicate()
    9197        return res
     
    9399    def write_browser_htmlfile(self):
    94100        markup = self.create_testRunnerHtml()
    95         with open("browser.runner.html",'w') as file:
     101        with open("browser.runner.html", 'w') as file:
    96102            file.write(markup)
    97103
    98104    def create_testRunnerHtml(self):
    99         return """
    100         <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    101           "http://www.w3.org/TR/html4/loose.dtd">
    102         <html>
    103         <head>
    104           <title></title>
    105           <link rel="stylesheet" type="text/css" href="file:///%(libDir)s/envjasmine/lib/jasmine/jasmine.css">
    106 
    107           <script type="text/javascript" src="file:///%(libDir)s/envjasmine/lib/jasmine/jasmine.js"></script>
    108           <script type="text/javascript" src="file:///%(libDir)s/envjasmine/lib/jasmine/jasmine-html.js"></script>
    109           <script type="text/javascript" src="file:///%(libDir)s/envjasmine/lib/jasmine-ajax/mock-ajax.js"></script>
    110           <script type="text/javascript" src="file:///%(libDir)s/envjasmine/lib/jasmine-ajax/spec-helper.js"></script>
    111           <script type="text/javascript" src="file:///%(libDir)s/envjasmine/lib/jasmine-jquery/jasmine-jquery.js"></script>
    112 
    113           <script type="text/javascript" src="browser.runner.js"></script>
    114           <script type="text/javascript">
    115               (function () {
    116                   "use strict";
    117                   var jasmineEnv = jasmine.getEnv();
    118                   jasmineEnv.updateInterval = 1000;
    119 
    120                   var trivialReporter = new jasmine.TrivialReporter();
    121                   jasmineEnv.addReporter(trivialReporter);
    122                   jasmineEnv.specFilter = function (spec) {
    123                       return trivialReporter.specFilter(spec);
    124                   };
    125 
    126                   var currentWindowOnload = window.onload;
    127 
    128                   window.onload = function () {
    129                       if (currentWindowOnload) {
    130                           currentWindowOnload();
    131                       }
    132                       execJasmine();
    133                   };
    134 
    135                   function execJasmine() {
    136                       jasmineEnv.execute();
    137                   }
    138               })();
    139               var EnvJasmine = {};
    140               EnvJasmine.jsDir = "";
    141               EnvJasmine.testDir = "%(testDir)s";
    142               EnvJasmine.load = function (path) {
    143                   appendScript(path);
    144               };
    145               EnvJasmine.loadGlobal = function (path) {
    146                   appendScript(path);
    147               };
    148               function appendScript(path){
    149                   var script = document.createElement('script');
    150                   script.src = path;
    151                   script.type ='text/javascript';
    152                   document.getElementsByTagName('head')[0].appendChild(script);
    153               }
    154           </script>
    155           <script type="text/javascript" src="%(browser_configfile)s"></script>
    156         </head>
    157         <body>
    158         </body>
    159         </html>
    160         """ %{"libDir": os.path.normpath(os.path.dirname(__file__)),
    161               "testDir": os.path.normpath(self.testdir),
    162               "browser_configfile": self.browser_configfile}
    163 
     105        with open(self.runner_html, 'r') as runner_html:
     106            html = runner_html.read()
     107            return html % {"libDir": os.path.normpath(self.libdir),
     108                           "testDir": os.path.normpath(self.testdir),
     109                           "browser_configfile": self.browser_configfile}
Note: See TracChangeset for help on using the changeset viewer.