123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- # -*- coding: utf-8 -*-
- """
- :codeauthor: Pedro Algarvio (pedro@algarvio.me)
- :copyright: Copyright 2014 by the SaltStack Team, see AUTHORS for more details.
- :license: Apache 2.0, see LICENSE for more details.
- tests.support.xmlunit
- ~~~~~~~~~~~~~~~~~~~
- XML Unit Tests
- """
- # pylint: disable=wrong-import-order,wrong-import-position
- # Import python libs
- from __future__ import absolute_import
- import io
- import logging
- import os
- from tests.support.runtests import RUNTIME_VARS
- log = logging.getLogger(__name__)
- try:
- import xmlrunner.runner
- import xmlrunner.result
- HAS_XMLRUNNER = True
- class _DuplicateWriter(io.TextIOBase):
- """
- Duplicate output from the first handle to the second handle
- The second handle is expected to be a StringIO and not to block.
- """
- def __init__(self, first, second):
- super(_DuplicateWriter, self).__init__()
- self._first = first
- self._second = second
- def flush(self):
- self._first.flush()
- self._second.flush()
- def writable(self):
- return True
- def writelines(self, lines):
- self._first.writelines(lines)
- self._second.writelines(lines)
- def write(self, b):
- if isinstance(self._first, io.TextIOBase):
- wrote = self._first.write(b)
- if wrote is not None:
- # expected to always succeed to write
- self._second.write(b[:wrote])
- return wrote
- else:
- # file-like object in Python2
- # It doesn't return wrote bytes.
- self._first.write(b)
- self._second.write(b)
- return len(b)
- def fileno(self):
- return self._first.fileno()
- xmlrunner.result._DuplicateWriter = _DuplicateWriter
- class _XMLTestResult(xmlrunner.result._XMLTestResult):
- def startTest(self, test):
- log.debug(">>>>> START >>>>> %s", test.id())
- # xmlrunner classes are NOT new-style classes
- xmlrunner.result._XMLTestResult.startTest(self, test)
- def stopTest(self, test):
- log.debug("<<<<< END <<<<<<< %s", test.id())
- # xmlrunner classes are NOT new-style classes
- return xmlrunner.result._XMLTestResult.stopTest(self, test)
- class XMLTestRunner(xmlrunner.runner.XMLTestRunner):
- def _make_result(self):
- return _XMLTestResult(
- self.stream, self.descriptions, self.verbosity, self.elapsed_times
- )
- def run(self, test):
- try:
- current_cwd = os.getcwd()
- if current_cwd != RUNTIME_VARS.CODE_DIR:
- log.critical(
- "CWD is not %s when running test %s",
- RUNTIME_VARS.CODE_DIR,
- test,
- )
- os.chdir(RUNTIME_VARS.CODE_DIR)
- except FileNotFoundError:
- log.critical("UNABLE TO GET CWD before running test: %s", test)
- os.chdir(RUNTIME_VARS.CODE_DIR)
- try:
- result = xmlrunner.runner.XMLTestRunner.run(self, test)
- except FileNotFoundError:
- log.critical(
- "FileNotFoundError was caught when running test: %s",
- test,
- exc_info=True,
- )
- raise
- self.stream.writeln("Finished generating XML reports")
- return result
- except ImportError:
- HAS_XMLRUNNER = False
- class XMLTestRunner(object):
- """
- This is a dumb class just so we don't break projects at import time
- """
|