invoker.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. # -*- coding: utf-8 -*-
  2. """GRASS Python testing framework test files invoker (runner)
  3. Copyright (C) 2014 by the GRASS Development Team
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS GIS
  6. for details.
  7. :authors: Vaclav Petras
  8. """
  9. import os
  10. import sys
  11. import shutil
  12. import subprocess
  13. from .checkers import text_to_keyvalue
  14. from .loader import GrassTestLoader, discover_modules
  15. from .reporters import (GrassTestFilesMultiReporter,
  16. GrassTestFilesTextReporter, GrassTestFilesHtmlReporter,
  17. TestsuiteDirReporter, GrassTestFilesKeyValueReporter,
  18. get_svn_path_authors,
  19. NoopFileAnonymizer, keyvalue_to_text)
  20. from .utils import silent_rmtree, ensure_dir
  21. from grass.script.utils import decode, encode, _get_encoding
  22. try:
  23. from string import maketrans
  24. except ImportError:
  25. maketrans = str.maketrans
  26. # needed for write_gisrc
  27. # TODO: it would be good to find some way of writing rc without the need to
  28. # have GRASS proprly set (anything from grass.script requires translations to
  29. # be set, i.e. the GRASS environment properly set)
  30. import grass.script.setup as gsetup
  31. import collections
  32. # TODO: this might be more extend then update
  33. def update_keyval_file(filename, module, returncode):
  34. if os.path.exists(filename):
  35. with open(filename, 'r') as keyval_file:
  36. keyval = text_to_keyvalue(keyval_file.read(), sep='=')
  37. else:
  38. keyval = {}
  39. # this is for one file
  40. test_file_authors = get_svn_path_authors(module.abs_file_path)
  41. # in case that SVN is not available use empty authors
  42. if test_file_authors is None:
  43. test_file_authors = ''
  44. # always owerwrite name and status
  45. keyval['name'] = module.name
  46. keyval['tested_dir'] = module.tested_dir
  47. if 'status' not in keyval.keys():
  48. keyval['status'] = 'failed' if returncode else 'passed'
  49. keyval['returncode'] = returncode
  50. keyval['test_file_authors'] = test_file_authors
  51. with open(filename, 'w') as keyval_file:
  52. keyval_file.write(keyvalue_to_text(keyval))
  53. return keyval
  54. class GrassTestFilesInvoker(object):
  55. """A class used to invoke test files and create the main report"""
  56. # TODO: it is not clear what clean_outputs mean, if should be split
  57. # std stream, random outputs, saved results, profiling
  58. # not stdout and stderr if they contain test results
  59. # we can also save only failed tests, or generate only if assert fails
  60. def __init__(self, start_dir,
  61. clean_mapsets=True, clean_outputs=True, clean_before=True,
  62. testsuite_dir='testsuite', file_anonymizer=None):
  63. """
  64. :param bool clean_mapsets: if the mapsets should be removed
  65. :param bool clean_outputs: meaning is unclear: random tests outputs,
  66. saved images from maps, profiling?
  67. :param bool clean_before: if mapsets, outputs, and results
  68. should be removed before the tests start
  69. (advantageous when the previous run left everything behind)
  70. """
  71. self.start_dir = start_dir
  72. self.clean_mapsets = clean_mapsets
  73. self.clean_outputs = clean_outputs
  74. self.clean_before = clean_before
  75. self.testsuite_dir = testsuite_dir # TODO: solve distribution of this constant
  76. # reporter is created for each call of run_in_location()
  77. self.reporter = None
  78. self.testsuite_dirs = None
  79. if file_anonymizer is None:
  80. self._file_anonymizer = NoopFileAnonymizer()
  81. else:
  82. self._file_anonymizer = file_anonymizer
  83. def _create_mapset(self, gisdbase, location, module):
  84. """Create mapset according to information in module.
  85. :param loader.GrassTestPythonModule module:
  86. """
  87. # using path.sep but also / and \ for cases when it is confused
  88. # (namely the case of Unix path on MS Windows)
  89. # replace . to get rid of unclean path
  90. # TODO: clean paths
  91. # note that backslash cannot be at the end of raw string
  92. dir_as_name = module.tested_dir.translate(maketrans(r'/\.', '___'))
  93. mapset = dir_as_name + '_' + module.name
  94. # TODO: use grass module to do this? but we are not in the right gisdbase
  95. mapset_dir = os.path.join(gisdbase, location, mapset)
  96. if self.clean_before:
  97. silent_rmtree(mapset_dir)
  98. os.mkdir(mapset_dir)
  99. # TODO: default region in mapset will be what?
  100. # copy WIND file from PERMANENT
  101. # TODO: this should be a function in grass.script (used also in gis_set.py, PyGRASS also has its way with Mapset)
  102. # TODO: are premisions an issue here?
  103. shutil.copy(os.path.join(gisdbase, location, 'PERMANENT', 'WIND'),
  104. os.path.join(mapset_dir))
  105. return mapset, mapset_dir
  106. def _run_test_module(self, module, results_dir, gisdbase, location):
  107. """Run one test file."""
  108. self.testsuite_dirs[module.tested_dir].append(module.name)
  109. cwd = os.path.join(results_dir, module.tested_dir, module.name)
  110. data_dir = os.path.join(module.file_dir, 'data')
  111. if os.path.exists(data_dir):
  112. # TODO: link dir instead of copy tree and remove link afterwads
  113. # (removing is good because of testsuite dir in samplecode)
  114. # TODO: use different dir name in samplecode and test if it works
  115. shutil.copytree(data_dir, os.path.join(cwd, 'data'),
  116. ignore=shutil.ignore_patterns('*.svn*'))
  117. ensure_dir(os.path.abspath(cwd))
  118. # TODO: put this to constructor and copy here again
  119. env = os.environ.copy()
  120. mapset, mapset_dir = self._create_mapset(gisdbase, location, module)
  121. gisrc = gsetup.write_gisrc(gisdbase, location, mapset)
  122. # here is special setting of environmental variables for running tests
  123. # some of them might be set from outside in the future and if the list
  124. # will be long they should be stored somewhere separately
  125. # use custom gisrc, not current session gisrc
  126. env['GISRC'] = gisrc
  127. # percentage in plain format is 0...10...20... ...100
  128. env['GRASS_MESSAGE_FORMAT'] = 'plain'
  129. stdout_path = os.path.join(cwd, 'stdout.txt')
  130. stderr_path = os.path.join(cwd, 'stderr.txt')
  131. self.reporter.start_file_test(module)
  132. # TODO: we might clean the directory here before test if non-empty
  133. if module.file_type == 'py':
  134. # ignoring shebang line to use current Python
  135. # and also pass parameters to it
  136. # add also '-Qwarn'?
  137. if sys.version_info.major >= 3:
  138. args = [sys.executable, '-tt', module.abs_file_path]
  139. else:
  140. args = [sys.executable, '-tt', '-3', module.abs_file_path]
  141. p = subprocess.Popen(args, cwd=cwd, env=env,
  142. stdout=subprocess.PIPE,
  143. stderr=subprocess.PIPE)
  144. elif module.file_type == 'sh':
  145. # ignoring shebang line to pass parameters to shell
  146. # expecting system to have sh or something compatible
  147. # TODO: add some special checks for MS Windows
  148. # using -x to see commands in stderr
  149. # using -e to terminate fast
  150. # from dash manual:
  151. # -e errexit If not interactive, exit immediately if any
  152. # untested command fails. The exit status of a com‐
  153. # mand is considered to be explicitly tested if the
  154. # command is used to control an if, elif, while, or
  155. # until; or if the command is the left hand operand
  156. # of an '&&' or '||' operator.
  157. p = subprocess.Popen(['sh', '-e', '-x', module.abs_file_path],
  158. cwd=cwd, env=env,
  159. stdout=subprocess.PIPE,
  160. stderr=subprocess.PIPE)
  161. else:
  162. p = subprocess.Popen([module.abs_file_path],
  163. cwd=cwd, env=env,
  164. stdout=subprocess.PIPE,
  165. stderr=subprocess.PIPE)
  166. stdout, stderr = p.communicate()
  167. returncode = p.returncode
  168. encodings = [_get_encoding(), 'utf8', 'latin-1', 'ascii']
  169. detected = False
  170. idx = 0
  171. while not detected:
  172. try:
  173. stdout = decode(stdout, encoding=encodings[idx])
  174. detected = True
  175. except:
  176. idx += 1
  177. pass
  178. detected = False
  179. idx = 0
  180. while not detected:
  181. try:
  182. stderr = decode(stderr, encoding=encodings[idx])
  183. detected = True
  184. except:
  185. idx += 1
  186. pass
  187. with open(stdout_path, 'w') as stdout_file:
  188. stdout_file.write(stdout)
  189. with open(stderr_path, 'w') as stderr_file:
  190. stderr_file.write(stderr)
  191. self._file_anonymizer.anonymize([stdout_path, stderr_path])
  192. test_summary = update_keyval_file(
  193. os.path.join(os.path.abspath(cwd), 'test_keyvalue_result.txt'),
  194. module=module, returncode=returncode)
  195. self.reporter.end_file_test(module=module, cwd=cwd,
  196. returncode=returncode,
  197. stdout=stdout_path, stderr=stderr_path,
  198. test_summary=test_summary)
  199. # TODO: add some try-except or with for better error handling
  200. os.remove(gisrc)
  201. # TODO: only if clean up
  202. if self.clean_mapsets:
  203. shutil.rmtree(mapset_dir)
  204. def run_in_location(self, gisdbase, location, location_type,
  205. results_dir):
  206. """Run tests in a given location"""
  207. if os.path.abspath(results_dir) == os.path.abspath(self.start_dir):
  208. raise RuntimeError("Results root directory should not be the same"
  209. " as discovery start directory")
  210. self.reporter = GrassTestFilesMultiReporter(
  211. reporters=[
  212. GrassTestFilesTextReporter(stream=sys.stderr),
  213. GrassTestFilesHtmlReporter(
  214. file_anonymizer=self._file_anonymizer,
  215. main_page_name='testfiles.html'),
  216. GrassTestFilesKeyValueReporter(
  217. info=dict(location=location, location_type=location_type))
  218. ])
  219. self.testsuite_dirs = collections.defaultdict(list) # reset list of dirs each time
  220. # TODO: move constants out of loader class or even module
  221. modules = discover_modules(start_dir=self.start_dir,
  222. grass_location=location_type,
  223. file_regexp=r'.*\.(py|sh)$',
  224. skip_dirs=GrassTestLoader.skip_dirs,
  225. testsuite_dir=GrassTestLoader.testsuite_dir,
  226. all_locations_value=GrassTestLoader.all_tests_value,
  227. universal_location_value=GrassTestLoader.universal_tests_value,
  228. import_modules=False)
  229. self.reporter.start(results_dir)
  230. for module in modules:
  231. self._run_test_module(module=module, results_dir=results_dir,
  232. gisdbase=gisdbase, location=location)
  233. self.reporter.finish()
  234. # TODO: move this to some (new?) reporter
  235. # TODO: add basic summary of linked files so that the page is not empty
  236. with open(os.path.join(results_dir, 'index.html'), 'w') as main_index:
  237. main_index.write(
  238. '<html><body>'
  239. '<h1>Tests for &lt;{location}&gt;'
  240. ' using &lt;{type}&gt; type tests</h1>'
  241. '<ul>'
  242. '<li><a href="testsuites.html">Results by testsuites</a>'
  243. ' (testsuite directories)</li>'
  244. '<li><a href="testfiles.html">Results by test files</a></li>'
  245. '<ul>'
  246. '</body></html>'
  247. .format(location=location, type=location_type))
  248. testsuite_dir_reporter = TestsuiteDirReporter(
  249. main_page_name='testsuites.html', testsuite_page_name='index.html',
  250. top_level_testsuite_page_name='testsuite_index.html')
  251. testsuite_dir_reporter.report_for_dirs(root=results_dir,
  252. directories=self.testsuite_dirs)