Skip to content

Commit

Permalink
Improve version testing (closes #52)
Browse files Browse the repository at this point in the history
  • Loading branch information
avalentino committed Sep 26, 2020
1 parent a81bfca commit 9a03dd6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Bundled liberfa version update to v1.7.1.
- Improved the setup machinery to ensure a proper configuration of the
embedded liberfa (see also https://github.com/liberfa/erfa/pull/73).
- Improve version testing (see gh-52).


1.7 (2020-05-31)
Expand Down
32 changes: 30 additions & 2 deletions erfa/tests/test_erfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ def __init__(self, *args, **kwargs):
pass


def embedded_liberfa(path=erfa.ufunc.__file__):
import platform
import subprocess

lddcmd = ['ldd']
if platform.system() == 'Darwin':
lddcmd = ['otool', '-L']
elif platform.system() == 'Windows':
# lddcmd = [''] # TODO
return None

lddcmd.append(path)
try:
out = subprocess.run(lddcmd, check=True,
encoding='utf-8', stdout=subprocess.PIPE)
# capture_output=True, text=True) # Python 3.7
except (subprocess.SubprocessError, OSError):
return None
else:
return 'erfa' not in out.stdout


class TestVersion:
def test_erfa_version(self):
assert hasattr(erfa.version, 'erfa_version')
Expand All @@ -38,9 +60,15 @@ def test_version(self):
assert hasattr(erfa, '__version__')
version = erfa.__version__
assert version is erfa.version.version

@pytest.mark.skipif(not embedded_liberfa(), reason='system liberfa')
def test_version_with_embedded_liberfa(self):
# Oops, we had the wrong version for quite a while...
assert (erfa.version.erfa_version == '1.6.0' and version.startswith('1.7.0')
or version.startswith(erfa.version.erfa_version))
version = erfa.__version__
if erfa.version.erfa_version == '1.6.0':
assert version == '1.7.0'
else:
assert version.startswith(erfa.version.erfa_version)


def test_erfa_wrapper():
Expand Down
77 changes: 48 additions & 29 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import os
import re
import sys
import inspect
import textwrap
import functools
import setuptools
import subprocess
from warnings import warn
Expand Down Expand Up @@ -131,7 +134,42 @@ def get_extensions():
return [erfa_ext]


VERSION_TEMPLATE = """
def _guess_next_dev(version, liberfadir=None):
from setuptools_scm import git
from setuptools_scm.version import guess_next_version

if liberfadir is None:
import pathlib
liberfadir = pathlib.Path(__file__).parent.parent / 'liberfa' / 'erfa'

erfa_version = git.parse(liberfadir)
if not erfa_version.exact:
warn(f'liberfa/erfa not at a tagged release, but at {erfa_version}')

erfa_tag = erfa_version.format_with("{tag}")
version_string = str(version.tag)

if version.exact:
if not version_string.startswith(erfa_tag):
warn(f'tag {version_string} does not start with liberfa/erfa tag {erfa_tag}')

return version_string

else:
if erfa_tag > version_string:
guessed = erfa_tag
elif 'dev' in version_string or len(version_string.split('.')) > 3:
return guess_next_version(version.tag)
else:
guessed = version_string.partition("+")[0] + '.1'
return version.format_with("{guessed}.dev{distance}", guessed=guessed)


code = textwrap.indent(inspect.getsource(_guess_next_dev), ' ')
escaped_code = code.replace('{', '{{').replace('}', '}}')


VERSION_TEMPLATE = f"""
'''Wrapper, ERFA and SOFA version information.'''
# Set the version numbers a bit indirectly, so that Sphinx can pick up
Expand All @@ -147,45 +185,26 @@ def get_extensions():
del ufunc
# Note that we need to fall back to the hard-coded version if either
# setuptools_scm can't be imported or setuptools_scm can't determine the
# version, so we catch the generic 'Exception'.
try:
from setuptools_scm import get_version
version = get_version(root='..', relative_to=__file__)
{escaped_code}
version = get_version(root='..', version_scheme=_guess_next_dev,
relative_to=__file__)
'''Version of the python wrappers.'''
except Exception:
version = '{version}'
version = '{{version}}'
else:
del get_version
del get_version, _guess_next_dev
""".lstrip()


def guess_next_dev(version):
from setuptools_scm import git
from setuptools_scm.version import guess_next_version

erfa_version = git.parse(LIBERFADIR)
if not erfa_version.exact:
warn(f'liberfa/erfa not at a tagged release, but at {erfa_version}')

erfa_tag = erfa_version.format_with("{tag}")
version_string = str(version.tag)

if version.exact:
if not version_string.startswith(erfa_tag):
warn(f'tag {version_string} does not start with liberfa/erfa tag {erfa_tag}')

return version_string

else:
if erfa_tag > version_string:
guessed = erfa_tag
elif 'dev' in version_string or len(version_string.split('.')) > 3:
return guess_next_version(version.tag)
else:
guessed = version_string.partition("+")[0] + '.1'
return version.format_with("{guessed}.dev{distance}", guessed=guessed)
guess_next_dev = functools.partial(_guess_next_dev, liberfadir=LIBERFADIR)


use_scm_version = {
Expand Down

0 comments on commit 9a03dd6

Please sign in to comment.