|
6 | 6 | from typing import List |
7 | 7 | from typing import Sequence |
8 | 8 |
|
| 9 | +import attr |
9 | 10 | import py.path |
10 | 11 |
|
11 | 12 | import _pytest._code |
@@ -250,107 +251,115 @@ def pytest_addoption(parser): |
250 | 251 | @pytest.mark.parametrize( |
251 | 252 | "ini_file_text, exception_text", |
252 | 253 | [ |
253 | | - ( |
254 | | - """ |
255 | | - [pytest] |
256 | | - required_plugins = fakePlugin1 fakePlugin2 |
257 | | - """, |
258 | | - "Missing required plugins: fakePlugin1, fakePlugin2", |
259 | | - ), |
260 | | - ( |
| 254 | + pytest.param( |
261 | 255 | """ |
262 | | - [pytest] |
263 | | - required_plugins = a pytest-xdist z |
264 | | - """, |
| 256 | + [pytest] |
| 257 | + required_plugins = a z |
| 258 | + """, |
265 | 259 | "Missing required plugins: a, z", |
| 260 | + id="2-missing", |
266 | 261 | ), |
267 | | - ( |
268 | | - """ |
269 | | - [pytest] |
270 | | - required_plugins = a q j b c z |
271 | | - """, |
272 | | - "Missing required plugins: a, b, c, j, q, z", |
273 | | - ), |
274 | | - ( |
275 | | - """ |
276 | | - [pytest] |
277 | | - required_plugins = pytest-xdist |
278 | | - """, |
279 | | - "", |
280 | | - ), |
281 | | - ( |
282 | | - """ |
283 | | - [pytest] |
284 | | - required_plugins = pytest-xdist==1.32.0 |
285 | | - """, |
286 | | - "", |
287 | | - ), |
288 | | - ( |
289 | | - """ |
290 | | - [pytest] |
291 | | - required_plugins = pytest-xdist>1.0.0,<2.0.0 |
292 | | - """, |
293 | | - "", |
294 | | - ), |
295 | | - ( |
| 262 | + pytest.param( |
296 | 263 | """ |
297 | | - [pytest] |
298 | | - required_plugins = pytest-xdist~=1.32.0 pytest-xdist==1.32.0 pytest-xdist!=0.0.1 pytest-xdist<=99.99.0 |
299 | | - pytest-xdist>=1.32.0 pytest-xdist<9.9.9 pytest-xdist>1.30.0 pytest-xdist===1.32.0 |
300 | | - """, |
301 | | - "", |
| 264 | + [pytest] |
| 265 | + required_plugins = a z myplugin |
| 266 | + """, |
| 267 | + "Missing required plugins: a, z", |
| 268 | + id="2-missing-1-ok", |
302 | 269 | ), |
303 | | - ( |
| 270 | + pytest.param( |
304 | 271 | """ |
305 | | - [pytest] |
306 | | - required_plugins = pytest-xdist>9.9.9 pytest-xdist==1.32.0 pytest-xdist==8.8.8 |
307 | | - """, |
308 | | - "Missing required plugins: pytest-xdist==8.8.8, pytest-xdist>9.9.9", |
| 272 | + [pytest] |
| 273 | + required_plugins = myplugin |
| 274 | + """, |
| 275 | + None, |
| 276 | + id="1-ok", |
309 | 277 | ), |
310 | | - ( |
| 278 | + pytest.param( |
311 | 279 | """ |
312 | | - [pytest] |
313 | | - required_plugins = pytest-xdist==aegsrgrsgs pytest-xdist==-1 pytest-xdist>2.1.1,>3.0.0 |
314 | | - """, |
315 | | - "Missing required plugins: pytest-xdist==-1, pytest-xdist==aegsrgrsgs, pytest-xdist>2.1.1,>3.0.0", |
| 280 | + [pytest] |
| 281 | + required_plugins = myplugin==1.5 |
| 282 | + """, |
| 283 | + None, |
| 284 | + id="1-ok-pin-exact", |
316 | 285 | ), |
317 | | - ( |
| 286 | + pytest.param( |
318 | 287 | """ |
319 | | - [pytest] |
320 | | - required_plugins = pytest-xdist== pytest-xdist<= |
321 | | - """, |
322 | | - "Missing required plugins: pytest-xdist<=, pytest-xdist==", |
| 288 | + [pytest] |
| 289 | + required_plugins = myplugin>1.0,<2.0 |
| 290 | + """, |
| 291 | + None, |
| 292 | + id="1-ok-pin-loose", |
323 | 293 | ), |
324 | | - ( |
| 294 | + pytest.param( |
325 | 295 | """ |
326 | | - [pytest] |
327 | | - required_plugins = pytest-xdist= pytest-xdist< |
328 | | - """, |
329 | | - "Missing required plugins: pytest-xdist<, pytest-xdist=", |
| 296 | + [pytest] |
| 297 | + required_plugins = pyplugin==1.6 |
| 298 | + """, |
| 299 | + "Missing required plugins: pyplugin==1.6", |
| 300 | + id="missing-version", |
330 | 301 | ), |
331 | | - ( |
| 302 | + pytest.param( |
332 | 303 | """ |
333 | | - [some_other_header] |
334 | | - required_plugins = wont be triggered |
335 | | - [pytest] |
336 | | - minversion = 5.0.0 |
337 | | - """, |
338 | | - "", |
| 304 | + [pytest] |
| 305 | + required_plugins = pyplugin==1.6 other==1.0 |
| 306 | + """, |
| 307 | + "Missing required plugins: other==1.0, pyplugin==1.6", |
| 308 | + id="missing-versions", |
339 | 309 | ), |
340 | | - ( |
| 310 | + pytest.param( |
341 | 311 | """ |
342 | | - [pytest] |
343 | | - minversion = 5.0.0 |
344 | | - """, |
345 | | - "", |
| 312 | + [some_other_header] |
| 313 | + required_plugins = wont be triggered |
| 314 | + [pytest] |
| 315 | + """, |
| 316 | + None, |
| 317 | + id="invalid-header", |
346 | 318 | ), |
347 | 319 | ], |
348 | 320 | ) |
349 | | - def test_missing_required_plugins(self, testdir, ini_file_text, exception_text): |
350 | | - pytest.importorskip("xdist") |
| 321 | + def test_missing_required_plugins( |
| 322 | + self, testdir, monkeypatch, ini_file_text, exception_text |
| 323 | + ): |
| 324 | + """Check 'required_plugins' option with various settings. |
351 | 325 |
|
352 | | - testdir.tmpdir.join("pytest.ini").write(textwrap.dedent(ini_file_text)) |
353 | | - testdir.monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD") |
| 326 | + This test installs a mock "myplugin-1.5" which is used in the parametrized test cases. |
| 327 | + """ |
| 328 | + |
| 329 | + @attr.s |
| 330 | + class DummyEntryPoint: |
| 331 | + name = attr.ib() |
| 332 | + module = attr.ib() |
| 333 | + group = "pytest11" |
| 334 | + |
| 335 | + def load(self): |
| 336 | + __import__(self.module) |
| 337 | + return sys.modules[self.module] |
| 338 | + |
| 339 | + entry_points = [ |
| 340 | + DummyEntryPoint("myplugin1", "myplugin1_module"), |
| 341 | + ] |
| 342 | + |
| 343 | + @attr.s |
| 344 | + class DummyDist: |
| 345 | + entry_points = attr.ib() |
| 346 | + files = () |
| 347 | + version = "1.5" |
| 348 | + |
| 349 | + @property |
| 350 | + def metadata(self): |
| 351 | + return {"name": "myplugin"} |
| 352 | + |
| 353 | + def my_dists(): |
| 354 | + return [DummyDist(entry_points)] |
| 355 | + |
| 356 | + testdir.makepyfile(myplugin1_module="# my plugin module") |
| 357 | + testdir.syspathinsert() |
| 358 | + |
| 359 | + monkeypatch.setattr(importlib_metadata, "distributions", my_dists) |
| 360 | + testdir.monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) |
| 361 | + |
| 362 | + testdir.makeini(ini_file_text) |
354 | 363 |
|
355 | 364 | if exception_text: |
356 | 365 | with pytest.raises(pytest.fail.Exception, match=exception_text): |
|
0 commit comments