diff options
author | Sybren A. Stüvel <sybren@stuvel.eu> | 2019-08-04 15:47:11 +0200 |
---|---|---|
committer | Sybren A. Stüvel <sybren@stuvel.eu> | 2019-08-04 17:05:58 +0200 |
commit | 6760eb76e665dc81863a82110164c4b3b38e7ee9 (patch) | |
tree | 3a9cf1606c851ad4067c2419fec78e7f6ca044ac /tests | |
parent | ded036cf988b0cf4b20002d88434282f30762638 (diff) | |
download | rsa-git-6760eb76e665dc81863a82110164c4b3b38e7ee9.tar.gz |
Added mypy for static type checking
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_cli.py | 40 | ||||
-rw-r--r-- | tests/test_mypy.py | 27 |
2 files changed, 45 insertions, 22 deletions
diff --git a/tests/test_cli.py b/tests/test_cli.py index 7cf7ed4..1cd92c2 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4,41 +4,37 @@ Unit tests for CLI entry points. from __future__ import print_function -import unittest -import sys import functools -from contextlib import contextmanager - +import io import os -from io import StringIO, BytesIO +import sys +import typing +import unittest +from contextlib import contextmanager, redirect_stdout, redirect_stderr import rsa import rsa.cli import rsa.util -def make_buffer() -> StringIO: - buf = StringIO() - buf.buffer = BytesIO() - return buf +@contextmanager +def captured_output() -> typing.Generator: + """Captures output to stdout and stderr""" + # According to mypy, we're not supposed to change buf_out.buffer. + # However, this is just a test, and it works, hence the 'type: ignore'. + buf_out = io.StringIO() + buf_out.buffer = io.BytesIO() # type: ignore -def get_bytes_out(out: StringIO) -> bytes: - # Python 3.x writes 'bytes' to stdout.buffer - return out.buffer.getvalue() + buf_err = io.StringIO() + buf_err.buffer = io.BytesIO() # type: ignore + with redirect_stdout(buf_out), redirect_stderr(buf_err): + yield buf_out, buf_err -@contextmanager -def captured_output(): - """Captures output to stdout and stderr""" - new_out, new_err = make_buffer(), make_buffer() - old_out, old_err = sys.stdout, sys.stderr - try: - sys.stdout, sys.stderr = new_out, new_err - yield new_out, new_err - finally: - sys.stdout, sys.stderr = old_out, old_err +def get_bytes_out(buf) -> bytes: + return buf.buffer.getvalue() @contextmanager diff --git a/tests/test_mypy.py b/tests/test_mypy.py new file mode 100644 index 0000000..c2a9745 --- /dev/null +++ b/tests/test_mypy.py @@ -0,0 +1,27 @@ +import pathlib +import unittest + +import mypy.api + +test_modules = ['rsa', 'tests'] + + +class MypyRunnerTest(unittest.TestCase): + def test_run_mypy(self): + proj_root = pathlib.Path(__file__).parent.parent + args = ['--incremental', '--ignore-missing-imports'] + [str(proj_root / dirname) for dirname + in test_modules] + + result = mypy.api.run(args) + + stdout, stderr, status = result + + messages = [] + if stderr: + messages.append(stderr) + if stdout: + messages.append(stdout) + if status: + messages.append('Mypy failed with status %d' % status) + if messages: + self.fail('\n'.join(['Mypy errors:'] + messages)) |