run.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. import unittest.signals
  29. import qubes.tests
  30. class CursesColor(dict):
  31. def __init__(self):
  32. super(CursesColor, self).__init__()
  33. try:
  34. curses.setupterm()
  35. except curses.error:
  36. return
  37. # pylint: disable=bad-whitespace
  38. self['black'] = curses.tparm(curses.tigetstr('setaf'), 0)
  39. self['red'] = curses.tparm(curses.tigetstr('setaf'), 1)
  40. self['green'] = curses.tparm(curses.tigetstr('setaf'), 2)
  41. self['yellow'] = curses.tparm(curses.tigetstr('setaf'), 3)
  42. self['blue'] = curses.tparm(curses.tigetstr('setaf'), 4)
  43. self['magenta'] = curses.tparm(curses.tigetstr('setaf'), 5)
  44. self['cyan'] = curses.tparm(curses.tigetstr('setaf'), 6)
  45. self['white'] = curses.tparm(curses.tigetstr('setaf'), 7)
  46. self['bold'] = curses.tigetstr('bold')
  47. self['normal'] = curses.tigetstr('sgr0')
  48. def __missing__(self, key):
  49. # pylint: disable=unused-argument,no-self-use
  50. return ''
  51. class CursesTestResult(unittest.TestResult):
  52. '''A test result class that can print colourful text results to a stream.
  53. Used by TextTestRunner. This is a lightly rewritten unittest.TextTestResult.
  54. '''
  55. separator1 = unittest.TextTestResult.separator1
  56. separator2 = unittest.TextTestResult.separator2
  57. def __init__(self, stream, descriptions, verbosity):
  58. super(CursesTestResult, self).__init__(stream, descriptions, verbosity)
  59. self.stream = stream
  60. self.showAll = verbosity > 1 # pylint: disable=invalid-name
  61. self.dots = verbosity == 1
  62. self.descriptions = descriptions
  63. self.color = CursesColor()
  64. self.hostname = socket.gethostname()
  65. def _fmtexc(self, err):
  66. if str(err[1]):
  67. return '{color[bold]}{}:{color[normal]} {!s}'.format(
  68. err[0].__name__, err[1], color=self.color)
  69. else:
  70. return '{color[bold]}{}{color[normal]}'.format(
  71. err[0].__name__, color=self.color)
  72. def getDescription(self, test): # pylint: disable=invalid-name
  73. teststr = str(test).split('/')
  74. for i in range(-2, 0):
  75. try:
  76. fullname = teststr[i].split('_', 2)
  77. except IndexError:
  78. continue
  79. fullname[-1] = '{color[bold]}{}{color[normal]}'.format(
  80. fullname[-1], color=self.color)
  81. teststr[i] = '_'.join(fullname)
  82. teststr = '/'.join(teststr)
  83. doc_first_line = test.shortDescription()
  84. if self.descriptions and doc_first_line:
  85. return '\n'.join((teststr, ' {}'.format(
  86. doc_first_line, color=self.color)))
  87. else:
  88. return teststr
  89. def startTest(self, test): # pylint: disable=invalid-name
  90. super(CursesTestResult, self).startTest(test)
  91. if self.showAll:
  92. # if not qubes.tests.in_git:
  93. self.stream.write('{}: '.format(self.hostname))
  94. self.stream.write(self.getDescription(test))
  95. self.stream.write(' ... ')
  96. self.stream.flush()
  97. def addSuccess(self, test): # pylint: disable=invalid-name
  98. super(CursesTestResult, self).addSuccess(test)
  99. if self.showAll:
  100. self.stream.writeln('{color[green]}ok{color[normal]}'.format(
  101. color=self.color))
  102. elif self.dots:
  103. self.stream.write('.')
  104. self.stream.flush()
  105. def addError(self, test, err): # pylint: disable=invalid-name
  106. super(CursesTestResult, self).addError(test, err)
  107. if self.showAll:
  108. self.stream.writeln(
  109. '{color[red]}{color[bold]}ERROR{color[normal]} ({})'.format(
  110. self._fmtexc(err), color=self.color))
  111. elif self.dots:
  112. self.stream.write(
  113. '{color[red]}{color[bold]}E{color[normal]}'.format(
  114. color=self.color))
  115. self.stream.flush()
  116. def addFailure(self, test, err): # pylint: disable=invalid-name
  117. super(CursesTestResult, self).addFailure(test, err)
  118. if self.showAll:
  119. self.stream.writeln('{color[red]}FAIL{color[normal]}'.format(
  120. color=self.color))
  121. elif self.dots:
  122. self.stream.write('{color[red]}F{color[normal]}'.format(
  123. color=self.color))
  124. self.stream.flush()
  125. def addSkip(self, test, reason): # pylint: disable=invalid-name
  126. super(CursesTestResult, self).addSkip(test, reason)
  127. if self.showAll:
  128. self.stream.writeln(
  129. '{color[cyan]}skipped{color[normal]} ({})'.format(
  130. reason, color=self.color))
  131. elif self.dots:
  132. self.stream.write('{color[cyan]}s{color[normal]}'.format(
  133. color=self.color))
  134. self.stream.flush()
  135. def addExpectedFailure(self, test, err): # pylint: disable=invalid-name
  136. super(CursesTestResult, self).addExpectedFailure(test, err)
  137. if self.showAll:
  138. self.stream.writeln(
  139. '{color[yellow]}expected failure{color[normal]}'.format(
  140. color=self.color))
  141. elif self.dots:
  142. self.stream.write('{color[yellow]}x{color[normal]}'.format(
  143. color=self.color))
  144. self.stream.flush()
  145. def addUnexpectedSuccess(self, test): # pylint: disable=invalid-name
  146. super(CursesTestResult, self).addUnexpectedSuccess(test)
  147. if self.showAll:
  148. self.stream.writeln(
  149. '{color[yellow]}{color[bold]}unexpected success'
  150. '{color[normal]}'.format(color=self.color))
  151. elif self.dots:
  152. self.stream.write(
  153. '{color[yellow]}{color[bold]}u{color[normal]}'.format(
  154. color=self.color))
  155. self.stream.flush()
  156. def printErrors(self): # pylint: disable=invalid-name
  157. if self.dots or self.showAll:
  158. self.stream.writeln()
  159. self.printErrorList(
  160. '{color[red]}{color[bold]}ERROR{color[normal]}'.format(
  161. color=self.color),
  162. self.errors)
  163. self.printErrorList(
  164. '{color[red]}FAIL{color[normal]}'.format(color=self.color),
  165. self.failures)
  166. def printErrorList(self, flavour, errors): # pylint: disable=invalid-name
  167. for test, err in errors:
  168. self.stream.writeln(self.separator1)
  169. self.stream.writeln('%s: %s' % (flavour, self.getDescription(test)))
  170. self.stream.writeln(self.separator2)
  171. self.stream.writeln('%s' % err)
  172. def main():
  173. suite = unittest.TestSuite()
  174. loader = unittest.TestLoader()
  175. suite.addTests(loader.loadTestsFromName('qubes.tests'))
  176. runner = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
  177. unittest.signals.installHandler()
  178. runner.resultclass = CursesTestResult
  179. return runner.run(suite).wasSuccessful()
  180. if __name__ == '__main__':
  181. sys.exit(not main())