source: pyenvjasmine/pyenvjasmine/runner.py@ 12:b385d1bc24d6

Last change on this file since 12:b385d1bc24d6 was 12:b385d1bc24d6, checked in by Borja Lopez <borja@…>, 9 years ago

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).

File size: 3.8 KB
Line 
1import os
2import subprocess
3
4
5def get_environment():
6 """
7 Get the environment parameter, depending on OS (Win/Unix).
8 """
9 if os.name == 'nt': # not tested!
10 environment = '--environment=WIN'
11 else:
12 environment = '--environment=UNIX'
13 return environment
14
15
16class TestRunner(object):
17 """
18 Setup to run envjasmine "specs" (tests).
19
20 To use it, probably best to put it inside a normal python
21 unit test suite, then just print out the output.
22 """
23
24 def __init__(self, rootdir=None, testdir=None, configfile=None,
25 browser_configfile=None):
26 """
27 Set up paths, by default everything is
28 inside the "envjasmine" folder right here.
29 Giving no paths, the sample specs from envjasmine will be run.
30 XXX: it would be more practical if this raised an exception
31 and you know you're not running the tests you want.
32
33 parameters:
34 testdir - the directory that holds the "mocks", "specs"
35 and "include" directories for the actual tests.
36 rootdir - the directory where the envjasmine code lives in.
37 configfile - path to an extra js config file that is run for the tests.
38 browser_configfile - path to an extra js config file for running
39 the tests in browser.
40 """
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
45 self.configfile = configfile
46 self.browser_configfile = browser_configfile
47 self.runner_html = os.path.join(here, 'runner.html')
48
49 def run(self, spec=None, capture_output=True):
50 """
51 Run the js tests with envjasmine.
52 spec: (relative) path to a spec file (run only that spec)
53 Returns the output
54 """
55 environment = get_environment()
56 rhino_path = os.path.join(self.rootdir, 'lib', 'rhino', 'js.jar')
57 envjasmine_js_path = os.path.join(self.rootdir, 'lib', 'envjasmine.js')
58 rootdir_param = '--rootDir=%s' % self.rootdir
59 testdir_param = '--testDir=%s' % self.testdir
60 if self.browser_configfile and os.path.exists(self.browser_configfile):
61 self.write_browser_htmlfile()
62
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
76 if self.configfile and os.path.exists(self.configfile):
77 command.append('--configFile=%s' % self.configfile)
78
79 # if we were asked to test only some of the spec files,
80 # addd them to the command line:
81 if spec is not None:
82 if not isinstance(spec, list):
83 spec = [spec]
84 command.extend(spec)
85
86 shell = False
87 stdout = None
88 stderr = None
89 if capture_output:
90 # override if we want to capture the output of the test run
91 stdout = subprocess.PIPE
92 stderr = subprocess.PIPE
93
94 p = subprocess.Popen(command, shell=shell, stdout=stdout,
95 stderr=stderr)
96 (res, stderr) = p.communicate()
97 return res
98
99 def write_browser_htmlfile(self):
100 markup = self.create_testRunnerHtml()
101 with open("browser.runner.html", 'w') as file:
102 file.write(markup)
103
104 def create_testRunnerHtml(self):
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 TracBrowser for help on using the repository browser.