summaryrefslogtreecommitdiff
path: root/tests/test_setup_command.py
blob: 054912feb597575144871610efbdcf0796c0d1ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""Test setup_command for distutils."""

import os
import subprocess
import sys
from collections import namedtuple
from textwrap import dedent

import pytest

import sphinx
from sphinx.util.osutil import cd


@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')
    args = marker.args if marker else []

    pkgrootdir = tempdir / 'test-setup'
    (rootdir / 'test-setup').copytree(pkgrootdir)

    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


@pytest.fixture
def nonascii_srcdir(request, setup_command):
    mb_name = '\u65e5\u672c\u8a9e'
    srcdir = (setup_command.pkgroot / 'doc')
    try:
        (srcdir / mb_name).makedirs()
    except UnicodeEncodeError:
        from sphinx.testing.path import FILESYSTEMENCODING
        pytest.skip(
            'non-ASCII filename not supported on this filesystem encoding: '
            '%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() + dedent("""
                          .. toctree::

                             %(mb_name)s/%(mb_name)s
                          """ % locals())).encode())


@pytest.mark.usefixtures('nonascii_srcdir')
def test_build_sphinx_with_nonascii_path(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', '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')
    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'


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`')
    proc = setup_command.proc
    out, err = proc.communicate()
    print(out.decode())
    print(err.decode())
    assert proc.returncode == 0


@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`')
    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'