Skip to content

Commit 012d76a

Browse files
authored
Fix re-build (#24)
* Fix re-build * Update test_ext.py * rename JSON
1 parent e431b90 commit 012d76a

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

sphinxext/rediraffe.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from os import rename
23
import re
34
import subprocess
@@ -36,7 +37,7 @@
3637
3738
"""
3839
)
39-
40+
REDIRECT_JSON_NAME = "_rediraffe_redirected.json"
4041
RE_OBJ = re.compile(r"(?:(\"|')(.*?)\1|(\S+))\s+(?:(\"|')(.*?)\4|(\S+))")
4142

4243
READTHEDOCS_BUILDERS = ["readthedocs", "readthedocsdirhtml"]
@@ -124,10 +125,11 @@ def build_redirects(app: Sphinx, exception: Union[Exception, None]) -> None:
124125
"""
125126
Build amd write redirects
126127
"""
127-
try:
128-
app.env.redirected
129-
except AttributeError:
130-
app.env.redirected = {}
128+
redirect_json_file = Path(app.outdir) / REDIRECT_JSON_NAME
129+
if redirect_json_file.exists():
130+
redirect_record = json.loads(redirect_json_file.read_text("utf8"))
131+
else:
132+
redirect_record = {}
131133

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

225227
if (
226228
build_redirect_from.exists()
227-
and src_redirect_from.as_posix() in app.env.redirected
229+
and src_redirect_from.as_posix() in redirect_record
228230
):
229231
# if it is still pointing to the same source, continue
230232
if (
231-
app.env.redirected[src_redirect_from.as_posix()]
233+
redirect_record[src_redirect_from.as_posix()]
232234
== src_redirect_to.as_posix()
233235
):
234236
continue
@@ -267,9 +269,9 @@ def build_redirects(app: Sphinx, exception: Union[Exception, None]) -> None:
267269
logger.info(
268270
f'{green("(good)")} {redirect_from} {green("-->")} {redirect_to}'
269271
)
270-
app.env.redirected[
271-
src_redirect_from.as_posix()
272-
] = src_redirect_to.as_posix()
272+
redirect_record[src_redirect_from.as_posix()] = src_redirect_to.as_posix()
273+
274+
redirect_json_file.write_text(json.dumps(redirect_record), encoding="utf8")
273275

274276

275277
class CheckRedirectsDiffBuilder(Builder):

tests/test_ext.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ def test_simple(self, app: Sphinx, ensure_redirect):
2929
ensure_redirect("another.html", "index.html")
3030

3131
@pytest.mark.sphinx("html", testroot="simple")
32-
def test_simple_rebuild(self, app: Sphinx, ensure_redirect):
32+
def test_simple_rebuild(self, app_params, make_app, ensure_redirect):
33+
args, kwargs = app_params
34+
app = make_app(*args, **kwargs)
3335
if Path(app.outdir).exists():
3436
shutil.rmtree(Path(app.outdir))
3537
app.build()
3638
assert app.statuscode == 0
37-
app.build()
38-
assert app.statuscode == 0
39+
app2 = make_app(*args, **kwargs)
40+
app2.build()
41+
assert app2.statuscode == 0
3942
ensure_redirect("another.html", "index.html")
4043

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

274277
@pytest.mark.sphinx("dirhtml", testroot="simple", freshenv=False)
275-
def test_simple_rebuild(self, app: Sphinx, ensure_redirect):
278+
def test_simple_rebuild(self, app_params, make_app, ensure_redirect):
279+
args, kwargs = app_params
280+
app = make_app(*args, **kwargs)
276281
if Path(app.outdir).exists():
277282
shutil.rmtree(Path(app.outdir))
278283
app.build()
279284
assert app.statuscode == 0
280-
app.build()
281-
assert app.statuscode == 0
285+
app2 = make_app(*args, **kwargs)
286+
app2.build()
287+
assert app2.statuscode == 0
282288
ensure_redirect("another/index.html", "index.html")
283289

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

0 commit comments

Comments
 (0)