sphinx/tests/test_setup_command.py

138 lines
4.2 KiB
Python
Raw Normal View History

2022-02-19 21:05:56 -06:00
"""Test setup_command for distutils."""
import os
import subprocess
2018-02-19 07:39:14 -06:00
import sys
2017-01-03 07:24:00 -06:00
from collections import namedtuple
2018-02-19 07:39:14 -06:00
from textwrap import dedent
2017-01-03 07:24:00 -06:00
import pytest
2018-02-19 07:39:14 -06:00
import sphinx
2017-01-03 07:24:00 -06:00
from sphinx.util.osutil import cd
2017-01-03 07:24:00 -06:00
@pytest.fixture
def setup_command(request, tempdir, rootdir):
"""
Run `setup.py build_sphinx` with args and kwargs,
pass it to the test and clean up properly.
"""
if hasattr(request.node, 'get_closest_marker'): # pytest-3.6.0 or newer
marker = request.node.get_closest_marker('setup_command')
else:
marker = request.node.get_marker('setup_command')
2017-01-03 07:24:00 -06:00
args = marker.args if marker else []
pkgrootdir = tempdir / 'test-setup'
(rootdir / 'test-setup').copytree(pkgrootdir)
2017-01-03 07:24:00 -06:00
with cd(pkgrootdir):
pythonpath = os.path.dirname(os.path.dirname(sphinx.__file__))
if os.getenv('PYTHONPATH'):
pythonpath = os.getenv('PYTHONPATH') + os.pathsep + pythonpath
command = [sys.executable, 'setup.py', 'build_sphinx']
command.extend(args)
proc = subprocess.Popen(
command,
env=dict(os.environ, PYTHONPATH=pythonpath),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
yield namedtuple('setup', 'pkgroot,proc')(pkgrootdir, proc)
def test_build_sphinx(setup_command):
proc = setup_command.proc
out, err = proc.communicate()
print(out.decode())
print(err.decode())
assert proc.returncode == 0
@pytest.mark.setup_command('-b', 'html,man')
def test_build_sphinx_multiple_builders(setup_command):
proc = setup_command.proc
out, err = proc.communicate()
print(out.decode())
print(err.decode())
assert proc.returncode == 0
@pytest.mark.setup_command('-b', 'html,bar')
def test_build_sphinx_multiple_invalid_builders(setup_command):
proc = setup_command.proc
out, err = proc.communicate()
print(out.decode())
print(err.decode())
assert proc.returncode == 1
2017-01-03 07:24:00 -06:00
@pytest.fixture
def nonascii_srcdir(request, setup_command):
2018-12-15 08:02:28 -06:00
mb_name = '\u65e5\u672c\u8a9e'
2017-01-03 07:24:00 -06:00
srcdir = (setup_command.pkgroot / 'doc')
try:
(srcdir / mb_name).makedirs()
except UnicodeEncodeError:
from sphinx.testing.path import FILESYSTEMENCODING
2017-01-03 07:24:00 -06:00
pytest.skip(
'non-ASCII filename not supported on this filesystem encoding: '
2017-01-03 07:24:00 -06:00
'%s' % FILESYSTEMENCODING)
(srcdir / mb_name / (mb_name + '.txt')).write_text(dedent("""
multi byte file name page
==========================
"""))
root_doc = srcdir / 'index.txt'
root_doc.write_bytes((root_doc.read_text(encoding='utf8') + dedent("""
.. toctree::
%(mb_name)s/%(mb_name)s
""" % locals())).encode())
2017-01-03 07:24:00 -06:00
2017-01-05 09:26:31 -06:00
@pytest.mark.usefixtures('nonascii_srcdir')
def test_build_sphinx_with_nonascii_path(setup_command):
2017-01-03 07:24:00 -06:00
proc = setup_command.proc
out, err = proc.communicate()
print(out.decode())
print(err.decode())
assert proc.returncode == 0
2017-01-03 07:24:00 -06:00
@pytest.mark.setup_command('-b', 'linkcheck')
def test_build_sphinx_return_nonzero_status(setup_command):
srcdir = (setup_command.pkgroot / 'doc')
(srcdir / 'contents.txt').write_text(
'http://localhost.unexistentdomain/index.html')
2017-01-03 07:24:00 -06:00
proc = setup_command.proc
out, err = proc.communicate()
print(out.decode())
print(err.decode())
assert proc.returncode != 0, 'expect non-zero status for setup.py'
2017-01-03 07:24:00 -06:00
def test_build_sphinx_warning_return_zero_status(setup_command):
srcdir = (setup_command.pkgroot / 'doc')
(srcdir / 'contents.txt').write_text(
'See :ref:`unexisting-reference-label`')
2017-01-03 07:24:00 -06:00
proc = setup_command.proc
out, err = proc.communicate()
print(out.decode())
print(err.decode())
assert proc.returncode == 0
2017-01-03 07:24:00 -06:00
@pytest.mark.setup_command('--warning-is-error')
def test_build_sphinx_warning_is_error_return_nonzero_status(setup_command):
srcdir = (setup_command.pkgroot / 'doc')
(srcdir / 'contents.txt').write_text(
'See :ref:`unexisting-reference-label`')
2017-01-03 07:24:00 -06:00
proc = setup_command.proc
out, err = proc.communicate()
print(out.decode())
print(err.decode())
assert proc.returncode != 0, 'expect non-zero status for setup.py'