Skip to content

Commit

Permalink
Fixing KeyError when banner main ref fails.
Browse files Browse the repository at this point in the history
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 #27
  • Loading branch information
Robpol86 committed Dec 10, 2016
1 parent d79edd5 commit 14e37d5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ Added
* Time value of ``html_last_updated_fmt`` will be the last git commit (authored) date.

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

2.2.0 - 2016-09-15
------------------
Expand Down
4 changes: 4 additions & 0 deletions sphinxcontrib/versioning/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ def build(config, rel_source, destination, **options):
# Pre-build.
log.info("Pre-running Sphinx to collect versions' master_doc and other info.")
exported_root = pre_build(config.git_root, versions)
if config.banner_main_ref and config.banner_main_ref not in [r['name'] for r in versions.remotes]:
log.warning('Banner main ref %s failed during pre-run. Disabling banner.', config.banner_main_ref)
config.update(dict(banner_greatest_tag=False, banner_main_ref=None, banner_recent_tag=False, show_banner=False),
overwrite=True)

# Build.
build_all(exported_root, destination, versions)
Expand Down
23 changes: 23 additions & 0 deletions tests/test__main__/test_main_build_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,3 +671,26 @@ def test_error_bad_root_ref(tmpdir, local_docs):
with pytest.raises(CalledProcessError) as exc:
pytest.run(local_docs, ['sphinx-versioning', '-N', '-v', 'build', '.', str(tmpdir), '-r', 'unknown'])
assert 'Root ref unknown not found in: master' in exc.value.output


def test_bad_banner(banner, local_docs):
"""Test bad banner main ref.
:param banner: conftest fixture.
:param local_docs: conftest fixture.
"""
pytest.run(local_docs, ['git', 'checkout', '-b', 'stable', 'master'])
local_docs.join('conf.py').write('bad\n', mode='a')
pytest.run(local_docs, ['git', 'commit', '-am', 'Breaking stable.'])
pytest.run(local_docs, ['git', 'push', 'origin', 'stable'])

# Run.
destination = local_docs.ensure_dir('..', 'destination')
args = ['--show-banner', '--banner-main-ref', 'stable']
output = pytest.run(local_docs, ['sphinx-versioning', 'build', '.', str(destination)] + args)
assert 'KeyError' not in output

# Check no banner.
assert 'Banner main ref is: stable' in output
assert 'Banner main ref stable failed during pre-run.' in output
banner(destination.join('contents.html'), None)

0 comments on commit 14e37d5

Please sign in to comment.