source: pyenvjasmine/pyenvjasmine/runner.py@ 0:0175515fceea

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

Imported sources from http://repos.betabug.ch/pyenvjasmine

File size: 6.3 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 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
48 self.configfile = configfile
49 self.browser_configfile = browser_configfile
50
51 def run(self, spec=None, capture_output=True):
52 """
53 Run the js tests with envjasmine.
54 spec: (relative) path to a spec file (run only that spec)
55 Returns the output
56 """
57 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')
62 rootdir_param = '--rootDir=%s' % self.rootdir
63 testdir_param = '--testDir=%s' % self.testdir
64 if self.browser_configfile and os.path.exists(self.browser_configfile):
65 self.write_browser_htmlfile()
66
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 ]
73 if self.configfile and os.path.exists(self.configfile):
74 command.append('--configFile=%s' % self.configfile)
75 # if we were asked to test only some of the spec files,
76 # addd them to the command line:
77 if spec is not None:
78 if not isinstance(spec, list):
79 spec = [spec]
80 command.extend(spec)
81 shell = False
82 if capture_output:
83 stdout = subprocess.PIPE
84 stderr = subprocess.PIPE
85 else:
86 stdout = None
87 stderr = None
88 p = subprocess.Popen(command, shell=shell,
89 stdout=stdout, stderr=stderr)
90 (res, stderr) = p.communicate()
91 return res
92
93 def write_browser_htmlfile(self):
94 markup = self.create_testRunnerHtml()
95 with open("browser.runner.html",'w') as file:
96 file.write(markup)
97
98 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
Note: See TracBrowser for help on using the repository browser.