|
2 | 2 | import importlib |
3 | 3 | import sys |
4 | 4 | import pytest |
| 5 | +import runpy |
| 6 | +from mypy.build import build |
| 7 | +from mypy.modulefinder import BuildSource |
| 8 | +from mypy.options import Options |
5 | 9 |
|
6 | 10 |
|
7 | 11 | def installed(): |
@@ -65,3 +69,74 @@ def test_import_error(): |
65 | 69 | with pytest.raises(ImportError, match="gino-nonexist"): |
66 | 70 | # noinspection PyUnresolvedReferences |
67 | 71 | from gino.ext import nonexist |
| 72 | + |
| 73 | + |
| 74 | +@pytest.fixture |
| 75 | +def extensions(mocker): |
| 76 | + EntryPoint = collections.namedtuple("EntryPoint", ["name", "value"]) |
| 77 | + importlib_metadata = mocker.Mock() |
| 78 | + importlib_metadata.entry_points = lambda: { |
| 79 | + "gino.extensions": [ |
| 80 | + EntryPoint("demo1", "tests.stub1"), |
| 81 | + EntryPoint("demo2", "tests.stub2"), |
| 82 | + ] |
| 83 | + } |
| 84 | + mocker.patch.dict("sys.modules", {"importlib.metadata": importlib_metadata}) |
| 85 | + |
| 86 | + |
| 87 | +def test_list(mocker, extensions): |
| 88 | + mocker.patch("sys.argv", ["", "list"]) |
| 89 | + stdout = mocker.patch("sys.stdout.write") |
| 90 | + runpy.run_module("gino.ext", run_name="__main__") |
| 91 | + out = "".join(args[0][0] for args in stdout.call_args_list) |
| 92 | + assert "tests.stub1" in out |
| 93 | + assert "tests.stub2" in out |
| 94 | + assert "gino.ext.demo1" in out |
| 95 | + assert "gino.ext.demo2" in out |
| 96 | + assert out.count("no stub file") == 2 |
| 97 | + |
| 98 | + mocker.patch("sys.argv", [""]) |
| 99 | + runpy.run_module("gino.ext", run_name="__main__") |
| 100 | + |
| 101 | + |
| 102 | +def test_type_check(mocker, extensions): |
| 103 | + mocker.patch("sys.argv", ["", "clean"]) |
| 104 | + runpy.run_module("gino.ext", run_name="__main__") |
| 105 | + |
| 106 | + result = build( |
| 107 | + [BuildSource(None, None, "from gino.ext.demo3 import s3")], Options() |
| 108 | + ) |
| 109 | + assert result.errors |
| 110 | + |
| 111 | + result = build( |
| 112 | + [BuildSource(None, None, "from gino.ext.demo1 import s1")], Options() |
| 113 | + ) |
| 114 | + assert result.errors |
| 115 | + |
| 116 | + mocker.patch("sys.argv", ["", "stub"]) |
| 117 | + runpy.run_module("gino.ext", run_name="__main__") |
| 118 | + runpy.run_module("gino.ext", run_name="__main__") |
| 119 | + |
| 120 | + try: |
| 121 | + result = build( |
| 122 | + [BuildSource(None, None, "from gino.ext.demo1 import s1")], Options() |
| 123 | + ) |
| 124 | + assert not result.errors |
| 125 | + |
| 126 | + result = build( |
| 127 | + [BuildSource(None, None, "from gino.ext.demo1 import s2")], Options() |
| 128 | + ) |
| 129 | + assert result.errors |
| 130 | + |
| 131 | + result = build( |
| 132 | + [BuildSource(None, None, "from gino.ext.demo2 import s2")], Options() |
| 133 | + ) |
| 134 | + assert not result.errors |
| 135 | + |
| 136 | + result = build( |
| 137 | + [BuildSource(None, None, "from gino.ext.demo2 import s1")], Options() |
| 138 | + ) |
| 139 | + assert result.errors |
| 140 | + finally: |
| 141 | + mocker.patch("sys.argv", ["", "clean"]) |
| 142 | + runpy.run_module("gino.ext", run_name="__main__") |
0 commit comments