Skip to content

Commit

Permalink
Fix re-build (#24)
Browse files Browse the repository at this point in the history
* Fix re-build

* Update test_ext.py

* rename JSON
  • Loading branch information
chrisjsewell authored Sep 1, 2020
1 parent e431b90 commit 012d76a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
22 changes: 12 additions & 10 deletions sphinxext/rediraffe.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from os import rename
import re
import subprocess
Expand Down Expand Up @@ -36,7 +37,7 @@
"""
)

REDIRECT_JSON_NAME = "_rediraffe_redirected.json"
RE_OBJ = re.compile(r"(?:(\"|')(.*?)\1|(\S+))\s+(?:(\"|')(.*?)\4|(\S+))")

READTHEDOCS_BUILDERS = ["readthedocs", "readthedocsdirhtml"]
Expand Down Expand Up @@ -124,10 +125,11 @@ def build_redirects(app: Sphinx, exception: Union[Exception, None]) -> None:
"""
Build amd write redirects
"""
try:
app.env.redirected
except AttributeError:
app.env.redirected = {}
redirect_json_file = Path(app.outdir) / REDIRECT_JSON_NAME
if redirect_json_file.exists():
redirect_record = json.loads(redirect_json_file.read_text("utf8"))
else:
redirect_record = {}

if exception != None:
return
Expand Down Expand Up @@ -224,11 +226,11 @@ def build_redirects(app: Sphinx, exception: Union[Exception, None]) -> None:

if (
build_redirect_from.exists()
and src_redirect_from.as_posix() in app.env.redirected
and src_redirect_from.as_posix() in redirect_record
):
# if it is still pointing to the same source, continue
if (
app.env.redirected[src_redirect_from.as_posix()]
redirect_record[src_redirect_from.as_posix()]
== src_redirect_to.as_posix()
):
continue
Expand Down Expand Up @@ -267,9 +269,9 @@ def build_redirects(app: Sphinx, exception: Union[Exception, None]) -> None:
logger.info(
f'{green("(good)")} {redirect_from} {green("-->")} {redirect_to}'
)
app.env.redirected[
src_redirect_from.as_posix()
] = src_redirect_to.as_posix()
redirect_record[src_redirect_from.as_posix()] = src_redirect_to.as_posix()

redirect_json_file.write_text(json.dumps(redirect_record), encoding="utf8")


class CheckRedirectsDiffBuilder(Builder):
Expand Down
18 changes: 12 additions & 6 deletions tests/test_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ def test_simple(self, app: Sphinx, ensure_redirect):
ensure_redirect("another.html", "index.html")

@pytest.mark.sphinx("html", testroot="simple")
def test_simple_rebuild(self, app: Sphinx, ensure_redirect):
def test_simple_rebuild(self, app_params, make_app, ensure_redirect):
args, kwargs = app_params
app = make_app(*args, **kwargs)
if Path(app.outdir).exists():
shutil.rmtree(Path(app.outdir))
app.build()
assert app.statuscode == 0
app.build()
assert app.statuscode == 0
app2 = make_app(*args, **kwargs)
app2.build()
assert app2.statuscode == 0
ensure_redirect("another.html", "index.html")

@pytest.mark.sphinx("html", testroot="no_cycle")
Expand Down Expand Up @@ -272,13 +275,16 @@ def test_simple(self, app: Sphinx, ensure_redirect):
ensure_redirect("another/index.html", "index.html")

@pytest.mark.sphinx("dirhtml", testroot="simple", freshenv=False)
def test_simple_rebuild(self, app: Sphinx, ensure_redirect):
def test_simple_rebuild(self, app_params, make_app, ensure_redirect):
args, kwargs = app_params
app = make_app(*args, **kwargs)
if Path(app.outdir).exists():
shutil.rmtree(Path(app.outdir))
app.build()
assert app.statuscode == 0
app.build()
assert app.statuscode == 0
app2 = make_app(*args, **kwargs)
app2.build()
assert app2.statuscode == 0
ensure_redirect("another/index.html", "index.html")

@pytest.mark.sphinx("dirhtml", testroot="no_cycle")
Expand Down

0 comments on commit 012d76a

Please sign in to comment.