1"""
2    pytest config for sphinx/tests
3    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5    :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
6    :license: BSD, see LICENSE for details.
7"""
8
9import os
10import shutil
11
12import docutils
13import pytest
14
15import sphinx
16from sphinx.testing import comparer
17from sphinx.testing.path import path
18
19pytest_plugins = 'sphinx.testing.fixtures'
20
21# Exclude 'roots' dirs for pytest test collector
22collect_ignore = ['roots']
23
24
25@pytest.fixture(scope='session')
26def rootdir():
27    return path(__file__).parent.abspath() / 'roots'
28
29
30def pytest_report_header(config):
31    header = ("libraries: Sphinx-%s, docutils-%s" %
32              (sphinx.__display_version__, docutils.__version__))
33    if hasattr(config, '_tmp_path_factory'):
34        header += "\nbase tempdir: %s" % config._tmp_path_factory.getbasetemp()
35
36    return header
37
38
39def pytest_assertrepr_compare(op, left, right):
40    comparer.pytest_assertrepr_compare(op, left, right)
41
42
43def _initialize_test_directory(session):
44    if 'SPHINX_TEST_TEMPDIR' in os.environ:
45        tempdir = os.path.abspath(os.getenv('SPHINX_TEST_TEMPDIR'))
46        print('Temporary files will be placed in %s.' % tempdir)
47
48        if os.path.exists(tempdir):
49            shutil.rmtree(tempdir)
50
51        os.makedirs(tempdir)
52
53
54def pytest_sessionstart(session):
55    _initialize_test_directory(session)
56