run.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 socket
  26. import sys
  27. import unittest
  28. test_order = [
  29. 'qubes.tests.events',
  30. 'qubes.tests.vm.init',
  31. 'qubes.tests.vm.qubesvm',
  32. 'qubes.tests.vm.adminvm',
  33. 'qubes.tests.init',
  34. 'qubes.tests.tools',
  35. ]
  36. sys.path.insert(1, '../../')
  37. import qubes.tests
  38. class ANSIColor(dict):
  39. def __init__(self):
  40. super(ANSIColor, self).__init__()
  41. try:
  42. curses.setupterm()
  43. except curses.error:
  44. return
  45. # pylint: disable=bad-whitespace
  46. self['black'] = curses.tparm(curses.tigetstr('setaf'), 0)
  47. self['red'] = curses.tparm(curses.tigetstr('setaf'), 1)
  48. self['green'] = curses.tparm(curses.tigetstr('setaf'), 2)
  49. self['yellow'] = curses.tparm(curses.tigetstr('setaf'), 3)
  50. self['blue'] = curses.tparm(curses.tigetstr('setaf'), 4)
  51. self['magenta'] = curses.tparm(curses.tigetstr('setaf'), 5)
  52. self['cyan'] = curses.tparm(curses.tigetstr('setaf'), 6)
  53. self['white'] = curses.tparm(curses.tigetstr('setaf'), 7)
  54. self['bold'] = curses.tigetstr('bold')
  55. self['normal'] = curses.tigetstr('sgr0')
  56. def __missing__(self, key):
  57. # pylint: disable=unused-argument,no-self-use
  58. return ''
  59. class ANSITestResult(unittest.TestResult):
  60. '''A test result class that can print colourful text results to a stream.
  61. Used by TextTestRunner. This is a lightly rewritten unittest.TextTestResult.
  62. '''
  63. separator1 = unittest.TextTestResult.separator1
  64. separator2 = unittest.TextTestResult.separator2
  65. def __init__(self, stream, descriptions, verbosity):
  66. super(ANSITestResult, self).__init__(stream, descriptions, verbosity)
  67. self.stream = stream
  68. self.showAll = verbosity > 1 # pylint: disable=invalid-name
  69. self.dots = verbosity == 1
  70. self.descriptions = descriptions
  71. self.color = ANSIColor()
  72. self.hostname = socket.gethostname()
  73. def _fmtexc(self, err):
  74. if str(err[1]):
  75. return '{color[bold]}{}:{color[normal]} {!s}'.format(
  76. err[0].__name__, err[1], color=self.color)
  77. else:
  78. return '{color[bold]}{}{color[normal]}'.format(
  79. err[0].__name__, color=self.color)
  80. def getDescription(self, test): # pylint: disable=invalid-name
  81. teststr = str(test).split('/')
  82. for i in range(-2, 0):
  83. try:
  84. fullname = teststr[i].split('_', 2)
  85. except IndexError:
  86. continue
  87. fullname[-1] = '{color[bold]}{}{color[normal]}'.format(
  88. fullname[-1], color=self.color)
  89. teststr[i] = '_'.join(fullname)
  90. teststr = '/'.join(teststr)
  91. doc_first_line = test.shortDescription()
  92. if self.descriptions and doc_first_line:
  93. return '\n'.join((teststr, ' {}'.format(
  94. doc_first_line, color=self.color)))
  95. else:
  96. return teststr
  97. def startTest(self, test): # pylint: disable=invalid-name
  98. super(ANSITestResult, self).startTest(test)
  99. if self.showAll:
  100. if not qubes.tests.in_git:
  101. self.stream.write('{}: '.format(self.hostname))
  102. self.stream.write(self.getDescription(test))
  103. self.stream.write(' ... ')
  104. self.stream.flush()
  105. def addSuccess(self, test): # pylint: disable=invalid-name
  106. super(ANSITestResult, self).addSuccess(test)
  107. if self.showAll:
  108. self.stream.writeln('{color[green]}ok{color[normal]}'.format(
  109. color=self.color))
  110. elif self.dots:
  111. self.stream.write('.')
  112. self.stream.flush()
  113. def addError(self, test, err): # pylint: disable=invalid-name
  114. super(ANSITestResult, self).addError(test, err)
  115. if self.showAll:
  116. self.stream.writeln(
  117. '{color[red]}{color[bold]}ERROR{color[normal]} ({})'.format(
  118. self._fmtexc(err), color=self.color))
  119. elif self.dots:
  120. self.stream.write(
  121. '{color[red]}{color[bold]}E{color[normal]}'.format(
  122. color=self.color))
  123. self.stream.flush()
  124. def addFailure(self, test, err): # pylint: disable=invalid-name
  125. super(ANSITestResult, self).addFailure(test, err)
  126. if self.showAll:
  127. self.stream.writeln('{color[red]}FAIL{color[normal]}'.format(
  128. color=self.color))
  129. elif self.dots:
  130. self.stream.write('{color[red]}F{color[normal]}'.format(
  131. color=self.color))
  132. self.stream.flush()
  133. def addSkip(self, test, reason): # pylint: disable=invalid-name
  134. super(ANSITestResult, self).addSkip(test, reason)
  135. if self.showAll:
  136. self.stream.writeln(
  137. '{color[cyan]}skipped{color[normal]} ({})'.format(
  138. reason, color=self.color))
  139. elif self.dots:
  140. self.stream.write('{color[cyan]}s{color[normal]}'.format(
  141. color=self.color))
  142. self.stream.flush()
  143. def addExpectedFailure(self, test, err): # pylint: disable=invalid-name
  144. super(ANSITestResult, self).addExpectedFailure(test, err)
  145. if self.showAll:
  146. self.stream.writeln(
  147. '{color[yellow]}expected failure{color[normal]}'.format(
  148. color=self.color))
  149. elif self.dots:
  150. self.stream.write('{color[yellow]}x{color[normal]}'.format(
  151. color=self.color))
  152. self.stream.flush()
  153. def addUnexpectedSuccess(self, test): # pylint: disable=invalid-name
  154. super(ANSITestResult, self).addUnexpectedSuccess(test)
  155. if self.showAll:
  156. self.stream.writeln(
  157. '{color[yellow]}{color[bold]}unexpected success'
  158. '{color[normal]}'.format(color=self.color))
  159. elif self.dots:
  160. self.stream.write(
  161. '{color[yellow]}{color[bold]}u{color[normal]}'.format(
  162. color=self.color))
  163. self.stream.flush()
  164. def printErrors(self): # pylint: disable=invalid-name
  165. if self.dots or self.showAll:
  166. self.stream.writeln()
  167. self.printErrorList(
  168. '{color[red]}{color[bold]}ERROR{color[normal]}'.format(
  169. color=self.color),
  170. self.errors)
  171. self.printErrorList(
  172. '{color[red]}FAIL{color[normal]}'.format(color=self.color),
  173. self.failures)
  174. def printErrorList(self, flavour, errors): # pylint: disable=invalid-name
  175. for test, err in errors:
  176. self.stream.writeln(self.separator1)
  177. self.stream.writeln('%s: %s' % (flavour, self.getDescription(test)))
  178. self.stream.writeln(self.separator2)
  179. self.stream.writeln('%s' % err)
  180. def demo(verbosity=2):
  181. class TC_00_Demo(qubes.tests.QubesTestCase):
  182. '''Demo class'''
  183. # pylint: disable=no-self-use
  184. def test_0_success(self):
  185. '''Demo test (success)'''
  186. pass
  187. def test_1_error(self):
  188. '''Demo test (error)'''
  189. raise Exception()
  190. def test_2_failure(self):
  191. '''Demo test (failure)'''
  192. self.fail('boo')
  193. def test_3_skip(self):
  194. '''Demo test (skipped by call to self.skipTest())'''
  195. self.skipTest('skip')
  196. @unittest.skip(None)
  197. def test_4_skip_decorator(self):
  198. '''Demo test (skipped by decorator)'''
  199. pass
  200. @unittest.expectedFailure
  201. def test_5_expected_failure(self):
  202. '''Demo test (expected failure)'''
  203. self.fail()
  204. @unittest.expectedFailure
  205. def test_6_unexpected_success(self):
  206. '''Demo test (unexpected success)'''
  207. pass
  208. suite = unittest.TestLoader().loadTestsFromTestCase(TC_00_Demo)
  209. runner = unittest.TextTestRunner(stream=sys.stdout, verbosity=verbosity)
  210. runner.resultclass = ANSITestResult
  211. return runner.run(suite).wasSuccessful()
  212. def main():
  213. suite = unittest.TestSuite()
  214. loader = unittest.TestLoader()
  215. for modname in test_order:
  216. suite.addTests(loader.loadTestsFromName(modname))
  217. runner = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
  218. runner.resultclass = ANSITestResult
  219. return runner.run(suite).wasSuccessful()
  220. if __name__ == '__main__':
  221. sys.exit(not main())