run.py 9.2 KB

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