1
1
from contextlib import contextmanager
2
2
import os
3
- import random
4
- import shutil
5
- import string
6
3
import sys
7
4
import tempfile
8
- from typing import Iterator , List , Generator
5
+ from typing import Tuple , List , Generator , Optional
9
6
from unittest import TestCase , main
10
7
11
8
import mypy .api
@@ -25,30 +22,40 @@ def check_mypy_run(cmd_line: List[str],
25
22
python_executable : str = sys .executable ,
26
23
expected_out : str = '' ,
27
24
expected_err : str = '' ,
28
- expected_returncode : int = 1 ) -> None :
25
+ expected_returncode : int = 1 ,
26
+ venv_dir : Optional [str ] = None ) -> None :
29
27
"""Helper to run mypy and check the output."""
30
- if python_executable != sys .executable :
31
- cmd_line .append ('--python-executable={}' .format (python_executable ))
32
- out , err , returncode = mypy .api .run (cmd_line )
33
- assert out == expected_out , err
34
- assert err == expected_err , out
35
- assert returncode == expected_returncode , returncode
28
+ if venv_dir is not None :
29
+ old_dir = os .getcwd ()
30
+ os .chdir (venv_dir )
31
+ try :
32
+ if python_executable != sys .executable :
33
+ cmd_line .append ('--python-executable={}' .format (python_executable ))
34
+ out , err , returncode = mypy .api .run (cmd_line )
35
+ assert out == expected_out , err
36
+ assert err == expected_err , out
37
+ assert returncode == expected_returncode , returncode
38
+ finally :
39
+ if venv_dir is not None :
40
+ os .chdir (old_dir )
36
41
37
42
38
43
class TestPEP561 (TestCase ):
39
44
40
45
@contextmanager
41
- def virtualenv (self , python_executable : str = sys .executable ) -> Generator [str , None , None ]:
46
+ def virtualenv (self ,
47
+ python_executable : str = sys .executable
48
+ ) -> Generator [Tuple [str , str ], None , None ]:
42
49
"""Context manager that creates a virtualenv in a temporary directory
43
50
44
51
returns the path to the created Python executable"""
45
52
with tempfile .TemporaryDirectory () as venv_dir :
46
53
run_command ([sys .executable , '-m' , 'virtualenv' , '-p{}' .format (python_executable ),
47
54
venv_dir ], cwd = os .getcwd ())
48
55
if sys .platform == 'win32' :
49
- yield os .path .abspath (os .path .join (venv_dir , 'Scripts' , 'python' ))
56
+ yield venv_dir , os .path .abspath (os .path .join (venv_dir , 'Scripts' , 'python' ))
50
57
else :
51
- yield os .path .abspath (os .path .join (venv_dir , 'bin' , 'python' ))
58
+ yield venv_dir , os .path .abspath (os .path .join (venv_dir , 'bin' , 'python' ))
52
59
53
60
def install_package (self , pkg : str ,
54
61
python_executable : str = sys .executable ) -> None :
@@ -78,53 +85,63 @@ def test_get_pkg_dirs(self) -> None:
78
85
assert dirs
79
86
80
87
def test_typedpkg_stub_package (self ) -> None :
81
- with self .virtualenv () as python_executable :
88
+ with self .virtualenv () as venv :
89
+ venv_dir , python_executable = venv
82
90
self .install_package ('typedpkg-stubs' , python_executable )
83
91
check_mypy_run (
84
92
[self .tempfile ],
85
93
python_executable ,
86
- self .msg_list ,
94
+ expected_out = self .msg_list ,
95
+ venv_dir = venv_dir ,
87
96
)
88
97
89
98
def test_typedpkg (self ) -> None :
90
- with self .virtualenv () as python_executable :
99
+ with self .virtualenv () as venv :
100
+ venv_dir , python_executable = venv
91
101
self .install_package ('typedpkg' , python_executable )
92
102
check_mypy_run (
93
103
[self .tempfile ],
94
104
python_executable ,
95
- self .msg_tuple ,
105
+ expected_out = self .msg_tuple ,
106
+ venv_dir = venv_dir ,
96
107
)
97
108
98
109
def test_stub_and_typed_pkg (self ) -> None :
99
- with self .virtualenv () as python_executable :
110
+ with self .virtualenv () as venv :
111
+ venv_dir , python_executable = venv
100
112
self .install_package ('typedpkg' , python_executable )
101
113
self .install_package ('typedpkg-stubs' , python_executable )
102
114
check_mypy_run (
103
115
[self .tempfile ],
104
116
python_executable ,
105
- self .msg_list ,
117
+ expected_out = self .msg_list ,
118
+ venv_dir = venv_dir ,
106
119
)
107
120
108
121
def test_typedpkg_stubs_python2 (self ) -> None :
109
122
python2 = try_find_python2_interpreter ()
110
123
if python2 :
111
- with self .virtualenv (python2 ) as py2 :
124
+ with self .virtualenv (python2 ) as venv :
125
+ venv_dir , py2 = venv
112
126
self .install_package ('typedpkg-stubs' , py2 )
113
127
check_mypy_run (
114
128
[self .tempfile ],
115
129
py2 ,
116
- self .msg_list ,
130
+ expected_out = self .msg_list ,
131
+ venv_dir = venv_dir ,
117
132
)
118
133
119
134
def test_typedpkg_python2 (self ) -> None :
120
135
python2 = try_find_python2_interpreter ()
121
136
if python2 :
122
- with self .virtualenv (python2 ) as py2 :
137
+ with self .virtualenv (python2 ) as venv :
138
+ venv_dir , py2 = venv
123
139
self .install_package ('typedpkg' , py2 )
124
140
check_mypy_run (
125
141
[self .tempfile ],
126
142
py2 ,
127
- self .msg_tuple ,
143
+ expected_out = self .msg_tuple ,
144
+ venv_dir = venv_dir ,
128
145
)
129
146
130
147
0 commit comments