Skip to content

Test that install actually works #918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ python:

install:
- pip install -r test-requirements.txt
- python setup.py install

script:
- python setup.py install
- mkdir elsewhere
- cd elsewhere
- mkdir tmp-test-dirs
- ln -s ../runtests.py
- python runtests.py -v

notifications:
Expand Down
38 changes: 14 additions & 24 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ def build(program_path: str,
argument: str = None,
program_text: Union[str, bytes] = None,
alt_lib_path: str = None,
bin_dir: str = None,
pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION,
custom_typing_module: str = None,
report_dirs: Dict[str, str] = {},
Expand All @@ -116,16 +115,14 @@ def build(program_path: str,
program_text: the main source file contents; if omitted, read from file
alt_lib_dir: an additional directory for looking up library modules
(takes precedence over other directories)
bin_dir: directory containing the mypy script, used for finding data
directories; if omitted, use '.' as the data directory
pyversion: Python version (major, minor)
custom_typing_module: if not None, use this module id as an alias for typing
flags: list of build options (e.g. COMPILE_ONLY)
"""
flags = flags or []
module = module or '__main__'

data_dir = default_data_dir(bin_dir)
data_dir = default_data_dir()

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


def default_data_dir(bin_dir: str) -> str:
# TODO fix this logic
if not bin_dir:
# Default to directory containing this file's parent.
return os.path.dirname(os.path.dirname(__file__))
base = os.path.basename(bin_dir)
dir = os.path.dirname(bin_dir)
if (sys.platform == 'win32' and base.lower() == 'mypy'
and not os.path.isdir(os.path.join(dir, 'stubs'))):
# Installed, on Windows.
return os.path.join(dir, 'Lib', 'mypy')
elif base == 'mypy':
# Assume that we have a repo check out or unpacked source tarball.
return os.path.dirname(bin_dir)
elif base == 'bin':
# Installed to somewhere (can be under /usr/local or anywhere).
return os.path.join(dir, 'lib', 'mypy')
elif base == 'python3':
# Assume we installed python3 with brew on os x
return os.path.join(os.path.dirname(dir), 'lib', 'mypy')
def is_installed() -> bool:
return 'site-packages' in __file__ or 'dist-packages' in __file__


def default_data_dir() -> str:
if is_installed():
# we are installed
rv = os.path.join(sys.prefix, 'lib', 'mypy')
else:
# we are from from a source checkout
rv = os.path.dirname(os.path.dirname(__file__))
if os.path.isdir(os.path.join(rv, 'stubs')):
return rv
else:
# Don't know where to find the data files!
raise RuntimeError("Broken installation: can't determine base dir")
Expand Down
21 changes: 2 additions & 19 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ def __init__(self) -> None:


def main() -> None:
bin_dir = find_bin_directory()
path, module, program_text, options = process_options(sys.argv[1:])
try:
if options.target == build.TYPE_CHECK:
type_check_only(path, module, program_text, bin_dir, options)
type_check_only(path, module, program_text, options)
else:
raise RuntimeError('unsupported target %d' % options.target)
except CompileError as e:
Expand All @@ -42,21 +41,6 @@ def main() -> None:
sys.exit(1)


def find_bin_directory() -> str:
"""Find the directory that contains this script.

This is used by build to find stubs and other data files.
"""
script = __file__
# Follow up to 5 symbolic links (cap to avoid cycles).
for i in range(5):
if os.path.islink(script):
script = readlinkabs(script)
else:
break
return os.path.dirname(script)


def readlinkabs(link: str) -> str:
"""Return an absolute path to symbolic link destination."""
# Adapted from code by Greg Smith.
Expand All @@ -68,12 +52,11 @@ def readlinkabs(link: str) -> str:


def type_check_only(path: str, module: str, program_text: str,
bin_dir: str, options: Options) -> None:
options: Options) -> None:
# Type check the program and dependencies and translate to Python.
build.build(path,
module=module,
program_text=program_text,
bin_dir=bin_dir,
target=build.TYPE_CHECK,
pyversion=options.pyversion,
custom_typing_module=options.custom_typing_module,
Expand Down
4 changes: 1 addition & 3 deletions mypy/test/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import typing


PREFIX = ''

# Location of test data files such as test case descriptions.
test_data_prefix = os.path.join(PREFIX, 'mypy', 'test', 'data')
test_data_prefix = os.path.join(os.path.dirname(__file__), 'data')

assert os.path.isdir(test_data_prefix), \
'Test data prefix ({}) not set correctly'.format(test_data_prefix)
Expand Down
22 changes: 0 additions & 22 deletions mypy/test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ def assert_string_arrays_equal(expected: List[str], actual: List[str],
Display any differences in a human-readable form.
"""

actual = clean_up(actual)

if actual != expected:
num_skip_start = num_skipped_prefix_lines(expected, actual)
num_skip_end = num_skipped_suffix_lines(expected, actual)
Expand Down Expand Up @@ -171,7 +169,6 @@ def assert_string_arrays_equal_wildcards(expected: List[str],
msg: str) -> None:
# Like above, but let a line with only '...' in expected match any number
# of lines in actual.
actual = clean_up(actual)

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


def clean_up(a):
"""Remove common directory prefix from all strings in a.

This uses a naive string replace; it seems to work well enough. Also
remove trailing carriage returns.
"""
res = []
for s in a:
prefix = config.PREFIX + os.sep
ss = s
for p in prefix, prefix.replace(os.sep, '/'):
if p != '/' and p != '//' and p != '\\' and p != '\\\\':
ss = ss.replace(p, '')
# Ignore spaces at end of line.
ss = re.sub(' +$', '', ss)
res.append(re.sub('\\r$', '', ss))
return res


def match_array(pattern: List[str], target: List[str]) -> List[str]:
"""Expand '...' wildcards in pattern by matching against target."""

Expand Down
20 changes: 14 additions & 6 deletions mypy/test/testpythoneval.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import typing

from mypy.build import is_installed
from mypy.myunit import Suite, SkipTestCaseException
from mypy.test.config import test_data_prefix, test_temp_dir
from mypy.test.data import parse_test_cases
Expand Down Expand Up @@ -72,7 +73,7 @@ def test_python_evaluation(testcase):
# Type check the program.
# This uses the same PYTHONPATH as the current process.
process = subprocess.Popen([python3_path,
os.path.join(testcase.old_cwd, 'scripts', 'mypy')]
'-m', 'mypy']
+ args + [program],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
Expand All @@ -83,11 +84,14 @@ def test_python_evaluation(testcase):
if not process.wait():
# Set up module path for the execution.
# This needs the typing module but *not* the mypy module.
vers_dir = '2.7' if py2 else '3.2'
typing_path = os.path.join(testcase.old_cwd, 'lib-typing', vers_dir)
assert os.path.isdir(typing_path)
env = os.environ.copy()
env['PYTHONPATH'] = typing_path
if is_installed():
env = None
else:
vers_dir = '2.7' if py2 else '3.2'
typing_path = os.path.join(testcase.old_cwd, 'lib-typing', vers_dir)
assert os.path.isdir(typing_path)
env = os.environ.copy()
env['PYTHONPATH'] = typing_path
process = subprocess.Popen([interpreter, program],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
Expand All @@ -110,6 +114,10 @@ def try_find_python2_interpreter():
stderr=subprocess.STDOUT)
stdout, stderr = process.communicate()
if b'Python 2.7' in stdout:
if is_installed():
print('WARNING: python2 interpreter found, but'
' typing is not installed for python2')
return None
return interpreter
except OSError:
pass
Expand Down
Loading