test.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://metrixplusplus.sourceforge.net
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Metrix++ is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 3 of the License.
  10. #
  11. # Metrix++ is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Metrix++. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import subprocess
  20. import os.path
  21. import itertools
  22. import mpp.log
  23. import mpp.cmdparser
  24. import mpp.api
  25. class Tool(mpp.api.ITool):
  26. def run(self, tool_args):
  27. return main(tool_args)
  28. def main(tool_args):
  29. exit_code = 0
  30. log_plugin = mpp.log.Plugin()
  31. parser = mpp.cmdparser.MultiOptionParser(usage="Usage: %prog test [options] -- [testgroup-dir-path-1[/testsuite-file-path-1]] ... [...path-N]")
  32. log_plugin.declare_configuration(parser, default_value='ERROR')
  33. parser.add_option("-g", "--generate-golds", "--gg", action="store_true", default=False,
  34. help="If the option is set (True), new gold files are generated (replacing existing) [default: %default]")
  35. (options, args) = parser.parse_args(tool_args)
  36. log_plugin.configure(options)
  37. os.environ['METRIXPLUSPLUS_TEST_GENERATE_GOLDS'] = str(options.__dict__['generate_golds'])
  38. os.environ['METRIXPLUSPLUS_TEST_MODE'] = str("True")
  39. tests_dir = os.path.join(os.environ['METRIXPLUSPLUS_INSTALL_DIR'], 'tests')
  40. process_data= ["python", "-m", "unittest", "discover", "-v", "-s"]
  41. if len(args) == 0 or tests_dir == os.path.abspath(args[0]):
  42. for fname in os.listdir(tests_dir):
  43. full_path = os.path.join(tests_dir, fname)
  44. if os.path.isdir(full_path) and fname != "sources":
  45. exit_code += subprocess.call(itertools.chain(process_data, [full_path]),
  46. cwd=os.environ['METRIXPLUSPLUS_INSTALL_DIR'])
  47. else:
  48. for arg in args:
  49. if os.path.isdir(arg):
  50. exit_code += subprocess.call(itertools.chain(process_data, [arg]),
  51. cwd=os.environ['METRIXPLUSPLUS_INSTALL_DIR'])
  52. else:
  53. dir_name = os.path.dirname(arg)
  54. file_name = os.path.basename(arg)
  55. exit_code += subprocess.call(itertools.chain(process_data, [dir_name, "-p", file_name]),
  56. cwd=os.environ['METRIXPLUSPLUS_INSTALL_DIR'])
  57. return exit_code