Skip to content
This repository was archived by the owner on Jun 11, 2019. It is now read-only.

Commit 14e37d5

Browse files
committed
Fixing KeyError when banner main ref fails.
If banner main ref exists but fails during pre-build (which then gets removed) an unhandled exception would be raised. Handling this by checking before and after pre_build() if banner main ref exists. Fixes sphinx-contrib#27
1 parent d79edd5 commit 14e37d5

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ Added
5656
* Time value of ``html_last_updated_fmt`` will be the last git commit (authored) date.
5757

5858
Fixed
59+
* Unhandled KeyError exception when banner main ref fails pre-build.
5960
* https://github.com/Robpol86/sphinxcontrib-versioning/issues/26
61+
* https://github.com/Robpol86/sphinxcontrib-versioning/issues/27
6062

6163
2.2.0 - 2016-09-15
6264
------------------

sphinxcontrib/versioning/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ def build(config, rel_source, destination, **options):
302302
# Pre-build.
303303
log.info("Pre-running Sphinx to collect versions' master_doc and other info.")
304304
exported_root = pre_build(config.git_root, versions)
305+
if config.banner_main_ref and config.banner_main_ref not in [r['name'] for r in versions.remotes]:
306+
log.warning('Banner main ref %s failed during pre-run. Disabling banner.', config.banner_main_ref)
307+
config.update(dict(banner_greatest_tag=False, banner_main_ref=None, banner_recent_tag=False, show_banner=False),
308+
overwrite=True)
305309

306310
# Build.
307311
build_all(exported_root, destination, versions)

tests/test__main__/test_main_build_scenarios.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,3 +671,26 @@ def test_error_bad_root_ref(tmpdir, local_docs):
671671
with pytest.raises(CalledProcessError) as exc:
672672
pytest.run(local_docs, ['sphinx-versioning', '-N', '-v', 'build', '.', str(tmpdir), '-r', 'unknown'])
673673
assert 'Root ref unknown not found in: master' in exc.value.output
674+
675+
676+
def test_bad_banner(banner, local_docs):
677+
"""Test bad banner main ref.
678+
679+
:param banner: conftest fixture.
680+
:param local_docs: conftest fixture.
681+
"""
682+
pytest.run(local_docs, ['git', 'checkout', '-b', 'stable', 'master'])
683+
local_docs.join('conf.py').write('bad\n', mode='a')
684+
pytest.run(local_docs, ['git', 'commit', '-am', 'Breaking stable.'])
685+
pytest.run(local_docs, ['git', 'push', 'origin', 'stable'])
686+
687+
# Run.
688+
destination = local_docs.ensure_dir('..', 'destination')
689+
args = ['--show-banner', '--banner-main-ref', 'stable']
690+
output = pytest.run(local_docs, ['sphinx-versioning', 'build', '.', str(destination)] + args)
691+
assert 'KeyError' not in output
692+
693+
# Check no banner.
694+
assert 'Banner main ref is: stable' in output
695+
assert 'Banner main ref stable failed during pre-run.' in output
696+
banner(destination.join('contents.html'), None)

0 commit comments

Comments
 (0)