run.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. import curses
  24. import importlib
  25. import sys
  26. import unittest
  27. test_order = [
  28. 'qubes.tests.events',
  29. 'qubes.tests.vm.init',
  30. 'qubes.tests.vm.qubesvm',
  31. 'qubes.tests.vm.adminvm',
  32. 'qubes.tests.init'
  33. ]
  34. sys.path.insert(1, '../../')
  35. class ANSIColor(dict):
  36. def __init__(self):
  37. super(ANSIColor, self).__init__()
  38. try:
  39. curses.setupterm()
  40. except curses.error:
  41. return
  42. # pylint: disable=bad-whitespace
  43. self['black'] = curses.tparm(curses.tigetstr('setaf'), 0)
  44. self['red'] = curses.tparm(curses.tigetstr('setaf'), 1)
  45. self['green'] = curses.tparm(curses.tigetstr('setaf'), 2)
  46. self['yellow'] = curses.tparm(curses.tigetstr('setaf'), 3)
  47. self['blue'] = curses.tparm(curses.tigetstr('setaf'), 4)
  48. self['magenta'] = curses.tparm(curses.tigetstr('setaf'), 5)
  49. self['cyan'] = curses.tparm(curses.tigetstr('setaf'), 6)
  50. self['white'] = curses.tparm(curses.tigetstr('setaf'), 7)
  51. self['bold'] = curses.tigetstr('bold')
  52. self['normal'] = curses.tigetstr('sgr0')
  53. def __missing__(self, key):
  54. # pylint: disable=unused-argument,no-self-use
  55. return ''
  56. class ANSITestResult(unittest.TestResult):
  57. '''A test result class that can print colourful text results to a stream.
  58. Used by TextTestRunner. This is a lightly rewritten unittest.TextTestResult.
  59. '''
  60. separator1 = unittest.TextTestResult.separator1
  61. separator2 = unittest.TextTestResult.separator2
  62. def __init__(self, stream, descriptions, verbosity):
  63. super(ANSITestResult, self).__init__(stream, descriptions, verbosity)
  64. self.stream = stream
  65. self.showAll = verbosity > 1 # pylint: disable=invalid-name
  66. self.dots = verbosity == 1
  67. self.descriptions = descriptions
  68. self.color = ANSIColor()
  69. def _fmtexc(self, err):
  70. s = str(err[1])
  71. if s:
  72. return '{color[bold]}{}:{color[normal]} {!s}'.format(
  73. err[0].__name__, err[1], color=self.color)
  74. else:
  75. return '{color[bold]}{}{color[normal]}'.format(
  76. err[0].__name__, color=self.color)
  77. def getDescription(self, test): # pylint: disable=invalid-name
  78. teststr = str(test).split('/')
  79. teststr[-1] = '{color[bold]}{}{color[normal]}'.format(
  80. teststr[-1], color=self.color)
  81. teststr = '/'.join(teststr)
  82. doc_first_line = test.shortDescription()
  83. if self.descriptions and doc_first_line:
  84. return '\n'.join((teststr, ' {}'.format(
  85. doc_first_line, color=self.color)))
  86. else:
  87. return teststr
  88. def startTest(self, test): # pylint: disable=invalid-name
  89. super(ANSITestResult, self).startTest(test)
  90. if self.showAll:
  91. self.stream.write(self.getDescription(test))
  92. self.stream.write(' ... ')
  93. self.stream.flush()
  94. def addSuccess(self, test): # pylint: disable=invalid-name
  95. super(ANSITestResult, self).addSuccess(test)
  96. if self.showAll:
  97. self.stream.writeln('{color[green]}ok{color[normal]}'.format(
  98. color=self.color))
  99. elif self.dots:
  100. self.stream.write('.')
  101. self.stream.flush()
  102. def addError(self, test, err): # pylint: disable=invalid-name
  103. super(ANSITestResult, self).addError(test, err)
  104. if self.showAll:
  105. self.stream.writeln(
  106. '{color[red]}{color[bold]}ERROR{color[normal]} ({})'.format(
  107. self._fmtexc(err), color=self.color))
  108. elif self.dots:
  109. self.stream.write(
  110. '{color[red]}{color[bold]}E{color[normal]}'.format(
  111. color=self.color))
  112. self.stream.flush()
  113. def addFailure(self, test, err): # pylint: disable=invalid-name
  114. super(ANSITestResult, self).addFailure(test, err)
  115. if self.showAll:
  116. self.stream.writeln('{color[red]}FAIL{color[normal]}'.format(
  117. color=self.color))
  118. elif self.dots:
  119. self.stream.write('{color[red]}F{color[normal]}'.format(
  120. color=self.color))
  121. self.stream.flush()
  122. def addSkip(self, test, reason): # pylint: disable=invalid-name
  123. super(ANSITestResult, self).addSkip(test, reason)
  124. if self.showAll:
  125. self.stream.writeln(
  126. '{color[cyan]}skipped{color[normal]} ({})'.format(
  127. reason, color=self.color))
  128. elif self.dots:
  129. self.stream.write('{color[cyan]}s{color[normal]}'.format(
  130. color=self.color))
  131. self.stream.flush()
  132. def addExpectedFailure(self, test, err): # pylint: disable=invalid-name
  133. super(ANSITestResult, self).addExpectedFailure(test, err)
  134. if self.showAll:
  135. self.stream.writeln(
  136. '{color[yellow]}expected failure{color[normal]}'.format(
  137. color=self.color))
  138. elif self.dots:
  139. self.stream.write('{color[yellow]}x{color[normal]}'.format(
  140. color=self.color))
  141. self.stream.flush()
  142. def addUnexpectedSuccess(self, test): # pylint: disable=invalid-name
  143. super(ANSITestResult, self).addUnexpectedSuccess(test)
  144. if self.showAll:
  145. self.stream.writeln(
  146. '{color[yellow]}{color[bold]}unexpected success'
  147. '{color[normal]}'.format(color=self.color))
  148. elif self.dots:
  149. self.stream.write(
  150. '{color[yellow]}{color[bold]}u{color[normal]}'.format(
  151. color=self.color))
  152. self.stream.flush()
  153. def printErrors(self): # pylint: disable=invalid-name
  154. if self.dots or self.showAll:
  155. self.stream.writeln()
  156. self.printErrorList(
  157. '{color[red]}{color[bold]}ERROR{color[normal]}'.format(
  158. color=self.color),
  159. self.errors)
  160. self.printErrorList(
  161. '{color[red]}FAIL{color[normal]}'.format(color=self.color),
  162. self.failures)
  163. def printErrorList(self, flavour, errors): # pylint: disable=invalid-name
  164. for test, err in errors:
  165. self.stream.writeln(self.separator1)
  166. self.stream.writeln('%s: %s' % (flavour, self.getDescription(test)))
  167. self.stream.writeln(self.separator2)
  168. self.stream.writeln('%s' % err)
  169. def demo(verbosity=2):
  170. import qubes.tests
  171. class TC_Demo(qubes.tests.QubesTestCase):
  172. '''Demo class'''
  173. # pylint: disable=no-self-use
  174. def test_0_success(self):
  175. '''Demo test (success)'''
  176. pass
  177. def test_1_error(self):
  178. '''Demo test (error)'''
  179. raise Exception()
  180. def test_2_failure(self):
  181. '''Demo test (failure)'''
  182. self.fail('boo')
  183. def test_3_skip(self):
  184. '''Demo test (skipped by call to self.skipTest())'''
  185. self.skipTest('skip')
  186. @unittest.skip(None)
  187. def test_4_skip_decorator(self):
  188. '''Demo test (skipped by decorator)'''
  189. pass
  190. @unittest.expectedFailure
  191. def test_5_expected_failure(self):
  192. '''Demo test (expected failure)'''
  193. self.fail()
  194. @unittest.expectedFailure
  195. def test_6_unexpected_success(self):
  196. '''Demo test (unexpected success)'''
  197. pass
  198. suite = unittest.TestLoader().loadTestsFromTestCase(TC_Demo)
  199. runner = unittest.TextTestRunner(stream=sys.stdout, verbosity=verbosity)
  200. runner.resultclass = ANSITestResult
  201. return runner.run(suite).wasSuccessful()
  202. def main():
  203. suite = unittest.TestSuite()
  204. loader = unittest.TestLoader()
  205. for modname in test_order:
  206. module = importlib.import_module(modname)
  207. suite.addTests(loader.loadTestsFromModule(module))
  208. runner = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
  209. runner.resultclass = ANSITestResult
  210. return runner.run(suite).wasSuccessful()
  211. if __name__ == '__main__':
  212. sys.exit(not main())