| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import os |
| 17 |
import pathlib |
| 18 |
import shutil |
| 19 |
|
| 20 |
import fixtures |
| 21 |
import tempfile |
| 22 |
import testtools |
| 23 |
|
| 24 |
from sphinx.testing.util import SphinxTestApp |
| 25 |
|
| 26 |
|
| 27 |
def example_dir(name=""): |
| 28 |
return os.path.join(os.path.dirname(__file__), 'examples', name) |
| 29 |
|
| 30 |
|
| 31 |
_TRUE_VALUES = ('True', 'true', '1', 'yes') |
| 32 |
|
| 33 |
|
| 34 |
class with_app: |
| 35 |
def __init__(self, **kwargs): |
| 36 |
self.srcdir = pathlib.Path(kwargs['srcdir']) |
| 37 |
self.sphinx_app_args = kwargs |
| 38 |
|
| 39 |
def __call__(self, f): |
| 40 |
def newf(*args, **kwargs): |
| 41 |
with tempfile.TemporaryDirectory() as tmpdirname: |
| 42 |
tmpdir = pathlib.Path(tmpdirname) |
| 43 |
tmproot = tmpdir.joinpath(self.srcdir.name) |
| 44 |
shutil.copytree(self.srcdir, tmproot) |
| 45 |
self.sphinx_app_args['srcdir'] = tmproot |
| 46 |
self.builddir = tmproot.joinpath('_build') |
| 47 |
|
| 48 |
app = SphinxTestApp(freshenv=True, **self.sphinx_app_args) |
| 49 |
|
| 50 |
f(*args, app, app._status, app._warning, **kwargs) |
| 51 |
|
| 52 |
app.cleanup() |
| 53 |
return newf |
| 54 |
|
| 55 |
|
| 56 |
class OutputStreamCapture(fixtures.Fixture): |
| 57 |
"""Capture output streams during tests. |
| 58 |
|
| 59 |
This fixture captures errant printing to stderr / stdout during |
| 60 |
the tests and lets us see those streams at the end of the test |
| 61 |
runs instead. Useful to see what was happening during failed |
| 62 |
tests. |
| 63 |
""" |
| 64 |
def setUp(self): |
| 65 |
super().setUp() |
| 66 |
if os.environ.get('OS_STDOUT_CAPTURE') in _TRUE_VALUES: |
| 67 |
self.out = self.useFixture(fixtures.StringStream('stdout')) |
| 68 |
self.useFixture( |
| 69 |
fixtures.MonkeyPatch('sys.stdout', self.out.stream)) |
| 70 |
if os.environ.get('OS_STDERR_CAPTURE') in _TRUE_VALUES: |
| 71 |
self.err = self.useFixture(fixtures.StringStream('stderr')) |
| 72 |
self.useFixture( |
| 73 |
fixtures.MonkeyPatch('sys.stderr', self.err.stream)) |
| 74 |
|
| 75 |
@property |
| 76 |
def stderr(self): |
| 77 |
return self.err._details["stderr"].as_text() |
| 78 |
|
| 79 |
@property |
| 80 |
def stdout(self): |
| 81 |
return self.out._details["stdout"].as_text() |
| 82 |
|
| 83 |
|
| 84 |
class Timeout(fixtures.Fixture): |
| 85 |
"""Setup per test timeouts. |
| 86 |
|
| 87 |
In order to avoid test deadlocks we support setting up a test |
| 88 |
timeout parameter read from the environment. In almost all |
| 89 |
cases where the timeout is reached this means a deadlock. |
| 90 |
|
| 91 |
A class level TIMEOUT_SCALING_FACTOR also exists, which allows |
| 92 |
extremely long tests to specify they need more time. |
| 93 |
""" |
| 94 |
|
| 95 |
def __init__(self, timeout, scaling=1): |
| 96 |
super().__init__() |
| 97 |
try: |
| 98 |
self.test_timeout = int(timeout) |
| 99 |
except ValueError: |
| 100 |
|
| 101 |
self.test_timeout = 0 |
| 102 |
if scaling >= 1: |
| 103 |
self.test_timeout *= scaling |
| 104 |
else: |
| 105 |
raise ValueError('scaling value must be >= 1') |
| 106 |
|
| 107 |
def setUp(self): |
| 108 |
super().setUp() |
| 109 |
if self.test_timeout > 0: |
| 110 |
self.useFixture(fixtures.Timeout(self.test_timeout, gentle=True)) |
| 111 |
|
| 112 |
|
| 113 |
class TestCase(testtools.TestCase): |
| 114 |
|
| 115 |
"""Test case base class for all unit tests.""" |
| 116 |
|
| 117 |
def setUp(self): |
| 118 |
"""Run before each test method to initialize test environment.""" |
| 119 |
super().setUp() |
| 120 |
self.useFixture(Timeout( |
| 121 |
os.environ.get('OS_TEST_TIMEOUT', 0))) |
| 122 |
self.useFixture(OutputStreamCapture()) |