|
10 | 10 | import lazy_loader as lazy
|
11 | 11 |
|
12 | 12 |
|
| 13 | +@pytest.fixture |
| 14 | +def clean_fake_pkg(): |
| 15 | + yield |
| 16 | + sys.modules.pop("tests.fake_pkg.some_func", None) |
| 17 | + sys.modules.pop("tests.fake_pkg", None) |
| 18 | + sys.modules.pop("tests", None) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize("attempt", [1, 2]) |
| 22 | +def test_cleanup_fixture(clean_fake_pkg, attempt): |
| 23 | + assert "tests.fake_pkg" not in sys.modules |
| 24 | + assert "tests.fake_pkg.some_func" not in sys.modules |
| 25 | + from tests import fake_pkg |
| 26 | + |
| 27 | + assert "tests.fake_pkg" in sys.modules |
| 28 | + assert "tests.fake_pkg.some_func" not in sys.modules |
| 29 | + assert isinstance(fake_pkg.some_func, types.FunctionType) |
| 30 | + assert "tests.fake_pkg.some_func" in sys.modules |
| 31 | + |
| 32 | + |
13 | 33 | def test_lazy_import_basics():
|
14 | 34 | math = lazy.load("math")
|
15 | 35 | anything_not_real = lazy.load("anything_not_real")
|
@@ -127,18 +147,24 @@ def test_lazy_attach_returns_copies():
|
127 | 147 | assert _all == [*expected, "modify_returned_all"]
|
128 | 148 |
|
129 | 149 |
|
130 |
| -def test_attach_same_module_and_attr_name(): |
131 |
| - from tests import fake_pkg |
| 150 | +@pytest.mark.parametrize("eager_import", [False, True]) |
| 151 | +def test_attach_same_module_and_attr_name(clean_fake_pkg, eager_import): |
| 152 | + env = {} |
| 153 | + if eager_import: |
| 154 | + env["EAGER_IMPORT"] = "1" |
132 | 155 |
|
133 |
| - # Grab attribute twice, to ensure that importing it does not |
134 |
| - # override function by module |
135 |
| - assert isinstance(fake_pkg.some_func, types.FunctionType) |
136 |
| - assert isinstance(fake_pkg.some_func, types.FunctionType) |
| 156 | + with mock.patch.dict(os.environ, env): |
| 157 | + from tests import fake_pkg |
| 158 | + |
| 159 | + # Grab attribute twice, to ensure that importing it does not |
| 160 | + # override function by module |
| 161 | + assert isinstance(fake_pkg.some_func, types.FunctionType) |
| 162 | + assert isinstance(fake_pkg.some_func, types.FunctionType) |
137 | 163 |
|
138 |
| - # Ensure imports from submodule still work |
139 |
| - from tests.fake_pkg.some_func import some_func |
| 164 | + # Ensure imports from submodule still work |
| 165 | + from tests.fake_pkg.some_func import some_func |
140 | 166 |
|
141 |
| - assert isinstance(some_func, types.FunctionType) |
| 167 | + assert isinstance(some_func, types.FunctionType) |
142 | 168 |
|
143 | 169 |
|
144 | 170 | FAKE_STUB = """
|
|
0 commit comments