|
1 |
| -import glob |
2 |
| -import importlib |
3 |
| -import os.path |
4 |
| -import random |
5 |
| -import shutil |
6 |
| -import sys |
7 |
| -import tempfile |
8 |
| -import time |
9 |
| -import re |
10 | 1 | from types import ModuleType
|
11 | 2 |
|
12 | 3 | from typing import List, Tuple
|
13 | 4 |
|
14 |
| -from mypy.myunit import Suite, AssertionFailure, assert_equal |
15 |
| -from mypy.test.helpers import assert_string_arrays_equal |
16 |
| -from mypy.test.data import parse_test_cases, DataDrivenTestCase |
17 |
| -from mypy.test import config |
18 |
| -from mypy.parse import parse |
19 |
| -from mypy.errors import CompileError |
20 |
| -from mypy.stubgen import generate_stub, generate_stub_for_module, parse_options, Options |
| 5 | +from mypy.myunit import Suite, assert_equal |
21 | 6 | from mypy.stubgenc import generate_c_type_stub, infer_method_sig
|
22 | 7 | from mypy.stubutil import (
|
23 | 8 | parse_signature, parse_all_signatures, build_signature, find_unique_signatures,
|
@@ -95,89 +80,6 @@ def test_infer_sig_from_docstring(self) -> None:
|
95 | 80 | assert_equal(infer_sig_from_docstring('\nfunc x', 'func'), None)
|
96 | 81 |
|
97 | 82 |
|
98 |
| -class StubgenPythonSuite(Suite): |
99 |
| - test_data_files = ['stubgen.test'] |
100 |
| - |
101 |
| - def cases(self) -> List[DataDrivenTestCase]: |
102 |
| - c = [] # type: List[DataDrivenTestCase] |
103 |
| - for path in self.test_data_files: |
104 |
| - c += parse_test_cases(os.path.join(config.test_data_prefix, path), test_stubgen) |
105 |
| - return c |
106 |
| - |
107 |
| - |
108 |
| -def parse_flags(program_text: str) -> Options: |
109 |
| - flags = re.search('# flags: (.*)$', program_text, flags=re.MULTILINE) |
110 |
| - if flags: |
111 |
| - flag_list = flags.group(1).split() |
112 |
| - else: |
113 |
| - flag_list = [] |
114 |
| - return parse_options(flag_list + ['dummy.py']) |
115 |
| - |
116 |
| - |
117 |
| -def test_stubgen(testcase: DataDrivenTestCase) -> None: |
118 |
| - if 'stubgen-test-path' not in sys.path: |
119 |
| - sys.path.insert(0, 'stubgen-test-path') |
120 |
| - os.mkdir('stubgen-test-path') |
121 |
| - source = '\n'.join(testcase.input) |
122 |
| - options = parse_flags(source) |
123 |
| - handle = tempfile.NamedTemporaryFile(prefix='prog_', suffix='.py', dir='stubgen-test-path', |
124 |
| - delete=False) |
125 |
| - assert os.path.isabs(handle.name) |
126 |
| - path = os.path.basename(handle.name) |
127 |
| - name = path[:-3] |
128 |
| - path = os.path.join('stubgen-test-path', path) |
129 |
| - out_dir = '_out' |
130 |
| - os.mkdir(out_dir) |
131 |
| - try: |
132 |
| - handle.write(bytes(source, 'ascii')) |
133 |
| - handle.close() |
134 |
| - # Without this we may sometimes be unable to import the module below, as importlib |
135 |
| - # caches os.listdir() results in Python 3.3+ (Guido explained this to me). |
136 |
| - reset_importlib_caches() |
137 |
| - try: |
138 |
| - if testcase.name.endswith('_import'): |
139 |
| - generate_stub_for_module(name, out_dir, quiet=True, |
140 |
| - no_import=options.no_import, |
141 |
| - include_private=options.include_private) |
142 |
| - else: |
143 |
| - generate_stub(path, out_dir, include_private=options.include_private) |
144 |
| - a = load_output(out_dir) |
145 |
| - except CompileError as e: |
146 |
| - a = e.messages |
147 |
| - assert_string_arrays_equal(testcase.output, a, |
148 |
| - 'Invalid output ({}, line {})'.format( |
149 |
| - testcase.file, testcase.line)) |
150 |
| - finally: |
151 |
| - handle.close() |
152 |
| - os.unlink(handle.name) |
153 |
| - shutil.rmtree(out_dir) |
154 |
| - |
155 |
| - |
156 |
| -def reset_importlib_caches() -> None: |
157 |
| - try: |
158 |
| - importlib.invalidate_caches() |
159 |
| - except (ImportError, AttributeError): |
160 |
| - pass |
161 |
| - |
162 |
| - |
163 |
| -def load_output(dirname: str) -> List[str]: |
164 |
| - result = [] # type: List[str] |
165 |
| - entries = glob.glob('%s/*' % dirname) |
166 |
| - assert entries, 'No files generated' |
167 |
| - if len(entries) == 1: |
168 |
| - add_file(entries[0], result) |
169 |
| - else: |
170 |
| - for entry in entries: |
171 |
| - result.append('## %s ##' % entry) |
172 |
| - add_file(entry, result) |
173 |
| - return result |
174 |
| - |
175 |
| - |
176 |
| -def add_file(path: str, result: List[str]) -> None: |
177 |
| - with open(path) as file: |
178 |
| - result.extend(file.read().splitlines()) |
179 |
| - |
180 |
| - |
181 | 83 | class StubgencSuite(Suite):
|
182 | 84 | def test_infer_hash_sig(self) -> None:
|
183 | 85 | assert_equal(infer_method_sig('__hash__'), '()')
|
|
0 commit comments