| The Gaudi Framework v27r1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

Run.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 import os
3 import sys
4 import xml.sax.saxutils as XSS
5 import BaseTest as GT
6 import logging
7
8 # FIXME: module alias for backward compatibility
9 sys.modules['GaudiTest'] = GT
10
11 def basic_report(results):
12 '''
13 Report function taking the dictionary from BasicTest.run() and display
14 stdout and stderr from it.
15 '''
16 print '=== stdout ==='
17 print results.get('stdout', '')
18 print '=== stderr ==='
19 print results.get('stderr', '')
20 print '=== result ==='
21 print results.get('Status')
22 if results.get('Status') != 'passed' and 'Causes' in results:
23 print ' ', 'unexpected ' + ', '.join(results['Causes'])
24
25
26 def quiet_report(results):
27 '''
28 Do not report anything from the result of the test.
29 '''
30 pass
31
32
33 def ctest_report(results):
34 '''
35 Report function taking the dictionary from BasicTest.run() and report data
36 from it in a CTest-friendly way.
37 '''
38 # It's weird, I know, but it tells CTest not to cut the output.
39 print 'CTEST_FULL_OUTPUT'
40 print results.get('stdout', '')
41 handler = {'Environment': lambda v: '\n'.join('{0}={1}'.format(*item)
42 for item in sorted(v.iteritems())),
43 'Causes': lambda v: 'unexpected ' + ', '.join(v)}
44 id_handler = lambda v: str(v)
45 ignore = set(['Status', 'Name', 'stdout', 'Exit Code'])
46 template = ('<DartMeasurement type="text/string" name="{0}">{1}</DartMeasurement>')
47
48 for key in results:
49 if key in ignore:
50 continue
51 hndlr = handler.get(key, id_handler)
52 data = XSS.escape(GT.sanitize_for_xml(hndlr(results[key])))
53 sys.stdout.write(template.format(key, data))
54
55
56 def pprint_report(results):
57 '''
58 Report function taking the dictionary from BasicTest.run() and print it with
59 the pprint module.
60 '''
61 from pprint import pprint
62 pprint(results)
63
64
65 def main():
66 '''
67 Main function of the script.
68 '''
69 from optparse import OptionParser, OptionGroup
70 parser = OptionParser()
71
72 parser.add_option('--report', action='store',
73 choices=[n.replace('_report', '')
74 for n in globals() if n.endswith('_report')],
75 help='choose a report method [default %default]')
76 parser.add_option('--common-tmpdir', action='store',
77 help='directory to be used as common temporary directory')
78 parser.add_option('-C', '--workdir', action='store',
79 help='directory to change to before starting the test')
80
81 parser.add_option('--skip-return-code', type='int',
82 help='return code to use to flag a test as skipped '
83 '[default %default]')
84
85 verbosity_opts = OptionGroup(parser, 'Verbosity Level',
86 'set the verbosity level of messages')
87 verbosity_opts.add_option('--silent',
88 action='store_const', dest='log_level',
89 const=logging.CRITICAL,
90 help='only critical error messages')
91 verbosity_opts.add_option('--quiet',
92 action='store_const', dest='log_level',
93 const=logging.ERROR,
94 help='error messages')
95 verbosity_opts.add_option('--warning',
96 action='store_const', dest='log_level',
97 const=logging.WARNING,
98 help='warning and error messages')
99 verbosity_opts.add_option('--verbose',
100 action='store_const', dest='log_level',
101 const=logging.INFO,
102 help='progress information messages')
103 verbosity_opts.add_option('--debug',
104 action='store_const', dest='log_level',
105 const=logging.DEBUG,
106 help='debugging messages')
107 parser.add_option_group(verbosity_opts)
108
109
110 parser.set_defaults(log_level=logging.WARNING,
111 report='basic',
112 workdir=os.curdir,
113 skip_return_code=0)
114
115
116 opts, args = parser.parse_args()
117 if len(args) != 1:
118 parser.error('only one test allowed')
119 filename = args[0]
120
121 logging.basicConfig(level=opts.log_level)
122
123 if opts.common_tmpdir:
124 if not os.path.isdir(opts.common_tmpdir):
125 os.makedirs(opts.common_tmpdir)
126 GT.BaseTest._common_tmpdir = opts.common_tmpdir
127
128 os.chdir(opts.workdir)
129
130 # FIXME: whithout this, we get some spurious '\x1b[?1034' in the std out on SLC6
131 if "slc6" in (os.environ.get('BINARY_TAG', '') or
132 os.environ.get('CMTCONFIG', '')):
133 os.environ['TERM'] = 'dumb'
134
135 # Testing the file beginning with "Test" or if it is a qmt file and doing the test
136 logging.debug('processing %s', filename)
137 if filename.endswith('_test.py') :
138 indexFilePart= filename.rfind("/")
139 fileToImport = filename[indexFilePart+1:]
140 sys.path.append(GT.RationalizePath(filename)[:-len(fileToImport)-1])
141 imp = __import__(fileToImport[:-3])
142 fileToExec = imp.Test()
143 results = fileToExec.run()
144 elif filename.endswith(".qmt"):
145 from QMTTest import QMTTest
146 fileToTest = QMTTest(filename)
147 results = fileToTest.run()
148
149 report = globals()[opts.report + '_report']
150 report(results)
151
152 if results.get('Status') == 'failed':
153 logging.debug('test failed: unexpected %s',
154 ', '.join(results['Causes']))
155 return int(results.get('Exit Code', '1'))
156 elif results.get('Status') == 'skipped':
157 return opts.skip_return_code
158 return 0
159
160 if __name__ == '__main__':
161 sys.exit(main())
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
GaudiTesting.Run.pprint_report
def pprint_report(results)
Definition: Run.py:56
GaudiTesting.Run.basic_report
def basic_report(results)
Definition: Run.py:11
GaudiTesting.Run.ctest_report
def ctest_report(results)
Definition: Run.py:33
GaudiTesting.Run.quiet_report
def quiet_report(results)
Definition: Run.py:26
GaudiTesting.Run.main
def main()
Definition: Run.py:65