|
| 1 | +from __future__ import unicode_literals |
| 2 | + |
| 3 | +import collections |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | + |
| 8 | +import pytest |
1 | 9 | from setuptools.dist import Distribution
|
2 | 10 |
|
3 | 11 | import setuptools_golang
|
4 | 12 |
|
5 | 13 |
|
| 14 | +xfailif_pypy = pytest.mark.xfail( |
| 15 | + setuptools_golang.PYPY, reason='pypy is a special snowflake', |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture(autouse=True, scope='session') |
| 20 | +def enable_coverage_subprocesses(): |
| 21 | + here = os.path.dirname(os.path.abspath(__file__)) |
| 22 | + os.environ['TOP'] = here |
| 23 | + os.environ['COVERAGE_PROCESS_START'] = os.path.join(here, '.coveragerc') |
| 24 | + |
| 25 | + |
| 26 | +def auto_namedtuple(**kwargs): |
| 27 | + return collections.namedtuple('auto_namedtuple', kwargs.keys())(**kwargs) |
| 28 | + |
| 29 | + |
| 30 | +def run(*cmd, **kwargs): |
| 31 | + returncode = kwargs.pop('returncode', 0) |
| 32 | + proc = subprocess.Popen(cmd, **kwargs) |
| 33 | + out, err = proc.communicate() |
| 34 | + out = out.decode('UTF-8') if out is not None else None |
| 35 | + err = err.decode('UTF-8') if err is not None else None |
| 36 | + if returncode is not None: |
| 37 | + if proc.returncode != returncode: |
| 38 | + raise AssertionError( |
| 39 | + '{!r} returned {} (expected {})\nout:\n{}err:\n{}'.format( |
| 40 | + cmd, proc.returncode, returncode, out, err, |
| 41 | + ) |
| 42 | + ) |
| 43 | + return auto_namedtuple(returncode=proc.returncode, out=out, err=err) |
| 44 | + |
| 45 | + |
| 46 | +def run_output(*cmd, **kwargs): |
| 47 | + return run(*cmd, stdout=subprocess.PIPE, **kwargs).out |
| 48 | + |
| 49 | + |
6 | 50 | def test_sets_cmdclass():
|
7 | 51 | dist = Distribution()
|
8 | 52 | setuptools_golang.set_build_ext(dist, 'build_golang', True)
|
9 |
| - assert dist.cmdclass['build_ext'] == setuptools_golang.BuildExtGolang |
| 53 | + assert dist.cmdclass['build_ext'] == setuptools_golang.build_ext |
10 | 54 |
|
11 | 55 |
|
12 | 56 | def test_sets_cmdclass_value_falsey():
|
13 | 57 | dist = Distribution()
|
14 | 58 | setuptools_golang.set_build_ext(dist, 'build_golang', False)
|
15 |
| - assert dist.cmdclass.get('build_ext') != setuptools_golang.BuildExtGolang |
| 59 | + assert dist.cmdclass.get('build_ext') != setuptools_golang.build_ext |
| 60 | + |
| 61 | + |
| 62 | +GET_LDFLAGS = ( |
| 63 | + 'import distutils.spawn;' |
| 64 | + "print(bool(distutils.spawn.find_executable('pkg-config')));" |
| 65 | + 'import setuptools_golang;' |
| 66 | + 'print(setuptools_golang._get_ldflags());' |
| 67 | +) |
| 68 | + |
| 69 | + |
| 70 | +@xfailif_pypy |
| 71 | +def test_from_pkg_config(): |
| 72 | + output = run_output(sys.executable, '-c', GET_LDFLAGS) |
| 73 | + assert output.startswith('True\n') |
| 74 | + assert '-lpython' in output |
| 75 | + |
| 76 | + |
| 77 | +@xfailif_pypy |
| 78 | +def test_no_pkg_config(): |
| 79 | + # Blank PATH so we don't have pkg-config |
| 80 | + env = dict(os.environ, PATH='') |
| 81 | + output = run_output(sys.executable, '-c', GET_LDFLAGS, env=env) |
| 82 | + assert output.startswith('False\n') |
| 83 | + assert '-lpython' in output |
| 84 | + |
| 85 | + |
| 86 | +@pytest.yield_fixture(scope='session') |
| 87 | +def venv(tmpdir_factory): |
| 88 | + """A shared virtualenv fixture, be careful not to install two of the same |
| 89 | + package into this -- or sadness... |
| 90 | + """ |
| 91 | + venv = tmpdir_factory.mktemp('venv').join('venv') |
| 92 | + pip = venv.join('bin/pip').strpath |
| 93 | + python = venv.join('bin/python').strpath |
| 94 | + # Make sure this virtualenv has the same executable |
| 95 | + run('virtualenv', venv.strpath, '-p', sys.executable) |
| 96 | + # Install this so we can get coverage |
| 97 | + run(pip, 'install', 'coverage-enable-subprocess') |
| 98 | + # Install us! |
| 99 | + run(pip, 'install', '-e', '.') |
| 100 | + yield auto_namedtuple(venv=venv, pip=pip, python=python) |
| 101 | + |
| 102 | + |
| 103 | +SUM = 'import {0}; print({0}.sum(1, 2))' |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.parametrize( |
| 107 | + ('pkg', 'mod'), |
| 108 | + ( |
| 109 | + ('testing/sum', 'sum'), |
| 110 | + ('testing/sum_pure_go', 'sum_pure_go'), |
| 111 | + ('testing/sum_sub_package', 'sum_sub_package.sum'), |
| 112 | + ), |
| 113 | +) |
| 114 | +def test_sum_integration(venv, pkg, mod): |
| 115 | + run(venv.pip, 'install', '-v', pkg) |
| 116 | + out = run_output(venv.python, '-c', SUM.format(mod)) |
| 117 | + assert out == '3\n' |
| 118 | + |
| 119 | + |
| 120 | +HELLO_WORLD = 'import project_with_c; print(project_with_c.hello_world())' |
| 121 | + |
| 122 | + |
| 123 | +def test_integration_project_with_c(venv): |
| 124 | + test_sum_integration( |
| 125 | + venv, 'testing/project_with_c', 'project_with_c_sum.sum', |
| 126 | + ) |
| 127 | + out = run_output(venv.python, '-c', HELLO_WORLD) |
| 128 | + assert out == 'hello world\n' |
| 129 | + |
| 130 | + |
| 131 | +def test_integration_notfound(venv): |
| 132 | + ret = run( |
| 133 | + venv.pip, 'install', 'testing/notfound', |
| 134 | + returncode=None, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
| 135 | + ) |
| 136 | + assert ret.returncode != 0 |
| 137 | + assert ( |
| 138 | + 'Error building extension `notfound`: notfound.go does not exist' in |
| 139 | + ret.out |
| 140 | + ) |
| 141 | + |
| 142 | + |
| 143 | +def test_integration_multidir(venv): |
| 144 | + ret = run( |
| 145 | + venv.pip, 'install', 'testing/multidir', |
| 146 | + returncode=None, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
| 147 | + ) |
| 148 | + assert ret.returncode != 0 |
| 149 | + assert ( |
| 150 | + 'Error building extension `multidir`: ' |
| 151 | + 'Cannot compile across directories: dir1 dir2' in ret.out |
| 152 | + ) |
0 commit comments