Skip to content

Commit c142274

Browse files
committed
Test that install actually works
1 parent 47b40a7 commit c142274

File tree

8 files changed

+109
-100
lines changed

8 files changed

+109
-100
lines changed

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ python:
1111

1212
install:
1313
- pip install -r test-requirements.txt
14-
- python setup.py install
1514

1615
script:
16+
- python setup.py install
17+
- mkdir elsewhere
18+
- cd elsewhere
19+
- mkdir tmp-test-dirs
20+
- ln -s ../runtests.py
1721
- python runtests.py -v
1822

1923
notifications:

mypy/build.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ def build(program_path: str,
9595
argument: str = None,
9696
program_text: Union[str, bytes] = None,
9797
alt_lib_path: str = None,
98-
bin_dir: str = None,
9998
pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION,
10099
custom_typing_module: str = None,
101100
report_dirs: Dict[str, str] = {},
@@ -116,16 +115,14 @@ def build(program_path: str,
116115
program_text: the main source file contents; if omitted, read from file
117116
alt_lib_dir: an additional directory for looking up library modules
118117
(takes precedence over other directories)
119-
bin_dir: directory containing the mypy script, used for finding data
120-
directories; if omitted, use '.' as the data directory
121118
pyversion: Python version (major, minor)
122119
custom_typing_module: if not None, use this module id as an alias for typing
123120
flags: list of build options (e.g. COMPILE_ONLY)
124121
"""
125122
flags = flags or []
126123
module = module or '__main__'
127124

128-
data_dir = default_data_dir(bin_dir)
125+
data_dir = default_data_dir()
129126

130127
# Determine the default module search path.
131128
lib_path = default_lib_path(data_dir, target, pyversion, python_path)
@@ -176,26 +173,19 @@ def build(program_path: str,
176173
return result
177174

178175

179-
def default_data_dir(bin_dir: str) -> str:
180-
# TODO fix this logic
181-
if not bin_dir:
182-
# Default to directory containing this file's parent.
183-
return os.path.dirname(os.path.dirname(__file__))
184-
base = os.path.basename(bin_dir)
185-
dir = os.path.dirname(bin_dir)
186-
if (sys.platform == 'win32' and base.lower() == 'mypy'
187-
and not os.path.isdir(os.path.join(dir, 'stubs'))):
188-
# Installed, on Windows.
189-
return os.path.join(dir, 'Lib', 'mypy')
190-
elif base == 'mypy':
191-
# Assume that we have a repo check out or unpacked source tarball.
192-
return os.path.dirname(bin_dir)
193-
elif base == 'bin':
194-
# Installed to somewhere (can be under /usr/local or anywhere).
195-
return os.path.join(dir, 'lib', 'mypy')
196-
elif base == 'python3':
197-
# Assume we installed python3 with brew on os x
198-
return os.path.join(os.path.dirname(dir), 'lib', 'mypy')
176+
def is_installed() -> bool:
177+
return 'site-packages' in __file__ or 'dist-packages' in __file__
178+
179+
180+
def default_data_dir() -> str:
181+
if is_installed():
182+
# we are installed
183+
rv = os.path.join(sys.prefix, 'lib', 'mypy')
184+
else:
185+
# we are from from a source checkout
186+
rv = os.path.dirname(os.path.dirname(__file__))
187+
if os.path.isdir(os.path.join(rv, 'stubs')):
188+
return rv
199189
else:
200190
# Don't know where to find the data files!
201191
raise RuntimeError("Broken installation: can't determine base dir")

mypy/main.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ def __init__(self) -> None:
2929

3030

3131
def main() -> None:
32-
bin_dir = find_bin_directory()
3332
path, module, program_text, options = process_options(sys.argv[1:])
3433
try:
3534
if options.target == build.TYPE_CHECK:
36-
type_check_only(path, module, program_text, bin_dir, options)
35+
type_check_only(path, module, program_text, options)
3736
else:
3837
raise RuntimeError('unsupported target %d' % options.target)
3938
except CompileError as e:
@@ -42,21 +41,6 @@ def main() -> None:
4241
sys.exit(1)
4342

4443

45-
def find_bin_directory() -> str:
46-
"""Find the directory that contains this script.
47-
48-
This is used by build to find stubs and other data files.
49-
"""
50-
script = __file__
51-
# Follow up to 5 symbolic links (cap to avoid cycles).
52-
for i in range(5):
53-
if os.path.islink(script):
54-
script = readlinkabs(script)
55-
else:
56-
break
57-
return os.path.dirname(script)
58-
59-
6044
def readlinkabs(link: str) -> str:
6145
"""Return an absolute path to symbolic link destination."""
6246
# Adapted from code by Greg Smith.
@@ -68,12 +52,11 @@ def readlinkabs(link: str) -> str:
6852

6953

7054
def type_check_only(path: str, module: str, program_text: str,
71-
bin_dir: str, options: Options) -> None:
55+
options: Options) -> None:
7256
# Type check the program and dependencies and translate to Python.
7357
build.build(path,
7458
module=module,
7559
program_text=program_text,
76-
bin_dir=bin_dir,
7760
target=build.TYPE_CHECK,
7861
pyversion=options.pyversion,
7962
custom_typing_module=options.custom_typing_module,

mypy/test/config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
import typing
55

66

7-
PREFIX = ''
8-
97
# Location of test data files such as test case descriptions.
10-
test_data_prefix = os.path.join(PREFIX, 'mypy', 'test', 'data')
8+
test_data_prefix = os.path.join(os.path.dirname(__file__), 'data')
119

1210
assert os.path.isdir(test_data_prefix), \
1311
'Test data prefix ({}) not set correctly'.format(test_data_prefix)

mypy/test/helpers.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ def assert_string_arrays_equal(expected: List[str], actual: List[str],
2121
Display any differences in a human-readable form.
2222
"""
2323

24-
actual = clean_up(actual)
25-
2624
if actual != expected:
2725
num_skip_start = num_skipped_prefix_lines(expected, actual)
2826
num_skip_end = num_skipped_suffix_lines(expected, actual)
@@ -171,7 +169,6 @@ def assert_string_arrays_equal_wildcards(expected: List[str],
171169
msg: str) -> None:
172170
# Like above, but let a line with only '...' in expected match any number
173171
# of lines in actual.
174-
actual = clean_up(actual)
175172

176173
while actual != [] and actual[-1] == '':
177174
actual = actual[:-1]
@@ -181,25 +178,6 @@ def assert_string_arrays_equal_wildcards(expected: List[str],
181178
assert_string_arrays_equal(expected, actual, msg)
182179

183180

184-
def clean_up(a):
185-
"""Remove common directory prefix from all strings in a.
186-
187-
This uses a naive string replace; it seems to work well enough. Also
188-
remove trailing carriage returns.
189-
"""
190-
res = []
191-
for s in a:
192-
prefix = config.PREFIX + os.sep
193-
ss = s
194-
for p in prefix, prefix.replace(os.sep, '/'):
195-
if p != '/' and p != '//' and p != '\\' and p != '\\\\':
196-
ss = ss.replace(p, '')
197-
# Ignore spaces at end of line.
198-
ss = re.sub(' +$', '', ss)
199-
res.append(re.sub('\\r$', '', ss))
200-
return res
201-
202-
203181
def match_array(pattern: List[str], target: List[str]) -> List[str]:
204182
"""Expand '...' wildcards in pattern by matching against target."""
205183

mypy/test/testpythoneval.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import typing
1919

20+
from mypy.build import is_installed
2021
from mypy.myunit import Suite, SkipTestCaseException
2122
from mypy.test.config import test_data_prefix, test_temp_dir
2223
from mypy.test.data import parse_test_cases
@@ -72,7 +73,7 @@ def test_python_evaluation(testcase):
7273
# Type check the program.
7374
# This uses the same PYTHONPATH as the current process.
7475
process = subprocess.Popen([python3_path,
75-
os.path.join(testcase.old_cwd, 'scripts', 'mypy')]
76+
'-m', 'mypy']
7677
+ args + [program],
7778
stdout=subprocess.PIPE,
7879
stderr=subprocess.STDOUT,
@@ -83,11 +84,14 @@ def test_python_evaluation(testcase):
8384
if not process.wait():
8485
# Set up module path for the execution.
8586
# This needs the typing module but *not* the mypy module.
86-
vers_dir = '2.7' if py2 else '3.2'
87-
typing_path = os.path.join(testcase.old_cwd, 'lib-typing', vers_dir)
88-
assert os.path.isdir(typing_path)
89-
env = os.environ.copy()
90-
env['PYTHONPATH'] = typing_path
87+
if is_installed():
88+
env = None
89+
else:
90+
vers_dir = '2.7' if py2 else '3.2'
91+
typing_path = os.path.join(testcase.old_cwd, 'lib-typing', vers_dir)
92+
assert os.path.isdir(typing_path)
93+
env = os.environ.copy()
94+
env['PYTHONPATH'] = typing_path
9195
process = subprocess.Popen([interpreter, program],
9296
stdout=subprocess.PIPE,
9397
stderr=subprocess.STDOUT,
@@ -110,6 +114,10 @@ def try_find_python2_interpreter():
110114
stderr=subprocess.STDOUT)
111115
stdout, stderr = process.communicate()
112116
if b'Python 2.7' in stdout:
117+
if is_installed():
118+
print('WARNING: python2 interpreter found, but'
119+
' typing is not installed for python2')
120+
return None
113121
return interpreter
114122
except OSError:
115123
pass

0 commit comments

Comments
 (0)