Skip to content
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

Add hooks for running tests on an installed version of mypy #5898

Merged
merged 3 commits into from
Nov 16, 2018
Merged
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
34 changes: 34 additions & 0 deletions misc/test_installed_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash -ex

# Usage: misc/test_installed_version.sh [wheel] [python command]
# Installs a version of mypy into a virtualenv and tests it.

# A bunch of stuff about mypy's code organization and test setup makes
# it annoying to test an installed version of mypy. If somebody has a
# better way please let me know.

TO_INSTALL="${1-.}"
PYTHON="${2-python3}"
VENV="$(mktemp -d --tmpdir mypy-test-venv.XXXXXXXXXX)"
trap "rm -rf '$VENV'" EXIT

"$PYTHON" -m venv "$VENV"
source "$VENV/bin/activate"

pip install -r test-requirements.txt
pip install $TO_INSTALL

# pytest looks for configuration files in the parent directories of
# where the tests live. Since we are trying to run the tests from
# their installed location, we copy those into the venv. Ew ew ew.
cp pytest.ini conftest.py "$VENV/"

ROOT="$PWD"

# Change directory so we can't pick up any of the stuff in the root
cd "$VENV"

# Find the directory that mypy tests were installed into
MYPY_TEST_DIR="$(python3 -c 'import mypy.test; print(mypy.test.__path__[0])')"
# Run the mypy tests
MYPY_TEST_PREFIX="$ROOT" python3 -m pytest "$MYPY_TEST_DIR"/test*.py
5 changes: 4 additions & 1 deletion mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,10 @@ def compute_search_paths(sources: List[BuildSource],
# Use stub builtins (to speed up test cases and to make them easier to
# debug). This is a test-only feature, so assume our files are laid out
# as in the source tree.
root_dir = os.path.dirname(os.path.dirname(__file__))
# We also need to allow overriding where to look for it. Argh.
root_dir = os.getenv('MYPY_TEST_PREFIX', None)
if not root_dir:
root_dir = os.path.dirname(os.path.dirname(__file__))
lib_path.appendleft(os.path.join(root_dir, 'test-data', 'unit', 'lib-stub'))
# alt_lib_path is used by some tests to bypass the normal lib_path mechanics.
# If we don't have one, grab directories of source files.
Expand Down
8 changes: 6 additions & 2 deletions mypy/test/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os.path

this_file_dir = os.path.dirname(os.path.realpath(__file__))
PREFIX = os.path.dirname(os.path.dirname(this_file_dir))
provided_prefix = os.getenv('MYPY_TEST_PREFIX', None)
if provided_prefix:
PREFIX = provided_prefix
else:
this_file_dir = os.path.dirname(os.path.realpath(__file__))
PREFIX = os.path.dirname(os.path.dirname(this_file_dir))

# Location of test data files such as test case descriptions.
test_data_prefix = os.path.join(PREFIX, 'test-data', 'unit')
Expand Down
4 changes: 2 additions & 2 deletions mypy/test/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import pytest # type: ignore # no pytest in typeshed
from typing import List, Tuple, Set, Optional, Iterator, Any, Dict, NamedTuple, Union

from mypy.test.config import test_data_prefix, test_temp_dir
from mypy.test.config import test_data_prefix, test_temp_dir, PREFIX

root_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), '..', '..'))
root_dir = os.path.normpath(PREFIX)

# File modify/create operation: copy module contents from source_path.
UpdateFile = NamedTuple('UpdateFile', [('module', str),
Expand Down
8 changes: 6 additions & 2 deletions mypy/test/testcmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from mypy.test.data import fix_cobertura_filename
from mypy.test.data import DataDrivenTestCase, DataSuite
from mypy.test.helpers import assert_string_arrays_equal, normalize_error_messages
from mypy.version import __version__, base_version
import mypy.version

# Path to Python 3 interpreter
python3_path = sys.executable
Expand Down Expand Up @@ -122,7 +122,11 @@ def normalize_file_output(content: List[str], current_abs_path: str) -> List[str
"""Normalize file output for comparison."""
timestamp_regex = re.compile(r'\d{10}')
result = [x.replace(current_abs_path, '$PWD') for x in content]
result = [re.sub(r'\b' + re.escape(__version__) + r'\b', '$VERSION', x) for x in result]
version = mypy.version.__version__
result = [re.sub(r'\b' + re.escape(version) + r'\b', '$VERSION', x) for x in result]
# We generate a new mypy.version when building mypy wheels that
# lacks base_version, so handle that case.
base_version = getattr(mypy.version, 'base_version', version)
msullivan marked this conversation as resolved.
Show resolved Hide resolved
result = [re.sub(r'\b' + re.escape(base_version) + r'\b', '$VERSION', x) for x in result]
result = [timestamp_regex.sub('$TIMESTAMP', x) for x in result]
return result