Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rebuilds, add pre-commit #19

Merged
merged 4 commits into from
Aug 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ name: ci
on: [push, pull_request]

jobs:

check-format:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install Black
run: |
pip install black
- name: Run Black
run: |
black --check --diff .
- uses: pre-commit/action@v2.0.0

test:
name: "Build (${{ matrix.os }} Python ${{ matrix.python-version }})"
Expand Down
22 changes: 22 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Install pre-commit hooks via
# pre-commit install

repos:

# - repo: git://github.com/pre-commit/pre-commit-hooks
# rev: v2.2.3
# hooks:
# - id: check-json
# - id: check-yaml
# - id: end-of-file-fixer
# - id: trailing-whitespace

# - repo: https://gitlab.com/pycqa/flake8
# rev: 3.7.9
# hooks:
# - id: flake8

- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
20 changes: 20 additions & 0 deletions sphinxext/rediraffe.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def build_redirects(app: Sphinx, exception: Union[Exception, None]) -> None:
"""
Build amd write redirects
"""
try:
app.env.redirected
except AttributeError:
app.env.redirected = {}

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

if (
build_redirect_from.exists()
and src_redirect_from.as_posix() in app.env.redirected
):
# if it is still pointing to the same source, continue
if (
app.env.redirected[src_redirect_from.as_posix()]
== src_redirect_to.as_posix()
):
continue
# otherwise remove and rewrite
build_redirect_from.unlink()

if build_redirect_from.exists():
logger.warning(
f'{yellow("(broken)")} {redirect_from} redirects to {redirect_to} but {build_redirect_from} already exists!'
Expand Down Expand Up @@ -240,6 +257,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()


class CheckRedirectsDiffBuilder(Builder):
Expand Down
8 changes: 8 additions & 0 deletions tests/roots/ext/test-simple_rebuild/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extensions = ["sphinxext.rediraffe"]

master_doc = "index"
exclude_patterns = ["_build"]

html_theme = "basic"

rediraffe_redirects = "redirects.txt"
1 change: 1 addition & 0 deletions tests/roots/ext/test-simple_rebuild/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index File
1 change: 1 addition & 0 deletions tests/roots/ext/test-simple_rebuild/redirects.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
another.rst index.rst
8 changes: 8 additions & 0 deletions tests/test_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ def test_simple(self, app: Sphinx, ensure_redirect):
assert app.statuscode == 0
ensure_redirect("another.html", "index.html")

@pytest.mark.sphinx("html", testroot="simple_rebuild")
def test_simple_rebuild(self, app: Sphinx, ensure_redirect):
app.build()
assert app.statuscode == 0
app.build()
assert app.statuscode == 0
ensure_redirect("another.html", "index.html")

@pytest.mark.sphinx("html", testroot="no_cycle")
def test_no_cycle(self, app: Sphinx, ensure_redirect):
app.build()
Expand Down