run.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. ]
  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. fullname = teststr[i].split('_', 2)
  83. fullname[-1] = '{color[bold]}{}{color[normal]}'.format(
  84. fullname[-1], color=self.color)
  85. teststr[i] = '_'.join(fullname)
  86. teststr = '/'.join(teststr)
  87. doc_first_line = test.shortDescription()
  88. if self.descriptions and doc_first_line:
  89. return '\n'.join((teststr, ' {}'.format(
  90. doc_first_line, color=self.color)))
  91. else:
  92. return teststr
  93. def startTest(self, test): # pylint: disable=invalid-name
  94. super(ANSITestResult, self).startTest(test)
  95. if self.showAll:
  96. if not qubes.tests.in_git:
  97. self.stream.write('{}: '.format(self.hostname))
  98. self.stream.write(self.getDescription(test))
  99. self.stream.write(' ... ')
  100. self.stream.flush()
  101. def addSuccess(self, test): # pylint: disable=invalid-name
  102. super(ANSITestResult, self).addSuccess(test)
  103. if self.showAll:
  104. self.stream.writeln('{color[green]}ok{color[normal]}'.format(
  105. color=self.color))
  106. elif self.dots:
  107. self.stream.write('.')
  108. self.stream.flush()
  109. def addError(self, test, err): # pylint: disable=invalid-name
  110. super(ANSITestResult, self).addError(test, err)
  111. if self.showAll:
  112. self.stream.writeln(
  113. '{color[red]}{color[bold]}ERROR{color[normal]} ({})'.format(
  114. self._fmtexc(err), color=self.color))
  115. elif self.dots:
  116. self.stream.write(
  117. '{color[red]}{color[bold]}E{color[normal]}'.format(
  118. color=self.color))
  119. self.stream.flush()
  120. def addFailure(self, test, err): # pylint: disable=invalid-name
  121. super(ANSITestResult, self).addFailure(test, err)
  122. if self.showAll:
  123. self.stream.writeln('{color[red]}FAIL{color[normal]}'.format(
  124. color=self.color))
  125. elif self.dots:
  126. self.stream.write('{color[red]}F{color[normal]}'.format(
  127. color=self.color))
  128. self.stream.flush()
  129. def addSkip(self, test, reason): # pylint: disable=invalid-name
  130. super(ANSITestResult, self).addSkip(test, reason)
  131. if self.showAll:
  132. self.stream.writeln(
  133. '{color[cyan]}skipped{color[normal]} ({})'.format(
  134. reason, color=self.color))
  135. elif self.dots:
  136. self.stream.write('{color[cyan]}s{color[normal]}'.format(
  137. color=self.color))
  138. self.stream.flush()
  139. def addExpectedFailure(self, test, err): # pylint: disable=invalid-name
  140. super(ANSITestResult, self).addExpectedFailure(test, err)
  141. if self.showAll:
  142. self.stream.writeln(
  143. '{color[yellow]}expected failure{color[normal]}'.format(
  144. color=self.color))
  145. elif self.dots:
  146. self.stream.write('{color[yellow]}x{color[normal]}'.format(
  147. color=self.color))
  148. self.stream.flush()
  149. def addUnexpectedSuccess(self, test): # pylint: disable=invalid-name
  150. super(ANSITestResult, self).addUnexpectedSuccess(test)
  151. if self.showAll:
  152. self.stream.writeln(
  153. '{color[yellow]}{color[bold]}unexpected success'
  154. '{color[normal]}'.format(color=self.color))
  155. elif self.dots:
  156. self.stream.write(
  157. '{color[yellow]}{color[bold]}u{color[normal]}'.format(
  158. color=self.color))
  159. self.stream.flush()
  160. def printErrors(self): # pylint: disable=invalid-name
  161. if self.dots or self.showAll:
  162. self.stream.writeln()
  163. self.printErrorList(
  164. '{color[red]}{color[bold]}ERROR{color[normal]}'.format(
  165. color=self.color),
  166. self.errors)
  167. self.printErrorList(
  168. '{color[red]}FAIL{color[normal]}'.format(color=self.color),
  169. self.failures)
  170. def printErrorList(self, flavour, errors): # pylint: disable=invalid-name
  171. for test, err in errors:
  172. self.stream.writeln(self.separator1)
  173. self.stream.writeln('%s: %s' % (flavour, self.getDescription(test)))
  174. self.stream.writeln(self.separator2)
  175. self.stream.writeln('%s' % err)
  176. def demo(verbosity=2):
  177. class TC_00_Demo(qubes.tests.QubesTestCase):
  178. '''Demo class'''
  179. # pylint: disable=no-self-use
  180. def test_0_success(self):
  181. '''Demo test (success)'''
  182. pass
  183. def test_1_error(self):
  184. '''Demo test (error)'''
  185. raise Exception()
  186. def test_2_failure(self):
  187. '''Demo test (failure)'''
  188. self.fail('boo')
  189. def test_3_skip(self):
  190. '''Demo test (skipped by call to self.skipTest())'''
  191. self.skipTest('skip')
  192. @unittest.skip(None)
  193. def test_4_skip_decorator(self):
  194. '''Demo test (skipped by decorator)'''
  195. pass
  196. @unittest.expectedFailure
  197. def test_5_expected_failure(self):
  198. '''Demo test (expected failure)'''
  199. self.fail()
  200. @unittest.expectedFailure
  201. def test_6_unexpected_success(self):
  202. '''Demo test (unexpected success)'''
  203. pass
  204. suite = unittest.TestLoader().loadTestsFromTestCase(TC_00_Demo)
  205. runner = unittest.TextTestRunner(stream=sys.stdout, verbosity=verbosity)
  206. runner.resultclass = ANSITestResult
  207. return runner.run(suite).wasSuccessful()
  208. def main():
  209. suite = unittest.TestSuite()
  210. loader = unittest.TestLoader()
  211. for modname in test_order:
  212. module = importlib.import_module(modname)
  213. suite.addTests(loader.loadTestsFromModule(module))
  214. runner = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
  215. runner.resultclass = ANSITestResult
  216. return runner.run(suite).wasSuccessful()
  217. if __name__ == '__main__':
  218. sys.exit(not main())