Skip to content

Commit ee2df60

Browse files
nedbatAA-Turner
andauthored
Fix rediraffecheckdiff when using a dict for rediraffe_redirects (#64)
Co-authored-by: Adam Turner <[email protected]>
1 parent bba8fc4 commit ee2df60

File tree

7 files changed

+63
-28
lines changed

7 files changed

+63
-28
lines changed

sphinxext/rediraffe.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -399,39 +399,41 @@ def abs_path_in_src_dir_w_src_suffix(filename: str) -> Path | None:
399399
logger.error(err_msg)
400400
self.app.statuscode = 1
401401

402-
with redirects_path.open('a', encoding='utf-8') as redirects_file:
403-
for renamed_file in rename_hints:
404-
hint_to, perc = rename_hints[renamed_file]
402+
for renamed_file in rename_hints:
403+
hint_to, perc = rename_hints[renamed_file]
405404

406-
if renamed_file in absolute_redirects:
405+
if renamed_file in absolute_redirects:
406+
logger.info(
407+
'renamed file %s redirects to %s.',
408+
renamed_file,
409+
absolute_redirects[renamed_file],
410+
)
411+
continue
412+
413+
if self.name == 'rediraffewritediff':
414+
if perc >= self.app.config.rediraffe_auto_redirect_perc:
415+
rel_rename_from = (
416+
f'"{str(PurePosixPath(renamed_file.relative_to(src_path)))}"'
417+
)
418+
rel_rename_to = (
419+
f'"{str(PurePosixPath(hint_to.relative_to(src_path)))}"'
420+
)
421+
with redirects_path.open('a', encoding='utf-8') as redirects_file:
422+
redirects_file.write(f'{rel_rename_from} {rel_rename_to}\n')
407423
logger.info(
408-
'renamed file %s redirects to %s.',
409-
renamed_file,
410-
absolute_redirects[renamed_file],
424+
'%s Renamed file %s has been redirected to %s in your redirects file!',
425+
green('(okay)'),
426+
rel_rename_from,
427+
rel_rename_to,
411428
)
412429
continue
413430

414-
if self.name == 'rediraffewritediff':
415-
if perc >= self.app.config.rediraffe_auto_redirect_perc:
416-
rel_rename_from = f'"{str(PurePosixPath(renamed_file.relative_to(src_path)))}"'
417-
rel_rename_to = (
418-
f'"{str(PurePosixPath(hint_to.relative_to(src_path)))}"'
419-
)
420-
redirects_file.write(f'{rel_rename_from} {rel_rename_to}\n')
421-
logger.info(
422-
'%s Renamed file %s has been redirected to %s in your redirects file!',
423-
green('(okay)'),
424-
rel_rename_from,
425-
rel_rename_to,
426-
)
427-
continue
428-
429-
err_msg = (
430-
f'{red("(broken)")} {renamed_file} was deleted but is not redirected!'
431-
f' Hint: This file was renamed to {hint_to} with a similarity of {perc}%.'
432-
)
433-
logger.error(err_msg)
434-
self.app.statuscode = 1
431+
err_msg = (
432+
f'{red("(broken)")} {renamed_file} was deleted but is not redirected!'
433+
f' Hint: This file was renamed to {hint_to} with a similarity of {perc}%.'
434+
)
435+
logger.error(err_msg)
436+
self.app.statuscode = 1
435437

436438
def get_outdated_docs(self):
437439
return []
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from __future__ import annotations
2+
3+
extensions = ['sphinxext.rediraffe']
4+
5+
master_doc = 'index'
6+
exclude_patterns = ['_build']
7+
8+
html_theme = 'basic'
9+
10+
rediraffe_branch = 'HEAD~1'
11+
rediraffe_redirects = {
12+
'another.rst': 'index.rst',
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
index file
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
another file
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from __future__ import annotations
2+
3+
extensions = ['sphinxext.rediraffe']
4+
5+
master_doc = 'index'
6+
exclude_patterns = ['_build']
7+
8+
html_theme = 'basic'
9+
10+
rediraffe_branch = 'HEAD~1'
11+
rediraffe_redirects = {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
index file

tests/test_builder.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,9 @@ def test_builder_deleted_file_redirected_commit(app_init_repo):
8585
def test_builder_deleted_file_not_redirected_commit(app_init_repo):
8686
app_init_repo.build()
8787
assert app_init_repo.statuscode == 1
88+
89+
90+
@pytest.mark.sphinx('rediraffecheckdiff', testroot='deleted_file_redirected_dict')
91+
def test_builder_deleted_file_redirected_dict(app_init_repo):
92+
app_init_repo.build()
93+
assert app_init_repo.statuscode == 0

0 commit comments

Comments
 (0)