1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
3 |
''' run_tests.py - Run Radiance test suite |
4 |
2013 - 2016 Georg Mischler |
5 |
|
6 |
Invocation: |
7 |
As script, call with -H for instructions. |
8 |
As module, see docstrings in class RadianceTests for instructions. |
9 |
''' |
10 |
from __future__ import division, print_function, unicode_literals |
11 |
__all__ = ['TESTCATS', 'RadianceTests', 'main'] |
12 |
import os |
13 |
import sys |
14 |
import types |
15 |
import argparse |
16 |
import unittest |
17 |
|
18 |
SHORTPROGN = os.path.splitext(os.path.basename(sys.argv[0]))[0] |
19 |
TESTCATS = ('pyrad','cal','cv','gen','hd','meta','ot','px', 'rt','util',) |
20 |
|
21 |
class Error(Exception): pass |
22 |
|
23 |
class RadianceTests(): |
24 |
'''Test Radiance programs below subdirectory "testcases" recursively. |
25 |
|
26 |
This class will create a virtual module "testsupport" with constants |
27 |
and functions for use in individual test cases. |
28 |
|
29 |
bindir - list of paths added to PATH |
30 |
path - complete list of paths currently in PATH |
31 |
radlib - list of paths added to RAYPATH |
32 |
raypath - complete list of paths currently in RAYPATH |
33 |
pyradlib - absolute path of python support library directory |
34 |
datadir - absolute path of test data directory |
35 |
datafile([sub, [sub,]] fn) - return absolute file path in |
36 |
data directory or in a subdirectory |
37 |
''' |
38 |
def __init__(self, **args): |
39 |
'''Args are: |
40 |
bindir=[directory ...] - will be prepended to PATH for tests |
41 |
radlib=[directory ...] - will be prepended to RAYPATH for tests |
42 |
cat=[category ...] - only test those categories (else TESTCATS) |
43 |
V=False - if True, verbose listing of executed tests |
44 |
''' |
45 |
self.bindir = args.get('bindir') |
46 |
self.radlib = args.get('radlib') |
47 |
self.testcats = args.get('cat') |
48 |
self.V = 2 if args.get('V') else 1 |
49 |
try: |
50 |
old_osenv = os.environ |
51 |
|
52 |
thisfile = os.path.abspath(__file__) |
53 |
self.thisdir = os.path.split(thisfile)[0] |
54 |
self.testdir = os.path.join(self.thisdir, 'testcases') |
55 |
old_syspath = sys.path |
56 |
if self.testdir not in sys.path: |
57 |
sys.path.insert(0, self.testdir) |
58 |
|
59 |
oldpath = old_osenv.get('PATH', '') |
60 |
pathlist = oldpath.split(os.pathsep) |
61 |
if self.bindir: |
62 |
for bdir in self.bindir: |
63 |
if bdir not in pathlist: |
64 |
pathlist.insert(0, bdir) |
65 |
os.environ['PATH'] = os.pathsep.join(pathlist) |
66 |
|
67 |
oldraypath = old_osenv.get('RAYPATH', '') |
68 |
raypathlist = oldraypath.split(os.pathsep) |
69 |
if self.radlib: |
70 |
for rlib in self.radlib + ['.']: |
71 |
if rlib not in raypathlist: |
72 |
raypathlist.insert(0, rlib) |
73 |
os.environ['RAYPATH'] = os.pathsep.join(raypathlist) |
74 |
|
75 |
pyradlib = None |
76 |
for rp in raypathlist: |
77 |
prd = os.path.join(rp, 'pyradlib') |
78 |
if os.path.isdir(prd) or os.path.islink(prd): |
79 |
if rp not in sys.path: |
80 |
sys.path.insert(0, rp) |
81 |
pyradlib = prd |
82 |
if not pyradlib: |
83 |
raise Error('Python support library directory "pyradlib" not found on RAYPATH') |
84 |
|
85 |
# Since this here is the best place to figure out where |
86 |
# everything is, we create an ad-hoc module to hold directory |
87 |
# paths and functions for creating file paths. |
88 |
# The individual test cases can then "import testsupport" to |
89 |
# get access to the complete information. |
90 |
supmod = types.ModuleType(str('testsupport')) |
91 |
datadir = os.path.join(self.thisdir, 'test data') |
92 |
supmod.bindir = self.bindir |
93 |
supmod.datadir = datadir |
94 |
supmod.datafile = lambda fn,*ffn: os.path.join(datadir, fn, *ffn) |
95 |
supmod.path = pathlist |
96 |
supmod.radlib = self.radlib |
97 |
supmod.raypath = raypathlist |
98 |
supmod.pyradlib = pyradlib |
99 |
sys.modules['testsupport'] = supmod |
100 |
|
101 |
self._run_all() |
102 |
finally: |
103 |
if hasattr(sys.modules, 'testsupport'): |
104 |
del sys.modules['testsupport'] |
105 |
os.environ = old_osenv |
106 |
sys.path = old_syspath |
107 |
|
108 |
def _run_all(self): |
109 |
runner = unittest.TextTestRunner(verbosity=self.V) |
110 |
if self.testcats: |
111 |
cats = self.testcats |
112 |
else: cats = TESTCATS |
113 |
for dir in cats: |
114 |
loader = unittest.defaultTestLoader |
115 |
fulldir = os.path.join(self.testdir, dir) |
116 |
if not os.path.isdir(fulldir): |
117 |
raise Error('No such directory: "%s"' % fulldir) |
118 |
suite = loader.discover(fulldir, 'test_*.py', |
119 |
top_level_dir=self.thisdir) |
120 |
count = suite.countTestCases() |
121 |
if count: |
122 |
print('\n--', dir, '----', file=sys.stderr) |
123 |
runner.run(suite) |
124 |
|
125 |
|
126 |
def main(): |
127 |
'''Main function for invocation as script. See usage instructions with -H''' |
128 |
parser = argparse.ArgumentParser(add_help=False, |
129 |
description='Run Radiance test suite',) |
130 |
parser.add_argument('-V', action='store_true', |
131 |
help='Verbose: Print all executed test cases to stderr') |
132 |
parser.add_argument('-H', action='help', |
133 |
help='Help: print this text to stderr and exit') |
134 |
parser.add_argument('-p', action='store', nargs=1, |
135 |
dest='bindir', metavar='bindir', help='Path to Radiance binaries') |
136 |
parser.add_argument('-l', action='store', nargs=1, |
137 |
dest='radlib', metavar='radlib', help='Path to Radiance library') |
138 |
parser.add_argument('-c', action='store', nargs=1, |
139 |
dest='cat', metavar='cat', help='Category of tests to run (else all)') |
140 |
args = parser.parse_args() |
141 |
RadianceTests(**vars(args)) |
142 |
|
143 |
|
144 |
if __name__ == '__main__': |
145 |
try: main() |
146 |
except KeyboardInterrupt: |
147 |
sys.stderr.write('*cancelled*\n') |
148 |
exit(1) |
149 |
except Error as e: |
150 |
sys.stderr.write('%s: %s\n' % (SHORTPROGN, str(e))) |
151 |
exit(-1) |
152 |
|
153 |
# vi: set ts=4 sw=4 : |