From 14e37d50882aee987c194e8252ccf51d69dcdaeb Mon Sep 17 00:00:00 2001 From: Robpol86 Date: Fri, 9 Dec 2016 22:36:38 -0800 Subject: [PATCH] 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 https://github.com/Robpol86/sphinxcontrib-versioning/issues/27 --- README.rst | 2 ++ sphinxcontrib/versioning/__main__.py | 4 ++++ .../test__main__/test_main_build_scenarios.py | 23 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/README.rst b/README.rst index 17d17c370..79c2cd98f 100644 --- a/README.rst +++ b/README.rst @@ -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 ------------------ diff --git a/sphinxcontrib/versioning/__main__.py b/sphinxcontrib/versioning/__main__.py index e3a8220a4..0608b27b7 100755 --- a/sphinxcontrib/versioning/__main__.py +++ b/sphinxcontrib/versioning/__main__.py @@ -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) diff --git a/tests/test__main__/test_main_build_scenarios.py b/tests/test__main__/test_main_build_scenarios.py index 9554f3aa5..b1eb01ce1 100644 --- a/tests/test__main__/test_main_build_scenarios.py +++ b/tests/test__main__/test_main_build_scenarios.py @@ -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)