Skip to content

Commit 84aa2a3

Browse files
Support rebuilds, add pre-commit (#19)
* Support rebuilds * Add pre-commit and CI * Add dirhtml test * Apply suggestions from code review Co-authored-by: Vasista Vovveti <[email protected]> Co-authored-by: Vasista Vovveti <[email protected]>
1 parent 0d8c92b commit 84aa2a3

File tree

6 files changed

+91
-7
lines changed

6 files changed

+91
-7
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@ name: ci
22
on: [push, pull_request]
33

44
jobs:
5+
56
check-format:
7+
68
runs-on: ubuntu-latest
9+
710
steps:
811
- uses: actions/checkout@v2
9-
- uses: actions/setup-python@v2
12+
- name: Set up Python 3.8
13+
uses: actions/setup-python@v2
1014
with:
1115
python-version: 3.8
12-
- name: Install Black
13-
run: |
14-
pip install black
15-
- name: Run Black
16-
run: |
17-
black --check --diff .
16+
- uses: pre-commit/[email protected]
1817

1918
test:
2019
name: "Build (${{ matrix.os }} Python ${{ matrix.python-version }})"

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,7 @@ $RECYCLE.BIN/
219219
# Windows shortcuts
220220
*.lnk
221221

222+
# VS Code config
223+
.vscode/
224+
222225
# End of https://www.toptal.com/developers/gitignore/api/python,linux,windows,macos,visualstudiocode

.pre-commit-config.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Install pre-commit hooks via
2+
# pre-commit install
3+
4+
repos:
5+
6+
# - repo: git://github.com/pre-commit/pre-commit-hooks
7+
# rev: v2.2.3
8+
# hooks:
9+
# - id: check-json
10+
# - id: check-yaml
11+
# - id: end-of-file-fixer
12+
# - id: trailing-whitespace
13+
14+
# - repo: https://gitlab.com/pycqa/flake8
15+
# rev: 3.7.9
16+
# hooks:
17+
# - id: flake8
18+
19+
- repo: https://github.com/psf/black
20+
rev: 20.8b1
21+
hooks:
22+
- id: black

sphinxext/rediraffe.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ def build_redirects(app: Sphinx, exception: Union[Exception, None]) -> None:
117117
"""
118118
Build amd write redirects
119119
"""
120+
try:
121+
app.env.redirected
122+
except AttributeError:
123+
app.env.redirected = {}
120124

121125
if exception != None:
122126
return
@@ -208,6 +212,19 @@ def build_redirects(app: Sphinx, exception: Union[Exception, None]) -> None:
208212
build_redirect_from = Path(app.outdir) / redirect_from
209213
build_redirect_to = Path(app.outdir) / redirect_to
210214

215+
if (
216+
build_redirect_from.exists()
217+
and src_redirect_from.as_posix() in app.env.redirected
218+
):
219+
# if it is still pointing to the same source, continue
220+
if (
221+
app.env.redirected[src_redirect_from.as_posix()]
222+
== src_redirect_to.as_posix()
223+
):
224+
continue
225+
# otherwise remove and rewrite
226+
build_redirect_from.unlink()
227+
211228
if build_redirect_from.exists():
212229
logger.warning(
213230
f'{yellow("(broken)")} {redirect_from} redirects to {redirect_to} but {build_redirect_from} already exists!'
@@ -240,6 +257,9 @@ def build_redirects(app: Sphinx, exception: Union[Exception, None]) -> None:
240257
logger.info(
241258
f'{green("(good)")} {redirect_from} {green("-->")} {redirect_to}'
242259
)
260+
app.env.redirected[
261+
src_redirect_from.as_posix()
262+
] = src_redirect_to.as_posix()
243263

244264

245265
class CheckRedirectsDiffBuilder(Builder):

tests/test_ext.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from sphinx.application import Sphinx
66
from sphinx.errors import ExtensionError
77
from pathlib import Path
8+
import shutil
89
import logging
910

1011
from conftest import rel2url
@@ -27,6 +28,16 @@ def test_simple(self, app: Sphinx, ensure_redirect):
2728
assert app.statuscode == 0
2829
ensure_redirect("another.html", "index.html")
2930

31+
@pytest.mark.sphinx("html", testroot="simple")
32+
def test_simple_rebuild(self, app: Sphinx, ensure_redirect):
33+
if Path(app.outdir).exists():
34+
shutil.rmtree(Path(app.outdir))
35+
app.build()
36+
assert app.statuscode == 0
37+
app.build()
38+
assert app.statuscode == 0
39+
ensure_redirect("another.html", "index.html")
40+
3041
@pytest.mark.sphinx("html", testroot="no_cycle")
3142
def test_no_cycle(self, app: Sphinx, ensure_redirect):
3243
app.build()
@@ -211,6 +222,16 @@ def test_simple(self, app: Sphinx, ensure_redirect):
211222
assert app.statuscode == 0
212223
ensure_redirect("another/index.html", "index.html")
213224

225+
@pytest.mark.sphinx("dirhtml", testroot="simple", freshenv=False)
226+
def test_simple_rebuild(self, app: Sphinx, ensure_redirect):
227+
if Path(app.outdir).exists():
228+
shutil.rmtree(Path(app.outdir))
229+
app.build()
230+
assert app.statuscode == 0
231+
app.build()
232+
assert app.statuscode == 0
233+
ensure_redirect("another/index.html", "index.html")
234+
214235
@pytest.mark.sphinx("dirhtml", testroot="no_cycle")
215236
def test_no_cycle(self, app: Sphinx, ensure_redirect):
216237
app.build()

tox.ini

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# To use tox, see https://tox.readthedocs.io
2+
# Simply pip or conda install tox
3+
# If you use conda, you may also want to install tox-conda
4+
# then run `tox` or `tox -- {pytest args}`
5+
# To run in parallel using `tox -p` (this does not appear to work for this repo)
6+
7+
# To rebuild the tox environment, for example when dependencies change, use
8+
# `tox -r`
9+
10+
# Note: if the following error is encountered: `ImportError while loading conftest`
11+
# then then deleting compiled files has been found to fix it: `find . -name \*.pyc -delete`
12+
13+
[tox]
14+
envlist = py{36,37,38}
15+
16+
[testenv:py{36,37,38}]
17+
; recreate = false
18+
deps = -rtest-requirements.txt
19+
commands = pytest {posargs}

0 commit comments

Comments
 (0)