unit_tests.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2019 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. test/unit_tests.py: Unit tests for the MMGen suite
  20. """
  21. import sys,os,time
  22. from tests_header import repo_root
  23. from mmgen.common import *
  24. opts_data = {
  25. 'text': {
  26. 'desc': "Unit tests for the MMGen suite",
  27. 'usage':'[options] [tests]',
  28. 'options': """
  29. -h, --help Print this help message
  30. -f, --fast Speed up execution by reducing rounds on some tests
  31. -l, --list List available tests
  32. -n, --names Print command names instead of descriptions
  33. -q, --quiet Produce quieter output
  34. -v, --verbose Produce more verbose output
  35. """,
  36. 'notes': """
  37. If no test is specified, all available tests are run
  38. """
  39. }
  40. }
  41. sys.argv = [sys.argv[0]] + ['--skip-cfg-file'] + sys.argv[1:]
  42. cmd_args = opts.init(opts_data)
  43. def exit_msg():
  44. t = int(time.time()) - start_time
  45. gmsg('All requested tests finished OK, elapsed time: {:02}:{:02}'.format(t//60,t%60))
  46. all_tests = [fn[3:-3] for fn in os.listdir(os.path.join(repo_root,'test','unit_tests_d')) if fn[:3] == 'ut_']
  47. start_time = int(time.time())
  48. if opt.list:
  49. Die(0,' '.join(all_tests))
  50. class UnitTestHelpers(object):
  51. @classmethod
  52. def process_bad_data(cls,data):
  53. desc_w = max(len(e[0]) for e in data)
  54. exc_w = max(len(e[1]) for e in data)
  55. for (desc,exc_chk,emsg_chk,func) in data:
  56. try:
  57. vmsg_r(' {:{w}}'.format(desc+':',w=desc_w+1))
  58. func()
  59. except Exception as e:
  60. exc = type(e).__name__
  61. emsg = e.args[0]
  62. vmsg(' {:{w}} [{}]'.format(exc,emsg,w=exc_w))
  63. assert exc == exc_chk,'{!r}: incorrect exception type (expected {!r})'.format(exc,exc_chk)
  64. assert emsg_chk in emsg,'{!r}: incorrect error msg (should contain {!r}'.format(emsg,emsg_chk)
  65. else:
  66. rdie(3,"\nillegal action '{}' failed to raise exception {!r}".format(desc,exc_chk))
  67. try:
  68. for test in cmd_args:
  69. if test not in all_tests:
  70. die(1,"'{}': test not recognized".format(test))
  71. import importlib
  72. for test in (cmd_args or all_tests):
  73. modname = 'test.unit_tests_d.ut_{}'.format(test)
  74. mod = importlib.import_module(modname)
  75. gmsg('Running unit test {}'.format(test))
  76. if not mod.unit_test().run_test(test,UnitTestHelpers):
  77. rdie(1,'Unit test {!r} failed'.format(test))
  78. del mod
  79. exit_msg()
  80. except KeyboardInterrupt:
  81. die(1,green('\nExiting at user request'))