test.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 logging
  20. import time
  21. import subprocess
  22. import os.path
  23. import itertools
  24. import core.log
  25. import core.cmdparser
  26. def main():
  27. exit_code = 0
  28. log_plugin = core.log.Plugin()
  29. parser = core.cmdparser.MultiOptionParser(usage="Usage: %prog [options] -- [testgroup-dir-path-1[/testsuite-file-path-1]] ... [...path-N]")
  30. log_plugin.declare_configuration(parser, default_value='ERROR')
  31. parser.add_option("-g", "--general.generate-golds", action="store_true", default=False,
  32. help="If the option is set (True), new gold files are generated (replacing existing) [default: %default]")
  33. (options, args) = parser.parse_args()
  34. log_plugin.configure(options)
  35. os.environ['METRIXPLUSPLUS_TEST_GENERATE_GOLDS'] = str(options.__dict__['general.generate_golds'])
  36. install_dir = os.path.dirname(os.path.abspath(__file__))
  37. tests_dir = os.path.join(install_dir, 'tests')
  38. os.environ['METRIXPLUSPLUS_INSTALL_DIR'] = install_dir
  39. process_data= ["python", "-m", "unittest", "discover", "-v", "-s"]
  40. if len(args) == 0 or tests_dir == os.path.abspath(args[0]):
  41. for fname in os.listdir(tests_dir):
  42. full_path = os.path.join(tests_dir, fname)
  43. if os.path.isdir(full_path) and fname != "sources":
  44. exit_code += subprocess.call(itertools.chain(process_data, [full_path]),
  45. cwd=os.environ['METRIXPLUSPLUS_INSTALL_DIR'])
  46. else:
  47. for arg in args:
  48. if os.path.isdir(arg):
  49. exit_code += subprocess.call(itertools.chain(process_data, [arg]),
  50. cwd=os.environ['METRIXPLUSPLUS_INSTALL_DIR'])
  51. else:
  52. dir_name = os.path.dirname(arg)
  53. file_name = os.path.basename(arg)
  54. exit_code += subprocess.call(itertools.chain(process_data, [dir_name, "-p", file_name]),
  55. cwd=os.environ['METRIXPLUSPLUS_INSTALL_DIR'])
  56. return exit_code
  57. if __name__ == '__main__':
  58. ts = time.time()
  59. core.log.set_default_format()
  60. exit_code = main()
  61. logging.warning("Exit code: " + str(exit_code) + ". Time spent: " + str(round((time.time() - ts), 2)) + " seconds. Done")
  62. exit(exit_code) # number of reported messages, errors are reported as non-handled exceptions