1 0
import glob
2 0
import imp
3 0
import os
4 0
import pkgutil
5 0
import re
6 0
import sys
7 0
import tarfile
8

9 0
import pytest
10 0
from warnings import catch_warnings
11

12 0
from . import reset_setup_helpers, reset_distutils_log  # noqa
13 0
from . import run_cmd, run_setup, cleanup_import
14 0
from astropy_helpers.git_helpers import get_git_devstr
15

16 0
_DEV_VERSION_RE = re.compile(r'\d+\.\d+(?:\.\d+)?\.dev(\d+)')
17

18 0
ASTROPY_HELPERS_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
19

20 0
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 0
TEST_VERSION_SETUP_CFG = """\
44
[metadata]
45
name = apyhtest_eva
46
version = {version}
47
"""
48

49 0
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 0
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 0
@pytest.fixture(params=["oldstyle", "newstyle"])
70 0
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 0
    def make_test_package_oldstyle(version='42.42.dev'):
77 0
        test_package = tmpdir.mkdir('test_package')
78 0
        test_package.join('setup.py').write(
79
            TEST_VERSION_SETUP_PY_OLDSTYLE.format(version=version,
80
                                                  astropy_helpers_path=ASTROPY_HELPERS_PATH))
81 0
        test_package.mkdir('apyhtest_eva').join('__init__.py').write(TEST_VERSION_INIT)
82 0
        with test_package.as_cwd():
83 0
            run_cmd('git', ['init'])
84 0
            run_cmd('git', ['add', '--all'])
85 0
            run_cmd('git', ['commit', '-m', 'test package'])
86

87 0
        if '' in sys.path:
88 0
            sys.path.remove('')
89

90 0
        sys.path.insert(0, '')
91

92 0
        def finalize():
93 0
            cleanup_import('apyhtest_eva')
94

95 0
        request.addfinalizer(finalize)
96

97 0
        return test_package
98

99 0
    def make_test_package_newstyle(version='42.42.dev'):
100 0
        test_package = tmpdir.mkdir('test_package')
101 0
        test_package.join('setup.cfg').write(
102
            TEST_VERSION_SETUP_CFG.format(version=version))
103

104 0
        test_package.join('setup.py').write(
105
            TEST_VERSION_SETUP_PY_NEWSTYLE.format(astropy_helpers_path=ASTROPY_HELPERS_PATH))
106

107 0
        test_package.mkdir('apyhtest_eva').join('__init__.py').write(TEST_VERSION_INIT)
108 0
        with test_package.as_cwd():
109 0
            run_cmd('git', ['init'])
110 0
            run_cmd('git', ['add', '--all'])
111 0
            run_cmd('git', ['commit', '-m', 'test package'])
112

113 0
        if '' in sys.path:
114 0
            sys.path.remove('')
115

116 0
        sys.path.insert(0, '')
117

118 0
        def finalize():
119 0
            cleanup_import('apyhtest_eva')
120

121 0
        request.addfinalizer(finalize)
122

123 0
        return test_package
124

125 0
    if request.param == 'oldstyle':
126 0
        return make_test_package_oldstyle
127
    else:
128 0
        return make_test_package_newstyle
129

130

131 0
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 0
    test_pkg = version_test_package()
138

139 0
    with test_pkg.as_cwd():
140 0
        run_setup('setup.py', ['--version'])
141

142 0
        stdout, stderr = capsys.readouterr()
143 0
        version = stdout.strip()
144

145 0
        m = _DEV_VERSION_RE.match(version)
146 0
        assert m, (
147
            "Stdout did not match the version string pattern:"
148
            "\n\n{0}\n\nStderr:\n\n{1}".format(stdout, stderr))
149 0
        revcount = int(m.group(1))
150

151 0
        import apyhtest_eva
152 0
        assert apyhtest_eva.__version__ == version
153

154
        # Make a silly git commit
155 0
        with open('.test', 'w'):
156 0
            pass
157

158 0
        run_cmd('git', ['add', '.test'])
159 0
        run_cmd('git', ['commit', '-m', 'test'])
160

161 0
        import apyhtest_eva.version
162 0
        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 0
    m = _DEV_VERSION_RE.match(apyhtest_eva.version.version)
169 0
    assert m
170 0
    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 0
    from astropy_helpers.git_helpers import update_git_devstr
178

179 0
    newversion = update_git_devstr(version, path=str(test_pkg))
180 0
    assert newversion == apyhtest_eva.version.version
181

182

183 0
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 0
    test_pkg = version_test_package()
190

191 0
    with test_pkg.as_cwd():
192 0
        run_setup('setup.py', ['build'])
193

194
    # Add the path to the test package to sys.path for now
195 0
    sys.path.insert(0, str(test_pkg))
196 0
    try:
197 0
        import apyhtest_eva
198 0
        m = _DEV_VERSION_RE.match(apyhtest_eva.__version__)
199 0
        assert m
200 0
        correct_revcount = int(m.group(1))
201

202 0
        with tmpdir.as_cwd():
203 0
            testrepo = tmpdir.mkdir('testrepo')
204 0
            testrepo.chdir()
205
            # Create an empty git repo
206 0
            run_cmd('git', ['init'])
207

208 0
            import apyhtest_eva.version
209 0
            imp.reload(apyhtest_eva.version)
210 0
            m = _DEV_VERSION_RE.match(apyhtest_eva.version.version)
211 0
            assert m
212 0
            assert int(m.group(1)) == correct_revcount
213 0
            correct_revcount = int(m.group(1))
214

215
            # Add several commits--more than the revcount for the apyhtest_eva package
216 0
            for idx in range(correct_revcount + 5):
217 0
                test_filename = '.test' + str(idx)
218 0
                testrepo.ensure(test_filename)
219 0
                run_cmd('git', ['add', test_filename])
220 0
                run_cmd('git', ['commit', '-m', 'A message'])
221

222 0
            import apyhtest_eva.version
223 0
            imp.reload(apyhtest_eva.version)
224 0
            m = _DEV_VERSION_RE.match(apyhtest_eva.version.version)
225 0
            assert m
226 0
            assert int(m.group(1)) == correct_revcount
227 0
            correct_revcount = int(m.group(1))
228
    finally:
229 0
        sys.path.remove(str(test_pkg))
230

231

232 0
@pytest.mark.parametrize('version', ['1.0.dev', '1.0'])
233 0
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 0
    test_pkg = version_test_package(version)
246

247 0
    with test_pkg.as_cwd():
248 0
        run_setup('setup.py', ['build'])
249

250 0
        try:
251 0
            import apyhtest_eva
252 0
            githash = apyhtest_eva.__githash__
253 0
            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 0
            assert re.match(r'[0-9a-f]{40}', githash)
257
        finally:
258 0
            cleanup_import('apyhtest_eva')
259

260 0
        run_setup('setup.py', ['sdist', '--dist-dir=dist', '--formats=gztar'])
261

262 0
        tgzs = glob.glob(os.path.join('dist', '*.tar.gz'))
263 0
        assert len(tgzs) == 1
264

265 0
        tgz = test_pkg.join(tgzs[0])
266

267 0
    build_dir = tmpdir.mkdir('build_dir')
268 0
    tf = tarfile.open(str(tgz), mode='r:gz')
269 0
    tf.extractall(str(build_dir))
270

271 0
    with build_dir.as_cwd():
272 0
        pkg_dir = glob.glob('apyhtest_eva-*')[0]
273 0
        os.chdir(pkg_dir)
274

275 0
        with catch_warnings(record=True) as w:
276 0
            run_setup('setup.py', ['build'])
277

278 0
        try:
279 0
            import apyhtest_eva
280 0
            loader = pkgutil.get_loader('apyhtest_eva')
281
            # Ensure we are importing the 'packagename' that was just unpacked
282
            # into the build_dir
283 0
            assert loader.get_filename().startswith(str(build_dir))
284 0
            assert apyhtest_eva.__githash__ == githash
285
        finally:
286 0
            cleanup_import('apyhtest_eva')
287

288

289 0
def test_get_git_devstr(tmpdir):
290 0
    dirpath = str(tmpdir)
291 0
    warn_msg = "No git repository present at"
292
    # Verify as much as possible, but avoid dealing with paths on windows
293 0
    if not sys.platform.startswith('win'):
294 0
        warn_msg += " '{}'".format(dirpath)
295

296 0
    with catch_warnings(record=True) as w:
297 0
        devstr = get_git_devstr(path=dirpath)
298 0
        assert devstr == '0'
299 0
        assert len(w) == 1
300 0
        assert str(w[0].message).startswith(warn_msg)

Read our documentation on viewing source code .

Loading