invoker.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # -*- coding: utf-8 -*-
  2. """!@package grass.gunittest.invoker
  3. @brief GRASS Python testing framework test files invoker (runner)
  4. Copyright (C) 2014 by the GRASS Development Team
  5. This program is free software under the GNU General Public
  6. License (>=v2). Read the file COPYING that comes with GRASS GIS
  7. for details.
  8. @author Vaclav Petras
  9. """
  10. import os
  11. import sys
  12. import shutil
  13. import string
  14. import subprocess
  15. from unittest.main import TestProgram, USAGE_AS_MAIN
  16. TestProgram.USAGE = USAGE_AS_MAIN
  17. from .loader import GrassTestLoader, discover_modules
  18. from .reporters import GrassTestFilesReporter
  19. from .utils import silent_rmtree, ensure_dir
  20. import grass.script.setup as gsetup
  21. class GrassTestFilesInvoker(object):
  22. """A class used to invoke test files and create the main report"""
  23. # TODO: it is not clear what clean_outputs mean, if should be split
  24. # std stream, random outputs, saved results, profiling
  25. # not stdout and stderr if they contain test results
  26. # we can also save only failed tests, or generate only if assert fails
  27. def __init__(self, start_dir,
  28. clean_mapsets=True, clean_outputs=True, clean_before=True,
  29. testsuite_dir='testsuite'):
  30. """
  31. :param bool clean_mapsets: if the mapsets should be removed
  32. :param bool clean_outputs: meaning is unclear: random tests outputs,
  33. saved images from maps, profiling?
  34. :param bool clean_before: if mapsets, outputs, and results
  35. should be removed before the tests start
  36. (advantageous when the previous run left everything behind)
  37. """
  38. self.start_dir = start_dir
  39. self.clean_mapsets = clean_mapsets
  40. self.clean_outputs = clean_outputs
  41. self.clean_before = clean_before
  42. self.testsuite_dir = testsuite_dir
  43. # reporter is created for each call of run_in_location()
  44. self.reporter = None
  45. def _create_mapset(self, gisdbase, location, module):
  46. """Create mapset according to informations in module.
  47. :param loader.GrassTestPythonModule module:
  48. """
  49. # using path.sep but also / and \ for cases when it is confused
  50. # (namely the case of Unix path on MS Windows)
  51. # replace . to get rid of unclean path
  52. # TODO: clean paths
  53. # note that backslash cannot be at the end of raw string
  54. dir_as_name = module.tested_dir.translate(string.maketrans(r'/\.', '___'))
  55. mapset = dir_as_name + '_' + module.name
  56. # TODO: use grass module to do this? but we are not in the right gisdbase
  57. mapset_dir = os.path.join(gisdbase, location, mapset)
  58. if self.clean_before:
  59. silent_rmtree(mapset_dir)
  60. os.mkdir(mapset_dir)
  61. # TODO: default region in mapset will be what?
  62. # copy WIND file from PERMANENT
  63. # TODO: this should be a function in grass.script (used also in gis_set.py, PyGRASS also has its way with Mapset)
  64. # TODO: are premisions an issue here?
  65. shutil.copy(os.path.join(gisdbase, location, 'PERMANENT', 'WIND'),
  66. os.path.join(mapset_dir))
  67. return mapset, mapset_dir
  68. def _run_test_module(self, module, results_dir, gisdbase, location):
  69. """Run one test file."""
  70. cwd = os.path.join(results_dir, module.tested_dir, module.name)
  71. data_dir = os.path.join(module.file_dir, 'data')
  72. if os.path.exists(data_dir):
  73. # TODO: link dir intead of copy tree
  74. shutil.copytree(data_dir, os.path.join(cwd, 'data'),
  75. ignore=shutil.ignore_patterns('*.svn*'))
  76. ensure_dir(os.path.abspath(cwd))
  77. # TODO: put this to constructor and copy here again
  78. env = os.environ.copy()
  79. mapset, mapset_dir = self._create_mapset(gisdbase, location, module)
  80. gisrc = gsetup.write_gisrc(gisdbase, location, mapset)
  81. env['GISRC'] = gisrc
  82. stdout_path = os.path.join(cwd, 'stdout.txt')
  83. stderr_path = os.path.join(cwd, 'stderr.txt')
  84. stdout = open(stdout_path, 'w')
  85. stderr = open(stderr_path, 'w')
  86. self.reporter.start_file_test(module)
  87. # TODO: we might clean the directory here before test if non-empty
  88. # TODO: use some grass function to run?
  89. # add also '-Qwarn'?
  90. p = subprocess.Popen([sys.executable, '-tt', '-3',
  91. module.abs_file_path],
  92. cwd=cwd, env=env,
  93. stdout=stdout, stderr=stderr)
  94. returncode = p.wait()
  95. stdout.close()
  96. stderr.close()
  97. self.reporter.end_file_test(module=module, cwd=cwd,
  98. returncode=returncode,
  99. stdout=stdout_path, stderr=stderr_path)
  100. # TODO: add some try-except or with for better error handling
  101. os.remove(gisrc)
  102. # TODO: only if clean up
  103. if self.clean_mapsets:
  104. shutil.rmtree(mapset_dir)
  105. def run_in_location(self, gisdbase, location, location_shortcut,
  106. results_dir):
  107. """Run tests in a given location"""
  108. if os.path.abspath(results_dir) == os.path.abspath(self.start_dir):
  109. raise RuntimeError("Results root directory should not be the same"
  110. " as discovery start directory")
  111. self.reporter = GrassTestFilesReporter(results_dir=results_dir)
  112. # TODO: move constants out of loader class or even module
  113. modules = discover_modules(start_dir=self.start_dir,
  114. grass_location=location_shortcut,
  115. file_pattern=GrassTestLoader.files_in_testsuite,
  116. skip_dirs=GrassTestLoader.skip_dirs,
  117. testsuite_dir=GrassTestLoader.testsuite_dir,
  118. all_locations_value=GrassTestLoader.all_tests_value,
  119. universal_location_value=GrassTestLoader.universal_tests_value,
  120. import_modules=False)
  121. for module in modules:
  122. self._run_test_module(module=module, results_dir=results_dir,
  123. gisdbase=gisdbase, location=location)
  124. self.reporter.finish()