| 1 |
import pytest |
| 2 |
from sphinx.application import Sphinx |
| 3 |
from sphinx.errors import ExtensionError |
| 4 |
|
| 5 |
|
| 6 |
@pytest.mark.sphinx( |
| 7 |
"html", |
| 8 |
testroot="redirects", |
| 9 |
freshenv=True, |
| 10 |
confoverrides={ |
| 11 |
"redirects": { |
| 12 |
"faq/*": "https://new.com/$source.html", |
| 13 |
"*all": "go-to-${source}", |
| 14 |
"setup": "install.html", |
| 15 |
"install/requirements": "https://web.com/docs/requirements.html", |
| 16 |
} |
| 17 |
}, |
| 18 |
) |
| 19 |
def test_ext(app: Sphinx, status, warning): |
| 20 |
app.build() |
| 21 |
status = status.getvalue() |
| 22 |
|
| 23 |
assert ( |
| 24 |
app.outdir / "faq/one.html" |
| 25 |
).read_text() == '<html><head><meta http-equiv="refresh" content="0; url=https://new.com/faq/one.html"></head></html>' # noqa: E501 |
| 26 |
|
| 27 |
assert ( |
| 28 |
"""Overwriting 'faq/one.html' with redirect to 'https://new.com/faq/one.html'.""" # noqa: E501 |
| 29 |
in status |
| 30 |
) |
| 31 |
|
| 32 |
assert ( |
| 33 |
app.outdir / "faq/two.html" |
| 34 |
).read_text() == '<html><head><meta http-equiv="refresh" content="0; url=https://new.com/faq/two.html"></head></html>' # noqa: E501 |
| 35 |
|
| 36 |
assert ( |
| 37 |
"""Overwriting 'faq/two.html' with redirect to 'https://new.com/faq/two.html'.""" # noqa: E501 |
| 38 |
in status |
| 39 |
) |
| 40 |
|
| 41 |
assert ( |
| 42 |
app.outdir / "install.html" |
| 43 |
).read_text() == '<html><head><meta http-equiv="refresh" content="0; url=go-to-install"></head></html>' # noqa: E501 |
| 44 |
|
| 45 |
assert ( |
| 46 |
"""Overwriting 'install.html' with redirect to 'go-to-install'.""" # noqa: E501 |
| 47 |
in status |
| 48 |
) |
| 49 |
|
| 50 |
assert ( |
| 51 |
app.outdir / "setup.html" |
| 52 |
).read_text() == '<html><head><meta http-equiv="refresh" content="0; url=install.html"></head></html>' # noqa: E501 |
| 53 |
|
| 54 |
assert """Creating redirect 'setup.html' to 'install.html'.""" in status |
| 55 |
|
| 56 |
assert ( |
| 57 |
app.outdir / "install/requirements.html" |
| 58 |
).read_text() == '<html><head><meta http-equiv="refresh" content="0; url=https://web.com/docs/requirements.html"></head></html>' # noqa: E501 |
| 59 |
|
| 60 |
assert ( |
| 61 |
"""Creating redirect 'install/requirements.html' to 'https://web.com/docs/requirements.html'.""" # noqa: E501 |
| 62 |
in status |
| 63 |
) |
| 64 |
|
| 65 |
|
| 66 |
@pytest.mark.sphinx( |
| 67 |
"dirhtml", |
| 68 |
testroot="dirhtml", |
| 69 |
freshenv=True, |
| 70 |
confoverrides={ |
| 71 |
"redirects": { |
| 72 |
"index.html": "/newindex/", |
| 73 |
"index": "http://new.com/index", |
| 74 |
"install": "/installing/trailingslash", |
| 75 |
"install/": "/installing/trailingslash", |
| 76 |
"install/index": "/installing", |
| 77 |
"install/index.html": "/installing.html", |
| 78 |
} |
| 79 |
}, |
| 80 |
) |
| 81 |
def test_dirhtml(app: Sphinx, status, warning): |
| 82 |
app.build() |
| 83 |
status = status.getvalue() |
| 84 |
|
| 85 |
assert ( |
| 86 |
app.outdir / "index.html" |
| 87 |
).read_text() == '<html><head><meta http-equiv="refresh" content="0; url=http://new.com/index"></head></html>' # noqa: E501 |
| 88 |
|
| 89 |
assert ( |
| 90 |
"""Overwriting 'index.html' with redirect to '/newindex/'.""" # noqa: E501 |
| 91 |
in status |
| 92 |
) |
| 93 |
|
| 94 |
assert ( |
| 95 |
"""Overwriting 'index.html' with redirect to 'http://new.com/index'.""" # noqa: E501 |
| 96 |
in status |
| 97 |
) |
| 98 |
|
| 99 |
assert ( |
| 100 |
app.outdir / "install/index.html" |
| 101 |
).read_text() == '<html><head><meta http-equiv="refresh" content="0; url=/installing.html"></head></html>' # noqa: E501 |
| 102 |
|
| 103 |
assert ( |
| 104 |
"""Creating redirect 'install/index.html' to '/installing/trailingslash'.""" # noqa: E501 |
| 105 |
in status |
| 106 |
) |
| 107 |
|
| 108 |
assert ( |
| 109 |
"""Overwriting 'install/index.html' with redirect to '/installing/trailingslash'.""" # noqa: E501 |
| 110 |
in status |
| 111 |
) |
| 112 |
|
| 113 |
assert ( |
| 114 |
"""Overwriting 'install/index.html' with redirect to '/installing'.""" # noqa: E501 |
| 115 |
in status |
| 116 |
) |
| 117 |
|
| 118 |
assert ( |
| 119 |
"""Overwriting 'install/index.html' with redirect to '/installing.html'.""" # noqa: E501 |
| 120 |
in status |
| 121 |
) |
| 122 |
|
| 123 |
|
| 124 |
# Redirect sources starting with a slash are not allowed, use relative URIs |
| 125 |
@pytest.mark.sphinx( |
| 126 |
"html", |
| 127 |
testroot="redirects", |
| 128 |
freshenv=True, |
| 129 |
confoverrides={ |
| 130 |
"redirects": { |
| 131 |
"/index": "http://new.com/faq/", |
| 132 |
} |
| 133 |
}, |
| 134 |
) |
| 135 |
def test_invalid_uri(app: Sphinx, status, warning): |
| 136 |
with pytest.raises(ExtensionError) as excinfo: |
| 137 |
app.build() |
| 138 |
|
| 139 |
assert "for event 'html-collect-pages' threw an exception" in str( |
| 140 |
excinfo.value |
| 141 |
) # noqa: E501 |
| 142 |
assert any( |
| 143 |
[ |
| 144 |
# Newer python versions: |
| 145 |
"exception: '/index.html' is not in the subpath of" in str(excinfo.value), |
| 146 |
# Older python versions: |
| 147 |
"'/index.html' does not start with" in str(excinfo.value), |
| 148 |
] |
| 149 |
) |