run.py 8.4 KB

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