Skip to content

Commit 546604a

Browse files
authored
Merge pull request astropy#330 from drdavella/fix-warnings
Fix warnings
2 parents aaa4506 + cf4ded0 commit 546604a

4 files changed

Lines changed: 65 additions & 14 deletions

File tree

‎CHANGES.rst‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ astropy-helpers Changelog
1313
2.0.2 (unreleased)
1414
------------------
1515

16-
- Nothing changed yet.
16+
- The function ``get_git_devstr`` now returns ``'0'`` instead of ``None`` when
17+
no git repository is present. This allows generation of development version
18+
strings that are in a format that ``setuptools`` expects (e.g. "1.1.3.dev0"
19+
instead of "1.1.3.dev"). [#330]
1720

1821

1922
2.0.1 (2017-07-28)

‎astropy_helpers/git_helpers.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ def get_git_devstr(sha=False, show_warning=True, path=None):
9090

9191
if path is None:
9292
path = os.getcwd()
93-
if not _get_repo_path(path, levels=0):
94-
return ''
9593

9694
if not os.path.isdir(path):
9795
path = os.path.abspath(os.path.dirname(path))
@@ -135,7 +133,12 @@ def run_git(cmd):
135133

136134
returncode, stdout, stderr = run_git(cmd)
137135

138-
if not sha and returncode == 129:
136+
if not sha and returncode == 128:
137+
# git returns 128 if the command is not run from within a git
138+
# repository tree. In this case, a warning is produced above but we
139+
# return the default dev version of '0'.
140+
return '0'
141+
elif not sha and returncode == 129:
139142
# git returns 129 if a command option failed to parse; in
140143
# particular this could happen in git versions older than 1.7.2
141144
# where the --count option is not supported
@@ -155,7 +158,9 @@ def run_git(cmd):
155158
return _decode_stdio(stdout).strip()
156159

157160

158-
def _get_repo_path(pathname, levels=None):
161+
# This function is tested but it is only ever executed within a subprocess when
162+
# creating a fake package, so it doesn't get picked up by coverage metrics.
163+
def _get_repo_path(pathname, levels=None): # pragma: no cover
159164
"""
160165
Given a file or directory name, determine the root of the git repository
161166
this path is under. If given, this won't look any higher than ``levels``

‎astropy_helpers/tests/test_git_helpers.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import tarfile
88

99
import pytest
10+
from warnings import catch_warnings
1011

1112
from . import reset_setup_helpers, reset_distutils_log, fix_hide_setuptools # noqa
1213
from . import run_cmd, run_setup, cleanup_import
14+
from astropy_helpers.git_helpers import get_git_devstr
1315

1416

1517
PY3 = sys.version_info[0] == 3
@@ -223,7 +225,9 @@ def test_installed_git_version(version_test_package, version, tmpdir, capsys):
223225
with build_dir.as_cwd():
224226
pkg_dir = glob.glob('apyhtest_eva-*')[0]
225227
os.chdir(pkg_dir)
226-
run_setup('setup.py', ['build'])
228+
229+
with catch_warnings(record=True) as w:
230+
run_setup('setup.py', ['build'])
227231

228232
try:
229233
import apyhtest_eva
@@ -234,3 +238,17 @@ def test_installed_git_version(version_test_package, version, tmpdir, capsys):
234238
assert apyhtest_eva.__githash__ == githash
235239
finally:
236240
cleanup_import('apyhtest_eva')
241+
242+
243+
def test_get_git_devstr(tmpdir):
244+
dirpath = str(tmpdir)
245+
warn_msg = "No git repository present at"
246+
# Verify as much as possible, but avoid dealing with paths on windows
247+
if not sys.platform.startswith('win'):
248+
warn_msg += " '{}'".format(dirpath)
249+
250+
with catch_warnings(record=True) as w:
251+
devstr = get_git_devstr(path=dirpath)
252+
assert devstr == '0'
253+
assert len(w) == 1
254+
assert str(w[0].message).startswith(warn_msg)

‎astropy_helpers/tests/test_setup_helpers.py‎

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
import stat
44
import shutil
5+
import warnings
56
import contextlib
67

78
import pytest
@@ -12,11 +13,16 @@
1213

1314
from ..setup_helpers import get_package_info, register_commands
1415
from ..commands import build_ext
16+
from ..utils import AstropyDeprecationWarning
1517

1618
from . import reset_setup_helpers, reset_distutils_log, fix_hide_setuptools # noqa
1719
from . import run_setup, cleanup_import
1820

1921

22+
# Determine whether we're in a PY2 environment without using six
23+
USING_PY2 = sys.version_info < (3,0,0)
24+
25+
2026
def _extension_test_package(tmpdir, request, extension_type='c'):
2127
"""Creates a simple test package with an extension module."""
2228

@@ -170,11 +176,18 @@ def test_compiler_module(c_extension_test_package):
170176
with test_pkg.as_cwd():
171177
# This is one of the simplest ways to install just a package into a
172178
# test directory
173-
run_setup('setup.py',
174-
['install',
175-
'--single-version-externally-managed',
176-
'--install-lib={0}'.format(install_temp),
177-
'--record={0}'.format(install_temp.join('record.txt'))])
179+
with warnings.catch_warnings(record=True) as w:
180+
run_setup('setup.py',
181+
['install',
182+
'--single-version-externally-managed',
183+
'--install-lib={0}'.format(install_temp),
184+
'--record={0}'.format(install_temp.join('record.txt'))])
185+
# Skip this portion of the test on windows systems with Py 2.7 since
186+
# it is known to produce additional warnings.
187+
if not (USING_PY2 or sys.platform.startswith('win')):
188+
# Warning expected from get_git_devstr, called by generate_version_py
189+
assert len(w) == 1
190+
assert str(w[0].message).startswith("No git repository present at")
178191

179192
with install_temp.as_cwd():
180193
import apyhtest_eva
@@ -205,7 +218,12 @@ def test_no_cython_buildext(c_extension_test_package, monkeypatch):
205218
lambda *args: False)
206219

207220
with test_pkg.as_cwd():
208-
run_setup('setup.py', ['build_ext', '--inplace'])
221+
with warnings.catch_warnings(record=True) as w:
222+
run_setup('setup.py', ['build_ext', '--inplace'])
223+
# Warning expected from get_git_devstr, called by generate_version_py
224+
if not USING_PY2:
225+
assert len(w) == 1
226+
assert str(w[0].message).startswith("No git repository present at")
209227

210228
sys.path.insert(0, str(test_pkg))
211229

@@ -234,7 +252,13 @@ def test_missing_cython_c_files(pyx_extension_test_package, monkeypatch):
234252

235253
with test_pkg.as_cwd():
236254
with pytest.raises(SystemExit) as exc_info:
237-
run_setup('setup.py', ['build_ext', '--inplace'])
255+
with warnings.catch_warnings(record=True) as w:
256+
run_setup('setup.py', ['build_ext', '--inplace'])
257+
# Warning expected from get_git_devstr, called by generate_version_py
258+
if not USING_PY2:
259+
assert len(w) == 1
260+
assert str(w[0].message).startswith(
261+
"No git repository present at")
238262

239263
msg = ('Could not find C/C++ file '
240264
'{0}.(c/cpp)'.format('apyhtest_eva/unit02'.replace('/', os.sep)))
@@ -323,7 +347,8 @@ class B(A):
323347
elif mode == 'cli-l':
324348
run_setup('setup.py', ['build_docs', '-l'])
325349
elif mode == 'deprecated':
326-
run_setup('setup.py', ['build_sphinx'])
350+
with pytest.warns(AstropyDeprecationWarning):
351+
run_setup('setup.py', ['build_sphinx'])
327352
elif mode == 'direct': # to check coverage
328353
with docs_dir.as_cwd():
329354
from sphinx import main

0 commit comments

Comments
 (0)