forked from asottile-archive/setuptools-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetuptools_golang_test.py
138 lines (103 loc) · 3.93 KB
/
setuptools_golang_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from __future__ import unicode_literals
import collections
import os
import subprocess
import sys
import pytest
from setuptools.dist import Distribution
import setuptools_golang
@pytest.fixture(autouse=True, scope='session')
def enable_coverage_subprocesses():
here = os.path.dirname(os.path.abspath(__file__))
os.environ['TOP'] = here
os.environ['COVERAGE_PROCESS_START'] = os.path.join(here, '.coveragerc')
def auto_namedtuple(**kwargs):
return collections.namedtuple('auto_namedtuple', kwargs.keys())(**kwargs)
def run(*cmd, **kwargs):
returncode = kwargs.pop('returncode', 0)
proc = subprocess.Popen(cmd, **kwargs)
out, err = proc.communicate()
out = out.decode('UTF-8') if out is not None else None
err = err.decode('UTF-8') if err is not None else None
if returncode is not None:
if proc.returncode != returncode:
raise AssertionError(
'{!r} returned {} (expected {})\nout:\n{}\nerr:\n{}\n'.format(
cmd, proc.returncode, returncode, out, err,
)
)
return auto_namedtuple(returncode=proc.returncode, out=out, err=err)
def run_output(*cmd, **kwargs):
return run(*cmd, stdout=subprocess.PIPE, **kwargs).out
def test_sets_cmdclass():
dist = Distribution()
assert not dist.cmdclass.get('build_ext')
setuptools_golang.set_build_ext(
dist, 'build_golang', {'root': 'github.com/asottile/fake'},
)
assert dist.cmdclass['build_ext']
@pytest.yield_fixture(scope='session')
def venv(tmpdir_factory):
"""A shared virtualenv fixture, be careful not to install two of the same
package into this -- or sadness...
"""
venv = tmpdir_factory.mktemp('venv').join('venv')
pip = venv.join('bin/pip').strpath
python = venv.join('bin/python').strpath
# Make sure this virtualenv has the same executable
run('virtualenv', venv.strpath, '-p', sys.executable)
# Install this so we can get coverage
run(pip, 'install', 'coverage-enable-subprocess')
# Install us!
run(pip, 'install', '-e', '.')
yield auto_namedtuple(venv=venv, pip=pip, python=python)
SUM = 'import {0}; print({0}.sum(1, 2))'
@pytest.mark.parametrize(
('pkg', 'mod'),
(
('testing/sum', 'sum'),
('testing/sum_pure_go', 'sum_pure_go'),
('testing/sum_sub_package', 'sum_sub_package.sum'),
),
)
def test_sum_integration(venv, pkg, mod):
run(venv.pip, 'install', '-v', pkg)
out = run_output(venv.python, '-c', SUM.format(mod))
assert out == '3\n'
HELLO_WORLD = 'import project_with_c; print(project_with_c.hello_world())'
def test_integration_project_with_c(venv):
test_sum_integration(
venv, 'testing/project_with_c', 'project_with_c_sum.sum',
)
out = run_output(venv.python, '-c', HELLO_WORLD)
assert out == 'hello world\n'
RED = 'import red; print(red.red(u"ohai"))'
def test_integration_imports_gh(venv):
run(venv.pip, 'install', 'testing/imports_gh')
out = run_output(venv.python, '-c', RED)
assert out == '\x1b[31mohai\x1b[0m\n'
def test_integration_notfound(venv):
ret = run(
venv.pip, 'install', 'testing/notfound',
returncode=None, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
)
assert ret.returncode != 0
assert (
'Error building extension `notfound`: notfound.go does not exist' in
ret.out
)
def test_integration_multidir(venv):
ret = run(
venv.pip, 'install', 'testing/multidir',
returncode=None, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
)
assert ret.returncode != 0
assert (
'Error building extension `multidir`: '
'sources must be a single file in the `main` package.' in ret.out
)
OHAI = 'import hello_lib; print(hello_lib.ohai(u"Anthony"))'
def test_integration_internal_imports(venv):
run(venv.pip, 'install', 'testing/internal_imports')
out = run_output(venv.python, '-c', OHAI)
assert out == 'ohai, Anthony\n'