From 0d5d2768ba7727977eaa9c750c067c60fa667631 Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Tue, 28 Jan 2020 12:40:36 +0000 Subject: [PATCH] Don't show astropy-helpers version in packages that don't use it --- CHANGES.rst | 5 +++++ pytest_astropy_header/display.py | 29 +++++++++++++++--------- tests/test_display.py | 38 ++++++++++++++++++++++++-------- 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index d215057..5d8af5f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,8 @@ +0.1.3 (unreleased) +================== + +- Don't show astropy-helpers version in packages that don't use it. [#16] + 0.1.2 (2019-12-18) ================== diff --git a/pytest_astropy_header/display.py b/pytest_astropy_header/display.py index 48c8a22..e23d820 100644 --- a/pytest_astropy_header/display.py +++ b/pytest_astropy_header/display.py @@ -8,10 +8,14 @@ import sys import datetime import locale -import math from collections import OrderedDict from distutils.version import LooseVersion +if sys.version_info[0] >= 3: + import builtins +else: + import __builtin__ as builtins + PYTEST_HEADER_MODULES = OrderedDict([('Numpy', 'numpy'), ('Scipy', 'scipy'), ('Matplotlib', 'matplotlib'), @@ -157,17 +161,20 @@ def pytest_report_header(config): version = 'unknown (no __version__ attribute)' s += "{module_display}: {version}\n".format(module_display=module_display, version=version) - # Helpers version - if 'astropy_helpers' in TESTED_VERSIONS: - astropy_helpers_version = TESTED_VERSIONS['astropy_helpers'] - else: - try: - from astropy.version import astropy_helpers_version - except ImportError: - astropy_helpers_version = None + # Show the astropy-helpers version, if appropriate. We only show this if + # the _ASTROPY_SETUP_ variable is set since this indicates an old-style + # setup.py that is usually associated with astropy-helpers + if getattr(builtins, '_ASTROPY_SETUP_', False): + if 'astropy_helpers' in TESTED_VERSIONS: + astropy_helpers_version = TESTED_VERSIONS['astropy_helpers'] + else: + try: + from astropy.version import astropy_helpers_version + except ImportError: + astropy_helpers_version = None - if astropy_helpers_version: - s += "astropy-helpers: {astropy_helpers_version}\n".format(astropy_helpers_version=astropy_helpers_version) + if astropy_helpers_version: + s += "astropy-helpers: {astropy_helpers_version}\n".format(astropy_helpers_version=astropy_helpers_version) s += "\n" diff --git a/tests/test_display.py b/tests/test_display.py index 0525a35..14fc2f5 100644 --- a/tests/test_display.py +++ b/tests/test_display.py @@ -1,7 +1,14 @@ +import sys import pytest import numpy +if sys.version_info[0] >= 3: + import builtins +else: + import __builtin__ as builtins + + NUMPY_VERSION = numpy.__version__ pytest_plugins = ['pytester'] @@ -42,7 +49,24 @@ def test_enabled(testdir, capsys, method): def pytest_configure(config): config.option.astropy_header = True """) - testdir.inline_run() + testdir.inline_run() + out, err = capsys.readouterr() + lines = extract_package_version_lines(out) + assert len(lines) == 5 + assert lines[0].startswith('Numpy: ') + assert lines[1].startswith('Scipy: ') + assert lines[2].startswith('Matplotlib: ') + assert lines[3].startswith('h5py: ') + assert lines[4].startswith('Pandas: ') + + + +def test_astropy_helpers(testdir, capsys): + try: + builtins._ASTROPY_SETUP_ = True + testdir.inline_run("--astropy-header") + finally: + del builtins._ASTROPY_SETUP_ out, err = capsys.readouterr() lines = extract_package_version_lines(out) assert len(lines) == 6 @@ -100,9 +124,8 @@ def pytest_configure(config): testdir.inline_run() out, err = capsys.readouterr() lines = extract_package_version_lines(out) - assert len(lines) == 2 + assert len(lines) == 1 assert lines[0] == 'numpy: {NUMPY_VERSION}'.format(NUMPY_VERSION=NUMPY_VERSION) - assert lines[1].startswith('astropy-helpers: ') @pytest.mark.parametrize('method', ['cli', 'ini', 'ini_list', 'conftest']) @@ -135,10 +158,9 @@ def pytest_configure(config): out, err = capsys.readouterr() print(out) lines = extract_package_version_lines(out) - assert len(lines) == 3 + assert len(lines) == 2 assert lines[0] == 'numpy: {NUMPY_VERSION}'.format(NUMPY_VERSION=NUMPY_VERSION) assert lines[1].startswith('pandas') - assert lines[2].startswith('astropy-helpers: ') @pytest.mark.parametrize('method', ['cli', 'ini', 'ini_list', 'conftest']) @@ -169,9 +191,8 @@ def pytest_configure(config): testdir.inline_run() out, err = capsys.readouterr() lines = extract_package_version_lines(out) - assert len(lines) == 2 + assert len(lines) == 1 assert lines[0] == 'apackagethatdoesnotexist: not available' - assert lines[1].startswith('astropy-helpers: ') def test_modify_in_conftest(testdir, capsys): @@ -188,11 +209,10 @@ def pytest_configure(config): out, err = capsys.readouterr() assert err == '' lines = extract_package_version_lines(out) - assert len(lines) == 6 + assert len(lines) == 5 assert lines[0].startswith('Numpy: ') assert lines[1].startswith('Scipy: ') assert lines[2].startswith('Matplotlib: ') assert lines[3].startswith('h5py: ') assert lines[4].startswith('scikit-image: ') - assert lines[5].startswith('astropy-helpers: ') assert 'Running tests with fakepackage version 1.0.2' in out