Fix CI failures
1 |
import glob |
|
2 |
import imp |
|
3 |
import os |
|
4 |
import pkgutil |
|
5 |
import re |
|
6 |
import sys |
|
7 |
import tarfile |
|
8 |
|
|
9 |
import pytest |
|
10 |
from warnings import catch_warnings |
|
11 |
|
|
12 |
from . import reset_setup_helpers, reset_distutils_log # noqa |
|
13 |
from . import run_cmd, run_setup, cleanup_import |
|
14 |
from astropy_helpers.git_helpers import get_git_devstr |
|
15 |
|
|
16 |
_DEV_VERSION_RE = re.compile(r'\d+\.\d+(?:\.\d+)?\.dev(\d+)') |
|
17 |
|
|
18 |
ASTROPY_HELPERS_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) |
|
19 |
|
|
20 |
TEST_VERSION_SETUP_PY_OLDSTYLE = """\ |
|
21 |
#!/usr/bin/env python
|
|
22 |
|
|
23 |
import sys
|
|
24 |
from setuptools import setup
|
|
25 |
|
|
26 |
NAME = 'apyhtest_eva'
|
|
27 |
VERSION = {version!r}
|
|
28 |
RELEASE = 'dev' not in VERSION
|
|
29 |
|
|
30 |
sys.path.insert(0, r'{astropy_helpers_path}')
|
|
31 |
|
|
32 |
from astropy_helpers.git_helpers import get_git_devstr
|
|
33 |
from astropy_helpers.version_helpers import generate_version_py
|
|
34 |
|
|
35 |
if not RELEASE:
|
|
36 |
VERSION += get_git_devstr(False)
|
|
37 |
|
|
38 |
generate_version_py(NAME, VERSION, RELEASE, False, uses_git=not RELEASE)
|
|
39 |
|
|
40 |
setup(name=NAME, version=VERSION, packages=['apyhtest_eva'])
|
|
41 |
"""
|
|
42 |
|
|
43 |
TEST_VERSION_SETUP_CFG = """\ |
|
44 |
[metadata]
|
|
45 |
name = apyhtest_eva
|
|
46 |
version = {version}
|
|
47 |
"""
|
|
48 |
|
|
49 |
TEST_VERSION_SETUP_PY_NEWSTYLE = """\ |
|
50 |
#!/usr/bin/env python
|
|
51 |
|
|
52 |
import sys
|
|
53 |
sys.path.insert(0, r'{astropy_helpers_path}')
|
|
54 |
|
|
55 |
from astropy_helpers.setup_helpers import setup
|
|
56 |
setup()
|
|
57 |
"""
|
|
58 |
|
|
59 |
|
|
60 |
TEST_VERSION_INIT = """\ |
|
61 |
try:
|
|
62 |
from .version import version as __version__
|
|
63 |
from .version import githash as __githash__
|
|
64 |
except ImportError:
|
|
65 |
__version__ = __githash__ = ''
|
|
66 |
"""
|
|
67 |
|
|
68 |
|
|
69 |
@pytest.fixture(params=["oldstyle", "newstyle"]) |
|
70 |
def version_test_package(tmpdir, request): |
|
71 |
|
|
72 |
# We test both the old-style syntax of deermining VERSION, RELEASE, etc.
|
|
73 |
# inside the setup.py, and the new style of getting these from the setup.cfg
|
|
74 |
# file.
|
|
75 |
|
|
76 |
def make_test_package_oldstyle(version='42.42.dev'): |
|
77 |
test_package = tmpdir.mkdir('test_package') |
|
78 |
test_package.join('setup.py').write( |
|
79 |
TEST_VERSION_SETUP_PY_OLDSTYLE.format(version=version, |
|
80 |
astropy_helpers_path=ASTROPY_HELPERS_PATH)) |
|
81 |
test_package.mkdir('apyhtest_eva').join('__init__.py').write(TEST_VERSION_INIT) |
|
82 |
with test_package.as_cwd(): |
|
83 |
run_cmd('git', ['init']) |
|
84 |
run_cmd('git', ['add', '--all']) |
|
85 |
run_cmd('git', ['commit', '-m', 'test package']) |
|
86 |
|
|
87 |
if '' in sys.path: |
|
88 |
sys.path.remove('') |
|
89 |
|
|
90 |
sys.path.insert(0, '') |
|
91 |
|
|
92 |
def finalize(): |
|
93 |
cleanup_import('apyhtest_eva') |
|
94 |
|
|
95 |
request.addfinalizer(finalize) |
|
96 |
|
|
97 |
return test_package |
|
98 |
|
|
99 |
def make_test_package_newstyle(version='42.42.dev'): |
|
100 |
test_package = tmpdir.mkdir('test_package') |
|
101 |
test_package.join('setup.cfg').write( |
|
102 |
TEST_VERSION_SETUP_CFG.format(version=version)) |
|
103 |
|
|
104 |
test_package.join('setup.py').write( |
|
105 |
TEST_VERSION_SETUP_PY_NEWSTYLE.format(astropy_helpers_path=ASTROPY_HELPERS_PATH)) |
|
106 |
|
|
107 |
test_package.mkdir('apyhtest_eva').join('__init__.py').write(TEST_VERSION_INIT) |
|
108 |
with test_package.as_cwd(): |
|
109 |
run_cmd('git', ['init']) |
|
110 |
run_cmd('git', ['add', '--all']) |
|
111 |
run_cmd('git', ['commit', '-m', 'test package']) |
|
112 |
|
|
113 |
if '' in sys.path: |
|
114 |
sys.path.remove('') |
|
115 |
|
|
116 |
sys.path.insert(0, '') |
|
117 |
|
|
118 |
def finalize(): |
|
119 |
cleanup_import('apyhtest_eva') |
|
120 |
|
|
121 |
request.addfinalizer(finalize) |
|
122 |
|
|
123 |
return test_package |
|
124 |
|
|
125 |
if request.param == 'oldstyle': |
|
126 |
return make_test_package_oldstyle |
|
127 |
else: |
|
128 |
return make_test_package_newstyle |
|
129 |
|
|
130 |
|
|
131 |
def test_update_git_devstr(version_test_package, capsys): |
|
132 |
"""Tests that the commit number in the package's version string updates
|
|
133 |
after git commits even without re-running setup.py.
|
|
134 |
"""
|
|
135 |
|
|
136 |
# We have to call version_test_package to actually create the package
|
|
137 |
test_pkg = version_test_package() |
|
138 |
|
|
139 |
with test_pkg.as_cwd(): |
|
140 |
run_setup('setup.py', ['--version']) |
|
141 |
|
|
142 |
stdout, stderr = capsys.readouterr() |
|
143 |
version = stdout.strip() |
|
144 |
|
|
145 |
m = _DEV_VERSION_RE.match(version) |
|
146 |
assert m, ( |
|
147 |
"Stdout did not match the version string pattern:"
|
|
148 |
"\n\n{0}\n\nStderr:\n\n{1}".format(stdout, stderr)) |
|
149 |
revcount = int(m.group(1)) |
|
150 |
|
|
151 |
import apyhtest_eva |
|
152 |
assert apyhtest_eva.__version__ == version |
|
153 |
|
|
154 |
# Make a silly git commit
|
|
155 |
with open('.test', 'w'): |
|
156 |
pass
|
|
157 |
|
|
158 |
run_cmd('git', ['add', '.test']) |
|
159 |
run_cmd('git', ['commit', '-m', 'test']) |
|
160 |
|
|
161 |
import apyhtest_eva.version |
|
162 |
imp.reload(apyhtest_eva.version) |
|
163 |
|
|
164 |
# Previously this checked packagename.__version__, but in order for that to
|
|
165 |
# be updated we also have to re-import _astropy_init which could be tricky.
|
|
166 |
# Checking directly that the packagename.version module was updated is
|
|
167 |
# sufficient:
|
|
168 |
m = _DEV_VERSION_RE.match(apyhtest_eva.version.version) |
|
169 |
assert m |
|
170 |
assert int(m.group(1)) == revcount + 1 |
|
171 |
|
|
172 |
# This doesn't test astropy_helpers.get_helpers.update_git_devstr directly
|
|
173 |
# since a copy of that function is made in packagename.version (so that it
|
|
174 |
# can work without astropy_helpers installed). In order to get test
|
|
175 |
# coverage on the actual astropy_helpers copy of that function just call it
|
|
176 |
# directly and compare to the value in packagename
|
|
177 |
from astropy_helpers.git_helpers import update_git_devstr |
|
178 |
|
|
179 |
newversion = update_git_devstr(version, path=str(test_pkg)) |
|
180 |
assert newversion == apyhtest_eva.version.version |
|
181 |
|
|
182 |
|
|
183 |
def test_version_update_in_other_repos(version_test_package, tmpdir): |
|
184 |
"""
|
|
185 |
Regression test for https://github.com/astropy/astropy-helpers/issues/114
|
|
186 |
and for https://github.com/astropy/astropy-helpers/issues/107
|
|
187 |
"""
|
|
188 |
|
|
189 |
test_pkg = version_test_package() |
|
190 |
|
|
191 |
with test_pkg.as_cwd(): |
|
192 |
run_setup('setup.py', ['build']) |
|
193 |
|
|
194 |
# Add the path to the test package to sys.path for now
|
|
195 |
sys.path.insert(0, str(test_pkg)) |
|
196 |
try: |
|
197 |
import apyhtest_eva |
|
198 |
m = _DEV_VERSION_RE.match(apyhtest_eva.__version__) |
|
199 |
assert m |
|
200 |
correct_revcount = int(m.group(1)) |
|
201 |
|
|
202 |
with tmpdir.as_cwd(): |
|
203 |
testrepo = tmpdir.mkdir('testrepo') |
|
204 |
testrepo.chdir() |
|
205 |
# Create an empty git repo
|
|
206 |
run_cmd('git', ['init']) |
|
207 |
|
|
208 |
import apyhtest_eva.version |
|
209 |
imp.reload(apyhtest_eva.version) |
|
210 |
m = _DEV_VERSION_RE.match(apyhtest_eva.version.version) |
|
211 |
assert m |
|
212 |
assert int(m.group(1)) == correct_revcount |
|
213 |
correct_revcount = int(m.group(1)) |
|
214 |
|
|
215 |
# Add several commits--more than the revcount for the apyhtest_eva package
|
|
216 |
for idx in range(correct_revcount + 5): |
|
217 |
test_filename = '.test' + str(idx) |
|
218 |
testrepo.ensure(test_filename) |
|
219 |
run_cmd('git', ['add', test_filename]) |
|
220 |
run_cmd('git', ['commit', '-m', 'A message']) |
|
221 |
|
|
222 |
import apyhtest_eva.version |
|
223 |
imp.reload(apyhtest_eva.version) |
|
224 |
m = _DEV_VERSION_RE.match(apyhtest_eva.version.version) |
|
225 |
assert m |
|
226 |
assert int(m.group(1)) == correct_revcount |
|
227 |
correct_revcount = int(m.group(1)) |
|
228 |
finally: |
|
229 |
sys.path.remove(str(test_pkg)) |
|
230 |
|
|
231 |
|
|
232 |
@pytest.mark.parametrize('version', ['1.0.dev', '1.0']) |
|
233 |
def test_installed_git_version(version_test_package, version, tmpdir, capsys): |
|
234 |
"""
|
|
235 |
Test for https://github.com/astropy/astropy-helpers/issues/87
|
|
236 |
|
|
237 |
Ensures that packages installed with astropy_helpers have a correct copy
|
|
238 |
of the git hash of the installed commit.
|
|
239 |
"""
|
|
240 |
|
|
241 |
# To test this, it should suffice to build a source dist, unpack it
|
|
242 |
# somewhere outside the git repository, and then do a build and import
|
|
243 |
# from the build directory--no need to "install" as such
|
|
244 |
|
|
245 |
test_pkg = version_test_package(version) |
|
246 |
|
|
247 |
with test_pkg.as_cwd(): |
|
248 |
run_setup('setup.py', ['build']) |
|
249 |
|
|
250 |
try: |
|
251 |
import apyhtest_eva |
|
252 |
githash = apyhtest_eva.__githash__ |
|
253 |
assert githash and isinstance(githash, str) |
|
254 |
# Ensure that it does in fact look like a git hash and not some
|
|
255 |
# other arbitrary string
|
|
256 |
assert re.match(r'[0-9a-f]{40}', githash) |
|
257 |
finally: |
|
258 |
cleanup_import('apyhtest_eva') |
|
259 |
|
|
260 |
run_setup('setup.py', ['sdist', '--dist-dir=dist', '--formats=gztar']) |
|
261 |
|
|
262 |
tgzs = glob.glob(os.path.join('dist', '*.tar.gz')) |
|
263 |
assert len(tgzs) == 1 |
|
264 |
|
|
265 |
tgz = test_pkg.join(tgzs[0]) |
|
266 |
|
|
267 |
build_dir = tmpdir.mkdir('build_dir') |
|
268 |
tf = tarfile.open(str(tgz), mode='r:gz') |
|
269 |
tf.extractall(str(build_dir)) |
|
270 |
|
|
271 |
with build_dir.as_cwd(): |
|
272 |
pkg_dir = glob.glob('apyhtest_eva-*')[0] |
|
273 |
os.chdir(pkg_dir) |
|
274 |
|
|
275 |
with catch_warnings(record=True) as w: |
|
276 |
run_setup('setup.py', ['build']) |
|
277 |
|
|
278 |
try: |
|
279 |
import apyhtest_eva |
|
280 |
loader = pkgutil.get_loader('apyhtest_eva') |
|
281 |
# Ensure we are importing the 'packagename' that was just unpacked
|
|
282 |
# into the build_dir
|
|
283 |
assert loader.get_filename().startswith(str(build_dir)) |
|
284 |
assert apyhtest_eva.__githash__ == githash |
|
285 |
finally: |
|
286 |
cleanup_import('apyhtest_eva') |
|
287 |
|
|
288 |
|
|
289 |
def test_get_git_devstr(tmpdir): |
|
290 |
dirpath = str(tmpdir) |
|
291 |
warn_msg = "No git repository present at" |
|
292 |
# Verify as much as possible, but avoid dealing with paths on windows
|
|
293 |
if not sys.platform.startswith('win'): |
|
294 |
warn_msg += " '{}'".format(dirpath) |
|
295 |
|
|
296 |
with catch_warnings(record=True) as w: |
|
297 |
devstr = get_git_devstr(path=dirpath) |
|
298 |
assert devstr == '0' |
|
299 |
assert len(w) == 1 |
|
300 |
assert str(w[0].message).startswith(warn_msg) |
Read our documentation on viewing source code .