From 1016ece49108accbe8505ec8c0ebdd2c5a3e29dd Mon Sep 17 00:00:00 2001 From: Maxim Kurnikov Date: Sat, 10 Nov 2018 16:56:38 +0300 Subject: [PATCH] add basic test infrastructure --- conftest.py | 3 +++ pytest.ini | 7 +++++ test/test-data/basic-check.test | 9 +++++++ test/testdjango.py | 48 +++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 conftest.py create mode 100644 pytest.ini create mode 100644 test/test-data/basic-check.test create mode 100644 test/testdjango.py diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..d8b9cf8 --- /dev/null +++ b/conftest.py @@ -0,0 +1,3 @@ +pytest_plugins = [ + 'mypy.test.data' +] \ No newline at end of file diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..778053f --- /dev/null +++ b/pytest.ini @@ -0,0 +1,7 @@ +[pytest] + +testpaths = test +python_files = test*.py +addopts = + -nauto + --tb=native \ No newline at end of file diff --git a/test/test-data/basic-check.test b/test/test-data/basic-check.test new file mode 100644 index 0000000..3bed52b --- /dev/null +++ b/test/test-data/basic-check.test @@ -0,0 +1,9 @@ +[case testBasicCheck] +from typing import Any + +class A: + pass + +class B(A): + pass +[out] \ No newline at end of file diff --git a/test/testdjango.py b/test/testdjango.py new file mode 100644 index 0000000..9176c16 --- /dev/null +++ b/test/testdjango.py @@ -0,0 +1,48 @@ +import os +import sys +from pathlib import Path + +from mypy import api +from mypy.test.config import test_temp_dir +from mypy.test.data import DataSuite, DataDrivenTestCase +from mypy.test.helpers import assert_string_arrays_equal + +ROOT_DIR = Path(__file__).parent.parent +TEST_DATA_DIR = ROOT_DIR / 'test' / 'test-data' + + +class DjangoTestSuite(DataSuite): + files = [ + 'basic-check.test' + ] + data_prefix = str(TEST_DATA_DIR) + + def run_case(self, testcase: DataDrivenTestCase) -> None: + assert testcase.old_cwd is not None, "test was not properly set up" + + mypy_cmdline = [] + mypy_cmdline.append('--python-version={}'.format('.'.join(map(str, + sys.version_info[:2])))) + + program_path = os.path.join(test_temp_dir, 'main.py') + mypy_cmdline.append(program_path) + + with open(program_path, 'w') as file: + for s in testcase.input: + file.write('{}\n'.format(s)) + + output = [] + # Type check the program. + out, err, returncode = api.run(mypy_cmdline) + # split lines, remove newlines, and remove directory of test case + for line in (out + err).splitlines(): + if line.startswith(test_temp_dir + os.sep): + output.append(line[len(test_temp_dir + os.sep):].rstrip("\r\n").replace('.py', '')) + else: + output.append(line.rstrip("\r\n")) + # Remove temp file. + os.remove(program_path) + + assert_string_arrays_equal(testcase.output, output, + 'Invalid output ({}, line {})'.format( + testcase.file, testcase.line))